Skip to content

Commit 625f5be

Browse files
committed
---@see use workspace-symbol
#1344
1 parent 3bf48c9 commit 625f5be

File tree

13 files changed

+144
-84
lines changed

13 files changed

+144
-84
lines changed

script/core/completion/completion.lua

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local guide = require 'parser.guide'
1919
local await = require 'await'
2020
local postfix = require 'core.completion.postfix'
2121
local diag = require 'proto.diagnostic'
22+
local wssymbol = require 'core.workspace-symbol'
2223

2324
local diagnosticModes = {
2425
'disable-next-line',
@@ -1715,6 +1716,7 @@ local function getluaDocByErr(state, start, position)
17151716
return targetError, targetDoc
17161717
end
17171718

1719+
---@async
17181720
local function tryluaDocBySource(state, position, source, results)
17191721
if source.type == 'doc.extends.name' then
17201722
if source.parent.type == 'doc.class' then
@@ -1823,8 +1825,8 @@ local function tryluaDocBySource(state, position, source, results)
18231825
if matchKey(source[1], name) then
18241826
results[#results+1] = {
18251827
label = name,
1826-
kind = define.CompletionItemKind.Variable,
1827-
id = stack(function () ---@async
1828+
kind = define.CompletionItemKind.Variable,
1829+
id = stack(function () ---@async
18281830
return {
18291831
detail = buildDetail(loc),
18301832
description = buildDesc(loc),
@@ -1863,10 +1865,33 @@ local function tryluaDocBySource(state, position, source, results)
18631865
end
18641866
end
18651867
return true
1868+
elseif source.type == 'doc.see.name' then
1869+
local symbolds = wssymbol(source[1])
1870+
table.sort(symbolds, function (a, b)
1871+
return a.name < b.name
1872+
end)
1873+
for _, symbol in ipairs(symbolds) do
1874+
results[#results+1] = {
1875+
label = symbol.name,
1876+
kind = symbol.ckind,
1877+
id = stack(function () ---@async
1878+
return {
1879+
detail = buildDetail(symbol.source),
1880+
description = buildDesc(symbol.source),
1881+
}
1882+
end),
1883+
textEdit = {
1884+
start = source.start,
1885+
finish = source.finish,
1886+
newText = symbol.name,
1887+
},
1888+
}
1889+
end
18661890
end
18671891
return false
18681892
end
18691893

1894+
---@async
18701895
local function tryluaDocByErr(state, position, err, docState, results)
18711896
if err.type == 'LUADOC_MISS_CLASS_EXTENDS_NAME' then
18721897
local used = {}
@@ -2003,6 +2028,23 @@ local function tryluaDocByErr(state, position, err, docState, results)
20032028
description = ('```lua\n%s\n```'):format(vm.OP_OTHER_MAP[name]),
20042029
}
20052030
end
2031+
elseif err.type == 'LUADOC_MISS_SEE_NAME' then
2032+
local symbolds = wssymbol('')
2033+
table.sort(symbolds, function (a, b)
2034+
return a.name < b.name
2035+
end)
2036+
for _, symbol in ipairs(symbolds) do
2037+
results[#results+1] = {
2038+
label = symbol.name,
2039+
kind = symbol.ckind,
2040+
id = stack(function () ---@async
2041+
return {
2042+
detail = buildDetail(symbol.source),
2043+
description = buildDesc(symbol.source),
2044+
}
2045+
end),
2046+
}
2047+
end
20062048
end
20072049
end
20082050

@@ -2080,6 +2122,7 @@ local function tryluaDocOfFunction(doc, results)
20802122
}
20812123
end
20822124

2125+
---@async
20832126
local function tryLuaDoc(state, position, results)
20842127
local doc = getLuaDoc(state, position)
20852128
if not doc then

script/core/definition.lua

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local findSource = require 'core.find-source'
55
local guide = require 'parser.guide'
66
local rpath = require 'workspace.require-path'
77
local jumpSource = require 'core.jump-source'
8+
local wssymbol = require 'core.workspace-symbol'
89

910
local function sortResults(results)
1011
-- 先按照顺序排序
@@ -53,7 +54,6 @@ local accept = {
5354
['doc.extends.name'] = true,
5455
['doc.alias.name'] = true,
5556
['doc.see.name'] = true,
56-
['doc.see.field'] = true,
5757
['doc.cast.name'] = true,
5858
['doc.enum.name'] = true,
5959
['doc.field.name'] = true,
@@ -107,6 +107,26 @@ local function convertIndex(source)
107107
return source
108108
end
109109

110+
---@async
111+
---@param source parser.object
112+
---@param results table
113+
local function checkSee(source, results)
114+
if source.type ~= 'doc.see.name' then
115+
return
116+
end
117+
local symbols = wssymbol(source[1])
118+
for _, symbol in ipairs(symbols) do
119+
if symbol.name == source[1] then
120+
results[#results+1] = {
121+
target = symbol.source,
122+
source = source,
123+
uri = guide.getUri(symbol.source),
124+
}
125+
end
126+
end
127+
end
128+
129+
---@async
110130
return function (uri, offset)
111131
local ast = files.getState(uri)
112132
if not ast then
@@ -134,6 +154,8 @@ return function (uri, offset)
134154
end
135155
end
136156

157+
checkSee(source, results)
158+
137159
local defs = vm.getDefs(source)
138160

139161
for _, src in ipairs(defs) do

script/core/semantic-tokens.lua

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -592,17 +592,6 @@ local Care = util.switch()
592592
type = define.TokenTypes.class,
593593
}
594594
end)
595-
: case 'doc.see.field'
596-
: call(function (source, options, results)
597-
if not options.annotation then
598-
return
599-
end
600-
results[#results+1] = {
601-
start = source.start,
602-
finish = source.finish,
603-
type = define.TokenTypes.property,
604-
}
605-
end)
606595
: case 'doc.diagnostic'
607596
: call(function (source, options, results)
608597
if not options.annotation then

script/core/type-definition.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ local accept = {
5454
['doc.alias.name'] = true,
5555
['doc.enum.name'] = true,
5656
['doc.see.name'] = true,
57-
['doc.see.field'] = true,
5857
}
5958

6059
local function checkRequire(source, offset)

script/core/workspace-symbol.lua

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ local function buildSource(uri, source, key, results)
1212
local name = source[1]
1313
if matchKey(key, name) then
1414
results[#results+1] = {
15-
name = name,
16-
kind = define.SymbolKind.Variable,
17-
uri = uri,
18-
range = { source.start, source.finish },
15+
name = name,
16+
skind = define.SymbolKind.Variable,
17+
ckind = define.CompletionItemKind.Variable,
18+
source = source,
1919
}
2020
end
2121
elseif source.type == 'setfield'
@@ -24,21 +24,21 @@ local function buildSource(uri, source, key, results)
2424
local name = field and field[1]
2525
if name and matchKey(key, name) then
2626
results[#results+1] = {
27-
name = name,
28-
kind = define.SymbolKind.Field,
29-
uri = uri,
30-
range = { field.start, field.finish },
27+
name = name,
28+
skind = define.SymbolKind.Field,
29+
ckind = define.CompletionItemKind.Field,
30+
source = field,
3131
}
3232
end
3333
elseif source.type == 'setmethod' then
3434
local method = source.method
3535
local name = method and method[1]
3636
if name and matchKey(key, name) then
3737
results[#results+1] = {
38-
name = name,
39-
kind = define.SymbolKind.Method,
40-
uri = uri,
41-
range = { method.start, method.finish },
38+
name = name,
39+
skind = define.SymbolKind.Method,
40+
ckind = define.CompletionItemKind.Method,
41+
source = method,
4242
}
4343
end
4444
end
@@ -63,19 +63,22 @@ local function searchGlobalAndClass(key, results)
6363
local name = global:getCodeName()
6464
if matchKey(key, name) then
6565
for _, set in ipairs(global:getAllSets()) do
66-
local kind
66+
local skind, ckind
6767
if set.type == 'doc.class' then
68-
kind = define.SymbolKind.Class
68+
skind = define.SymbolKind.Class
69+
ckind = define.CompletionItemKind.Class
6970
elseif set.type == 'doc.alias' then
70-
kind = define.SymbolKind.Namespace
71+
skind = define.SymbolKind.Struct
72+
ckind = define.CompletionItemKind.Struct
7173
else
72-
kind = define.SymbolKind.Variable
74+
skind = define.SymbolKind.Variable
75+
ckind = define.CompletionItemKind.Variable
7376
end
7477
results[#results+1] = {
75-
name = name,
76-
kind = kind,
77-
uri = guide.getUri(set),
78-
range = { set.start, set.finish },
78+
name = name,
79+
skind = skind,
80+
ckind = ckind,
81+
source = set,
7982
}
8083
end
8184
await.delay()
@@ -113,10 +116,10 @@ local function searchClassField(key, results)
113116
return
114117
end
115118
results[#results+1] = {
116-
name = class .. '.' .. keyName,
117-
kind = define.SymbolKind.Field,
118-
uri = guide.getUri(field),
119-
range = { field.start, field.finish },
119+
name = class .. '.' .. keyName,
120+
skind = define.SymbolKind.Field,
121+
ckind = define.SymbolKind.Field,
122+
source = field,
120123
}
121124
end)
122125
end
@@ -135,12 +138,14 @@ local function searchWords(key, results)
135138
end
136139

137140
---@async
138-
return function (key)
141+
return function (key, includeWords)
139142
local results = {}
140143

141144
searchGlobalAndClass(key, results)
142145
searchClassField(key, results)
143-
searchWords(key, results)
146+
if includeWords then
147+
searchWords(key, results)
148+
end
144149

145150
return results
146151
end

script/parser/guide.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ local childMap = {
160160
['doc.type.field'] = {'name', 'extends'},
161161
['doc.type.sign'] = {'node', '#signs'},
162162
['doc.overload'] = {'overload', 'comment'},
163-
['doc.see'] = {'name', 'field'},
163+
['doc.see'] = {'name'},
164164
['doc.version'] = {'#versions'},
165165
['doc.diagnostic'] = {'#names'},
166166
['doc.as'] = {'as'},
@@ -1015,8 +1015,7 @@ function m.getKeyName(obj)
10151015
elseif tp == 'tableexp' then
10161016
return obj.tindex
10171017
elseif tp == 'field'
1018-
or tp == 'method'
1019-
or tp == 'doc.see.field' then
1018+
or tp == 'method' then
10201019
return obj[1]
10211020
elseif tp == 'doc.class' then
10221021
return obj.class[1]
@@ -1080,8 +1079,7 @@ function m.getKeyType(obj)
10801079
elseif tp == 'tableexp' then
10811080
return 'integer'
10821081
elseif tp == 'field'
1083-
or tp == 'method'
1084-
or tp == 'doc.see.field' then
1082+
or tp == 'method' then
10851083
return 'string'
10861084
elseif tp == 'doc.class' then
10871085
return 'string'

script/parser/luadoc.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,15 +1192,15 @@ local docSwitch = util.switch()
11921192
}
11931193
result.name = parseName('doc.see.name', result)
11941194
if not result.name then
1195+
pushWarning {
1196+
type = 'LUADOC_MISS_SEE_NAME',
1197+
start = getStart(),
1198+
finish = getFinish(),
1199+
}
11951200
return nil
11961201
end
1197-
result.start = result.name.start
1202+
result.start = result.name.start
11981203
result.finish = result.name.finish
1199-
if checkToken('symbol', '#', 1) then
1200-
nextToken()
1201-
result.field = parseName('doc.see.field', result)
1202-
result.finish = getFinish()
1203-
end
12041204
return result
12051205
end)
12061206
: case 'diagnostic'

script/provider/provider.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -954,25 +954,26 @@ m.register 'workspace/symbol' {
954954
local _ <close> = progress.create(workspace.getFirstScope().uri, lang.script.WINDOW_PROCESSING_WS_SYMBOL, 0.5)
955955
local core = require 'core.workspace-symbol'
956956

957-
local symbols = core(params.query)
957+
local symbols = core(params.query, true)
958958
if not symbols or #symbols == 0 then
959959
return nil
960960
end
961961

962962
local function convert(symbol)
963-
local state = files.getState(symbol.uri)
963+
local uri = guide.getUri(symbol.source)
964+
local state = files.getState(uri)
964965
if not state then
965966
return nil
966967
end
967968
return {
968969
name = symbol.name,
969-
kind = symbol.kind,
970+
kind = symbol.skind,
970971
location = converter.location(
971-
symbol.uri,
972+
uri,
972973
converter.packRange(
973974
state,
974-
symbol.range[1],
975-
symbol.range[2]
975+
symbol.source.start,
976+
symbol.source.finish
976977
)
977978
)
978979
}

script/vm/compiler.lua

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,13 +1666,6 @@ local compilerSwitch = util.switch()
16661666
: call(function (source)
16671667
vm.setNode(source, vm.compileNode(source.overload))
16681668
end)
1669-
: case 'doc.see.name'
1670-
: call(function (source)
1671-
local type = vm.getGlobal('type', source[1])
1672-
if type then
1673-
vm.setNode(source, type)
1674-
end
1675-
end)
16761669
: case 'doc.type.arg'
16771670
: call(function (source)
16781671
if source.extends then
@@ -1842,17 +1835,6 @@ local nodeSwitch;nodeSwitch = util.switch()
18421835
searchFieldSwitch(pn.type, uri, pn, key, false, pushResult)
18431836
end
18441837
end)
1845-
: case 'doc.see.field'
1846-
: call(function (source, lastKey, pushResult)
1847-
if lastKey then
1848-
return
1849-
end
1850-
local parentNode = vm.compileNode(source.parent.name)
1851-
local uri = guide.getUri(source)
1852-
for pn in parentNode:eachObject() do
1853-
searchFieldSwitch(pn.type, uri, pn, source[1], false, pushResult)
1854-
end
1855-
end)
18561838

18571839
function vm.compileByNodeChain(source, pushResult)
18581840
local lastKey

0 commit comments

Comments
 (0)