summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-11-02 17:29:18 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-11-02 17:29:18 +0800
commit625f5be9a6ae1c30dd5821f1dd2485073d054209 (patch)
tree2581abc44fa0b6a42b590fc842501b8a9edf9a1f
parent3bf48c900605681216ce7c14cfe0cb43e583de4f (diff)
downloadlua-language-server-625f5be9a6ae1c30dd5821f1dd2485073d054209.zip
`---@see` use workspace-symbol
#1344
-rw-r--r--script/core/completion/completion.lua47
-rw-r--r--script/core/definition.lua24
-rw-r--r--script/core/semantic-tokens.lua11
-rw-r--r--script/core/type-definition.lua1
-rw-r--r--script/core/workspace-symbol.lua57
-rw-r--r--script/parser/guide.lua8
-rw-r--r--script/parser/luadoc.lua12
-rw-r--r--script/provider/provider.lua13
-rw-r--r--script/vm/compiler.lua18
-rw-r--r--test/completion/common.lua12
-rw-r--r--test/crossfile/definition.lua1
-rw-r--r--test/definition/init.lua1
-rw-r--r--test/definition/luadoc.lua23
13 files changed, 144 insertions, 84 deletions
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua
index b665bcd1..cac74543 100644
--- a/script/core/completion/completion.lua
+++ b/script/core/completion/completion.lua
@@ -19,6 +19,7 @@ local guide = require 'parser.guide'
local await = require 'await'
local postfix = require 'core.completion.postfix'
local diag = require 'proto.diagnostic'
+local wssymbol = require 'core.workspace-symbol'
local diagnosticModes = {
'disable-next-line',
@@ -1715,6 +1716,7 @@ local function getluaDocByErr(state, start, position)
return targetError, targetDoc
end
+---@async
local function tryluaDocBySource(state, position, source, results)
if source.type == 'doc.extends.name' then
if source.parent.type == 'doc.class' then
@@ -1823,8 +1825,8 @@ local function tryluaDocBySource(state, position, source, results)
if matchKey(source[1], name) then
results[#results+1] = {
label = name,
- kind = define.CompletionItemKind.Variable,
- id = stack(function () ---@async
+ kind = define.CompletionItemKind.Variable,
+ id = stack(function () ---@async
return {
detail = buildDetail(loc),
description = buildDesc(loc),
@@ -1863,10 +1865,33 @@ local function tryluaDocBySource(state, position, source, results)
end
end
return true
+ elseif source.type == 'doc.see.name' then
+ local symbolds = wssymbol(source[1])
+ table.sort(symbolds, function (a, b)
+ return a.name < b.name
+ end)
+ for _, symbol in ipairs(symbolds) do
+ results[#results+1] = {
+ label = symbol.name,
+ kind = symbol.ckind,
+ id = stack(function () ---@async
+ return {
+ detail = buildDetail(symbol.source),
+ description = buildDesc(symbol.source),
+ }
+ end),
+ textEdit = {
+ start = source.start,
+ finish = source.finish,
+ newText = symbol.name,
+ },
+ }
+ end
end
return false
end
+---@async
local function tryluaDocByErr(state, position, err, docState, results)
if err.type == 'LUADOC_MISS_CLASS_EXTENDS_NAME' then
local used = {}
@@ -2003,6 +2028,23 @@ local function tryluaDocByErr(state, position, err, docState, results)
description = ('```lua\n%s\n```'):format(vm.OP_OTHER_MAP[name]),
}
end
+ elseif err.type == 'LUADOC_MISS_SEE_NAME' then
+ local symbolds = wssymbol('')
+ table.sort(symbolds, function (a, b)
+ return a.name < b.name
+ end)
+ for _, symbol in ipairs(symbolds) do
+ results[#results+1] = {
+ label = symbol.name,
+ kind = symbol.ckind,
+ id = stack(function () ---@async
+ return {
+ detail = buildDetail(symbol.source),
+ description = buildDesc(symbol.source),
+ }
+ end),
+ }
+ end
end
end
@@ -2080,6 +2122,7 @@ local function tryluaDocOfFunction(doc, results)
}
end
+---@async
local function tryLuaDoc(state, position, results)
local doc = getLuaDoc(state, position)
if not doc then
diff --git a/script/core/definition.lua b/script/core/definition.lua
index 1a886a91..22a43bbf 100644
--- a/script/core/definition.lua
+++ b/script/core/definition.lua
@@ -5,6 +5,7 @@ local findSource = require 'core.find-source'
local guide = require 'parser.guide'
local rpath = require 'workspace.require-path'
local jumpSource = require 'core.jump-source'
+local wssymbol = require 'core.workspace-symbol'
local function sortResults(results)
-- 先按照顺序排序
@@ -53,7 +54,6 @@ local accept = {
['doc.extends.name'] = true,
['doc.alias.name'] = true,
['doc.see.name'] = true,
- ['doc.see.field'] = true,
['doc.cast.name'] = true,
['doc.enum.name'] = true,
['doc.field.name'] = true,
@@ -107,6 +107,26 @@ local function convertIndex(source)
return source
end
+---@async
+---@param source parser.object
+---@param results table
+local function checkSee(source, results)
+ if source.type ~= 'doc.see.name' then
+ return
+ end
+ local symbols = wssymbol(source[1])
+ for _, symbol in ipairs(symbols) do
+ if symbol.name == source[1] then
+ results[#results+1] = {
+ target = symbol.source,
+ source = source,
+ uri = guide.getUri(symbol.source),
+ }
+ end
+ end
+end
+
+---@async
return function (uri, offset)
local ast = files.getState(uri)
if not ast then
@@ -134,6 +154,8 @@ return function (uri, offset)
end
end
+ checkSee(source, results)
+
local defs = vm.getDefs(source)
for _, src in ipairs(defs) do
diff --git a/script/core/semantic-tokens.lua b/script/core/semantic-tokens.lua
index b470fec2..14d6ddc8 100644
--- a/script/core/semantic-tokens.lua
+++ b/script/core/semantic-tokens.lua
@@ -592,17 +592,6 @@ local Care = util.switch()
type = define.TokenTypes.class,
}
end)
- : case 'doc.see.field'
- : call(function (source, options, results)
- if not options.annotation then
- return
- end
- results[#results+1] = {
- start = source.start,
- finish = source.finish,
- type = define.TokenTypes.property,
- }
- end)
: case 'doc.diagnostic'
: call(function (source, options, results)
if not options.annotation then
diff --git a/script/core/type-definition.lua b/script/core/type-definition.lua
index a1c2b29f..0a821f25 100644
--- a/script/core/type-definition.lua
+++ b/script/core/type-definition.lua
@@ -54,7 +54,6 @@ local accept = {
['doc.alias.name'] = true,
['doc.enum.name'] = true,
['doc.see.name'] = true,
- ['doc.see.field'] = true,
}
local function checkRequire(source, offset)
diff --git a/script/core/workspace-symbol.lua b/script/core/workspace-symbol.lua
index 6501708d..2e865594 100644
--- a/script/core/workspace-symbol.lua
+++ b/script/core/workspace-symbol.lua
@@ -12,10 +12,10 @@ local function buildSource(uri, source, key, results)
local name = source[1]
if matchKey(key, name) then
results[#results+1] = {
- name = name,
- kind = define.SymbolKind.Variable,
- uri = uri,
- range = { source.start, source.finish },
+ name = name,
+ skind = define.SymbolKind.Variable,
+ ckind = define.CompletionItemKind.Variable,
+ source = source,
}
end
elseif source.type == 'setfield'
@@ -24,10 +24,10 @@ local function buildSource(uri, source, key, results)
local name = field and field[1]
if name and matchKey(key, name) then
results[#results+1] = {
- name = name,
- kind = define.SymbolKind.Field,
- uri = uri,
- range = { field.start, field.finish },
+ name = name,
+ skind = define.SymbolKind.Field,
+ ckind = define.CompletionItemKind.Field,
+ source = field,
}
end
elseif source.type == 'setmethod' then
@@ -35,10 +35,10 @@ local function buildSource(uri, source, key, results)
local name = method and method[1]
if name and matchKey(key, name) then
results[#results+1] = {
- name = name,
- kind = define.SymbolKind.Method,
- uri = uri,
- range = { method.start, method.finish },
+ name = name,
+ skind = define.SymbolKind.Method,
+ ckind = define.CompletionItemKind.Method,
+ source = method,
}
end
end
@@ -63,19 +63,22 @@ local function searchGlobalAndClass(key, results)
local name = global:getCodeName()
if matchKey(key, name) then
for _, set in ipairs(global:getAllSets()) do
- local kind
+ local skind, ckind
if set.type == 'doc.class' then
- kind = define.SymbolKind.Class
+ skind = define.SymbolKind.Class
+ ckind = define.CompletionItemKind.Class
elseif set.type == 'doc.alias' then
- kind = define.SymbolKind.Namespace
+ skind = define.SymbolKind.Struct
+ ckind = define.CompletionItemKind.Struct
else
- kind = define.SymbolKind.Variable
+ skind = define.SymbolKind.Variable
+ ckind = define.CompletionItemKind.Variable
end
results[#results+1] = {
- name = name,
- kind = kind,
- uri = guide.getUri(set),
- range = { set.start, set.finish },
+ name = name,
+ skind = skind,
+ ckind = ckind,
+ source = set,
}
end
await.delay()
@@ -113,10 +116,10 @@ local function searchClassField(key, results)
return
end
results[#results+1] = {
- name = class .. '.' .. keyName,
- kind = define.SymbolKind.Field,
- uri = guide.getUri(field),
- range = { field.start, field.finish },
+ name = class .. '.' .. keyName,
+ skind = define.SymbolKind.Field,
+ ckind = define.SymbolKind.Field,
+ source = field,
}
end)
end
@@ -135,12 +138,14 @@ local function searchWords(key, results)
end
---@async
-return function (key)
+return function (key, includeWords)
local results = {}
searchGlobalAndClass(key, results)
searchClassField(key, results)
- searchWords(key, results)
+ if includeWords then
+ searchWords(key, results)
+ end
return results
end
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 424de63e..abc82375 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -160,7 +160,7 @@ local childMap = {
['doc.type.field'] = {'name', 'extends'},
['doc.type.sign'] = {'node', '#signs'},
['doc.overload'] = {'overload', 'comment'},
- ['doc.see'] = {'name', 'field'},
+ ['doc.see'] = {'name'},
['doc.version'] = {'#versions'},
['doc.diagnostic'] = {'#names'},
['doc.as'] = {'as'},
@@ -1015,8 +1015,7 @@ function m.getKeyName(obj)
elseif tp == 'tableexp' then
return obj.tindex
elseif tp == 'field'
- or tp == 'method'
- or tp == 'doc.see.field' then
+ or tp == 'method' then
return obj[1]
elseif tp == 'doc.class' then
return obj.class[1]
@@ -1080,8 +1079,7 @@ function m.getKeyType(obj)
elseif tp == 'tableexp' then
return 'integer'
elseif tp == 'field'
- or tp == 'method'
- or tp == 'doc.see.field' then
+ or tp == 'method' then
return 'string'
elseif tp == 'doc.class' then
return 'string'
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index 040a9a9b..b646d1a9 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -1192,15 +1192,15 @@ local docSwitch = util.switch()
}
result.name = parseName('doc.see.name', result)
if not result.name then
+ pushWarning {
+ type = 'LUADOC_MISS_SEE_NAME',
+ start = getStart(),
+ finish = getFinish(),
+ }
return nil
end
- result.start = result.name.start
+ result.start = result.name.start
result.finish = result.name.finish
- if checkToken('symbol', '#', 1) then
- nextToken()
- result.field = parseName('doc.see.field', result)
- result.finish = getFinish()
- end
return result
end)
: case 'diagnostic'
diff --git a/script/provider/provider.lua b/script/provider/provider.lua
index 8e99b354..9fede5cc 100644
--- a/script/provider/provider.lua
+++ b/script/provider/provider.lua
@@ -954,25 +954,26 @@ m.register 'workspace/symbol' {
local _ <close> = progress.create(workspace.getFirstScope().uri, lang.script.WINDOW_PROCESSING_WS_SYMBOL, 0.5)
local core = require 'core.workspace-symbol'
- local symbols = core(params.query)
+ local symbols = core(params.query, true)
if not symbols or #symbols == 0 then
return nil
end
local function convert(symbol)
- local state = files.getState(symbol.uri)
+ local uri = guide.getUri(symbol.source)
+ local state = files.getState(uri)
if not state then
return nil
end
return {
name = symbol.name,
- kind = symbol.kind,
+ kind = symbol.skind,
location = converter.location(
- symbol.uri,
+ uri,
converter.packRange(
state,
- symbol.range[1],
- symbol.range[2]
+ symbol.source.start,
+ symbol.source.finish
)
)
}
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index 03a34dbd..dcd32e77 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -1666,13 +1666,6 @@ local compilerSwitch = util.switch()
: call(function (source)
vm.setNode(source, vm.compileNode(source.overload))
end)
- : case 'doc.see.name'
- : call(function (source)
- local type = vm.getGlobal('type', source[1])
- if type then
- vm.setNode(source, type)
- end
- end)
: case 'doc.type.arg'
: call(function (source)
if source.extends then
@@ -1842,17 +1835,6 @@ local nodeSwitch;nodeSwitch = util.switch()
searchFieldSwitch(pn.type, uri, pn, key, false, pushResult)
end
end)
- : case 'doc.see.field'
- : call(function (source, lastKey, pushResult)
- if lastKey then
- return
- end
- local parentNode = vm.compileNode(source.parent.name)
- local uri = guide.getUri(source)
- for pn in parentNode:eachObject() do
- searchFieldSwitch(pn.type, uri, pn, source[1], false, pushResult)
- end
- end)
function vm.compileByNodeChain(source, pushResult)
local lastKey
diff --git a/test/completion/common.lua b/test/completion/common.lua
index bf43a463..7266de16 100644
--- a/test/completion/common.lua
+++ b/test/completion/common.lua
@@ -3963,3 +3963,15 @@ t.<??>
kind = define.CompletionItemKind.Field,
},
}
+
+TEST [[
+---@class ABCD
+
+---@see ABCD<??>
+]]
+{
+ {
+ label = 'ABCD',
+ kind = define.CompletionItemKind.Class,
+ },
+}
diff --git a/test/crossfile/definition.lua b/test/crossfile/definition.lua
index 45f21697..ef9b5240 100644
--- a/test/crossfile/definition.lua
+++ b/test/crossfile/definition.lua
@@ -26,6 +26,7 @@ local function founded(targets, results)
return true
end
+---@async
function TEST(datas)
local targetList = {}
local sourceList
diff --git a/test/definition/init.lua b/test/definition/init.lua
index 7e0a7989..eb6f1435 100644
--- a/test/definition/init.lua
+++ b/test/definition/init.lua
@@ -21,6 +21,7 @@ local function founded(targets, results)
return true
end
+---@async
function TEST(script)
local newScript, catched = catch(script, '!?')
diff --git a/test/definition/luadoc.lua b/test/definition/luadoc.lua
index c14e1242..d8da938e 100644
--- a/test/definition/luadoc.lua
+++ b/test/definition/luadoc.lua
@@ -207,23 +207,30 @@ y.<?a?>
]]
TEST [[
----@class <!loli!>
-local unit!>
+---@class <!A!>
+local mt
-function unit:pants()
+function mt:f()
end
----@see <?loli?>
+---@see <?A?>
]]
TEST [[
----@class loli
-local unit
+---@class A
+local mt
-function unit:<!pants!>()
+function <!mt:f!>()
end
----@see loli#<?pants?>
+---@see <?A.f?>
+]]
+
+TEST [[
+AAA = {}
+<!AAA.BBB!> = 1
+
+---@see <?AAA.BBB?>
]]
TEST [[