Skip to content

Commit

Permalink
---@see use workspace-symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Nov 2, 2022
1 parent 3bf48c9 commit 625f5be
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 84 deletions.
47 changes: 45 additions & 2 deletions script/core/completion/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion script/core/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-- 先按照顺序排序
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -134,6 +154,8 @@ return function (uri, offset)
end
end

checkSee(source, results)

local defs = vm.getDefs(source)

for _, src in ipairs(defs) do
Expand Down
11 changes: 0 additions & 11 deletions script/core/semantic-tokens.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion script/core/type-definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
57 changes: 31 additions & 26 deletions script/core/workspace-symbol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -24,21 +24,21 @@ 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
local method = source.method
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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
8 changes: 3 additions & 5 deletions script/parser/guide.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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'
Expand Down
12 changes: 6 additions & 6 deletions script/parser/luadoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
13 changes: 7 additions & 6 deletions script/provider/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
}
Expand Down
18 changes: 0 additions & 18 deletions script/vm/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 625f5be

Please sign in to comment.