Skip to content

Commit ac9e09f

Browse files
committed
improve completion speed
1 parent d066720 commit ac9e09f

File tree

8 files changed

+62
-30
lines changed

8 files changed

+62
-30
lines changed

changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
+ `Lua.semantic.variable`
99
+ `Lua.semantic.annotation`
1010
+ `Lua.semantic.keyword`
11-
* `CHG` diagnostic: smoother
11+
* `CHG` completion: improve response speed
1212
* `CHG` completion: can be triggered in `LuaDoc` and strings
13+
* `CHG` diagnostic: smoother
1314
* `CHG` settings `Lua.color.mode` removed
1415
* `FIX` [#879](https://github.com/sumneko/lua-language-server/issues/879)
1516
* `FIX` [#884](https://github.com/sumneko/lua-language-server/issues/884)

script/core/completion/completion.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -645,14 +645,11 @@ local function checkCommon(state, word, position, results)
645645
for _, data in ipairs(keyWordMap) do
646646
used[data[1]] = true
647647
end
648-
if not config.get(state.uri, 'Lua.completion.workspaceWord')
649-
or #word >= 2 then
650-
results.complete = true
651-
end
652648
if config.get(state.uri, 'Lua.completion.workspaceWord') and #word >= 2 then
653649
local myHead = word:sub(1, 2)
654650
for uri in files.eachFile() do
655651
if #results >= 100 then
652+
results.incomplete = true
656653
break
657654
end
658655
if myUri == uri then
@@ -702,6 +699,7 @@ local function checkCommon(state, word, position, results)
702699
end
703700
for str, offset in state.lua:gmatch '([%a_][%w_]+)()' do
704701
if #results >= 100 then
702+
results.incomplete = true
705703
break
706704
end
707705
if #str >= 3
@@ -716,9 +714,6 @@ local function checkCommon(state, word, position, results)
716714
end
717715
end
718716
end
719-
if #results >= 100 then
720-
results.complete = false
721-
end
722717
end
723718

724719
local function checkKeyWord(state, start, position, word, hasSpace, afterLocal, results)
@@ -1973,7 +1968,7 @@ end
19731968

19741969
---@async
19751970
local function completion(uri, position, triggerCharacter)
1976-
local state = files.getState(uri)
1971+
local state = files.getLastState(uri) or files.getState(uri)
19771972
if not state then
19781973
return nil
19791974
end

script/files.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,17 @@ function m.setText(uri, text, isTrust, version)
194194
file.originText = text
195195
file.rows = nil
196196
file.words = nil
197-
m.astMap[uri] = nil
197+
m.astMap[uri] = nil
198198
file.cache = {}
199199
file.cacheActiveTime = math.huge
200200
m.globalVersion = m.globalVersion + 1
201201
await.close('files.version')
202202
m.onWatch('version')
203203
if create then
204204
m.onWatch('create', uri)
205+
m.onWatch('update', uri)
206+
else
207+
m.onWatch('update', uri)
205208
end
206209
if DEVELOP then
207210
if text ~= newText then
@@ -210,7 +213,6 @@ function m.setText(uri, text, isTrust, version)
210213
end
211214

212215
--if instance or TEST then
213-
m.onWatch('update', uri)
214216
--else
215217
-- await.call(function ()
216218
-- await.close('update:' .. uri)
@@ -519,12 +521,21 @@ function m.getState(uri)
519521
if not state then
520522
state = m.compileState(uri, file.text)
521523
m.astMap[uri] = state
524+
file.ast = state
522525
--await.delay()
523526
end
524527
file.cacheActiveTime = timer.clock()
525528
return state
526529
end
527530

531+
function m.getLastState(uri)
532+
local file = m.fileMap[uri]
533+
if not file then
534+
return nil
535+
end
536+
return file.ast
537+
end
538+
528539
---设置文件的当前可见范围
529540
---@param uri uri
530541
---@param ranges range[]

script/provider/diagnostic.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ function m.diagnosticsScope(uri, force)
352352
end
353353
await.close ('diagnosticsScope:' .. uri)
354354
await.call(function () ---@async
355-
await.sleep(delay)
355+
await.sleep(math.max(delay, 0.1))
356356
local clock = os.clock()
357357
local bar <close> = progress.create(ws.getScope(uri), lang.script.WORKSPACE_DIAGNOSTIC, 1)
358358
local cancelled

script/provider/provider.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ m.register 'textDocument/completion' {
584584
items[i] = item
585585
end
586586
return {
587-
isIncomplete = not result.complete,
587+
isIncomplete = result.incomplete,
588588
items = items,
589589
}
590590
end

script/pub/pub.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function m.popReport(brave, name, params)
101101
end
102102

103103
--- 发布任务
104-
---@parma name string
104+
---@param name string
105105
---@param params any
106106
---@return any
107107
---@async
@@ -122,7 +122,7 @@ end
122122

123123
--- 发布同步任务,如果任务进入了队列,会返回执行器
124124
--- 通过 jumpQueue 可以插队
125-
---@parma name string
125+
---@param name string
126126
---@param params any
127127
---@param callback function
128128
function m.task(name, params, callback)

script/service/service.lua

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,46 @@ function m.report()
141141
t:onTimer()
142142
end
143143

144-
function m.startTimer()
144+
function m.eventLoop()
145145
pub.task('timer', 1)
146-
while true do
147-
pub.step(not m.workingClock)
148-
if await.step() then
149-
m.sleeping = false
150-
if not m.workingClock then
151-
m.workingClock = time.monotonic()
152-
end
153-
else
154-
if m.workingClock then
155-
m.workingClock = nil
156-
m.idleClock = time.monotonic()
157-
m.reportStatus()
146+
147+
local function doSomething()
148+
pub.step(false)
149+
if not await.step() then
150+
return false
151+
end
152+
m.sleeping = false
153+
if not m.workingClock then
154+
m.workingClock = time.monotonic()
155+
end
156+
return true
157+
end
158+
159+
local function sleep()
160+
if m.workingClock then
161+
m.workingClock = nil
162+
m.idleClock = time.monotonic()
163+
m.reportStatus()
164+
end
165+
for _ = 1, 10 do
166+
thread.sleep(0.1)
167+
if doSomething() then
168+
return
158169
end
159170
end
171+
pub.step(true)
172+
end
173+
174+
while true do
175+
if doSomething() then
176+
goto CONTINUE
177+
end
160178
timer.update()
179+
if doSomething() then
180+
goto CONTINUE
181+
end
182+
sleep()
183+
::CONTINUE::
161184
end
162185
end
163186

@@ -234,7 +257,7 @@ function m.start()
234257

235258
require 'provider'
236259

237-
m.startTimer()
260+
m.eventLoop()
238261
end
239262

240263
return m

script/timer.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _ENV = nil
1111
local curFrame = 0
1212
local maxFrame = 0
1313
local curIndex = 0
14+
local tarFrame = 0
1415
local freeQueue = {}
1516
local timer = {}
1617

@@ -29,7 +30,7 @@ local function mTimeout(self, timeout)
2930
if self._pauseRemaining or self._running then
3031
return
3132
end
32-
local ti = curFrame + timeout
33+
local ti = tarFrame + timeout
3334
local q = timer[ti]
3435
if q == nil then
3536
q = allocQueue()
@@ -212,6 +213,7 @@ function m.update()
212213
curFrame = curFrame - 1
213214
end
214215
maxFrame = maxFrame + delta
216+
tarFrame = mathFloor(maxFrame)
215217
while curFrame < maxFrame do
216218
curFrame = curFrame + 1
217219
onTick()

0 commit comments

Comments
 (0)