summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/core/completion.lua76
-rw-r--r--script/core/keyword.lua15
-rw-r--r--test/completion/common.lua64
3 files changed, 87 insertions, 68 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua
index 8d861aeb..e37dceed 100644
--- a/script/core/completion.lua
+++ b/script/core/completion.lua
@@ -405,19 +405,28 @@ local function checkModule(state, word, position, results)
end
end
-local function checkFieldFromFieldToIndex(name, src, parent, word, start, position)
+local function checkFieldFromFieldToIndex(name, src, parent, word, startPos, position)
if name:match '^[%a_][%w_]*$' then
return nil
end
local textEdit, additionalTextEdits
- local uri = guide.getUri(parent)
- local text = files.getText(uri)
- local wordStart
+ local uri = guide.getUri(parent)
+ local text = files.getText(uri)
+ local state = files.getState(uri)
+ local startOffset = guide.positionToOffset(state, startPos)
+ local offset = guide.positionToOffset(state, position)
+ local wordStartOffset
if word == '' then
- wordStart = text:match('()%S', start + 1) or (position + 1)
+ wordStartOffset = text:match('()%S', startOffset + 1)
+ if wordStartOffset then
+ wordStartOffset = wordStartOffset - 1
+ else
+ wordStartOffset = offset
+ end
else
- wordStart = position - #word + 1
+ wordStartOffset = offset - #word
end
+ local wordStartPos = guide.offsetToPosition(state, wordStartOffset)
local newText
if vm.getKeyType(src) == 'string' then
newText = ('[%q]'):format(name)
@@ -425,26 +434,28 @@ local function checkFieldFromFieldToIndex(name, src, parent, word, start, positi
newText = ('[%s]'):format(name)
end
textEdit = {
- start = wordStart,
+ start = wordStartPos,
finish = position,
newText = newText,
}
local nxt = parent.next
if nxt then
- local dotStart
+ local dotStart, dotFinish
if nxt.type == 'setfield'
or nxt.type == 'getfield'
or nxt.type == 'tablefield' then
dotStart = nxt.dot.start
+ dotFinish = nxt.dot.finish
elseif nxt.type == 'setmethod'
or nxt.type == 'getmethod' then
dotStart = nxt.colon.start
+ dotFinish = nxt.colon.finish
end
if dotStart then
additionalTextEdits = {
{
start = dotStart,
- finish = dotStart,
+ finish = dotFinish,
newText = '',
}
}
@@ -460,7 +471,7 @@ local function checkFieldFromFieldToIndex(name, src, parent, word, start, positi
return textEdit, additionalTextEdits
end
-local function checkFieldThen(name, src, word, start, position, parent, oop, results)
+local function checkFieldThen(name, src, word, startPos, position, parent, oop, results)
local value = searcher.getObjectValue(src) or src
local kind = define.CompletionItemKind.Field
if value.type == 'function'
@@ -501,7 +512,7 @@ local function checkFieldThen(name, src, word, start, position, parent, oop, res
newText = name,
}
else
- textEdit, additionalTextEdits = checkFieldFromFieldToIndex(name, src, parent, word, start, position)
+ textEdit, additionalTextEdits = checkFieldFromFieldToIndex(name, src, parent, word, startPos, position)
end
results[#results+1] = {
label = name,
@@ -518,7 +529,7 @@ local function checkFieldThen(name, src, word, start, position, parent, oop, res
}
end
-local function checkFieldOfRefs(refs, state, word, start, position, parent, oop, results, locals, isGlobal)
+local function checkFieldOfRefs(refs, state, word, startPos, position, parent, oop, results, locals, isGlobal)
local fields = {}
local funcs = {}
local count = 0
@@ -527,7 +538,7 @@ local function checkFieldOfRefs(refs, state, word, start, position, parent, oop,
if not name then
goto CONTINUE
end
- if isSameSource(state, src, start) then
+ if isSameSource(state, src, startPos) then
goto CONTINUE
end
if isGlobal and locals and locals[name] then
@@ -576,15 +587,15 @@ local function checkFieldOfRefs(refs, state, word, start, position, parent, oop,
end
for name, src in util.sortPairs(fields) do
if src then
- checkFieldThen(name, src, word, start, position, parent, oop, results)
+ checkFieldThen(name, src, word, startPos, position, parent, oop, results)
end
end
end
-local function checkGlobal(state, word, start, position, parent, oop, results)
+local function checkGlobal(state, word, startPos, position, parent, oop, results)
local locals = guide.getVisibleLocals(state.ast, position)
local globals = vm.getGlobalSets '*'
- checkFieldOfRefs(globals, state, word, start, position, parent, oop, results, locals, 'global')
+ checkFieldOfRefs(globals, state, word, startPos, position, parent, oop, results, locals, 'global')
end
local function checkField(state, word, start, position, parent, oop, results)
@@ -876,9 +887,10 @@ local function checkFunctionArgByDocParam(state, word, start, results)
end
end
-local function isAfterLocal(text, start)
- local pos = lookBackward.skipSpace(text, start-1)
- local word = lookBackward.findWord(text, pos)
+local function isAfterLocal(state, text, startPos)
+ local offset = guide.positionToOffset(state, startPos)
+ local pos = lookBackward.skipSpace(text, offset)
+ local word = lookBackward.findWord(text, pos)
return word == 'local'
end
@@ -1006,19 +1018,23 @@ local function checkLenPlusOne(state, text, position, results)
guide.eachSourceContain(state.ast, position, function (source)
if source.type == 'getindex'
or source.type == 'setindex' then
- local _, pos = text:find('%s*%[%s*%#', source.node.finish)
- if not pos then
+ local finish = guide.positionToOffset(state, source.node.finish)
+ local _, offset = text:find('%s*%[%s*%#', finish)
+ if not offset then
return
end
- local nodeText = text:sub(source.node.start, source.node.finish)
- local writingText = trim(text:sub(pos + 1, position - 1)) or ''
+ local start = guide.positionToOffset(state, source.node.start) + 1
+ local nodeText = text:sub(start, finish)
+ local writingText = trim(text:sub(offset + 1, guide.positionToOffset(state, position))) or ''
if not matchKey(writingText, nodeText) then
return
end
+ local offsetPos = guide.offsetToPosition(state, offset) - 1
if source.parent == guide.getParentBlock(source) then
+ local sourceFinish = guide.positionToOffset(state, source.finish)
-- state
- local label = text:match('%#[ \t]*', pos) .. nodeText .. '+1'
- local eq = text:find('^%s*%]?%s*%=', source.finish)
+ local label = text:match('%#[ \t]*', offset) .. nodeText .. '+1'
+ local eq = text:find('^%s*%]?%s*%=', sourceFinish)
local newText = label .. ']'
if not eq then
newText = newText .. ' = '
@@ -1028,20 +1044,20 @@ local function checkLenPlusOne(state, text, position, results)
match = nodeText,
kind = define.CompletionItemKind.Snippet,
textEdit = {
- start = pos,
+ start = offsetPos,
finish = source.finish,
newText = newText,
},
}
else
-- exp
- local label = text:match('%#[ \t]*', pos) .. nodeText
+ local label = text:match('%#[ \t]*', offset) .. nodeText
local newText = label .. ']'
results[#results+1] = {
label = label,
kind = define.CompletionItemKind.Snippet,
textEdit = {
- start = pos,
+ start = offsetPos,
finish = source.finish,
newText = newText,
},
@@ -1137,7 +1153,7 @@ local function checkEqualEnum(state, text, position, results)
eqOrNeq = true
end
start = lookBackward.skipSpace(text, start - 1)
- local source = findNearestSource(state, start)
+ local source = findNearestSource(state, guide.offsetToPosition(state, start))
if not source then
return
end
@@ -1237,7 +1253,7 @@ local function tryWord(state, text, position, triggerCharacter, results)
checkProvideLocal(state, word, startPos, results)
checkFunctionArgByDocParam(state, word, startPos, results)
else
- local afterLocal = isAfterLocal(text, startPos)
+ local afterLocal = isAfterLocal(state, text, startPos)
local stop = checkKeyWord(state, text, startPos, position, word, hasSpace, afterLocal, results)
if stop then
return
diff --git a/script/core/keyword.lua b/script/core/keyword.lua
index 0825f1bc..b0c75f7e 100644
--- a/script/core/keyword.lua
+++ b/script/core/keyword.lua
@@ -38,8 +38,9 @@ end",
{'break'},
{'else'},
{'elseif', function (info, results)
- if info.text:find('^%s*then', info.offset + 1)
- or info.text:find('^%s*do', info.offset + 1) then
+ local offset = guide.positionToOffset(info.state, info.position)
+ if info.text:find('^%s*then', offset + 1)
+ or info.text:find('^%s*do', offset + 1) then
return false
end
if info.hasSpace then
@@ -153,8 +154,9 @@ end"
end},
{'goto'},
{'if', function (info, results)
- if info.text:find('^%s*then', info.offset + 1)
- or info.text:find('^%s*do', info.offset + 1) then
+ local offset = guide.positionToOffset(info.state, info.position)
+ if info.text:find('^%s*then', offset + 1)
+ or info.text:find('^%s*do', offset + 1) then
return false
end
if info.hasSpace then
@@ -181,8 +183,9 @@ end"
return true
end},
{'in', function (info, results)
- if info.text:find('^%s*then', info.offset + 1)
- or info.text:find('^%s*do', info.offset + 1) then
+ local offset = guide.positionToOffset(info.state, info.position)
+ if info.text:find('^%s*then', offset + 1)
+ or info.text:find('^%s*do', offset + 1) then
return false
end
if info.hasSpace then
diff --git a/test/completion/common.lua b/test/completion/common.lua
index 66cc6fe4..ff36ee0a 100644
--- a/test/completion/common.lua
+++ b/test/completion/common.lua
@@ -443,7 +443,7 @@ collectgarbage('<??>')
label = "'collect'",
kind = define.CompletionItemKind.EnumMember,
textEdit = {
- start = 16,
+ start = 15,
finish = 17,
newText = "'collect'",
},
@@ -452,7 +452,7 @@ collectgarbage('<??>')
label = "'stop'",
kind = define.CompletionItemKind.EnumMember,
textEdit = {
- start = 16,
+ start = 15,
finish = 17,
newText = "'stop'",
},
@@ -461,7 +461,7 @@ collectgarbage('<??>')
label = "'restart'",
kind = define.CompletionItemKind.EnumMember,
textEdit = {
- start = 16,
+ start = 15,
finish = 17,
newText = "'restart'",
},
@@ -470,7 +470,7 @@ collectgarbage('<??>')
label = "'count'",
kind = define.CompletionItemKind.EnumMember,
textEdit = {
- start = 16,
+ start = 15,
finish = 17,
newText = "'count'",
},
@@ -479,7 +479,7 @@ collectgarbage('<??>')
label = "'step'",
kind = define.CompletionItemKind.EnumMember,
textEdit = {
- start = 16,
+ start = 15,
finish = 17,
newText = "'step'",
},
@@ -488,7 +488,7 @@ collectgarbage('<??>')
label = "'isrunning'",
kind = define.CompletionItemKind.EnumMember,
textEdit = {
- start = 16,
+ start = 15,
finish = 17,
newText = "'isrunning'",
},
@@ -497,7 +497,7 @@ collectgarbage('<??>')
label = "'incremental'",
kind = define.CompletionItemKind.EnumMember,
textEdit = {
- start = 16,
+ start = 15,
finish = 17,
newText = "'incremental'",
},
@@ -506,7 +506,7 @@ collectgarbage('<??>')
label = "'generational'",
kind = define.CompletionItemKind.EnumMember,
textEdit = {
- start = 16,
+ start = 15,
finish = 17,
newText = "'generational'",
},
@@ -554,7 +554,7 @@ self.results.list[#<??>]
label = '#self.results.list+1',
kind = define.CompletionItemKind.Snippet,
textEdit = {
- start = 19,
+ start = 18,
finish = 20,
newText = '#self.results.list+1] = ',
},
@@ -570,7 +570,7 @@ local n = 1
label = '#self.results.list+1',
kind = define.CompletionItemKind.Snippet,
textEdit = {
- start = 19,
+ start = 18,
finish = 20,
newText = '#self.results.list+1] = ',
},
@@ -585,7 +585,7 @@ self.results.list[#<??>] = 1
label = '#self.results.list+1',
kind = define.CompletionItemKind.Snippet,
textEdit = {
- start = 19,
+ start = 18,
finish = 20,
newText = '#self.results.list+1]',
},
@@ -600,7 +600,7 @@ self.results.list[#self.re<??>]
label = '#self.results.list+1',
kind = define.CompletionItemKind.Snippet,
textEdit = {
- start = 19,
+ start = 18,
finish = 27,
newText = '#self.results.list+1] = ',
},
@@ -619,7 +619,7 @@ fff[#ff<??>]
label = '#fff+1',
kind = define.CompletionItemKind.Snippet,
textEdit = {
- start = 5,
+ start = 4,
finish = 8,
newText = '#fff+1] = ',
},
@@ -638,7 +638,7 @@ local _ = fff.kkk[#<??>]
label = '#fff.kkk',
kind = define.CompletionItemKind.Snippet,
textEdit = {
- start = 19,
+ start = 18,
finish = 20,
newText = '#fff.kkk]',
},
@@ -653,7 +653,7 @@ fff.kkk[#<??>].yy
label = '#fff.kkk',
kind = define.CompletionItemKind.Snippet,
textEdit = {
- start = 9,
+ start = 8,
finish = 10,
newText = '#fff.kkk]',
},
@@ -911,7 +911,7 @@ mt:f<??>
{
label = 'f(a, b, c)',
kind = define.CompletionItemKind.Snippet,
- insertText = 'f(<??>{1:a: any}, <??>{2:b: any}, <??>{3:c: any})',
+ insertText = 'f(${1:a: any}, ${2:b: any}, ${3:c: any})',
},
}
@@ -927,8 +927,8 @@ function<??>
label = 'function ()',
kind = define.CompletionItemKind.Snippet,
insertText = "\z
-function <??>1(<??>2)\
-\t<??>0\
+function $1($2)\
+\t$0\
end",
},
}
@@ -945,8 +945,8 @@ local t = function<??>
label = 'function ()',
kind = define.CompletionItemKind.Snippet,
insertText = "\z
-function (<??>1)\
-\t<??>0\
+function ($1)\
+\t$0\
end",
},
}
@@ -986,14 +986,14 @@ t.<??>
label = 'a.b.c',
kind = define.CompletionItemKind.Field,
textEdit = {
- start = 37,
- finish = 36,
+ start = 40002,
+ finish = 40002,
newText = '["a.b.c"]',
},
additionalTextEdits = {
{
- start = 36,
- finish = 36,
+ start = 40001,
+ finish = 40002,
newText = '',
},
},
@@ -1012,14 +1012,14 @@ t. <??>
label = 'a.b.c',
kind = define.CompletionItemKind.Field,
textEdit = {
- start = 40,
- finish = 39,
+ start = 40005,
+ finish = 40005,
newText = '["a.b.c"]',
},
additionalTextEdits = {
{
- start = 36,
- finish = 36,
+ start = 40001,
+ finish = 40002,
newText = '',
},
},
@@ -1038,8 +1038,8 @@ t['<??>']
label = 'a.b.c',
kind = define.CompletionItemKind.Field,
textEdit = {
- start = 38,
- finish = 37,
+ start = 40003,
+ finish = 40003,
newText = 'a.b.c',
}
}
@@ -1055,8 +1055,8 @@ z<??>
label = 'z.b.c',
kind = define.CompletionItemKind.Field,
textEdit = {
- start = 21,
- finish = 21,
+ start = 20001,
+ finish = 20001,
newText = '_ENV["z.b.c"]',
},
},