summaryrefslogtreecommitdiff
path: root/script/core/command/removeSpace.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-09-24 15:08:02 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-09-24 15:08:02 +0800
commit4b085b8aea5f33ec114baa31d2b9d72341383c32 (patch)
treefe35a326408e762711a31d3e803464f0c1a8468d /script/core/command/removeSpace.lua
parent0c8c6bbf23082d0b858646846a47a3001f718ae2 (diff)
parent35ce57976db3b4c42193279dd55972ea013fecad (diff)
downloadlua-language-server-4b085b8aea5f33ec114baa31d2b9d72341383c32.zip
Merge branch 'newparser'
Diffstat (limited to 'script/core/command/removeSpace.lua')
-rw-r--r--script/core/command/removeSpace.lua51
1 files changed, 31 insertions, 20 deletions
diff --git a/script/core/command/removeSpace.lua b/script/core/command/removeSpace.lua
index b94f9788..3021d4a4 100644
--- a/script/core/command/removeSpace.lua
+++ b/script/core/command/removeSpace.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 function isInString(ast, offset)
return guide.eachSourceContain(ast.ast, offset, function (source)
@@ -13,29 +14,39 @@ end
return function (data)
local uri = data.uri
- local lines = files.getLines(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 = state.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 = files.range(uri, start, finish),
- newText = '',
- }
+ for i = 0, #lines do
+ local startOffset = lines[i]
+ 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