diff options
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | script/core/signature.lua | 44 | ||||
-rw-r--r-- | script/parser/ast.lua | 12 | ||||
-rw-r--r-- | script/parser/grammar.lua | 12 | ||||
-rw-r--r-- | test/signature/init.lua | 45 |
5 files changed, 88 insertions, 26 deletions
diff --git a/changelog.md b/changelog.md index a73512e7..2ebb2f9e 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ ## 2.0.3 * `CHG` improve memory usage * `FIX` some dialog boxes block the initialization process +* `FIX` [#565](https://github.com/sumneko/lua-language-server/issues/565) ## 2.0.2 `2021-6-23` diff --git a/script/core/signature.lua b/script/core/signature.lua index 8de1c374..76189fb7 100644 --- a/script/core/signature.lua +++ b/script/core/signature.lua @@ -4,6 +4,7 @@ local vm = require 'vm' local hoverLabel = require 'core.hover.label' local hoverDesc = require 'core.hover.description' local guide = require 'parser.guide' +local lookback = require 'core.look-backward' local function findNearCall(uri, ast, pos) local text = files.getText(uri) @@ -71,7 +72,7 @@ local function makeOneSignature(source, oop, index) } end -local function makeSignatures(call, pos) +local function makeSignatures(text, call, pos) local node = call.node local oop = node.type == 'method' or node.type == 'getmethod' @@ -85,13 +86,26 @@ local function makeSignatures(call, pos) end end for i, arg in ipairs(args) do - if arg.start <= pos and arg.finish >= pos then + local start = lookback.findTargetSymbol(text, arg.start - 1, '(') + or lookback.findTargetSymbol(text, arg.start - 1, ',') + or arg.start + if start > pos then + index = i - 1 + break + end + if pos <= arg.finish then index = i break end end if not index then - index = #args + 1 + local backSymbol = lookback.findSymbol(text, pos) + if backSymbol == ',' + or backSymbol == '(' then + index = #args + 1 + else + index = #args + end end else index = 1 @@ -112,38 +126,18 @@ local function makeSignatures(call, pos) return signs end -local function isSpace(char) - if char == ' ' - or char == '\n' - or char == '\r' - or char == '\t' then - return true - end - return false -end - -local function skipSpace(text, offset) - for i = offset, 1, -1 do - local char = text:sub(i, i) - if not isSpace(char) then - return i - end - end - return 0 -end - return function (uri, pos) local ast = files.getState(uri) if not ast then return nil end local text = files.getText(uri) - pos = skipSpace(text, pos) + pos = lookback.skipSpace(text, pos) local call = findNearCall(uri, ast, pos) if not call then return nil end - local signs = makeSignatures(call, pos) + local signs = makeSignatures(text, call, pos) if not signs or #signs == 0 then return nil end diff --git a/script/parser/ast.lua b/script/parser/ast.lua index bd8a66cf..e92e3e16 100644 --- a/script/parser/ast.lua +++ b/script/parser/ast.lua @@ -6,6 +6,7 @@ local mathType = math.type local tableRemove = table.remove local pairs = pairs local tableSort = table.sort +local print = print _ENV = nil @@ -1925,6 +1926,17 @@ local Defs = { } } } + end, + CallArgSnip = function (name, tailStart, tailSymbol) + PushError { + type = 'UNEXPECT_SYMBOL', + start = tailStart, + finish = tailStart, + info = { + symbol = tailSymbol, + } + } + return name end } diff --git a/script/parser/grammar.lua b/script/parser/grammar.lua index 01756c2a..2c046f0e 100644 --- a/script/parser/grammar.lua +++ b/script/parser/grammar.lua @@ -304,6 +304,13 @@ MustName <- Name / DirtyName DirtyName <- {} -> DirtyName ]] +grammar 'DocType' [[ +DocType <- (!%nl !')' !',' DocChar)+ +DocChar <- '(' (!%nl !')' .)+ ')'? + / '<' (!%nl !'>' .)+ '>'? + / . +]] + grammar 'Exp' [[ Exp <- (UnUnit BinUnit*) -> Binary @@ -332,9 +339,12 @@ Single <- !FUNCTION Name Suffix <- SuffixWithoutCall / ({} PL SuffixCall DirtyPR {}) -> Call -SuffixCall <- Sp ({} {| (COMMA / Exp->NoNil)+ |} {}) +SuffixCall <- Sp ({} {| (COMMA / CallArg)+ |} {}) -> PackExpList / %nil +CallArg <- Sp (Name {} {'?'? ':'} Sps DocType) + -> CallArgSnip + / Exp->NoNil SuffixWithoutCall <- (DOT (Name / MissField)) -> GetField diff --git a/test/signature/init.lua b/test/signature/init.lua index 931a8d73..40fb33dc 100644 --- a/test/signature/init.lua +++ b/test/signature/init.lua @@ -242,3 +242,48 @@ function f(x: table<number, string>, y: any, z: any) ]], arg = {12, 35}, } + + +TEST [[ +local function x(a, b) +end + +x( aaaa $, 2) +]] +{ + label = "function x(a: any, b: any)", + arg = {12, 17}, +} + +TEST [[ +local function x(a, b) +end + +x($ aaaa , 2) +]] +{ + label = "function x(a: any, b: any)", + arg = {12, 17}, +} + +TEST [[ +local function x(a, b) +end + +x(aaaa ,$ 2) +]] +{ + label = "function x(a: any, b: any)", + arg = {20, 25}, +} + +TEST [[ +local function x(a, b) +end + +x(aaaa , 2 $) +]] +{ + label = "function x(a: any, b: any)", + arg = {20, 25}, +} |