summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-09-22 21:50:50 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-09-22 21:50:50 +0800
commitc608e84003aed61a16484296edad3fe4ff363322 (patch)
tree5b1c7fd404dde785973ea6fa56e80fa0f990aa73
parent087a93d3df198aef7f0b7fb5a5af34b09fa630a9 (diff)
downloadlua-language-server-c608e84003aed61a16484296edad3fe4ff363322.zip
update
-rw-r--r--script/core/signature.lua36
-rw-r--r--test/signature/init.lua193
2 files changed, 77 insertions, 152 deletions
diff --git a/script/core/signature.lua b/script/core/signature.lua
index 26d9867c..007a3787 100644
--- a/script/core/signature.lua
+++ b/script/core/signature.lua
@@ -7,20 +7,22 @@ local guide = require 'parser.guide'
local lookback = require 'core.look-backward'
local function findNearCall(uri, ast, pos)
- local text = files.getText(uri)
+ local text = files.getText(uri)
+ local state = files.getState(uri)
local nearCall
guide.eachSourceContain(ast.ast, pos, function (src)
if src.type == 'call'
or src.type == 'table'
or src.type == 'function' then
+ local finishOffset = guide.positionToOffset(state, src.finish)
-- call(),$
if src.finish <= pos
- and text:sub(src.finish, src.finish) == ')' then
+ and text:sub(finishOffset, finishOffset) == ')' then
return
end
-- {},$
if src.finish <= pos
- and text:sub(src.finish, src.finish) == '}' then
+ and text:sub(finishOffset, finishOffset) == '}' then
return
end
if not nearCall or nearCall.start <= src.start then
@@ -56,13 +58,13 @@ local function makeOneSignature(source, oop, index)
for start, finish in converted:gmatch '%s*()[^,]+()' do
i = i + 1
params[i] = {
- label = {start + argStart, finish - 1 + argStart},
+ label = {start + argStart - 1, finish - 1 + argStart},
}
end
-- 不定参数
if index > i and i > 0 then
local lastLabel = params[i].label
- local text = label:sub(lastLabel[1], lastLabel[2])
+ local text = label:sub(lastLabel[1] + 1, lastLabel[2])
if text == '...' then
index = i
end
@@ -88,11 +90,15 @@ local function makeSignatures(text, call, pos)
args[#args+1] = arg
end
end
+ local uri = guide.getUri(call)
+ local state = files.getState(uri)
for i, arg in ipairs(args) do
- local start = lookback.findTargetSymbol(text, arg.start - 1, '(')
- or lookback.findTargetSymbol(text, arg.start - 1, ',')
- or arg.start
- if start > pos then
+ local startOffset = guide.positionToOffset(state, arg.start)
+ startOffset = lookback.findTargetSymbol(text, startOffset, '(')
+ or lookback.findTargetSymbol(text, startOffset, ',')
+ or startOffset
+ local startPos = guide.offsetToPosition(state, startOffset)
+ if startPos > pos then
index = i - 1
break
end
@@ -102,7 +108,8 @@ local function makeSignatures(text, call, pos)
end
end
if not index then
- local backSymbol = lookback.findSymbol(text, pos)
+ local offset = guide.positionToOffset(state, pos)
+ local backSymbol = lookback.findSymbol(text, offset)
if backSymbol == ','
or backSymbol == '(' then
index = #args + 1
@@ -130,13 +137,14 @@ local function makeSignatures(text, call, pos)
end
return function (uri, pos)
- local ast = files.getState(uri)
- if not ast then
+ local state = files.getState(uri)
+ if not state then
return nil
end
local text = files.getText(uri)
- pos = lookback.skipSpace(text, pos)
- local call = findNearCall(uri, ast, pos)
+ local offset = guide.positionToOffset(state, pos)
+ pos = guide.offsetToPosition(state, lookback.skipSpace(text, offset))
+ local call = findNearCall(uri, state, pos)
if not call then
return nil
end
diff --git a/test/signature/init.lua b/test/signature/init.lua
index 27051310..43bce29e 100644
--- a/test/signature/init.lua
+++ b/test/signature/init.lua
@@ -1,26 +1,25 @@
-local core = require 'core.signature'
+local core = require 'core.signature'
local files = require 'files'
+local catch = require 'catch'
rawset(_G, 'TEST', true)
function TEST(script)
return function (expect)
- local pos = script:find('$', 1, true) - 1
- local new_script = script:gsub('%$', '')
+ local newScript, catched1 = catch(script, '?')
+ local newExpect, catched2 = catch(expect or '', '!')
files.removeAll()
- files.setText('', new_script)
- local hovers = core('', pos)
+ files.setText('', newScript)
+ local hovers = core('', catched1['?'][1][1])
if hovers then
assert(expect)
local hover = hovers[#hovers]
- local label = hover.label:gsub('^[\r\n]*(.-)[\r\n]*$', '%1'):gsub('\r\n', '\n')
- expect.label = expect.label:gsub('^[\r\n]*(.-)[\r\n]*$', '%1'):gsub('\r\n', '\n')
local arg = hover.params[hover.index].label
- assert(expect.label == label)
- assert(expect.arg[1] == arg[1])
- assert(expect.arg[2] == arg[2])
+ assert(newExpect == hover.label)
+ assert(catched2['!'][1][1] == arg[1])
+ assert(catched2['!'][1][2] == arg[2])
else
assert(expect == nil)
end
@@ -31,93 +30,67 @@ TEST [[
local function x(a, b)
end
-x($
+x(<??>
]]
-{
- label = "function x(a: any, b: any)",
- arg = {12, 17},
-}
+'function x(<!a: any!>, b: any)'
TEST [[
local function x(a, b)
end
-x($)
+x(<??>)
]]
-{
- label = "function x(a: any, b: any)",
- arg = {12, 17},
-}
+'function x(<!a: any!>, b: any)'
TEST [[
local function x(a, b)
end
-x(xxx$)
+x(xxx<??>)
]]
-{
- label = "function x(a: any, b: any)",
- arg = {12, 17},
-}
+'function x(<!a: any!>, b: any)'
TEST [[
local function x(a, b)
end
-x(xxx, $)
+x(xxx, <??>)
]]
-{
- label = "function x(a: any, b: any)",
- arg = {20, 25},
-}
+'function x(a: any, <!b: any!>)'
TEST [[
function mt:f(a)
end
-mt:f($
+mt:f(<??>
]]
-{
- label = 'function mt:f(a: any)',
- arg = {15, 20},
-}
+'function mt:f(<!a: any!>)'
TEST [[
local function x(a, b)
return 1
end
-x($
+x(<??>
]]
-{
- label = "function x(a: any, b: any)",
- arg = {12, 17},
-}
+'function x(<!a: any!>, b: any)'
TEST [[
local function x(a, ...)
return 1
end
-x(1, 2, 3, $
+x(1, 2, 3, <??>
]]
-{
- label = "function x(a: any, ...)",
- arg = {20, 22},
-}
+'function x(a: any, <!...!>)'
TEST [[
-(''):sub($
+(''):sub(<??>
]]
-{
- label = [[
-function string:sub(i: integer, j?: integer)
-]],
- arg = {21, 30},
-}
+'function string:sub(<!i: integer!>, j?: integer)'
TEST [[
-(''):sub(1)$
+(''):sub(1)<??>
]]
(nil)
@@ -125,39 +98,29 @@ TEST [[
local function f(a, b, c)
end
-f(1, 'string$')
+f(1, 'string<??>')
]]
-{
- label = [[
-function f(a: any, b: any, c: any)
-]],
- arg = {20, 25},
-}
+'function f(a: any, <!b: any!>, c: any)'
TEST [[
-pcall(function () $ end)
+pcall(function () <??> end)
]]
(nil)
TEST [[
-table.unpack {$}
+table.unpack {<??>}
]]
(nil)
TEST [[
---@type fun(x: number, y: number):boolean
local zzzz
-zzzz($)
+zzzz(<??>)
]]
-{
- label = [[
-function zzzz(x: number, y: number)
-]],
- arg = {15, 23},
-}
+'function zzzz(<!x: number!>, y: number)'
TEST [[
-('abc'):format(f($))
+('abc'):format(f(<??>))
]]
(nil)
@@ -166,14 +129,9 @@ function Foo(param01, param02)
end
-Foo($)
+Foo(<??>)
]]
-{
- label = [[
-function Foo(param01: any, param02: any)
-]],
- arg = {14, 25},
-}
+'function Foo(<!param01: any!>, param02: any)'
TEST [[
function f1(a, b)
@@ -182,51 +140,31 @@ end
function f2(c, d)
end
-f2(f1(),$)
+f2(f1(),<??>)
]]
-{
- label = [[
-function f2(c: any, d: any)
-]],
- arg = {21, 26},
-}
+'function f2(c: any, <!d: any!>)'
TEST [[
local function f(a, b, c)
end
-f({},$)
+f({},<??>)
]]
-{
- label = [[
-function f(a: any, b: any, c: any)
-]],
- arg = {20, 25},
-}
+'function f(a: any, <!b: any!>, c: any)'
TEST [[
-for _ in pairs($) do
+for _ in pairs(<??>) do
end
]]
-{
- label = [[
-function pairs(t: <T>)
-]],
- arg = {16, 21},
-}
+'function pairs(<!t: <T>!>)'
TEST [[
function m:f()
end
-m.f($)
+m.f(<??>)
]]
-{
- label = [[
-function m.f(self: table)
-]],
- arg = {14, 24},
-}
+'function m.f(<!self: table!>)'
TEST [[
---@alias nnn table<number, string>
@@ -234,59 +172,41 @@ TEST [[
---@param x nnn
local function f(x, y, z) end
-f($)
+f(<??>)
]]
-{
- label = [[
-function f(x: table<number, string>, y: any, z: any)
-]],
- arg = {12, 35},
-}
-
+'function f(<!x: table<number, string>!>, y: any, z: any)'
TEST [[
local function x(a, b)
end
-x( aaaa $, 2)
+x( aaaa <??>, 2)
]]
-{
- label = "function x(a: any, b: any)",
- arg = {12, 17},
-}
+"function x(<!a: any!>, b: any)"
TEST [[
local function x(a, b)
end
-x($ aaaa , 2)
+x(<??> aaaa , 2)
]]
-{
- label = "function x(a: any, b: any)",
- arg = {12, 17},
-}
+'function x(<!a: any!>, b: any)'
TEST [[
local function x(a, b)
end
-x(aaaa ,$ 2)
+x(aaaa ,<??> 2)
]]
-{
- label = "function x(a: any, b: any)",
- arg = {20, 25},
-}
+'function x(a: any, <!b: any!>)'
TEST [[
local function x(a, b)
end
-x(aaaa , 2 $)
+x(aaaa , 2 <??>)
]]
-{
- label = "function x(a: any, b: any)",
- arg = {20, 25},
-}
+'function x(a: any, <!b: any!>)'
TEST [[
local fooC
@@ -298,9 +218,6 @@ function fooC(callback, par) end
fooC(function (x, s)
-end,$)
+end,<??>)
]]
-{
- label = 'function fooC(callback: fun(x: number, s: string):nil, par: number)',
- arg = {56, 66},
-}
+'function fooC(callback: fun(x: number, s: string):nil, <!par: number!>)'