|
| 1 | +import type { DependencyInfo, Extractor } from '#types/extractor' |
| 2 | +import type { CodeLensProvider, TextDocument } from 'vscode' |
| 3 | +import { internalCommands } from '#state' |
| 4 | +import { getPackageInfo } from '#utils/api/package' |
| 5 | +import { resolveExactVersion } from '#utils/package' |
| 6 | +import { resolveUpgradeTargetVersion } from '#utils/upgrade' |
| 7 | +import { formatUpgradeVersion, isSupportedProtocol, parseVersion } from '#utils/version' |
| 8 | +import { debounce } from 'perfect-debounce' |
| 9 | +import diff from 'semver/functions/diff' |
| 10 | +import { CodeLens, EventEmitter } from 'vscode' |
| 11 | + |
| 12 | +const dataMap = new WeakMap<CodeLens, DependencyInfo>() |
| 13 | + |
| 14 | +export class VersionCodeLensProvider<T extends Extractor> implements CodeLensProvider { |
| 15 | + extractor: T |
| 16 | + private readonly onDidChangeCodeLensesEmitter = new EventEmitter<void>() |
| 17 | + readonly onDidChangeCodeLenses = this.onDidChangeCodeLensesEmitter.event |
| 18 | + private readonly scheduleRefresh = debounce(() => { |
| 19 | + this.onDidChangeCodeLensesEmitter.fire() |
| 20 | + }, 100, { leading: false, trailing: true }) |
| 21 | + |
| 22 | + constructor(extractor: T) { |
| 23 | + this.extractor = extractor |
| 24 | + } |
| 25 | + |
| 26 | + provideCodeLenses(document: TextDocument): CodeLens[] { |
| 27 | + const root = this.extractor.parse(document) |
| 28 | + if (!root) |
| 29 | + return [] |
| 30 | + |
| 31 | + const deps = this.extractor.getDependenciesInfo(root) |
| 32 | + const lenses: CodeLens[] = [] |
| 33 | + |
| 34 | + for (const dep of deps) { |
| 35 | + const versionRange = this.extractor.getNodeRange(document, dep.versionNode) |
| 36 | + const lens = new CodeLens(versionRange) |
| 37 | + dataMap.set(lens, dep) |
| 38 | + lenses.push(lens) |
| 39 | + } |
| 40 | + |
| 41 | + return lenses |
| 42 | + } |
| 43 | + |
| 44 | + resolveCodeLens(lens: CodeLens) { |
| 45 | + const dep = dataMap.get(lens) |
| 46 | + if (!dep) |
| 47 | + return lens |
| 48 | + |
| 49 | + const parsed = parseVersion(dep.version) |
| 50 | + if (!parsed || !isSupportedProtocol(parsed.protocol)) { |
| 51 | + lens.command = { title: '$(question) unknown', command: '' } |
| 52 | + return lens |
| 53 | + } |
| 54 | + |
| 55 | + const pkg = getPackageInfo(dep.name) |
| 56 | + if (pkg instanceof Promise) { |
| 57 | + lens.command = { title: '$(sync~spin) checking...', command: '' } |
| 58 | + pkg.finally(() => this.scheduleRefresh()) |
| 59 | + return lens |
| 60 | + } |
| 61 | + |
| 62 | + if (!pkg) { |
| 63 | + lens.command = { title: '$(question) unknown', command: '' } |
| 64 | + return lens |
| 65 | + } |
| 66 | + |
| 67 | + const exactVersion = resolveExactVersion(pkg, parsed.version) |
| 68 | + if (!exactVersion) { |
| 69 | + lens.command = { title: '$(question) unknown', command: '' } |
| 70 | + return lens |
| 71 | + } |
| 72 | + |
| 73 | + const targetVersion = resolveUpgradeTargetVersion(pkg, exactVersion) |
| 74 | + if (!targetVersion) { |
| 75 | + lens.command = { title: '$(check) latest', command: '' } |
| 76 | + return lens |
| 77 | + } |
| 78 | + |
| 79 | + const newVersion = formatUpgradeVersion(parsed, targetVersion) |
| 80 | + const updateType = diff(exactVersion, targetVersion) |
| 81 | + lens.command = { |
| 82 | + title: updateType |
| 83 | + ? `$(arrow-up) ${newVersion} (${updateType})` |
| 84 | + : `$(arrow-up) ${newVersion}`, |
| 85 | + command: internalCommands.replaceText, |
| 86 | + arguments: [lens.range, newVersion], |
| 87 | + } |
| 88 | + |
| 89 | + return lens |
| 90 | + } |
| 91 | +} |
0 commit comments