diff options
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | script/core/hint.lua (renamed from script/core/type-hint.lua) | 8 | ||||
-rw-r--r-- | script/files.lua | 34 | ||||
-rw-r--r-- | script/provider/provider.lua | 67 |
4 files changed, 77 insertions, 34 deletions
diff --git a/changelog.md b/changelog.md index d3953843..6915291d 100644 --- a/changelog.md +++ b/changelog.md @@ -1,7 +1,7 @@ # changelog ## 1.14.0 -* `NEW` type hint: disabled by default +* `NEW` hint: disabled by default * `NEW` flush cache after 5 min ## 1.13.1 diff --git a/script/core/type-hint.lua b/script/core/hint.lua index 9e717f14..90182c39 100644 --- a/script/core/type-hint.lua +++ b/script/core/hint.lua @@ -2,7 +2,7 @@ local files = require 'files' local guide = require 'parser.guide' local vm = require 'vm' -return function (uri, start, finish) +local function typeHint(uri, start, finish) local ast = files.getAst(uri) if not ast then return nil @@ -33,9 +33,13 @@ return function (uri, start, finish) end edits[#edits+1] = { newText = (':%s'):format(infer), - start = src.finish + 1, + start = src.finish, finish = src.finish, } end) return edits end + +return { + typeHint = typeHint, +} diff --git a/script/files.lua b/script/files.lua index b3447cc4..17b0e290 100644 --- a/script/files.lua +++ b/script/files.lua @@ -21,6 +21,7 @@ m.fileMap = {} m.dllMap = {} m.watchList = {} m.notifyCache = {} +m.visible = {} m.assocVersion = -1 m.assocMatcher = nil m.globalVersion = 0 @@ -379,6 +380,38 @@ function m.getAst(uri) return ast end +---设置文件的当前可见范围 +---@param uri uri +---@param ranges range[] +function m.setVisibles(uri, ranges) + uri = m.getUri(uri) + m.visible[uri] = ranges +end + +---获取文件的当前可见范围 +---@param uri uri +---@return table[] +function m.getVisibles(uri) + uri = m.getUri(uri) + local file = m.fileMap[uri] + if not file then + return nil + end + local ranges = m.visible[uri] + if not ranges or #ranges == 0 then + return nil + end + local visibles = {} + for i, range in ipairs(ranges) do + local start, finish = m.unrange(uri, range) + visibles[i] = { + start = start, + finish = finish, + } + end + return visibles +end + --- 获取文件行信息 ---@param uri uri ---@return table lines @@ -396,7 +429,6 @@ function m.getLines(uri) return lines end - function m.getOriginLines(uri) uri = getUriKey(uri) local file = m.fileMap[uri] diff --git a/script/provider/provider.lua b/script/provider/provider.lua index be9b6fc9..b478e6a7 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -95,6 +95,41 @@ local function updateConfig() end end +local function updateTypeHint(uri, visibles, edits) + if not config.config.typeHint.enable then + return + end + local hint = require 'core.hint' + local _ <close> = progress.create(lang.script.WINDOW_PROCESSING_TYPE_HINT, 0.5) + for _, visible in ipairs(visibles) do + local piece = hint.typeHint(uri, visible.start, visible.finish) + if piece then + for _, edit in ipairs(piece) do + edits[#edits+1] = { + newText = edit.newText, + range = files.range(uri, edit.start, edit.finish) + } + end + end + end +end + +local function updateHint(uri) + local visibles = files.getVisibles(uri) + if not visibles then + return + end + local edits = {} + updateTypeHint(uri, visibles, edits) + if #edits == 0 then + return + end + proto.notify('$/hint', { + uri = uri, + edits = edits, + }) +end + proto.on('initialize', function (params) client.init(params) library.init() @@ -728,34 +763,6 @@ proto.on('window/workDoneProgress/cancel', function (params) end) proto.on('$/didChangeVisibleRanges', function (params) - local typeHint = require 'core.type-hint' - local edits = {} - local uri = params.uri - while not files.exists(uri) do - await.sleep(0.1) - end - - -- compute type-hint - if config.config.typeHint.enable then - local _ <close> = progress.create(lang.script.WINDOW_PROCESSING_TYPE_HINT, 0.5) - for _, range in ipairs(params.ranges) do - local start, finish = files.unrange(uri, range) - local piece = typeHint(uri, start, finish) - if piece then - for _, edit in ipairs(piece) do - edits[#edits+1] = { - newText = edit.newText, - range = files.range(uri, edit.start, edit.finish) - } - end - end - end - if #edits == 0 then - return - end - proto.notify('$/typeHint', { - uri = uri, - edits = edits, - }) - end + files.setVisibles(params.uri, params.ranges) + updateHint(params.uri) end) |