Skip to content

Commit bfb576d

Browse files
committed
use state instead of uri for converter
The state may have changed when responding, so we need to use the state when requesting. Try not to get the state on the spot.
1 parent 3bdd74a commit bfb576d

File tree

17 files changed

+340
-209
lines changed

17 files changed

+340
-209
lines changed

script/client.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,14 @@ end
382382
---@param uri uri
383383
---@param edits textEditor[]
384384
function m.editText(uri, edits)
385-
local files = require 'files'
385+
local files = require 'files'
386+
local state = files.getState(uri)
387+
if not state then
388+
return
389+
end
386390
local textEdits = {}
387391
for i, edit in ipairs(edits) do
388-
textEdits[i] = converter.textEdit(converter.packRange(uri, edit.start, edit.finish), edit.text)
392+
textEdits[i] = converter.textEdit(converter.packRange(state, edit.start, edit.finish), edit.text)
389393
end
390394
proto.request('workspace/applyEdit', {
391395
edit = {

script/core/code-action.lua

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ local function solveUndefinedGlobal(uri, diag, results)
135135
if not state then
136136
return
137137
end
138-
local start = converter.unpackRange(uri, diag.range)
138+
local start = converter.unpackRange(state, diag.range)
139139
guide.eachSourceContain(state.ast, start, function (source)
140140
if source.type ~= 'getglobal' then
141141
return
@@ -157,7 +157,7 @@ local function solveLowercaseGlobal(uri, diag, results)
157157
if not state then
158158
return
159159
end
160-
local start = converter.unpackRange(uri, diag.range)
160+
local start = converter.unpackRange(state, diag.range)
161161
guide.eachSourceContain(state.ast, start, function (source)
162162
if source.type ~= 'setglobal' then
163163
return
@@ -175,7 +175,7 @@ local function findSyntax(uri, diag)
175175
end
176176
for _, err in ipairs(state.errs) do
177177
if err.type:lower():gsub('_', '-') == diag.code then
178-
local range = converter.packRange(uri, err.start, err.finish)
178+
local range = converter.packRange(state, err.start, err.finish)
179179
if util.equal(range, diag.range) then
180180
return err
181181
end
@@ -276,7 +276,11 @@ local function solveSyntax(uri, diag, results)
276276
end
277277

278278
local function solveNewlineCall(uri, diag, results)
279-
local start = converter.unpackRange(uri, diag.range)
279+
local state = files.getState(uri)
280+
if not state then
281+
return
282+
end
283+
local start = converter.unpackRange(state, diag.range)
280284
results[#results+1] = {
281285
title = lang.script.ACTION_ADD_SEMICOLON,
282286
kind = 'quickfix',
@@ -333,7 +337,7 @@ local function solveAwaitInSync(uri, diag, results)
333337
if not state then
334338
return
335339
end
336-
local start, finish = converter.unpackRange(uri, diag.range)
340+
local start, finish = converter.unpackRange(state, diag.range)
337341
local parentFunction
338342
guide.eachSourceType(state.ast, 'function', function (source)
339343
if source.start > finish
@@ -369,6 +373,10 @@ local function solveAwaitInSync(uri, diag, results)
369373
end
370374

371375
local function solveSpell(uri, diag, results)
376+
local state = files.getState(uri)
377+
if not state then
378+
return
379+
end
372380
local spell = require 'provider.spell'
373381
local word = diag.data
374382
if word == nil then
@@ -401,8 +409,8 @@ local function solveSpell(uri, diag, results)
401409
changes = {
402410
[uri] = {
403411
{
404-
start = converter.unpackPosition(uri, diag.range.start),
405-
finish = converter.unpackPosition(uri, diag.range["end"]),
412+
start = converter.unpackPosition(state, diag.range.start),
413+
finish = converter.unpackPosition(state, diag.range["end"]),
406414
newText = suggest
407415
}
408416
}

script/core/command/jsonToLua.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ local guide = require 'parser.guide'
1111
return function (data)
1212
local state = files.getState(data.uri)
1313
local text = files.getText(data.uri)
14-
if not text then
14+
if not text or not state then
1515
return
1616
end
1717
local start = guide.positionToOffset(state, data.start)
@@ -43,7 +43,7 @@ return function (data)
4343
changes = {
4444
[data.uri] = {
4545
{
46-
range = converter.packRange(data.uri, data.start, data.finish),
46+
range = converter.packRange(state, data.start, data.finish),
4747
newText = luaStr,
4848
}
4949
}

script/core/command/removeSpace.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ return function (data)
3838
end
3939
local firstPos = guide.offsetToPosition(state, firstOffset) - 1
4040
textEdit[#textEdit+1] = {
41-
range = converter.packRange(uri, firstPos, lastPos),
41+
range = converter.packRange(state, firstPos, lastPos),
4242
newText = '',
4343
}
4444

script/core/command/solve.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ return function (data)
3636
return
3737
end
3838

39-
local start, finish = converter.unpackRange(uri, data.range)
39+
local start, finish = converter.unpackRange(state, data.range)
4040

4141
local result = guide.eachSourceContain(state.ast, start, function (source)
4242
if source.start ~= start
@@ -86,7 +86,7 @@ return function (data)
8686
changes = {
8787
[uri] = {
8888
{
89-
range = converter.packRange(uri, result.start, result.finish),
89+
range = converter.packRange(state, result.start, result.finish),
9090
newText = ('(%s)'):format(text:sub(
9191
guide.positionToOffset(state, result.start + 1),
9292
guide.positionToOffset(state, result.finish)

script/core/diagnostics/codestyle-check.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ local pformatting = require 'provider.formatting'
66

77
---@async
88
return function(uri, callback)
9-
local text = files.getOriginText(uri)
10-
if not text then
9+
local state = files.getState(uri)
10+
if not state then
1111
return
1212
end
13+
local text = state.originText
1314

1415
local suc, codeFormat = pcall(require, 'code_format')
1516
if not suc then
@@ -31,8 +32,8 @@ return function(uri, callback)
3132
if diagnosticInfos then
3233
for _, diagnosticInfo in ipairs(diagnosticInfos) do
3334
callback {
34-
start = converter.unpackPosition(uri, diagnosticInfo.range.start),
35-
finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]),
35+
start = converter.unpackPosition(state, diagnosticInfo.range.start),
36+
finish = converter.unpackPosition(state, diagnosticInfo.range["end"]),
3637
message = diagnosticInfo.message
3738
}
3839
end

script/core/diagnostics/spell-check.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ local spell = require 'provider.spell'
66

77
---@async
88
return function(uri, callback)
9-
local text = files.getOriginText(uri)
10-
if not text then
9+
local state = files.getState(uri)
10+
if not state then
1111
return
1212
end
13+
local text = state.originText
1314

1415
local status, diagnosticInfos = spell.spellCheck(uri, text)
1516

@@ -24,8 +25,8 @@ return function(uri, callback)
2425
if diagnosticInfos then
2526
for _, diagnosticInfo in ipairs(diagnosticInfos) do
2627
callback {
27-
start = converter.unpackPosition(uri, diagnosticInfo.range.start),
28-
finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]),
28+
start = converter.unpackPosition(state, diagnosticInfo.range.start),
29+
finish = converter.unpackPosition(state, diagnosticInfo.range["end"]),
2930
message = diagnosticInfo.message,
3031
data = diagnosticInfo.data
3132
}

script/core/rangeformatting.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ local log = require("log")
33
local converter = require("proto.converter")
44

55
return function(uri, range, options)
6+
local state = files.getState(uri)
7+
if not state then
8+
return
9+
end
610
local suc, codeFormat = pcall(require, "code_format")
711
if not suc then
812
return
913
end
10-
local text = files.getOriginText(uri)
14+
local text = state.originText
1115
local status, formattedText, startLine, endLine = codeFormat.range_format(
1216
uri, text, range.start.line, range["end"].line, options)
1317

@@ -21,8 +25,8 @@ return function(uri, range, options)
2125

2226
return {
2327
{
24-
start = converter.unpackPosition(uri, { line = startLine, character = 0 }),
25-
finish = converter.unpackPosition(uri, { line = endLine + 1, character = 0 }),
28+
start = converter.unpackPosition(state, { line = startLine, character = 0 }),
29+
finish = converter.unpackPosition(state, { line = endLine + 1, character = 0 }),
2630
text = formattedText,
2731
}
2832
}

script/core/semantic-tokens.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,13 +699,15 @@ local Care = util.switch()
699699
}
700700
end)
701701

702-
local function buildTokens(uri, results)
702+
---@param state table
703+
---@param results table
704+
local function buildTokens(state, results)
703705
local tokens = {}
704706
local lastLine = 0
705707
local lastStartChar = 0
706708
for i, source in ipairs(results) do
707-
local startPos = converter.packPosition(uri, source.start)
708-
local finishPos = converter.packPosition(uri, source.finish)
709+
local startPos = converter.packPosition(state, source.start)
710+
local finishPos = converter.packPosition(state, source.finish)
709711
local line = startPos.line
710712
local startChar = startPos.character
711713
local deltaLine = line - lastLine
@@ -881,7 +883,7 @@ return function (uri, start, finish)
881883

882884
results = solveMultilineAndOverlapping(state, results)
883885

884-
local tokens = buildTokens(uri, results)
886+
local tokens = buildTokens(state, results)
885887

886888
return tokens
887889
end

script/core/type-formatting.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,22 @@ local function typeFormat(results, uri, position, ch, options)
199199
return
200200
end
201201
local converter = require("proto.converter")
202-
local pos = converter.packPosition(uri, position)
202+
local pos = converter.packPosition(state, position)
203203
local typeFormatOptions = config.get(uri, 'Lua.typeFormat.config')
204204
local success, result = codeFormat.type_format(uri, text, pos.line, pos.character, options, typeFormatOptions)
205205
if success then
206206
local range = result.range
207207
results[#results+1] = {
208208
text = result.newText,
209-
start = converter.unpackPosition(uri, { line = range.start.line, character = range.start.character }),
210-
finish = converter.unpackPosition(uri, { line = range["end"].line, character = range["end"].character }),
209+
start = converter.unpackPosition(state, { line = range.start.line, character = range.start.character }),
210+
finish = converter.unpackPosition(state, { line = range["end"].line, character = range["end"].character }),
211211
}
212212
end
213213
end
214214

215215
return function (uri, position, ch, options)
216-
local ast = files.getState(uri)
217-
if not ast then
216+
local state = files.getState(uri)
217+
if not state then
218218
return nil
219219
end
220220

0 commit comments

Comments
 (0)