|
| 1 | +local files = require 'files' |
| 2 | +local guide = require 'parser.guide' |
| 3 | +local await = require 'await' |
| 4 | +local conv = require 'proto.converter' |
| 5 | +local getRef = require 'core.reference' |
| 6 | + |
| 7 | +---@type codeLens |
| 8 | +local latestCodeLens |
| 9 | + |
| 10 | +---@class codeLens.resolving |
| 11 | +---@field mode 'reference' |
| 12 | +---@field source parser.object |
| 13 | + |
| 14 | +---@class codeLens.result |
| 15 | +---@field position integer |
| 16 | +---@field id integer |
| 17 | + |
| 18 | +---@class codeLens |
| 19 | +local mt = {} |
| 20 | +mt.__index = mt |
| 21 | +mt.type = 'codeLens' |
| 22 | +mt.id = 0 |
| 23 | + |
| 24 | +---@param uri uri |
| 25 | +---@return boolean |
| 26 | +function mt:init(uri) |
| 27 | + self.state = files.getState(uri) |
| 28 | + if not self.state then |
| 29 | + return false |
| 30 | + end |
| 31 | + ---@type uri |
| 32 | + self.uri = uri |
| 33 | + ---@type codeLens.result[] |
| 34 | + self.results = {} |
| 35 | + ---@type table<integer, codeLens.resolving> |
| 36 | + self.resolving = {} |
| 37 | + return true |
| 38 | +end |
| 39 | + |
| 40 | +---@param pos integer |
| 41 | +---@param resolving codeLens.resolving |
| 42 | +function mt:addResult(pos, resolving) |
| 43 | + self.id = self.id + 1 |
| 44 | + self.results[#self.results+1] = { |
| 45 | + position = pos, |
| 46 | + id = self.id, |
| 47 | + } |
| 48 | + self.resolving[self.id] = resolving |
| 49 | +end |
| 50 | + |
| 51 | +---@async |
| 52 | +---@param id integer |
| 53 | +---@return proto.command? |
| 54 | +function mt:resolve(id) |
| 55 | + local resolving = self.resolving[id] |
| 56 | + if not resolving then |
| 57 | + return nil |
| 58 | + end |
| 59 | + if resolving.mode == 'reference' then |
| 60 | + return self:resolveReference(resolving.source) |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +---@async |
| 65 | +function mt:collectReferences() |
| 66 | + await.delay() |
| 67 | + guide.eachSourceType(self.state.ast, 'function', function (src) |
| 68 | + local assign = src.parent |
| 69 | + if not guide.isSet(assign) then |
| 70 | + return |
| 71 | + end |
| 72 | + self:addResult(src.start, { |
| 73 | + mode = 'reference', |
| 74 | + source = assign, |
| 75 | + }) |
| 76 | + end) |
| 77 | +end |
| 78 | + |
| 79 | +---@async |
| 80 | +---@param source parser.object |
| 81 | +---@return proto.command? |
| 82 | +function mt:resolveReference(source) |
| 83 | + local refs = getRef(self.uri, source.start, false) |
| 84 | + local count = refs and #refs or 0 |
| 85 | + local command = conv.command( |
| 86 | + ('%d个引用'):format(count), |
| 87 | + 'editor.action.showReferences', |
| 88 | + { |
| 89 | + self.uri, |
| 90 | + conv.packPosition(self.state, source.start), |
| 91 | + } |
| 92 | + ) |
| 93 | + return command |
| 94 | +end |
| 95 | + |
| 96 | +---@async |
| 97 | +---@param uri uri |
| 98 | +---@return codeLens.result[]? |
| 99 | +local function codeLens(uri) |
| 100 | + latestCodeLens = setmetatable({}, mt) |
| 101 | + local suc = latestCodeLens:init(uri) |
| 102 | + if not suc then |
| 103 | + return nil |
| 104 | + end |
| 105 | + |
| 106 | + latestCodeLens:collectReferences() |
| 107 | + |
| 108 | + if #latestCodeLens.results == 0 then |
| 109 | + return nil |
| 110 | + end |
| 111 | + |
| 112 | + return latestCodeLens.results |
| 113 | +end |
| 114 | + |
| 115 | +---@async |
| 116 | +---@param id integer |
| 117 | +---@return proto.command? |
| 118 | +local function resolve(id) |
| 119 | + if not latestCodeLens then |
| 120 | + return nil |
| 121 | + end |
| 122 | + local command = latestCodeLens:resolve(id) |
| 123 | + return command |
| 124 | +end |
| 125 | + |
| 126 | +return { |
| 127 | + codeLens = codeLens, |
| 128 | + resolve = resolve, |
| 129 | +} |
0 commit comments