diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-03-11 15:53:15 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-03-11 15:53:15 +0800 |
commit | 396cc15abad6f2d331412714a04965b0ee1ca2a7 (patch) | |
tree | addece21790372650b1ad5e30bd926b9fc5f4020 /server | |
parent | 764084f9d2e8f7bb69aea8fc97453f10c9ba7f77 (diff) | |
download | lua-language-server-396cc15abad6f2d331412714a04965b0ee1ca2a7.zip |
require 自动完成与跨文件符号
Diffstat (limited to 'server')
-rw-r--r-- | server/src/core/completion.lua | 27 | ||||
-rw-r--r-- | server/src/core/document_symbol.lua | 37 | ||||
-rw-r--r-- | server/test/crossfile/document_symbol.lua | 6 |
3 files changed, 55 insertions, 15 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index 92a93552..f5444e67 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -327,6 +327,28 @@ local function searchSource(vm, source, word, callback) end end +local function searchInRequire(vm, select, source, callback) + if select ~= 1 then + return + end + if not vm.lsp or not vm.lsp.workspace then + return + end + if source.type ~= 'string' then + return + end + local list = vm.lsp.workspace:matchPath(vm.uri, source[1]) + for _, str in ipairs(list) do + callback(str, nil, CompletionItemKind.File, { + textEdit = { + start = source.start, + finish = source.finish, + newText = ('%q'):format(str), + } + }) + end +end + local function searchCallArg(vm, source, word, callback, pos) local results = {} for _, src in ipairs(vm.sources) do @@ -387,6 +409,11 @@ local function searchCallArg(vm, source, word, callback, pos) end end end + + -- 搜索特殊函数 + if lib.special == 'require' then + searchInRequire(vm, select, source, callback) + end end local function searchAllWords(vm, source, word, callback) diff --git a/server/src/core/document_symbol.lua b/server/src/core/document_symbol.lua index 47389ec2..dd18ac23 100644 --- a/server/src/core/document_symbol.lua +++ b/server/src/core/document_symbol.lua @@ -31,7 +31,7 @@ local SymbolKind = { TypeParameter = 26, } -local function buildLocal(source, callback) +local function buildLocal(vm, source, callback) local loc = source:bindLocal() local value = loc:getInitValue() local hvr = hover(source) @@ -47,15 +47,19 @@ local function buildLocal(source, callback) kind = SymbolKind.Variable end local valueSource = value.source - if valueSource.start == 0 then + if valueSource.start == 0 or value.uri ~= vm.uri then valueSource = source end + local name = hvr.name + if vm.uri ~= value.uri then + name = tostring(source[1] or '') + end -- 由于范围不允许交叉,为了支持 local x, y, z = 1, 2, 3 的形式 -- 范围只能限定在变量上 -- 而 local function xx() 的形式范围会包含整个 function if source.start > valueSource.start then callback { - name = hvr.name, + name = name, detail = hvr.label:gsub('[\r\n]', ''), kind = kind, range = { valueSource.start, valueSource.finish }, @@ -64,7 +68,7 @@ local function buildLocal(source, callback) } else callback { - name = hvr.name, + name = name, detail = hvr.label:gsub('[\r\n]', ''), kind = kind, range = { source.start, source.finish }, @@ -74,7 +78,7 @@ local function buildLocal(source, callback) end end -local function buildSet(source, callback) +local function buildSet(vm, source, callback) if source:bindLocal() then return end @@ -97,12 +101,19 @@ local function buildSet(source, callback) kind = SymbolKind.Object end local valueSource = value.source + if valueSource.start == 0 or value.uri ~= vm.uri then + valueSource = source + end + local name = hvr.name + if vm.uri ~= value.uri then + name = tostring(source[1] or '') + end -- 由于范围不允许交叉,为了支持 x, y, z = 1, 2, 3 的形式 -- 范围只能限定在变量上 -- 而 function xx() 的形式范围会包含整个 function if source.start > valueSource.start then callback { - name = hvr.name, + name = name, -- 前端不支持多行 detail = hvr.label:gsub('[\r\n]', ''), kind = kind, @@ -112,7 +123,7 @@ local function buildSet(source, callback) } else callback { - name = hvr.name, + name = name, -- 前端不支持多行 detail = hvr.label:gsub('[\r\n]', ''), kind = kind, @@ -123,7 +134,7 @@ local function buildSet(source, callback) end end -local function buildReturn(source, callback) +local function buildReturn(vm, source, callback) local value = source:bindFunction() if not value then return @@ -144,18 +155,18 @@ local function buildReturn(source, callback) } end -local function buildSource(source, callback) +local function buildSource(vm, source, callback) if source:action() == 'local' then - buildLocal(source, callback) + buildLocal(vm, source, callback) return end if source:action() == 'set' then - buildSet(source, callback) + buildSet(vm, source, callback) return end if source.type == 'return' then for _, src in ipairs(source) do - buildReturn(src, callback) + buildReturn(vm, src, callback) end return end @@ -197,7 +208,7 @@ return function (vm) local symbols = {} for _, source in ipairs(vm.sources) do - buildSource(source, function (data) + buildSource(vm, source, function (data) symbols[#symbols+1] = data end) end diff --git a/server/test/crossfile/document_symbol.lua b/server/test/crossfile/document_symbol.lua index e8ffbedf..1ce571d9 100644 --- a/server/test/crossfile/document_symbol.lua +++ b/server/test/crossfile/document_symbol.lua @@ -102,15 +102,17 @@ local t = { name = 't', detail = EXISTS, kind = SymbolKind.Variable, - range = {7, 34}, + range = {7, 7}, selectionRange = {7, 7}, + valueRange = {11, 34}, children = { [1] = { name = 'x', detail = EXISTS, - kind = SymbolKind.Class, + kind = SymbolKind.Function, range = {17, 17}, selectionRange = {17, 17}, + valueRange = {17, 17}, }, } } |