summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-06-26 20:01:10 +0800
committerOpportunityLiu <[email protected]>2019-06-26 20:01:10 +0800
commit318bb739dd64d19a4b4d91d8be49b22fc3403cff (patch)
tree738798d2645128ad516f9db0a02ea4c477e279b3
parent0dac0aec2688530d675d00df0debcd88226ff875 (diff)
improve tab complete
-rw-r--r--xmake/core/base/os.lua26
-rw-r--r--xmake/core/base/table.lua32
-rw-r--r--xmake/core/sandbox/modules/import/core/base/option.lua14
-rw-r--r--xmake/core/sandbox/modules/import/core/base/task.lua8
-rw-r--r--xmake/modules/private/utils/complete.lua32
5 files changed, 90 insertions, 22 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 09274450d..631276fca 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -788,28 +788,30 @@ end
-- get the system null device
function os.nuldev(input)
- -- init the output nuldev
- if xmake._NULDEV_OUTPUT == nil then
- if os.host() == "windows" then
- xmake._NULDEV_OUTPUT = "nul"
+ if os.host() == "windows" then
+ -- init the output nuldev
+ if xmake._NULDEV_OUTPUT == nil then
+ xmake._NULDEV_OUTPUT = os.tmpfile()
else
- xmake._NULDEV_OUTPUT = "/dev/null"
+ os.rm(xmake._NULDEV_OUTPUT)
end
- end
-
- -- init the input nuldev
- if xmake._NULDEV_INPUT == nil then
- if os.host() == "windows" then
+ -- init the input nuldev
+ if xmake._NULDEV_INPUT == nil then
-- create an empty file
--
-- for fix issue on mingw:
-- $ gcc -fopenmp -S -o nul -xc nul
-- gcc: fatal error:input file ‘nul’ is the same as output file
- --
+ --
local inputfile = os.tmpfile()
io.writefile(inputfile, "")
xmake._NULDEV_INPUT = inputfile
- else
+ end
+ else
+ if xmake._NULDEV_OUTPUT == nil then
+ xmake._NULDEV_OUTPUT = "/dev/null"
+ end
+ if xmake._NULDEV_INPUT == nil then
xmake._NULDEV_INPUT = "/dev/null"
end
end
diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua
index 3fd35c082..43e8b6db9 100644
--- a/xmake/core/base/table.lua
+++ b/xmake/core/base/table.lua
@@ -257,13 +257,41 @@ function table.unique(array, barrier)
end
-- pack arguments into a table
+-- polyfill of lua 5.2, @see https://www.lua.org/manual/5.2/manual.html#pdf-table.pack
function table.pack(...)
return { n = select("#", ...), ... }
end
-- unpack table values
-function table.unpack(list, i, j)
- return unpack(list, i, j)
+-- polyfill of lua 5.2, @see https://www.lua.org/manual/5.2/manual.html#pdf-table.unpack
+table.unpack = unpack
+
+-- get keys of a table
+function table.keys(tab)
+
+ assert(tab)
+
+ local keyset = {}
+ local n = 0
+ for k, _ in pairs(tab) do
+ n = n + 1
+ keyset[n] = k
+ end
+ return keyset, n
+end
+
+-- get values of a table
+function table.values(tab)
+
+ assert(tab)
+
+ local valueset = {}
+ local n = 0
+ for _, v in pairs(tab) do
+ n = n + 1
+ valueset[n] = v
+ end
+ return valueset, n
end
-- return module: table
diff --git a/xmake/core/sandbox/modules/import/core/base/option.lua b/xmake/core/sandbox/modules/import/core/base/option.lua
index 417638b43..660560a22 100644
--- a/xmake/core/sandbox/modules/import/core/base/option.lua
+++ b/xmake/core/sandbox/modules/import/core/base/option.lua
@@ -57,6 +57,20 @@ function sandbox_core_base_option.defaults()
end
-- parse arguments with the given options
+function sandbox_core_base_option.raw_parse(argv, options)
+
+ -- check
+ assert(argv and options)
+
+ -- parse it
+ local results, errors = option.parse(argv, options)
+ if not results then
+ raise(errors)
+ end
+ return results
+end
+
+-- parse arguments with the given options
function sandbox_core_base_option.parse(argv, options, ...)
-- check
diff --git a/xmake/core/sandbox/modules/import/core/base/task.lua b/xmake/core/sandbox/modules/import/core/base/task.lua
index 030780102..9f3b74bc6 100644
--- a/xmake/core/sandbox/modules/import/core/base/task.lua
+++ b/xmake/core/sandbox/modules/import/core/base/task.lua
@@ -71,11 +71,9 @@ end
function sandbox_core_base_task.names()
- local names = {}
- for k, _ in pairs(task.tasks()) do
- table.insert(names, k)
- end
- return names
+ local default_tasks = table.keys(task.tasks())
+ local project_tasks = table.keys(project.tasks())
+ return table.join(default_tasks, project_tasks)
end
-- return module
diff --git a/xmake/modules/private/utils/complete.lua b/xmake/modules/private/utils/complete.lua
index af393cb7f..b2f9bca2d 100644
--- a/xmake/modules/private/utils/complete.lua
+++ b/xmake/modules/private/utils/complete.lua
@@ -44,6 +44,11 @@ function _complete_task(tasks, name)
has_candidate = true
end
end
+
+ if name == "" then
+ return has_candidate
+ end
+
for k, _ in pairs(tasks) do
-- not startswith
if k:find(name, 2, true) then
@@ -54,7 +59,16 @@ function _complete_task(tasks, name)
return has_candidate
end
-function _complete_option(options, name)
+function _complete_option(options, segs, name)
+ local current_options = try
+ {
+ function()
+ return option.raw_parse(segs, options)
+ end
+ }
+ -- current options is invalid
+ if not current_options then return end
+
local state = 0
if name == "-" or name == "--" then
name = ""
@@ -71,7 +85,9 @@ function _complete_option(options, name)
local opcandi = {}
for _, v in ipairs(options) do
- if v[3] == "kv" or v[3] == "k" then table.insert(opcandi, v) end
+ if current_options[v[2]] == nil then
+ if v[3] == "kv" or v[3] == "k" then table.insert(opcandi, v) end
+ end
end
for _, v in ipairs(opcandi) do
@@ -109,6 +125,12 @@ function main(position, config_use_spaces, ...)
local has_space = word:endswith(" ") or position > #word
word = word:trim()
+ if is_host("windows") then
+ if word:lower():startswith("xmake.exe") then
+ word = "xmake" .. word:sub(#"xmake.exe" + 1)
+ end
+ end
+
if word:lower():startswith("xmake ") then
word = word:sub(#"xmake " + 1)
end
@@ -140,5 +162,9 @@ function main(position, config_use_spaces, ...)
table.insert(segs, 1, task_name)
task_name = "run"
end
- _complete_option(tasks[task_name].options, has_space and "" or segs[#segs])
+
+ local incomplete_option = has_space and "" or segs[#segs]
+ if not has_space then segs[#segs] = nil end
+
+ _complete_option(tasks[task_name].options, segs, incomplete_option)
end \ No newline at end of file