summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2024-08-19 14:43:05 +0800
committer最萌小汐 <sumneko@hotmail.com>2024-08-19 14:43:05 +0800
commitf02a14597a0a689ef809f78c7baea4ae492910b7 (patch)
treed8101d7a7a657d3fb4a101e3b5f1322dfa336cf0 /script
parent97d2408f7eb03fcb875896d99584e132d0c9585d (diff)
downloadlua-language-server-f02a14597a0a689ef809f78c7baea4ae492910b7.zip
fix performance issue
Diffstat (limited to 'script')
-rw-r--r--script/core/fix-indent.lua34
1 files changed, 22 insertions, 12 deletions
diff --git a/script/core/fix-indent.lua b/script/core/fix-indent.lua
index 0542e4a8..0b4b6985 100644
--- a/script/core/fix-indent.lua
+++ b/script/core/fix-indent.lua
@@ -5,12 +5,16 @@ local lookBackward = require 'core.look-backward'
local util = require 'utility'
local client = require 'client'
----@param state parser.state
+---@param uri uri
---@param change table
-local function removeSpacesAfterEnter(state, change)
+local function removeSpacesAfterEnter(uri, change)
if not change.text:match '^\r?\n[\t ]+\r?\n$' then
return false
end
+ local state = files.getState(uri)
+ if not state then
+ return false
+ end
local lines = state.originLines or state.lines
local text = state.originText or state.lua
---@cast text -?
@@ -76,10 +80,15 @@ local function getBlock(state, pos)
return block
end
-local function fixWrongIndent(state, change)
+---@param uri uri
+local function fixWrongIndent(uri, change)
if not change.text:match '^\r?\n[\t ]+$' then
return false
end
+ local state = files.getState(uri)
+ if not state then
+ return false
+ end
local position = guide.positionOf(change.range.start.line, change.range.start.character)
local row = guide.rowColOf(position)
local myIndent = getIndent(state, row + 1)
@@ -114,12 +123,17 @@ local function fixWrongIndent(state, change)
return edits
end
----@param state parser.state
-local function applyEdits(state, edits)
+---@param uri uri
+local function applyEdits(uri, edits)
if #edits == 0 then
return
end
+ local state = files.getState(uri)
+ if not state then
+ return
+ end
+
local lines = state.originLines or state.lines
local results = {}
@@ -157,17 +171,13 @@ return function (uri, changes)
if not client.getOption('fixIndents') then
return
end
- local state = files.compileState(uri)
- if not state then
- return
- end
local firstChange = changes[1]
if firstChange.range then
- local edits = removeSpacesAfterEnter(state, firstChange)
- or fixWrongIndent(state, firstChange)
+ local edits = removeSpacesAfterEnter(uri, firstChange)
+ or fixWrongIndent(uri, firstChange)
if edits then
- applyEdits(state, edits)
+ applyEdits(uri, edits)
end
end
end