diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-07 17:11:26 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-07 17:11:26 +0800 |
commit | f3260ceb043848c26dd7ef45d0a5210ea6d4a5ba (patch) | |
tree | 90a0faf0de315b175154f23ba3287c9ea86aaf95 /server | |
parent | 2b03350caa83e66d6bb4cd73be3809ac9cea2225 (diff) | |
download | lua-language-server-f3260ceb043848c26dd7ef45d0a5210ea6d4a5ba.zip |
hover支持变量对象
Diffstat (limited to 'server')
-rw-r--r-- | server/libs/lua53/string.lni | 33 | ||||
-rw-r--r-- | server/main.lua | 3 | ||||
-rw-r--r-- | server/src/matcher/find_lib.lua | 33 | ||||
-rw-r--r-- | server/src/matcher/hover.lua | 40 | ||||
-rw-r--r-- | server/test/find_lib/init.lua | 5 | ||||
-rw-r--r-- | server/test/main.lua | 3 |
6 files changed, 91 insertions, 26 deletions
diff --git a/server/libs/lua53/string.lni b/server/libs/lua53/string.lni index 0c1fc2b8..4b22ef16 100644 --- a/server/libs/lua53/string.lni +++ b/server/libs/lua53/string.lni @@ -1,5 +1,34 @@ +[string] +type = 'table' +[[.source]] +type = 'global' +[[.source]] +type = 'library' +name = 'string' + [default] type = 'function' +[[.parent]] +type = 'global' +name = 'string' +[[.parent]] +type = 'library' +name = 'string' +[[.parent]] +type = 'object' +name = 'string' -[string] -type = 'table' +[sub] +[[.args]] +name = 's' +type = 'string' +[[.args]] +name = 'i' +type = 'integer' +[[.args]] +name = 'j' +type = 'integer' +optional = 'self' +default = -1 +[[.returns]] +type = 'string' diff --git a/server/main.lua b/server/main.lua index 3ea02e6b..84106c26 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,5 +1,8 @@ local fs = require 'bee.filesystem' + ROOT = fs.current_path() +LANG = LANG or 'en-US' + package.path = (ROOT / 'src' / '?.lua'):string() .. ';' .. (ROOT / 'src' / '?' / 'init.lua'):string() diff --git a/server/src/matcher/find_lib.lua b/server/src/matcher/find_lib.lua index e91568fc..5da379c6 100644 --- a/server/src/matcher/find_lib.lua +++ b/server/src/matcher/find_lib.lua @@ -160,7 +160,7 @@ local function checkParentAsGlobal(parentValue, name, parent) if not parentName then return nil end - return ('%s.%s'):format(parentName, name) + return ('%s.%s'):format(parent.name, name) end local function checkParentAsLibrary(parentValue, name, parent) @@ -168,7 +168,14 @@ local function checkParentAsLibrary(parentValue, name, parent) if not parentName then return nil end - return ('%s.%s'):format(parentName, name) + return ('%s.%s'):format(parent.name, name) +end + +local function checkParentAsObject(parentValue, name, parent) + if parentValue.type ~= parent.name then + return nil + end + return ('*%s:%s'):format(parent.name, name) end local function checkParent(value, name, lib) @@ -187,12 +194,17 @@ local function checkParent(value, name, lib) if parent.type == 'global' then local fullKey = checkParentAsGlobal(parentValue, name, parent) if fullKey then - return fullKey + return fullKey, false end elseif parent.type == 'library' then local fullKey = checkParentAsLibrary(parentValue, name, parent) if fullKey then - return fullKey + return fullKey, false + end + elseif parent.type == 'object' then + local fullKey = checkParentAsObject(parentValue, name, parent) + if fullKey then + return fullKey, true end end end @@ -203,16 +215,19 @@ local function findLib(var, libs) local value = var.value or var for name, lib in pairs(libs) do local fullKey = checkSource(value, name, lib) - or checkParent(value, name, lib) if fullKey then - return lib, fullKey + return lib, fullKey, false + end + local fullKey, oo = checkParent(value, name, lib) + if fullKey then + return lib, fullKey, oo end end - return nil, nil + return nil, nil, nil end return function (var) local libs = getLibs() - local lib, fullKey = findLib(var, libs) - return lib, fullKey + local lib, fullKey, oo = findLib(var, libs) + return lib, fullKey, oo end diff --git a/server/src/matcher/hover.lua b/server/src/matcher/hover.lua index 123dcfde..9faa426b 100644 --- a/server/src/matcher/hover.lua +++ b/server/src/matcher/hover.lua @@ -2,21 +2,29 @@ local findResult = require 'matcher.find_result' local findLib = require 'matcher.find_lib' local Cache = {} +local OoCache = {} -local function buildArgs(lib) +local function buildArgs(lib, oo) if not lib.args then return '' end + local start + if oo then + start = 2 + else + start = 1 + end local strs = {} - for i, arg in ipairs(lib.args) do + for i = start, #lib.args do + local arg = lib.args[i] if arg.optional then - if i > 1 then + if i > start then strs[#strs+1] = ' [' else strs[#strs+1] = '[' end end - if i > 1 then + if i > start then strs[#strs+1] = ', ' end if arg.name then @@ -111,8 +119,8 @@ local function buildEnum(lib) return table.concat(strs) end -local function buildFunctionHover(lib, name) - local title = ('function %s(%s)%s'):format(name, buildArgs(lib), buildReturns(lib)) +local function buildFunctionHover(lib, fullKey, oo) + local title = ('function %s(%s)%s'):format(fullKey, buildArgs(lib, oo), buildReturns(lib)) local enum = buildEnum(lib) local tip = lib.description or '' return ([[ @@ -137,8 +145,8 @@ local function buildField(lib) return table.concat(strs) end -local function buildTableHover(lib, name) - local title = ('table %s'):format(name) +local function buildTableHover(lib, fullKey) + local title = ('table %s'):format(fullKey) local field = buildField(lib) local tip = lib.description or '' return ([[ @@ -162,22 +170,24 @@ return function (results, pos) return nil end local var = result.var - local lib, name = findLib(var) + local lib, fullKey, oo = findLib(var) if not lib then return nil end - if not Cache[lib] then + local cache = oo and OoCache or Cache + + if not cache[lib] then if lib.type == 'function' then - Cache[lib] = buildFunctionHover(lib, name) or '' + cache[lib] = buildFunctionHover(lib, fullKey, oo) or '' elseif lib.type == 'table' then - Cache[lib] = buildTableHover(lib, name) or '' + cache[lib] = buildTableHover(lib, fullKey) or '' elseif lib.type == 'string' then - Cache[lib] = lib.description or '' + cache[lib] = lib.description or '' else - Cache[lib] = '' + cache[lib] = '' end end - return Cache[lib] + return cache[lib] end diff --git a/server/test/find_lib/init.lua b/server/test/find_lib/init.lua index 1efb2423..910167c8 100644 --- a/server/test/find_lib/init.lua +++ b/server/test/find_lib/init.lua @@ -78,3 +78,8 @@ t.<?insert?>() TEST 'table.insert' [[ require 'table'.<?insert?>() ]] + +TEST '*string:sub' [[ +local str = 'xxx' +str:<?sub?>(1, 1) +]] diff --git a/server/test/main.lua b/server/test/main.lua index 9b6e2c02..fc9c99a5 100644 --- a/server/test/main.lua +++ b/server/test/main.lua @@ -1,5 +1,8 @@ local fs = require 'bee.filesystem' + ROOT = fs.current_path() +LANG = 'en-US' + package.path = (ROOT / 'src' / '?.lua'):string() .. ';' .. (ROOT / 'src' / '?' / 'init.lua'):string() .. ';' .. (ROOT / 'test' / '?.lua'):string() |