summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-09-23 17:02:59 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-09-23 17:02:59 +0800
commitc73a5ccba1b39784b8b5ce8ce3f8694aee3e19da (patch)
tree11d215343ca0572b53331a97b6f01b9857bfed08
parent7253c49258c847601fe27d7e623d7d416556ba06 (diff)
downloadlua-language-server-c73a5ccba1b39784b8b5ce8ce3f8694aee3e19da.zip
update
-rw-r--r--script/core/code-action.lua58
-rw-r--r--script/core/command/removeSpace.lua42
-rw-r--r--script/files.lua8
3 files changed, 53 insertions, 55 deletions
diff --git a/script/core/code-action.lua b/script/core/code-action.lua
index 40aef8fc..c9f2ddd2 100644
--- a/script/core/code-action.lua
+++ b/script/core/code-action.lua
@@ -6,51 +6,37 @@ local guide = require "parser.guide"
local converter = require 'proto.converter'
local function checkDisableByLuaDocExits(uri, row, mode, code)
- local ast = files.getState(uri)
+ local state = files.getState(uri)
local text = files.getOriginText(uri)
- local line = lines[row]
- if ast.ast.docs and line then
- for _, doc in ipairs(ast.ast.docs) do
- if doc.start >= line.start
- and doc.finish <= line.finish then
- if doc.type == 'doc.diagnostic' then
- if doc.mode == mode then
- if doc.names then
- return {
- start = doc.finish,
- finish = doc.finish,
- newText = text:sub(doc.finish, doc.finish)
- .. ', '
- .. code
- }
- else
- return {
- start = doc.finish,
- finish = doc.finish,
- newText = text:sub(doc.finish, doc.finish)
- .. ': '
- .. code
- }
- end
- end
+ local lines = state.lines
+ if state.ast.docs and lines then
+ guide.eachSourceBetween(state.ast.docs, guide.positionOf(row, 0), guide.positionOf(row + 1, 0), function (doc)
+ if doc.type == 'doc.diagnostic'
+ and doc.mode == mode then
+ if doc.names then
+ return {
+ start = doc.finish,
+ finish = doc.finish,
+ newText = ', ' .. code,
+ }
+ else
+ return {
+ start = doc.finish,
+ finish = doc.finish,
+ newText = ': ' .. code,
+ }
end
end
- end
+ end)
end
return nil
end
local function checkDisableByLuaDocInsert(uri, row, mode, code)
- local ast = files.getState(uri)
- local text = files.getOriginText(uri)
- -- 先看看上一行是不是已经有了
- -- 没有的话就插入一行
- local line = lines[row]
return {
- start = line.start,
- finish = line.start,
- newText = '---@diagnostic ' .. mode .. ': ' .. code .. '\n'
- .. text:sub(line.start, line.start)
+ start = guide.positionOf(row, 0),
+ finish = guide.positionOf(row, 0),
+ newText = '---@diagnostic ' .. mode .. ': ' .. code .. '\n',
}
end
diff --git a/script/core/command/removeSpace.lua b/script/core/command/removeSpace.lua
index 3d7795a1..d92fee79 100644
--- a/script/core/command/removeSpace.lua
+++ b/script/core/command/removeSpace.lua
@@ -15,27 +15,39 @@ end
return function (data)
local uri = data.uri
local text = files.getText(uri)
- local ast = files.getState(uri)
- if not lines then
+ local state = files.getState(uri)
+ if not state then
return
end
+ local lines = {}
local textEdit = {}
- for i = 1, #lines do
- local line = guide.lineContent(lines, text, i, true)
- local pos = line:find '[ \t]+$'
- if pos then
- local start, finish = guide.lineRange(lines, i, true)
- start = start + pos
- if isInString(ast, start) then
- goto NEXT_LINE
- end
- textEdit[#textEdit+1] = {
- range = converter.packRange(uri, start, finish),
- newText = '',
- }
+ for i = 0, #lines do
+ local startPos = lines[i]
+ local startOffset = guide.positionToOffset(state, startPos)
+ local finishOffset = text:find('[\r\n]', startOffset) or (#text + 1)
+ local lastOffset = finishOffset - 1
+ local lastChar = text:sub(lastOffset, lastOffset)
+ if lastChar ~= ' ' and lastChar ~= '\t' then
+ goto NEXT_LINE
+ end
+ local lastPos = guide.offsetToPosition(state, lastOffset)
+ if isInString(state.ast, lastPos) then
goto NEXT_LINE
end
+ local firstOffset = startOffset
+ for n = lastOffset - 1, startOffset, -1 do
+ local char = text:sub(n, n)
+ if char ~= ' ' and char ~= '\t' then
+ firstOffset = n + 1
+ break
+ end
+ end
+ local firstPos = guide.offsetToPosition(state, firstOffset) - 1
+ textEdit[#textEdit+1] = {
+ range = converter.packRange(uri, firstPos, lastPos),
+ newText = '',
+ }
::NEXT_LINE::
end
diff --git a/script/files.lua b/script/files.lua
index e7999533..0c2c2874 100644
--- a/script/files.lua
+++ b/script/files.lua
@@ -518,11 +518,11 @@ function m.getVisibles(uri)
end
local visibles = {}
for i, range in ipairs(ranges) do
- -- TODO 改成行号
- local start, finish = m.unrange(uri, range)
+ local startRow = guide.rowColOf(range.start)
+ local finishRow = guide.rowColOf(range['end'])
visibles[i] = {
- start = start,
- finish = finish,
+ start = guide.positionOf(startRow, 0),
+ finish = guide.positionOf(finishRow, 0),
}
end
return visibles