@@ -5,12 +5,16 @@ local lookBackward = require 'core.look-backward'
55local util = require ' utility'
66local 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
7781end
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
115124end
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
173183end
0 commit comments