Skip to content

Commit 7047393

Browse files
committed
improve document-symbol
1 parent f7f0929 commit 7047393

File tree

3 files changed

+79
-29
lines changed

3 files changed

+79
-29
lines changed

script/core/document-symbol.lua

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,28 +199,57 @@ local function buildValue(source, sub, used, symbols)
199199
}
200200
end
201201

202-
local function buildAnonymousFunction(source, sub, used, symbols)
202+
local function buildAnonymous(source, sub, used, symbols)
203203
if used[source] then
204204
return
205205
end
206206
used[source] = true
207207
local head = ''
208208
local parent = source.parent
209209
if parent.type == 'return' then
210-
head = 'return '
210+
head = 'return'
211211
elseif parent.type == 'callargs' then
212212
local call = parent.parent
213213
local node = call.node
214-
head = buildName(node, sub) .. ' -> '
214+
head = buildName(node, sub) .. ' ->'
215+
end
216+
if source.type == 'function' then
217+
symbols[#symbols+1] = {
218+
name = head,
219+
detail = ('function (%s)'):format(buildFunctionParams(source)),
220+
kind = define.SymbolKind.Function,
221+
range = { source.start, source.finish },
222+
selectionRange = { source.keyword[1], source.keyword[2] },
223+
valueRange = { source.start, source.finish },
224+
}
225+
elseif source.type == 'table' then
226+
local kind = define.SymbolKind.Object
227+
local details = {}
228+
local lastField = source[#source]
229+
if lastField then
230+
if lastField.type == 'tableexp'
231+
and lastField.tindex == #source then
232+
-- Array
233+
kind = define.SymbolKind.Array
234+
details[#details+1] = '['
235+
details[#details+1] = buildArray(source, sub)
236+
details[#details+1] = ']'
237+
else
238+
-- Object
239+
details[#details+1] = '{'
240+
details[#details+1] = buildTable(source, sub)
241+
details[#details+1] = '}'
242+
end
243+
end
244+
symbols[#symbols+1] = {
245+
name = head,
246+
detail = table.concat(details),
247+
kind = kind,
248+
range = { source.start, source.finish },
249+
selectionRange = { source.start, source.finish },
250+
valueRange = { source.start, source.finish },
251+
}
215252
end
216-
symbols[#symbols+1] = {
217-
name = '',
218-
detail = ('%sfunction (%s)'):format(head, buildFunctionParams(source)),
219-
kind = define.SymbolKind.Function,
220-
range = { source.start, source.finish },
221-
selectionRange = { source.keyword[1], source.keyword[2] },
222-
valueRange = { source.start, source.finish },
223-
}
224253
end
225254

226255
local function buildBlock(source, sub, used, symbols)
@@ -280,8 +309,9 @@ local function buildSource(source, sub, used, symbols)
280309
or source.type == 'tableexp'
281310
or source.type == 'tableindex' then
282311
buildValue(source, sub, used, symbols)
283-
elseif source.type == 'function' then
284-
buildAnonymousFunction(source, sub, used, symbols)
312+
elseif source.type == 'function'
313+
or source.type == 'table' then
314+
buildAnonymous(source, sub, used, symbols)
285315
elseif source.type == 'if'
286316
or source.type == 'while'
287317
or source.type == 'in'

script/provider/provider.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ m.register 'textDocument/documentSymbol' {
785785
symbol.selectionRange[2]
786786
)
787787
if symbol.name == '' then
788-
symbol.name = lang.script.SYMBOL_ANONYMOUS
788+
symbol.name = ' '
789789
end
790790
symbol.valueRange = nil
791791
if symbol.children then

test/document_symbol/init.lua

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ end
105105
]]
106106
{
107107
[1] = {
108-
name = '',
109-
detail = 'return function ()',
108+
name = 'return',
109+
detail = 'function ()',
110110
kind = define.SymbolKind.Function,
111111
range = {7, 10003},
112112
selectionRange = {7, 15},
@@ -565,11 +565,21 @@ local t = f({
565565
valueRange = {10, 20002},
566566
children = {
567567
[1] = {
568-
name = 'k',
569-
detail = '1',
570-
kind = define.SymbolKind.Number,
571-
range = {10004, 10009},
572-
selectionRange = {10004, 10005},
568+
name = 'f ->',
569+
detail = '{k}',
570+
kind = define.SymbolKind.Object,
571+
range = {12, 20001},
572+
selectionRange = {12, 20001},
573+
valueRange = {12, 20001},
574+
children = {
575+
[1] = {
576+
name = 'k',
577+
detail = '1',
578+
kind = define.SymbolKind.Number,
579+
range = {10004, 10009},
580+
selectionRange = {10004, 10005},
581+
}
582+
}
573583
}
574584
}
575585
}
@@ -632,12 +642,22 @@ local a = f {
632642
valueRange = {10, 30001},
633643
children = {
634644
[1] = {
635-
name = 'x',
636-
detail = 'function ()',
637-
kind = define.SymbolKind.Function,
638-
range = {10004, 20007},
639-
selectionRange = {10004, 10005},
640-
valueRange = {10008, 20007},
645+
name = 'f ->',
646+
detail = '{x}',
647+
kind = define.SymbolKind.Object,
648+
range = {12, 30001},
649+
selectionRange = {12, 30001},
650+
valueRange = {12, 30001},
651+
children = {
652+
[1] = {
653+
name = 'x',
654+
detail = 'function ()',
655+
kind = define.SymbolKind.Function,
656+
range = {10004, 20007},
657+
selectionRange = {10004, 10005},
658+
valueRange = {10008, 20007},
659+
}
660+
}
641661
}
642662
}
643663
}
@@ -650,8 +670,8 @@ end)
650670
]]
651671
{
652672
[1] = {
653-
name = '',
654-
detail = 'table.sort -> function (a, b)',
673+
name = 'table.sort ->',
674+
detail = 'function (a, b)',
655675
kind = define.SymbolKind.Function,
656676
range = {14, 20003},
657677
selectionRange = {14, 22},

0 commit comments

Comments
 (0)