diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-09-23 15:00:50 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-09-23 15:00:50 +0800 |
commit | 5c9b1b467af1636ea21a9bd3d398e220790eea54 (patch) | |
tree | 8a834bcb5e9c8e6c32f118c23fbeafcaf01630ab /script | |
parent | 0a60aa17c240d4fb080b091891f659a9d74aeffd (diff) | |
download | lua-language-server-5c9b1b467af1636ea21a9bd3d398e220790eea54.zip |
cleanup
Diffstat (limited to 'script')
-rw-r--r-- | script/core/code-action.lua | 21 | ||||
-rw-r--r-- | script/core/command/solve.lua | 12 | ||||
-rw-r--r-- | script/core/semantic-tokens.lua | 5 | ||||
-rw-r--r-- | script/files.lua | 5 | ||||
-rw-r--r-- | script/proto/converter.lua | 50 | ||||
-rw-r--r-- | script/provider/provider.lua | 56 |
6 files changed, 100 insertions, 49 deletions
diff --git a/script/core/code-action.lua b/script/core/code-action.lua index 5c8f1634..2bc6ae50 100644 --- a/script/core/code-action.lua +++ b/script/core/code-action.lua @@ -1,8 +1,9 @@ -local files = require 'files' -local lang = require 'language' -local util = require 'utility' -local sp = require 'bee.subprocess' -local guide = require "parser.guide" +local files = require 'files' +local lang = require 'language' +local util = require 'utility' +local sp = require 'bee.subprocess' +local guide = require "parser.guide" +local converter = require 'proto.converter' local function checkDisableByLuaDocExits(uri, row, mode, code) local ast = files.getState(uri) @@ -131,8 +132,8 @@ end local function solveUndefinedGlobal(uri, diag, results) local ast = files.getState(uri) - local offset = files.offsetOfWord(uri, diag.range.start) - guide.eachSourceContain(ast.ast, offset, function (source) + local start = converter.unpackRange(uri, diag.range) + guide.eachSourceContain(ast.ast, start, function (source) if source.type ~= 'getglobal' then return end @@ -150,8 +151,8 @@ end local function solveLowercaseGlobal(uri, diag, results) local ast = files.getState(uri) - local offset = files.offsetOfWord(uri, diag.range.start) - guide.eachSourceContain(ast.ast, offset, function (source) + local start = converter.unpackRange(uri, diag.range) + guide.eachSourceContain(ast.ast, start, function (source) if source.type ~= 'setglobal' then return end @@ -267,7 +268,7 @@ local function solveSyntax(uri, diag, results) end local function solveNewlineCall(uri, diag, results) - local start = files.unrange(uri, diag.range) + local start = converter.unpackRange(uri, diag.range) results[#results+1] = { title = lang.script.ACTION_ADD_SEMICOLON, kind = 'quickfix', diff --git a/script/core/command/solve.lua b/script/core/command/solve.lua index a493de24..790adbee 100644 --- a/script/core/command/solve.lua +++ b/script/core/command/solve.lua @@ -1,7 +1,8 @@ -local files = require 'files' -local guide = require 'parser.guide' -local proto = require 'proto' -local lang = require 'language' +local files = require 'files' +local guide = require 'parser.guide' +local proto = require 'proto' +local lang = require 'language' +local converter = require 'proto.converter' local opMap = { ['+'] = true, @@ -34,8 +35,7 @@ return function (data) return end - local start = files.offsetOfWord(uri, data.range.start) - local finish = files.offsetOfWord(uri, data.range['end']) + local start, finish = converter.unpackRange(uri, data.range) local result = guide.eachSourceContain(ast.ast, start, function (source) if source.start ~= start diff --git a/script/core/semantic-tokens.lua b/script/core/semantic-tokens.lua index 238df862..405f2735 100644 --- a/script/core/semantic-tokens.lua +++ b/script/core/semantic-tokens.lua @@ -5,6 +5,7 @@ local define = require 'proto.define' local vm = require 'vm' local util = require 'utility' local guide = require 'parser.guide' +local converter = require 'proto.converter' local Care = {} Care['setglobal'] = function (source, results) @@ -188,8 +189,8 @@ local function buildTokens(uri, results) local lastLine = 0 local lastStartChar = 0 for i, source in ipairs(results) do - local startPos = files.position(uri, source.start, 'left') - local finishPos = files.position(uri, source.finish, 'right') + local startPos = converter.packPosition(uri, source.start) + local finishPos = converter.packPosition(uri, source.finish) local line = startPos.line local startChar = startPos.character local deltaLine = line - lastLine diff --git a/script/files.lua b/script/files.lua index e0c0dccb..d6ea057f 100644 --- a/script/files.lua +++ b/script/files.lua @@ -518,6 +518,7 @@ function m.getVisibles(uri) end local visibles = {} for i, range in ipairs(ranges) do + -- TODO 改成行号 local start, finish = m.unrange(uri, range) visibles[i] = { start = start, @@ -543,8 +544,6 @@ local function isNameChar(text) return false end ----@alias position table - --- 将应用差异前的offset转换为应用差异后的offset ---@param uri uri ---@param offset integer @@ -626,7 +625,7 @@ function m.position(uri, offset, leftOrRight) end --- 将起点与终点位置转化为 range ----@alias range table + ---@param uri uri ---@param offset1 integer ---@param offset2 integer diff --git a/script/proto/converter.lua b/script/proto/converter.lua new file mode 100644 index 00000000..4b4b9f5d --- /dev/null +++ b/script/proto/converter.lua @@ -0,0 +1,50 @@ +local guide = require 'parser.guide' + +local m = {} + + +---@alias position {line: integer, character: integer} + +---@param uri uri +---@param pos integer +---@return position +function m.packPosition(uri, pos) + local row, col = guide.rowColOf(pos) + return { + line = row, + character = col, + } +end + +---@param uri uri +---@param position position +function m.unpackPosition(uri, position) + local pos = guide.positionOf(position.line, position.character) + return pos +end + +---@alias range {start: position, end: position} + +---@param uri uri +---@param start integer +---@param finish integer +---@return range +function m.packRange(uri, start, finish) + local range = { + start = m.packPosition(uri, start), + ['end'] = m.packPosition(uri, finish), + } + return range +end + +---@param uri uri +---@param range range +---@return integer start +---@return integer finish +function m.unpackRange(uri, range) + local start = m.unpackPosition(uri, range.start) + local finish = m.unpackPosition(uri, range['end']) + return start, finish +end + +return m diff --git a/script/provider/provider.lua b/script/provider/provider.lua index e1bd0f4f..d4bf0bee 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -13,6 +13,7 @@ local lang = require 'language' local progress = require 'progress' local tm = require 'text-merger' local cfgLoader = require 'config.loader' +local converter = require 'proto.converter' local function updateConfig() local new @@ -183,8 +184,8 @@ proto.on('textDocument/hover', function (params) if not files.exists(uri) then return nil end - local offset = files.offsetOfWord(uri, params.position) - local hover, source = core.byUri(uri, offset) + local pos = converter.unpackPosition(uri, params.position) + local hover, source = core.byUri(uri, pos) if not hover then return nil end @@ -205,8 +206,8 @@ proto.on('textDocument/definition', function (params) if not files.exists(uri) then return nil end - local offset = files.offsetOfWord(uri, params.position) - local result = core(uri, offset) + local pos = converter.unpackPosition(uri, params.position) + local result = core(uri, pos) if not result then return nil end @@ -240,8 +241,8 @@ proto.on('textDocument/typeDefinition', function (params) if not files.exists(uri) then return nil end - local offset = files.offsetOfWord(uri, params.position) - local result = core(uri, offset) + local pos = converter.unpackPosition(uri, params.position) + local result = core(uri, pos) if not result then return nil end @@ -275,8 +276,8 @@ proto.on('textDocument/references', function (params) if not files.exists(uri) then return nil end - local offset = files.offsetOfWord(uri, params.position) - local result = core(uri, offset) + local pos = converter.unpackPosition(uri, params.position) + local result = core(uri, pos) if not result then return nil end @@ -296,8 +297,8 @@ proto.on('textDocument/documentHighlight', function (params) if not files.exists(uri) then return nil end - local offset = files.offsetOfWord(uri, params.position) - local result = core(uri, offset) + local pos = converter.unpackPosition(uri, params.position) + local result = core(uri, pos) if not result then return nil end @@ -319,8 +320,8 @@ proto.on('textDocument/rename', function (params) if not files.exists(uri) then return nil end - local offset = files.offsetOfWord(uri, params.position) - local result = core.rename(uri, offset, params.newName) + local pos = converter.unpackPosition(uri, params.position) + local result = core.rename(uri, pos, params.newName) if not result then return nil end @@ -344,8 +345,8 @@ proto.on('textDocument/prepareRename', function (params) if not files.exists(uri) then return nil end - local offset = files.offsetOfWord(uri, params.position) - local result = core.prepareRename(uri, offset) + local pos = converter.unpackPosition(uri, params.position) + local result = core.prepareRename(uri, pos) if not result then return nil end @@ -391,8 +392,8 @@ proto.on('textDocument/completion', function (params) end await.setPriority(1000) local clock = os.clock() - local offset = files.offset(uri, params.position) - local result = core.completion(uri, offset - 1, triggerCharacter) + local pos = converter.unpackPosition(uri, params.position) + local result = core.completion(uri, pos, triggerCharacter) local passed = os.clock() - clock if passed > 0.1 then log.warn(('Completion takes %.3f sec.'):format(passed)) @@ -517,9 +518,9 @@ proto.on('textDocument/signatureHelp', function (params) end await.close('signatureHelp') await.setID('signatureHelp') - local offset = files.offset(uri, params.position) + local pos = converter.unpackPosition(uri, params.position) local core = require 'core.signature' - local results = core(uri, offset - 1) + local results = core(uri, pos) if not results then return nil end @@ -599,7 +600,7 @@ proto.on('textDocument/codeAction', function (params) return nil end - local start, finish = files.unrange(uri, range) + local start, finish = converter.unpackRange(uri, range) local results = core(uri, start, finish, diagnostics) if not results or #results == 0 then @@ -705,8 +706,7 @@ proto.on('textDocument/semanticTokens/range', function (params) start = 0 finish = #files.getText(uri) else - start = files.offsetOfWord(uri, params.range.start) - finish = files.offsetOfWord(uri, params.range['end']) + start, finish = converter.unpackRange(uri, params.range) end local results = core(uri, start, finish) return { @@ -729,8 +729,8 @@ proto.on('textDocument/foldingRange', function (params) local results = {} for _, region in ipairs(regions) do - local startLine = files.position(uri, region.start, 'left').line - local endLine = files.position(uri, region.finish, 'right').line + local startLine = converter.packPosition(uri, region.start).line + local endLine = converter.packPosition(uri, region.finish).line if not region.hideLastLine then endLine = endLine - 1 end @@ -778,8 +778,8 @@ proto.on('textDocument/onTypeFormatting', function (params) return nil end local core = require 'core.type-formatting' - local offset = files.offset(uri, params.position) - local edits = core(uri, offset - 1, ch) + local pos = converter.unpackPosition(uri, params.position) + local edits = core(uri, pos, ch) if not edits or #edits == 0 then return nil end @@ -804,13 +804,13 @@ proto.on('$/requestHint', function (params) end workspace.awaitReady() local uri = params.textDocument.uri - local start, finish = files.unrange(uri, params.range) + local start, finish = converter.unpackRange(uri, params.range) local results = core(uri, start, finish) local hintResults = {} for i, res in ipairs(results) do hintResults[i] = { text = res.text, - pos = files.position(uri, res.offset, res.where), + pos = converter.packPosition(uri, res.offset), kind = res.kind, } end @@ -840,7 +840,7 @@ do for _, edit in ipairs(piece) do edits[#edits+1] = { text = edit.text, - pos = files.position(uri, edit.offset, edit.where), + pos = converter.packPosition(uri, edit.offset), } end end |