summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/core/document-symbol.lua130
-rw-r--r--test/document_symbol/init.lua149
2 files changed, 153 insertions, 126 deletions
diff --git a/script/core/document-symbol.lua b/script/core/document-symbol.lua
index 6629ccbc..90d312e3 100644
--- a/script/core/document-symbol.lua
+++ b/script/core/document-symbol.lua
@@ -23,6 +23,12 @@ local function buildName(source, text)
return text:sub(startOffset + 1, finishOffset)
end
end
+ if source.type == 'tableindex' then
+ if source.index then
+ local finishOffset = guide.positionToOffset(state, source.finish)
+ return text:sub(startOffset + 1, finishOffset)
+ end
+ end
local finishOffset = guide.positionToOffset(state, source.finish)
return text:sub(startOffset + 1, finishOffset)
end
@@ -46,39 +52,6 @@ local function buildFunctionParams(func)
return table.concat(params, ', ')
end
-local function buildFunction(source, text, symbols)
- local name = buildName(source, text)
- local func = source.value
- if source.type == 'tablefield'
- or source.type == 'setfield' then
- source = source.field
- if not source then
- return
- end
- end
- local range, kind
- if func.start > source.finish then
- -- a = function()
- range = { source.start, func.finish }
- else
- -- function f()
- range = { func.start, func.finish }
- end
- if source.type == 'setmethod' then
- kind = define.SymbolKind.Method
- else
- kind = define.SymbolKind.Function
- end
- symbols[#symbols+1] = {
- name = name,
- detail = ('function (%s)'):format(buildFunctionParams(func)),
- kind = kind,
- range = range,
- selectionRange = { source.start, source.finish },
- valueRange = { func.start, func.finish },
- }
-end
-
local function buildTable(tbl)
local buf = {}
for i = 1, 3 do
@@ -91,107 +64,110 @@ local function buildTable(tbl)
buf[#buf+1] = ('%s'):format(field.field[1])
end
end
+ if #tbl > 3 then
+ buf[#buf+1] = ('...(+%d)'):format(#tbl - 3)
+ end
return table.concat(buf, ', ')
end
-local function buildValue(source, text, symbols)
+local function buildValue(source, text, used, symbols)
local name = buildName(source, text)
local range, sRange, valueRange, kind
local details = {}
- if source.type == 'local' then
+ if source.type == 'local' then
if source.parent.type == 'funcargs' then
- details[1] = 'param'
- range = { source.start, source.finish }
- sRange = { source.start, source.finish }
- kind = define.SymbolKind.Constant
+ kind = define.SymbolKind.Constant
else
- details[1] = 'local'
- range = { source.start, source.finish }
- sRange = { source.start, source.finish }
- kind = define.SymbolKind.Variable
+ kind = define.SymbolKind.Variable
end
+ range = { source.start, source.finish }
+ sRange = { source.start, source.finish }
elseif source.type == 'setlocal' then
- details[1] = 'setlocal'
range = { source.start, source.finish }
sRange = { source.start, source.finish }
- kind = define.SymbolKind.Variable
elseif source.type == 'setglobal' then
- details[1] = 'global'
range = { source.start, source.finish }
sRange = { source.start, source.finish }
- kind = define.SymbolKind.Class
elseif source.type == 'tablefield' then
if not source.field then
return
end
- details[1] = 'field'
range = { source.field.start, source.field.finish }
sRange = { source.field.start, source.field.finish }
- kind = define.SymbolKind.Property
+ elseif source.type == 'tableindex' then
+ if not source.index then
+ return
+ end
+ range = { source.index.start, source.index.finish }
+ sRange = { source.index.start, source.index.finish }
elseif source.type == 'setfield' then
if not source.field then
return
end
- details[1] = 'field'
range = { source.field.start, source.field.finish }
sRange = { source.field.start, source.field.finish }
- kind = define.SymbolKind.Field
+ elseif source.type == 'setmethod' then
+ if not source.method then
+ return
+ end
+ range = { source.method.start, source.method.finish }
+ sRange = { source.start, source.finish }
else
return
end
if source.value then
+ used[source.value] = true
local literal = source.value[1]
if source.value.type == 'boolean' then
- details[2] = ' boolean'
+ kind = define.SymbolKind.Boolean
if literal ~= nil then
- details[3] = ' = '
- details[4] = util.viewLiteral(source.value[1])
+ details[#details+1] = util.viewLiteral(source.value[1])
end
elseif source.value.type == 'string' then
- details[2] = ' string'
+ kind = define.SymbolKind.String
if literal ~= nil then
- details[3] = ' = '
- details[4] = util.viewLiteral(source.value[1])
+ details[#details+1] = util.viewLiteral(source.value[1])
end
elseif source.value.type == 'number'
or source.value.type == 'integer' then
- details[2] = ' number'
+ kind = define.SymbolKind.Number
if literal ~= nil then
- details[3] = ' = '
- details[4] = util.viewLiteral(source.value[1])
+ details[#details+1] = util.viewLiteral(source.value[1])
end
elseif source.value.type == 'table' then
- details[2] = ' {'
- details[3] = buildTable(source.value)
- details[4] = '}'
+ kind = define.SymbolKind.Object
+ if #source.value > 0 then
+ details[#details+1] = '{'
+ details[#details+1] = buildTable(source.value)
+ details[#details+1] = '}'
+ end
valueRange = { source.value.start, source.value.finish }
elseif source.value.type == 'select' then
if source.value.vararg and source.value.vararg.type == 'call' then
valueRange = { source.value.start, source.value.finish }
end
+ elseif source.value.type == 'function' then
+ details[#details+1] = ('function (%s)'):format(buildFunctionParams(source.value))
+ if source.type == 'setmethod' then
+ kind = define.SymbolKind.Method
+ else
+ kind = define.SymbolKind.Function
+ end
+ valueRange = { source.value.start, source.value.finish }
+ range[1] = math.min(source.value.start, source.start)
end
range = { range[1], source.value.finish }
end
symbols[#symbols+1] = {
name = name,
detail = table.concat(details),
- kind = kind,
+ kind = kind or define.SymbolKind.Variable,
range = range,
selectionRange = sRange,
valueRange = valueRange,
}
end
-local function buildSet(source, text, used, symbols)
- local value = source.value
- if value and value.type == 'function' then
- used[value] = true
- buildFunction(source, text, symbols)
- else
- buildValue(source, text, symbols)
- end
-end
-
local function buildAnonymousFunction(source, text, used, symbols)
if used[source] then
return
@@ -223,9 +199,11 @@ local function buildSource(source, text, used, symbols)
or source.type == 'setglobal'
or source.type == 'setfield'
or source.type == 'setmethod'
- or source.type == 'tablefield' then
+ or source.type == 'tablefield'
+ or source.type == 'tableexp'
+ or source.type == 'tableindex' then
await.delay()
- buildSet(source, text, used, symbols)
+ buildValue(source, text, used, symbols)
elseif source.type == 'function' then
await.delay()
buildAnonymousFunction(source, text, used, symbols)
diff --git a/test/document_symbol/init.lua b/test/document_symbol/init.lua
index 4ce75573..2150e879 100644
--- a/test/document_symbol/init.lua
+++ b/test/document_symbol/init.lua
@@ -62,8 +62,8 @@ A = 1
{
[1] = {
name = 'A',
- detail = 'global number = 1',
- kind = define.SymbolKind.Class,
+ detail = '1',
+ kind = define.SymbolKind.Number,
range = {0, 5},
selectionRange = {0, 1},
}
@@ -219,8 +219,8 @@ local z
{
[1] = {
name = 'x',
- detail = 'local number = 1',
- kind = define.SymbolKind.Variable,
+ detail = '1',
+ kind = define.SymbolKind.Number,
range = {6, 11},
selectionRange = {6, 7},
},
@@ -234,15 +234,15 @@ local z
children = {
[1] = {
name = 'x',
- detail = 'local string = "x"',
- kind = define.SymbolKind.Variable,
+ detail = '"x"',
+ kind = define.SymbolKind.String,
range = {20010, 20017},
selectionRange = {20010, 20011},
},
[2] = {
name = 'y',
- detail = 'local {}',
- kind = define.SymbolKind.Variable,
+ detail = '',
+ kind = define.SymbolKind.Object,
range = {30010, 30016},
selectionRange = {30010, 30011},
valueRange = {30014, 30016},
@@ -259,14 +259,14 @@ local z
},
[3] = {
name = 'y',
- detail = 'local boolean = true',
- kind = define.SymbolKind.Variable,
+ detail = 'true',
+ kind = define.SymbolKind.Boolean,
range = {60006, 60014},
selectionRange = {60006, 60007},
},
[4] = {
name = 'z',
- detail = 'local',
+ detail = '',
kind = define.SymbolKind.Variable,
range = {70006, 70007},
selectionRange = {70006, 70007},
@@ -283,33 +283,82 @@ local t = {
{
[1] = {
name = 't',
- detail = 'local {a, b, c}',
- kind = define.SymbolKind.Variable,
+ detail = '{a, b, c}',
+ kind = define.SymbolKind.Object,
range = {6, 40001},
selectionRange = {6, 7},
valueRange = {10, 40001},
children = {
[1] = {
name = 'a',
- detail = 'field number = 1',
- kind = define.SymbolKind.Property,
+ detail = '1',
+ kind = define.SymbolKind.Number,
+ range = {10004, 10009},
+ selectionRange = {10004, 10005},
+ },
+ [2] = {
+ name = 'b',
+ detail = '2',
+ kind = define.SymbolKind.Number,
+ range = {20004, 20009},
+ selectionRange = {20004, 20005},
+ },
+ [3] = {
+ name = 'c',
+ detail = '3',
+ kind = define.SymbolKind.Number,
+ range = {30004, 30009},
+ selectionRange = {30004, 30005},
+ },
+ }
+ }
+}
+
+TEST [[
+local t = {
+ a = 1,
+ b = 2,
+ c = 3,
+ d = 4,
+}
+]]
+{
+ [1] = {
+ name = 't',
+ detail = '{a, b, c, ...(+1)}',
+ kind = define.SymbolKind.Object,
+ range = {6, 50001},
+ selectionRange = {6, 7},
+ valueRange = {10, 50001},
+ children = {
+ [1] = {
+ name = 'a',
+ detail = '1',
+ kind = define.SymbolKind.Number,
range = {10004, 10009},
selectionRange = {10004, 10005},
},
[2] = {
name = 'b',
- detail = 'field number = 2',
- kind = define.SymbolKind.Property,
+ detail = '2',
+ kind = define.SymbolKind.Number,
range = {20004, 20009},
selectionRange = {20004, 20005},
},
[3] = {
name = 'c',
- detail = 'field number = 3',
- kind = define.SymbolKind.Property,
+ detail = '3',
+ kind = define.SymbolKind.Number,
range = {30004, 30009},
selectionRange = {30004, 30005},
},
+ [4] = {
+ name = 'd',
+ detail = '4',
+ kind = define.SymbolKind.Number,
+ range = {40004, 40009},
+ selectionRange = {40004, 40005},
+ },
}
}
}
@@ -324,24 +373,24 @@ local t = {
{
[1] = {
name = 't',
- detail = 'local {a}',
- kind = define.SymbolKind.Variable,
+ detail = '{a}',
+ kind = define.SymbolKind.Object,
range = {6, 40001},
selectionRange = {6, 7},
valueRange = {10, 40001},
children = {
[1] = {
name = 'a',
- detail = 'field {b}',
- kind = define.SymbolKind.Property,
+ detail = '{b}',
+ kind = define.SymbolKind.Object,
range = {10004, 30005},
selectionRange = {10004, 10005},
valueRange = {10008, 30005},
children = {
[1] = {
name = 'b',
- detail = EXISTS,
- kind = define.SymbolKind.Property,
+ detail = '1',
+ kind = define.SymbolKind.Number,
range = {20008, 20013},
selectionRange = {20008, 20009},
}
@@ -367,8 +416,8 @@ g = 1
},
[2] = {
name = 'g',
- detail = 'setlocal number = 1',
- kind = define.SymbolKind.Variable,
+ detail = '1',
+ kind = define.SymbolKind.Number,
range = {30000, 30005},
selectionRange = {30000, 30001},
}
@@ -390,21 +439,21 @@ end
children = {
[1] = {
name = 'a',
- detail = 'param',
+ detail = '',
kind = define.SymbolKind.Constant,
range = {11, 12},
selectionRange = {11, 12},
},
[2] = {
name = 'b',
- detail = 'param',
+ detail = '',
kind = define.SymbolKind.Constant,
range = {14, 15},
selectionRange = {14, 15},
},
[3] = {
name = 'x',
- detail = 'local',
+ detail = '',
kind = define.SymbolKind.Variable,
range = {10010, 10017},
selectionRange = {10010, 10011},
@@ -423,8 +472,8 @@ local v = t
]]{
[1] = {
name = 't',
- detail = 'local {a, b}',
- kind = define.SymbolKind.Variable,
+ detail = '{a, b}',
+ kind = define.SymbolKind.Object,
range = {6, 30001},
selectionRange = {6, 7},
valueRange = {10, 30001},
@@ -432,7 +481,7 @@ local v = t
},
[2] = {
name = 'v',
- detail = 'local',
+ detail = '',
kind = define.SymbolKind.Variable,
range = {50006, 50011},
selectionRange = {50006, 50007},
@@ -445,7 +494,7 @@ local function
]]{
[1] = {
name = 'x',
- detail = 'local',
+ detail = '',
kind = define.SymbolKind.Variable,
range = {6, 7},
selectionRange = {6, 7},
@@ -453,7 +502,7 @@ local function
[2] = {
name = "",
detail = "function ()",
- kind = 12,
+ kind = define.SymbolKind.Function,
range = {10006, 10014},
selectionRange = {10006, 10014},
valueRange = {10006, 10014},
@@ -474,8 +523,8 @@ local a, b = {
]]{
[1] = {
name = 'a',
- detail = 'local {x1, y1, z1}',
- kind = define.SymbolKind.Variable,
+ detail = '{x1, y1, z1}',
+ kind = define.SymbolKind.Object,
range = {6, 40001},
selectionRange = {6, 7},
valueRange = {13, 40001},
@@ -483,8 +532,8 @@ local a, b = {
},
[2] = {
name = 'b',
- detail = 'local {x2, y2, z2}',
- kind = define.SymbolKind.Variable,
+ detail = '{x2, y2, z2}',
+ kind = define.SymbolKind.Object,
range = {9, 80001},
selectionRange = {9, 10},
valueRange = {40003, 80001},
@@ -519,7 +568,7 @@ end
children = {
[1] = {
name = 'c',
- detail = 'local',
+ detail = '',
kind = define.SymbolKind.Variable,
range = {40010, 40011},
selectionRange = {40010, 40011},
@@ -536,7 +585,7 @@ local t = f({
{
[1] = {
name = 't',
- detail = 'local',
+ detail = '',
kind = define.SymbolKind.Variable,
range = {6, 20002},
selectionRange = {6, 7},
@@ -544,8 +593,8 @@ local t = f({
children = {
[1] = {
name = 'k',
- detail = 'field number = 1',
- kind = define.SymbolKind.Property,
+ detail = '1',
+ kind = define.SymbolKind.Number,
range = {10004, 10009},
selectionRange = {10004, 10005},
}
@@ -562,8 +611,8 @@ end
{
[1] = {
name = 't',
- detail = 'local {}',
- kind = define.SymbolKind.Variable,
+ detail = '',
+ kind = define.SymbolKind.Object,
range = {6, 12},
selectionRange = {6, 7},
valueRange = {10, 12},
@@ -578,14 +627,14 @@ end
children = {
[1] = {
name = 'a',
- detail = 'param',
+ detail = '',
kind = define.SymbolKind.Constant,
range = {20017, 20018},
selectionRange = {20017, 20018},
},
[2] = {
name = 'b',
- detail = 'param',
+ detail = '',
kind = define.SymbolKind.Constant,
range = {20020, 20021},
selectionRange = {20020, 20021},
@@ -603,7 +652,7 @@ local a = f {
{
[1] = {
name = 'a',
- detail = 'local',
+ detail = '',
kind = define.SymbolKind.Variable,
range = {6, 30001},
selectionRange = {6, 7},
@@ -649,8 +698,8 @@ local root = {
{
[1] = {
name = 'root',
- detail = 'local {inner_function}',
- kind = define.SymbolKind.Variable,
+ detail = '{inner_function}',
+ kind = define.SymbolKind.Object,
range = {6, 50001},
selectionRange = {6, 10},
valueRange = {13, 50001},