diff options
Diffstat (limited to 'script')
-rw-r--r-- | script/core/completion.lua | 27 | ||||
-rw-r--r-- | script/core/keyword.lua | 8 | ||||
-rw-r--r-- | script/core/look-backward.lua | 2 | ||||
-rw-r--r-- | script/core/type-formatting.lua | 2 | ||||
-rw-r--r-- | script/parser/guide.lua | 4 | ||||
-rw-r--r-- | script/parser/newparser.lua | 4 |
6 files changed, 25 insertions, 22 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua index a3df659b..08d6f5f2 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -75,7 +75,8 @@ local function findNearestTableField(state, position) end local function findParent(state, text, position) - for i = position, 1, -1 do + local offset = guide.positionToOffset(state, position) + for i = offset, 1, -1 do local char = text:sub(i, i) if lookBackward.isSpace(char) then goto CONTINUE @@ -92,10 +93,11 @@ local function findParent(state, text, position) else return nil, nil end - local anyPos = lookBackward.findAnyPos(text, i-1) - if not anyPos then + local anyOffset = lookBackward.findAnyOffset(text, i-1) + if not anyOffset then return nil, nil end + local anyPos = guide.offsetToPosition(state, anyOffset) local parent = guide.eachSourceContain(state.ast, anyPos, function (source) if source.finish == anyPos then return source @@ -169,17 +171,18 @@ local function getSnip(source) if def ~= source and def.type == 'function' then local uri = guide.getUri(def) local text = files.getText(uri) - local lines = files.getLines(uri) + local state = files.getState(uri) + local lines = state.lines if not text then goto CONTINUE end if vm.isMetaFile(uri) then goto CONTINUE end - local row = guide.rowColOf(def.start) - local firstRow = lines[row] - local lastRow = lines[math.min(row + context - 1, #lines)] - local snip = text:sub(firstRow.start, lastRow.finish) + local firstRow = guide.rowColOf(def.start) + local lastRow = firstRow + context + local lastOffset = lines[lastRow] and (lines[lastRow] - 1) or #text + local snip = text:sub(lines[firstRow], lastOffset) return snip end ::CONTINUE:: @@ -698,7 +701,7 @@ local function checkCommon(myUri, word, text, position, results) end if #str >= 3 and not used[str] - and guide.offsetToPosition(state, offset) ~= position then + and guide.offsetToPosition(state, offset - 1) ~= position then used[str] = true if matchKey(word, str) then results[#results+1] = { @@ -723,7 +726,7 @@ end local function checkKeyWord(state, text, start, position, word, hasSpace, afterLocal, results) local snipType = config.get 'Lua.completion.keywordSnippet' - local symbol = lookBackward.findSymbol(text, guide.positionToOffset(state, start - 1)) + local symbol = lookBackward.findSymbol(text, guide.positionToOffset(state, start)) local isExp = symbol == '(' or symbol == ',' or symbol == '=' local info = { hasSpace = hasSpace, @@ -1215,7 +1218,7 @@ local function tryWord(state, text, position, triggerCharacter, results) return nil end else - startPos = guide.offsetToPosition(state, start) + startPos = guide.offsetToPosition(state, start - 1) end local hasSpace = triggerCharacter ~= nil and finish ~= offset if isInString(state, position) then @@ -1416,7 +1419,7 @@ local function checkTableLiteralField(state, text, position, tbl, fields, result -- {$} local left = lookBackward.findWord(text, guide.positionToOffset(state, position)) if not left then - local pos = lookBackward.findAnyPos(text, guide.positionToOffset(state, position)) + local pos = lookBackward.findAnyOffset(text, guide.positionToOffset(state, position)) local char = text:sub(pos, pos) if char == '{' or char == ',' or char == ';' then left = '' diff --git a/script/core/keyword.lua b/script/core/keyword.lua index 8e041f1c..0825f1bc 100644 --- a/script/core/keyword.lua +++ b/script/core/keyword.lua @@ -24,14 +24,12 @@ end", end return true end, function (info) - return guide.eachSourceContain(info.ast.ast, info.start, function (source) + return guide.eachSourceContain(info.state.ast, info.start, function (source) if source.type == 'while' or source.type == 'in' or source.type == 'loop' then - for i = 1, #source.keyword do - if info.start == source.keyword[i] then - return true - end + if source.finish - info.start <= 2 then + return true end end end) diff --git a/script/core/look-backward.lua b/script/core/look-backward.lua index ee89078f..2f90b768 100644 --- a/script/core/look-backward.lua +++ b/script/core/look-backward.lua @@ -77,7 +77,7 @@ function m.findTargetSymbol(text, offset, symbol) return nil end -function m.findAnyPos(text, offset) +function m.findAnyOffset(text, offset) for i = offset, 1, -1 do if not m.isSpace(text:sub(i, i)) then return i diff --git a/script/core/type-formatting.lua b/script/core/type-formatting.lua index 157a446d..d7bacbf8 100644 --- a/script/core/type-formatting.lua +++ b/script/core/type-formatting.lua @@ -27,7 +27,7 @@ local function findForward(text, offset, ...) end local function findBackward(text, offset, ...) - local pos = lookBackward.findAnyPos(text, offset) + local pos = lookBackward.findAnyOffset(text, offset) for _, symbol in ipairs { ... } do if text:sub(pos - #symbol + 1, pos) == symbol then return pos - #symbol + 1, symbol diff --git a/script/parser/guide.lua b/script/parser/guide.lua index e2262cfd..af623753 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -763,7 +763,7 @@ end function m.positionToOffset(state, position) local lines = state.lines local row, col = m.rowColOf(position) - return (lines[row] or 1) + col - 1 + return lines[row] + col - 1 end function m.offsetToPosition(state, offset) @@ -788,7 +788,7 @@ function m.offsetToPosition(state, offset) left = row end end - local col = offset - (lines[row] or 1) + local col = offset - lines[row] + 1 return m.positionOf(row, col) end diff --git a/script/parser/newparser.lua b/script/parser/newparser.lua index 9721526c..e0144dcd 100644 --- a/script/parser/newparser.lua +++ b/script/parser/newparser.lua @@ -3544,7 +3544,9 @@ local function initState(lua, version, options) errs = {}, diags = {}, comms = {}, - lines = {}, + lines = { + [0] = 1, + }, options = options or {}, } if version == 'Lua 5.1' or version == 'LuaJIT' then |