diff options
Diffstat (limited to 'server-beta/src')
-rw-r--r-- | server-beta/src/core/definition.lua | 44 | ||||
-rw-r--r-- | server-beta/src/files.lua | 4 | ||||
-rw-r--r-- | server-beta/src/parser/ast.lua | 6 | ||||
-rw-r--r-- | server-beta/src/parser/grammar.lua | 2 | ||||
-rw-r--r-- | server-beta/src/parser/guide.lua | 25 | ||||
-rw-r--r-- | server-beta/src/proto/interface.lua | 18 | ||||
-rw-r--r-- | server-beta/src/proto/provider.lua | 34 | ||||
-rw-r--r-- | server-beta/src/pub/pub.lua | 2 |
8 files changed, 114 insertions, 21 deletions
diff --git a/server-beta/src/core/definition.lua b/server-beta/src/core/definition.lua index e69de29b..da0e6d69 100644 --- a/server-beta/src/core/definition.lua +++ b/server-beta/src/core/definition.lua @@ -0,0 +1,44 @@ +local guide = require 'parser.guide' + +local m = {} + +function m.asgetlocal(ast, source, callback) + local loc = ast.root[source.loc] + if not loc then + return + end + return m.aslocal(ast, loc, callback) +end + +function m.assetlocal(ast, source, callback) + local loc = ast.root[source.loc] + if not loc then + return + end + return m.aslocal(ast, loc, callback) +end + +function m.aslocal(ast, source, callback) + callback(source, ast.uri) +end + +return function (ast, text, offset) + local results = {} + guide.eachSource(ast.root, offset, function (source) + local tp = source.type + local f = m['as' .. tp] + if f then + f(ast, source, function (target, uri) + results[#results+1] = { + uri = uri or ast.uri, + source = source, + target = target, + } + end) + end + end) + if #results == 0 then + return nil + end + return results +end diff --git a/server-beta/src/files.lua b/server-beta/src/files.lua index 0096e055..7d1fe0da 100644 --- a/server-beta/src/files.lua +++ b/server-beta/src/files.lua @@ -32,11 +32,15 @@ function m.setText(uri, text) m.fileMap[uri] = {} end local file = m.fileMap[uri] + if file.text == text then + return + end file.text = text if file.compiling then pub.removeTask(file.compiling) end file.compiling = pub.syncTask('compile', text, function (ast) + ast.uri = uri file.ast = ast file.compiling = nil local onCompiledList = file.onCompiledList diff --git a/server-beta/src/parser/ast.lua b/server-beta/src/parser/ast.lua index a7cb1d08..f22033db 100644 --- a/server-beta/src/parser/ast.lua +++ b/server-beta/src/parser/ast.lua @@ -1255,8 +1255,10 @@ local Defs = { block.filter = filter return block end, - Lua = function (actions) - actions.type = 'main' + Lua = function (start, actions, finish) + actions.type = 'main' + actions.start = start + actions.finish = finish - 1 return actions end, diff --git a/server-beta/src/parser/grammar.lua b/server-beta/src/parser/grammar.lua index dfee8a1b..8c491c64 100644 --- a/server-beta/src/parser/grammar.lua +++ b/server-beta/src/parser/grammar.lua @@ -517,7 +517,7 @@ FuncName <- {| Single (Sp SuffixWithoutCall)* |} grammar 'Lua' [[ Lua <- Head? - {| Action* |} -> Lua + ({} {| Action* |} {}) -> Lua Sp Head <- '#' (!%nl .)* ]] diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua index 7cec136d..d3ae7100 100644 --- a/server-beta/src/parser/guide.lua +++ b/server-beta/src/parser/guide.lua @@ -174,6 +174,27 @@ function m.getLabel(root, block, name) error('guide.getLocal overstack') end +--- 判断source是否包含offset +function m.isContain(source, offset) + if not source.start then + return false + end + return source.start <= offset and source.finish >= offset - 1 +end + +--- 遍历所有包含offset的source +function m.eachSource(root, offset, callback) + for i = 1, #root do + local source = root[i] + if m.isContain(source, offset) then + local res = callback(source) + if res ~= nil then + return res + end + end + end +end + --- 获取偏移对应的坐标(row从0开始,col为光标位置) ---@param lines table ---@return integer {name = 'row'} @@ -231,7 +252,7 @@ function m.offsetOf(lines, row, col) end function m.lineContent(lines, text, row) - local line = lines[row] + local line = lines[row + 1] if not line then return '' end @@ -239,7 +260,7 @@ function m.lineContent(lines, text, row) end function m.lineRange(lines, row) - local line = lines[row] + local line = lines[row + 1] if not line then return 0, 0 end diff --git a/server-beta/src/proto/interface.lua b/server-beta/src/proto/interface.lua index 21be1cdf..ddba623a 100644 --- a/server-beta/src/proto/interface.lua +++ b/server-beta/src/proto/interface.lua @@ -11,7 +11,7 @@ local m = {} function m.offset(lines, text, position) local row = position.line local start = guide.lineRange(lines, row) - local col = utf8.offset(text, position.character, start) + local col = utf8.offset(text, position.character + 1, start) local offset = guide.offsetOf(lines, row, col) return offset end @@ -25,7 +25,7 @@ end function m.position(lines, text, offset) local row, col = guide.positionOf(lines, offset) local start = guide.lineRange(lines, row) - local ucol = utf8.len(text, start, col, true) + local ucol = utf8.len(text, start + 1, col, true) return { line = row, character = ucol, @@ -56,4 +56,18 @@ function m.location(uri, range) } end +---@alias locationLink table +---@param uri string +---@param range range +---@param selection range +---@param origin range +function m.locationLink(uri, range, selection, origin) + return { + targetUri = uri, + targetRange = range, + targetSelectionRange = selection, + originSelectionRange = origin, + } +end + return m diff --git a/server-beta/src/proto/provider.lua b/server-beta/src/proto/provider.lua index 41b41fdd..9e3fe019 100644 --- a/server-beta/src/proto/provider.lua +++ b/server-beta/src/proto/provider.lua @@ -1,10 +1,10 @@ -local util = require 'utility' -local cap = require 'proto.capability' -local pub = require 'pub' -local task = require 'task' -local files = require 'files' -local proto = require 'proto.proto' -local interface = require 'proto.interface' +local util = require 'utility' +local cap = require 'proto.capability' +local pub = require 'pub' +local task = require 'task' +local files = require 'files' +local proto = require 'proto.proto' +local inte = require 'proto.interface' proto.on('initialize', function (params) --log.debug(util.dump(params)) @@ -59,14 +59,22 @@ proto.on('textDocument/hover', function () end) proto.on('textDocument/definition', function (params) - local clock = os.clock() local core = require 'core.definition' local uri = params.textDocument.uri local ast = files.getAst(uri) local text = files.getText(uri) - local offset = interface.offset(ast.lines, text, params.position) - local result, correct - repeat - result, correct = core(ast, text, offset) - until correct or os.clock() - clock >= 1.0 + local offset = inte.offset(ast.lines, text, params.position) + local result = core(ast, text, offset) + if not result then + return nil + end + local response = {} + for i, info in ipairs(result) do + response[i] = inte.locationLink(info.uri + , inte.range(ast.lines, text, info.target.start, info.target.finish) + , inte.range(ast.lines, text, info.target.start, info.target.finish) + , inte.range(ast.lines, text, info.source.start, info.source.finish) + ) + end + return response end) diff --git a/server-beta/src/pub/pub.lua b/server-beta/src/pub/pub.lua index 8fc6f2b2..f5ed9e8f 100644 --- a/server-beta/src/pub/pub.lua +++ b/server-beta/src/pub/pub.lua @@ -46,7 +46,7 @@ function m.recruitBraves(num) package.cpath, id )), - taskList = {}, + taskMap = {}, currentTask = nil, memory = 0, } |