From dac2aa7b79c89c5590c86b3552c5ac4b1227500e Mon Sep 17 00:00:00 2001 From: Gly Date: Thu, 22 Dec 2022 00:01:11 +0100 Subject: setup json output --- xmake/modules/private/utils/complete.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/xmake/modules/private/utils/complete.lua b/xmake/modules/private/utils/complete.lua index 8dcad25c4..bcc961d94 100644 --- a/xmake/modules/private/utils/complete.lua +++ b/xmake/modules/private/utils/complete.lua @@ -22,10 +22,12 @@ import("core.base.option") import("core.base.task") import("core.base.cli") +import("core.base.json") local raw_words = {} local raw_config local position = 0 +local no_json = false local use_spaces = true local no_key = false local reenter = false @@ -42,6 +44,16 @@ function _print_candidate(is_complate, ...) end end +function _print_candidates(is_complate, candidates) + if to_json then + printf("%s", json.encode(candidates)) + else + for _, v in ipairs(candidates) do + _print_candidate(is_complate, "%s", v.value) + end + end +end + function _find_candidates(candidates, find) if type(candidates) ~= 'table' then @@ -292,6 +304,9 @@ function main(pos, config, ...) if raw_config:find("debug", 1, true) then debug = true end + if raw_config:find("json", 1, true) then + to_json = true + end local word = table.concat(raw_words, " ") or "" position = tonumber(pos) or 0 -- cgit v1.3.1 From c3ca3ff48a2dfb9dd15af62102af539dec58c385 Mon Sep 17 00:00:00 2001 From: Gly Date: Thu, 22 Dec 2022 00:02:01 +0100 Subject: rewrite complete tasks --- xmake/modules/private/utils/complete.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/xmake/modules/private/utils/complete.lua b/xmake/modules/private/utils/complete.lua index bcc961d94..09f0b8ca6 100644 --- a/xmake/modules/private/utils/complete.lua +++ b/xmake/modules/private/utils/complete.lua @@ -87,12 +87,12 @@ function _find_candidates(candidates, find) 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 + local found_candidates = {} + for i, v in ipairs(_find_candidates((table.keys(tasks)), name)) do + table.insert(found_candidates, { value = v, description = tasks[v].description }) end - return has_candidate + _print_candidates(has_candidate, found_candidates) + return #found_candidates > 0 end -- complete values of kv -- cgit v1.3.1 From 524a23ae6a0a5fcdd23c8c32a52ab62bcd07830b Mon Sep 17 00:00:00 2001 From: Gly Date: Thu, 22 Dec 2022 01:22:05 +0100 Subject: embed candid info into object --- xmake/modules/private/utils/complete.lua | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/xmake/modules/private/utils/complete.lua b/xmake/modules/private/utils/complete.lua index 09f0b8ca6..dd396572e 100644 --- a/xmake/modules/private/utils/complete.lua +++ b/xmake/modules/private/utils/complete.lua @@ -32,11 +32,10 @@ local use_spaces = true local no_key = false local reenter = false -function _print_candidate(is_complate, ...) - local candidate = format(...) - if candidate and #candidate ~= 0 then - printf(candidate) - if use_spaces and is_complate then +function _print_candidate(candidate) + if candidate.value and #candidate.value ~= 0 then + printf(candidate.value) + if use_spaces and candidate.is_complete then print(" ") else print("") @@ -44,12 +43,12 @@ function _print_candidate(is_complate, ...) end end -function _print_candidates(is_complate, candidates) +function _print_candidates(candidates) if to_json then - printf("%s", json.encode(candidates)) + print(json.encode(candidates)) else for _, v in ipairs(candidates) do - _print_candidate(is_complate, "%s", v.value) + _print_candidate(v) end end end @@ -89,9 +88,9 @@ end function _complete_task(tasks, name) local found_candidates = {} for i, v in ipairs(_find_candidates((table.keys(tasks)), name)) do - table.insert(found_candidates, { value = v, description = tasks[v].description }) + table.insert(found_candidates, { value = v, is_complete = true, description = tasks[v].description }) end - _print_candidates(has_candidate, found_candidates) + _print_candidates(found_candidates) return #found_candidates > 0 end -- cgit v1.3.1 From 95ee81deecf956d4f87a2315ffa55996c41bfa2a Mon Sep 17 00:00:00 2001 From: Gly Date: Thu, 22 Dec 2022 01:22:28 +0100 Subject: complete options --- xmake/modules/private/utils/complete.lua | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/xmake/modules/private/utils/complete.lua b/xmake/modules/private/utils/complete.lua index dd396572e..072c69704 100644 --- a/xmake/modules/private/utils/complete.lua +++ b/xmake/modules/private/utils/complete.lua @@ -121,13 +121,15 @@ function _complete_option_kv_v(options, current, completing, name, value) end -- match values starts with value first + local found_candidates = {} for _, v in ipairs(_find_candidates(values, value)) do if no_key then - _print_candidate(true, "%s", v) + table.insert(found_candidates, { value = v, is_complete = true }) else - _print_candidate(true, "--%s=%s", name, v) + table.insert(found_candidates, { value = format("--%s=%s", name, v), is_complete = true }) end end + _print_candidates(found_candidates) -- whether any candidates has been found, finish complete since we don't have more info return true @@ -142,11 +144,13 @@ function _complete_option_kv_k(options, current, completing, name) opcandi[opt[2]] = opt end end - + local found_candidates = {} 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]) + local name = format((opt[3] == "k") and "--%s" or "--%s=", opt[2]) + table.insert(found_candidates, { value = name, description = opt[5], is_complete = (opt[3] == "k") }) end + _print_candidates(found_candidates) return true end @@ -190,7 +194,7 @@ function _complete_option_v(options, current, completing) optvs = v end end - + local found_candidates = {} if opt then -- show candidates of values local values = opt.values @@ -199,14 +203,12 @@ function _complete_option_v(options, current, completing) end for _, v in ipairs(values) do if tostring(v):startswith(completing) then - _print_candidate(true, "%s", v) + table.insert(found_candidates, { value = v, is_complete = true }) end end - return true end - if optvs then - + if optvs and #found_candidates == 0 then -- show candidates of values local values = optvs.values if type(values) == "function" then @@ -214,13 +216,12 @@ function _complete_option_v(options, current, completing) end for _, v in ipairs(values) do if tostring(v):startswith(completing) then - _print_candidate(true, "%s", v) + table.insert(found_candidates, { value = v, is_complete = true }) end end - return true end - - return false + _print_candidates(found_candidates) + return #found_candidates > 0 end function _complete_option(options, segs, completing) -- cgit v1.3.1