Skip to content

Commit f02a145

Browse files
committed
fix performance issue
1 parent 97d2408 commit f02a145

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* `NEW` using `enum (partial)`, it suggests all fields with the same `enum` type rather than just the fields from the current table.
66
* `NEW` When using `enum["<key>" or <index>]`, undefined fields will raise an 'undefined' error.
77
* `FIX` Renaming files in the directory leads to the auto-correction in "require" adding extra characters.
8+
* `FIX` Performance issue
89

910
## 3.10.4
1011
`2024-8-16`

script/core/fix-indent.lua

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ local lookBackward = require 'core.look-backward'
55
local util = require 'utility'
66
local client = require 'client'
77

8-
---@param state parser.state
8+
---@param uri uri
99
---@param change table
10-
local function removeSpacesAfterEnter(state, change)
10+
local function removeSpacesAfterEnter(uri, change)
1111
if not change.text:match '^\r?\n[\t ]+\r?\n$' then
1212
return false
1313
end
14+
local state = files.getState(uri)
15+
if not state then
16+
return false
17+
end
1418
local lines = state.originLines or state.lines
1519
local text = state.originText or state.lua
1620
---@cast text -?
@@ -76,10 +80,15 @@ local function getBlock(state, pos)
7680
return block
7781
end
7882

79-
local function fixWrongIndent(state, change)
83+
---@param uri uri
84+
local function fixWrongIndent(uri, change)
8085
if not change.text:match '^\r?\n[\t ]+$' then
8186
return false
8287
end
88+
local state = files.getState(uri)
89+
if not state then
90+
return false
91+
end
8392
local position = guide.positionOf(change.range.start.line, change.range.start.character)
8493
local row = guide.rowColOf(position)
8594
local myIndent = getIndent(state, row + 1)
@@ -114,12 +123,17 @@ local function fixWrongIndent(state, change)
114123
return edits
115124
end
116125

117-
---@param state parser.state
118-
local function applyEdits(state, edits)
126+
---@param uri uri
127+
local function applyEdits(uri, edits)
119128
if #edits == 0 then
120129
return
121130
end
122131

132+
local state = files.getState(uri)
133+
if not state then
134+
return
135+
end
136+
123137
local lines = state.originLines or state.lines
124138

125139
local results = {}
@@ -157,17 +171,13 @@ return function (uri, changes)
157171
if not client.getOption('fixIndents') then
158172
return
159173
end
160-
local state = files.compileState(uri)
161-
if not state then
162-
return
163-
end
164174

165175
local firstChange = changes[1]
166176
if firstChange.range then
167-
local edits = removeSpacesAfterEnter(state, firstChange)
168-
or fixWrongIndent(state, firstChange)
177+
local edits = removeSpacesAfterEnter(uri, firstChange)
178+
or fixWrongIndent(uri, firstChange)
169179
if edits then
170-
applyEdits(state, edits)
180+
applyEdits(uri, edits)
171181
end
172182
end
173183
end

0 commit comments

Comments
 (0)