summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script-beta/core/completion.lua104
-rw-r--r--script-beta/parser/guide.lua8
-rw-r--r--test-beta/completion/init.lua6
3 files changed, 83 insertions, 35 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua
index 8c99b38e..27dc35ba 100644
--- a/script-beta/core/completion.lua
+++ b/script-beta/core/completion.lua
@@ -139,6 +139,24 @@ local function findParent(ast, text, offset)
return nil, nil
end
+local function findParentInStringIndex(ast, text, offset)
+ local near
+ guide.eachSourceContain(ast.ast, offset, function (source)
+ if not near or near.start < source.start then
+ near = source
+ end
+ end)
+ if not near or near.type ~= 'string' then
+ return
+ end
+ local parent = near.parent
+ if parent.index ~= near then
+ return
+ end
+ -- index不可能是oop模式
+ return parent.node, false
+end
+
local function buildFunctionSnip(source, oop)
local name = getName(source):gsub('^.-[$.:]', '')
local defs = vm.getDefs(source)
@@ -249,6 +267,46 @@ local function checkLocal(ast, word, offset, results)
end
end
+local function checkFieldFromFieldToIndex(name, parent, word, start, offset)
+ if name:match '^[%a_][%w_]*$' then
+ return nil
+ end
+ local textEdit, additionalTextEdits
+ local uri = guide.getUri(parent)
+ local text = files.getText(uri)
+ local wordStart
+ if word == '' then
+ wordStart = text:match('()%S', start + 1) or (offset + 1)
+ else
+ wordStart = offset - #word + 1
+ end
+ textEdit = {
+ start = wordStart,
+ finish = offset,
+ newText = ('[%q]'):format(name),
+ }
+ local nxt = parent.next
+ local dotStart
+ if nxt.type == 'setfield'
+ or nxt.type == 'getfield'
+ or nxt.type == 'tablefield' then
+ dotStart = nxt.dot.start
+ elseif nxt.type == 'setmethod'
+ or nxt.type == 'getmethod' then
+ dotStart = nxt.colon.start
+ end
+ if dotStart then
+ additionalTextEdits = {
+ {
+ start = dotStart,
+ finish = dotStart,
+ newText = '',
+ }
+ }
+ end
+ return textEdit, additionalTextEdits
+end
+
local function checkFieldThen(ast, key, src, word, start, offset, parent, oop, results)
local name = key:sub(3)
if not matchKey(word, name) then
@@ -282,39 +340,15 @@ local function checkFieldThen(ast, key, src, word, start, offset, parent, oop, r
kind = ckind.Enum
end
local textEdit, additionalTextEdits
- if not name:match '^[%a_][%w_]*$' then
- local uri = guide.getUri(parent)
- local text = files.getText(uri)
- local wordStart
- if word == '' then
- wordStart = text:match('()%S', start + 1) or (offset + 1)
- else
- wordStart = offset - #word + 1
- end
+ if parent.next and parent.next.index then
+ local str = parent.next.index
textEdit = {
- start = wordStart,
+ start = str.start + #str[2],
finish = offset,
- newText = ('[%q]'):format(name),
+ newText = name,
}
- local nxt = parent.next
- local dotStart
- if nxt.type == 'setfield'
- or nxt.type == 'getfield'
- or nxt.type == 'tablefield' then
- dotStart = nxt.dot.start
- elseif nxt.type == 'setmethod'
- or nxt.type == 'getmethod' then
- dotStart = nxt.colon.start
- end
- if dotStart then
- additionalTextEdits = {
- {
- start = dotStart,
- finish = dotStart,
- newText = '',
- }
- }
- end
+ else
+ textEdit, additionalTextEdits = checkFieldFromFieldToIndex(name, parent, word, start, offset)
end
results[#results+1] = {
label = name,
@@ -668,6 +702,15 @@ local function trySpecial(ast, text, offset, results)
checkLenPlusOne(ast, text, offset, results)
end
+local function tryIndex(ast, text, offset, results)
+ local parent, oop = findParentInStringIndex(ast, text, offset)
+ if not parent then
+ return
+ end
+ local word = parent.next.index[1]
+ checkField(ast, word, offset, offset, parent, oop, results)
+end
+
local function tryWord(ast, text, offset, results)
local finish = skipSpace(text, offset)
local word, start = findWord(text, finish)
@@ -848,6 +891,7 @@ local function completion(uri, offset)
if ast then
trySpecial(ast, text, offset, results)
tryWord(ast, text, offset, results)
+ tryIndex(ast, text, offset, results)
trySymbol(ast, text, offset, results)
tryCallArg(ast, text, offset, results)
else
diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua
index b94d8f4a..95ed6d17 100644
--- a/script-beta/parser/guide.lua
+++ b/script-beta/parser/guide.lua
@@ -1122,6 +1122,9 @@ function m.getObjectValue(obj)
while obj.type == 'paren' do
obj = obj.exp
end
+ if obj.library then
+ return nil
+ end
if obj.type == 'boolean'
or obj.type == 'number'
or obj.type == 'integer'
@@ -1696,8 +1699,9 @@ function m.checkSameSimple(status, simple, data, mode, results, queue)
end
end
m.pushResult(status, mode, ref, simple)
- if ref.value then
- m.pushResult(status, mode, ref.value, simple)
+ local value = m.getObjectValue(ref)
+ if value then
+ m.pushResult(status, mode, value, simple)
end
end
diff --git a/test-beta/completion/init.lua b/test-beta/completion/init.lua
index f2fa9edf..d26ec2b6 100644
--- a/test-beta/completion/init.lua
+++ b/test-beta/completion/init.lua
@@ -1103,10 +1103,10 @@ t['$']
label = 'a.b.c',
kind = CompletionItemKind.Field,
textEdit = {
- start = 37,
- finish = 36,
+ start = 38,
+ finish = 37,
newText = 'a.b.c',
- },
+ }
}
}