diff options
| author | OpportunityLiu <[email protected]> | 2020-01-21 20:01:42 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2020-01-21 20:01:42 +0800 |
| commit | a2e9c36efd709056507bb260f7d9ccaf7337a2f6 (patch) | |
| tree | b94438ef1187079dc1356cba5f01baab41c9a420 /xmake/modules/private/utils/complete.lua | |
| parent | 19e11d5150fd2496a74f2e461673698e591be008 (diff) | |
fix
Diffstat (limited to 'xmake/modules/private/utils/complete.lua')
| -rw-r--r-- | xmake/modules/private/utils/complete.lua | 88 |
1 files changed, 37 insertions, 51 deletions
diff --git a/xmake/modules/private/utils/complete.lua b/xmake/modules/private/utils/complete.lua index d06aef29a..10c9e7d66 100644 --- a/xmake/modules/private/utils/complete.lua +++ b/xmake/modules/private/utils/complete.lua @@ -42,26 +42,39 @@ function _print_candidate(is_complate, ...) end end -function _complete_task(tasks, name) +function _find_candidates(candidates, find) local has_candidate = false + local results = table.new(#candidates, 0) - -- match tasks starts with name first - for k, _ in pairs(tasks) do - if k:startswith(name) then - _print_candidate(true, "%s", k) + -- find candidate starts with find str + for _, v in ipairs(candidates) do + if tostring(v):startswith(find) then + table.insert(results, v) has_candidate = true end end - if has_candidate then return true end + -- stop searching if found any + if has_candidate then + return results + end - -- not found? keep searching - for k, _ in pairs(tasks) do - if k:find(name, 1, true) then - _print_candidate(true, "%s", k) - has_candidate = true + -- find candidate contains find str + for _, v in ipairs(candidates) do + if tostring(v):find(find, 1, true) then + table.insert(results, v) end end + + return results +end + +function _complete_task(tasks, name) + local has_candidate = false + for _, v in ipairs(_find_candidates((table.keys(tasks)), name)) do + _print_candidate(true, "%s", v) + has_candidate = true + end return has_candidate end @@ -91,28 +104,12 @@ function _complete_option_kv_v(options, current, completing, name, value) value = "" end - local has_candidate = false -- match values starts with value first - for _, v in ipairs(values) do - if tostring(v):startswith(value) then - has_candidate = true - if no_key then - _print_candidate(true, "%s", v) - else - _print_candidate(true, "--%s=%s", name, v) - end - end - end - if has_candidate then return true end - - -- not found? keep searching - for _, v in ipairs(values) do - if tostring(v):find(value, 1 , true) then - if no_key then - _print_candidate(true, "%s", v) - else - _print_candidate(true, "--%s=%s", name, v) - end + for _, v in ipairs(_find_candidates(values, value)) do + if no_key then + _print_candidate(true, "%s", v) + else + _print_candidate(true, "--%s=%s", name, v) end end @@ -123,28 +120,16 @@ end -- complete keys of kv function _complete_option_kv_k(options, current, completing, name) - local opcandi = table.new(10, 0) - for _, v in ipairs(options) do - if current[v[2]] == nil then - if (v[3] == "kv" or v[3] == "k") and v[2] then table.insert(opcandi, v) end + local opcandi = table.new(0, 10) + for _, opt in ipairs(options) do + if opt[2] and current[opt[2]] == nil and (opt[3] == "kv" or opt[3] == "k") then + opcandi[opt[2]] = opt end end - local has_candidate = false - -- match keys starts with name first - for _, v in ipairs(opcandi) do - if v[2]:startswith(name) then - _print_candidate((v[3] == "k"), (v[3] == "k") and "--%s" or "--%s=", v[2]) - end - end - - if has_candidate then return true end - - -- not found? keep searching - for _, v in ipairs(opcandi) do - if v[2]:find(name, 1, true) then - _print_candidate((v[3] == "k"), (v[3] == "k") and "--%s" or "--%s=", v[2]) - end + for _, k in ipairs(_find_candidates((table.keys(opcandi)), name)) do + local opt = opcandi[k] + _print_candidate((opt[3] == "k"), (opt[3] == "k") and "--%s" or "--%s=", opt[2]) end return true @@ -176,6 +161,7 @@ function _complete_option_kv(options, current, completing) end end +-- complete options v and vs function _complete_option_v(options, current, completing) -- find completion option local opt |
