diff options
author | Arcanox <arcanox@arcanox.me> | 2021-09-25 18:00:15 -0500 |
---|---|---|
committer | Arcanox <arcanox@arcanox.me> | 2021-09-25 18:00:15 -0500 |
commit | dc685d1addad2f2e57f55a20bb6cca79c222c130 (patch) | |
tree | abb200fd7f217460a4543cb6f32af7ebac308bc0 /script/proto | |
parent | a465b35d5eefc11c1daf3c29b41ce95ee098a782 (diff) | |
parent | 1f0a2d0e9283a4cb7f7b3fc72258eb1c5ba4e5dd (diff) | |
download | lua-language-server-dc685d1addad2f2e57f55a20bb6cca79c222c130.zip |
Merge branch 'master' into improve-semantic-highlighting
# Conflicts:
# script/core/semantic-tokens.lua
Diffstat (limited to 'script/proto')
-rw-r--r-- | script/proto/converter.lua | 168 | ||||
-rw-r--r-- | script/proto/define.lua | 36 | ||||
-rw-r--r-- | script/proto/proto.lua | 5 |
3 files changed, 174 insertions, 35 deletions
diff --git a/script/proto/converter.lua b/script/proto/converter.lua new file mode 100644 index 00000000..19d592fe --- /dev/null +++ b/script/proto/converter.lua @@ -0,0 +1,168 @@ +local guide = require 'parser.guide' +local files = require 'files' + +local m = {} + +---@alias position {line: integer, character: integer} + +local function rawPackPosition(uri, pos) + local row, col = guide.rowColOf(pos) + if col > 0 then + local state = files.getState(uri) + local text = files.getText(uri) + if text then + local lineOffset = state.lines[row] + col = utf8.len(text, lineOffset, lineOffset + col - 1, true) + end + end + return { + line = row, + character = col, + } +end + +local function diffedPackPosition(uri, pos) + local state = files.getState(uri) + local offset = guide.positionToOffset(state, pos) + local originOffset = files.diffedOffsetBack(uri, offset) + local originLines = files.getOriginLines(uri) + local originPos = guide.offsetToPositionByLines(originLines, originOffset) + local row, col = guide.rowColOf(originPos) + if col > 0 then + local text = files.getOriginText(uri) + if text then + local lineOffset = originLines[row] + col = utf8.len(text, lineOffset, lineOffset + col - 1, true) + end + end + return { + line = row, + character = col, + } +end + +---@param uri uri +---@param pos integer +---@return position +function m.packPosition(uri, pos) + if files.hasDiffed(uri) then + return diffedPackPosition(uri, pos) + else + return rawPackPosition(uri, pos) + end +end + +local function rawUnpackPosition(uri, position) + local row, col = position.line, position.character + if col > 0 then + local state = files.getState(uri) + local text = files.getText(uri) + if text then + local lineOffset = state.lines[row] + local textOffset = utf8.offset(text, col + 1, lineOffset) + if textOffset then + col = textOffset - lineOffset + end + end + end + local pos = guide.positionOf(row, col) + return pos +end + +local function diffedUnpackPosition(uri, position) + local row, col = position.line, position.character + local originLines = files.getOriginLines(uri) + if col > 0 then + local text = files.getOriginText(uri) + if text then + local lineOffset = originLines[row] + local textOffset = utf8.offset(text, col + 1, lineOffset) + if textOffset then + col = textOffset - lineOffset + end + end + end + local state = files.getState(uri) + local originPos = guide.positionOf(row, col) + local originOffset = guide.positionToOffsetByLines(originLines, originPos) + local offset = files.diffedOffset(uri, originOffset) + local pos = guide.offsetToPosition(state, offset) + return pos +end + +---@param uri uri +---@param position position +---@return integer +function m.unpackPosition(uri, position) + if files.hasDiffed(uri) then + return diffedUnpackPosition(uri, position) + else + return rawUnpackPosition(uri, position) + end +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 + +---@alias location {uri: uri, range: range} + +---@param uri string +---@param range range +---@return location +function m.location(uri, range) + return { + uri = uri, + range = range, + } +end + +---@alias locationLink {targetUri:uri, targetRange: range, targetSelectionRange: range, originSelectionRange: range} + +---@param uri string +---@param range range +---@param selection range +---@param origin range +---@return locationLink +function m.locationLink(uri, range, selection, origin) + return { + targetUri = uri, + targetRange = range, + targetSelectionRange = selection, + originSelectionRange = origin, + } +end + +---@alias textEdit {range: range, newText: string} + +---@param range range +---@param newtext string +---@return textEdit +function m.textEdit(range, newtext) + return { + range = range, + newText = newtext, + } +end + +return m diff --git a/script/proto/define.lua b/script/proto/define.lua index f3fa1ba5..731c5cc6 100644 --- a/script/proto/define.lua +++ b/script/proto/define.lua @@ -1,37 +1,5 @@ local m = {} ----@alias location table ----@param uri string ----@param range range ----@return location -function m.location(uri, range) - return { - uri = uri, - range = 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 - -function m.textEdit(range, newtext) - return { - range = range, - newText = newtext, - } -end - --- 诊断等级 m.DiagnosticSeverity = { Error = 1, @@ -60,14 +28,14 @@ m.DiagnosticDefaultSeverity = { ['redefined-local'] = 'Hint', ['newline-call'] = 'Information', ['newfield-call'] = 'Warning', - ['redundant-parameter'] = 'Hint', + ['redundant-parameter'] = 'Warning', ['ambiguity-1'] = 'Warning', ['lowercase-global'] = 'Information', ['undefined-env-child'] = 'Information', ['duplicate-index'] = 'Warning', ['duplicate-set-field'] = 'Warning', ['empty-block'] = 'Hint', - ['redundant-value'] = 'Hint', + ['redundant-value'] = 'Warning', ['code-after-break'] = 'Hint', ['unbalanced-assignments'] = 'Warning', ['close-non-object'] = 'Warning', diff --git a/script/proto/proto.lua b/script/proto/proto.lua index a0880011..61306b9f 100644 --- a/script/proto/proto.lua +++ b/script/proto/proto.lua @@ -4,7 +4,6 @@ local await = require 'await' local pub = require 'pub' local jsonrpc = require 'jsonrpc' local define = require 'proto.define' -local timer = require 'timer' local json = require 'json' local reqCounter = util.counter() @@ -121,6 +120,9 @@ function m.doMethod(proto) end await.call(function () --log.debug('Start method:', method) + if proto.id then + await.setID('proto:' .. proto.id) + end local clock = os.clock() local ok = true local res @@ -134,6 +136,7 @@ function m.doMethod(proto) if not proto.id then return end + await.close('proto:' .. proto.id) if ok then m.response(proto.id, res) else |