|
| 1 | +import type { DecorationOptions } from 'vscode' |
| 2 | +import { getResolvedDependencies } from '#core/workspace' |
| 3 | +import { logger } from '#state' |
| 4 | +import { offsetRangeToRange } from '#utils/ast' |
| 5 | +import { isPackageManifestPath } from '#utils/file' |
| 6 | +import { useActiveTextEditor, useEditorDecorations, watch } from 'reactive-vscode' |
| 7 | +import { Range } from 'vscode' |
| 8 | + |
| 9 | +export function useDecorators() { |
| 10 | + const activeEditor = useActiveTextEditor() |
| 11 | + |
| 12 | + const { update } = useEditorDecorations( |
| 13 | + activeEditor, |
| 14 | + { |
| 15 | + after: { color: 'rgba(136, 136, 136, 0.63)' }, |
| 16 | + }, |
| 17 | + async (editor) => { |
| 18 | + const document = editor.document |
| 19 | + if (!isPackageManifestPath(document.uri.path)) |
| 20 | + return [] |
| 21 | + logger.info(`[decorators] updating ${document.uri.path}`) |
| 22 | + |
| 23 | + const dependencies = await getResolvedDependencies(document.uri) |
| 24 | + if (!dependencies) |
| 25 | + return [] |
| 26 | + |
| 27 | + const result: DecorationOptions[] = [] |
| 28 | + |
| 29 | + for (const dep of dependencies) { |
| 30 | + if (dep.protocol !== 'catalog') |
| 31 | + continue |
| 32 | + |
| 33 | + const range = offsetRangeToRange(document, dep.specRange) |
| 34 | + const line = range.end.line |
| 35 | + const len = document.lineAt(line).text.length |
| 36 | + result.push({ |
| 37 | + range: new Range(line, 0, line, len), |
| 38 | + renderOptions: { |
| 39 | + after: { |
| 40 | + contentText: `\t\t# ${dep.resolvedSpec}`, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + return result |
| 47 | + }, |
| 48 | + ) |
| 49 | + |
| 50 | + watch(activeEditor, update) |
| 51 | +} |
0 commit comments