|
| 1 | +import type { CompletionItemKind, CompletionList, LanguageServicePlugin, LanguageServicePluginInstance, LocationLink } from '@volar/language-service' |
| 2 | +import type { DependencyInfo } from 'npmx-language-core/workspace' |
| 3 | +import type { IWorkspaceState } from '../types' |
| 4 | +import { isPackageManifest, normalizeCatalogName } from 'npmx-language-core/utils' |
| 5 | +import { URI } from 'vscode-uri' |
| 6 | +import { getResolvedDependencyAtOffset } from '../utils/range' |
| 7 | + |
| 8 | +export function create(workspaceState: IWorkspaceState): LanguageServicePlugin { |
| 9 | + function getDependencyFileUri(documentUri: string): URI | undefined { |
| 10 | + const uri = URI.parse(documentUri) |
| 11 | + if (uri.scheme !== 'file' || !isPackageManifest(uri.path)) |
| 12 | + return |
| 13 | + |
| 14 | + return uri |
| 15 | + } |
| 16 | + |
| 17 | + async function getCatalogDependency(documentUri: string, offset: number): Promise<DependencyInfo | undefined> { |
| 18 | + const dependencies = await workspaceState.getResolvedDependencies(documentUri) |
| 19 | + if (!dependencies) |
| 20 | + return |
| 21 | + |
| 22 | + const dependency = getResolvedDependencyAtOffset(dependencies, offset) |
| 23 | + if (!dependency?.rawSpec.startsWith('catalog:')) |
| 24 | + return |
| 25 | + |
| 26 | + return dependency |
| 27 | + } |
| 28 | + |
| 29 | + function matchesCatalogDependency(candidate: DependencyInfo, dependency: DependencyInfo): boolean { |
| 30 | + return candidate.rawName === dependency.resolvedName |
| 31 | + && candidate.categoryName != null |
| 32 | + && dependency.categoryName != null |
| 33 | + && normalizeCatalogName(candidate.categoryName) === normalizeCatalogName(dependency.categoryName) |
| 34 | + } |
| 35 | + |
| 36 | + return { |
| 37 | + name: 'npmx-catalog', |
| 38 | + capabilities: { |
| 39 | + completionProvider: { |
| 40 | + triggerCharacters: [':'], |
| 41 | + }, |
| 42 | + definitionProvider: true, |
| 43 | + }, |
| 44 | + create(context): LanguageServicePluginInstance { |
| 45 | + return { |
| 46 | + async provideCompletionItems(document, position): Promise<CompletionList | undefined> { |
| 47 | + const dependencyFileUri = getDependencyFileUri(document.uri) |
| 48 | + if (!dependencyFileUri) |
| 49 | + return |
| 50 | + |
| 51 | + const offset = document.offsetAt(position) |
| 52 | + const dependency = await getCatalogDependency(document.uri, offset) |
| 53 | + if (!dependency) |
| 54 | + return |
| 55 | + |
| 56 | + const workspaceContext = await workspaceState.getWorkspaceContext(document.uri) |
| 57 | + if (!workspaceContext) |
| 58 | + return |
| 59 | + |
| 60 | + const catalogs = await workspaceContext.getCatalogs() |
| 61 | + if (!catalogs) |
| 62 | + return |
| 63 | + |
| 64 | + const items: CompletionList['items'] = [] |
| 65 | + |
| 66 | + for (const [name, catalog] of Object.entries(catalogs)) { |
| 67 | + const version = catalog[dependency.resolvedName] |
| 68 | + if (!version) |
| 69 | + continue |
| 70 | + |
| 71 | + items.push({ |
| 72 | + label: name, |
| 73 | + kind: 12 satisfies typeof CompletionItemKind.Value, |
| 74 | + detail: version, |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + return { isIncomplete: false, items } |
| 79 | + }, |
| 80 | + |
| 81 | + async provideDefinition(document, position): Promise<LocationLink[] | undefined> { |
| 82 | + const dependencyFileUri = getDependencyFileUri(document.uri) |
| 83 | + if (!dependencyFileUri) |
| 84 | + return |
| 85 | + |
| 86 | + const offset = document.offsetAt(position) |
| 87 | + const dependency = await getCatalogDependency(document.uri, offset) |
| 88 | + if (!dependency) |
| 89 | + return |
| 90 | + |
| 91 | + const workspaceContext = await workspaceState.getWorkspaceContext(document.uri) |
| 92 | + if (!workspaceContext?.workspaceFilePath) |
| 93 | + return |
| 94 | + |
| 95 | + const workspaceFileInfo = await workspaceContext.loadWorkspaceFileInfo(workspaceContext.workspaceFilePath) |
| 96 | + if (!workspaceFileInfo) |
| 97 | + return |
| 98 | + |
| 99 | + const targetDependency = workspaceFileInfo.dependencies.find((candidate) => |
| 100 | + matchesCatalogDependency(candidate, dependency), |
| 101 | + ) |
| 102 | + if (!targetDependency) |
| 103 | + return |
| 104 | + |
| 105 | + const workspaceFileUri = dependencyFileUri.with({ path: workspaceContext.workspaceFilePath }) |
| 106 | + const sourceScript = context.language.scripts.get(workspaceFileUri) |
| 107 | + if (!sourceScript) |
| 108 | + return |
| 109 | + |
| 110 | + const workspaceDocument = context.documents.get(sourceScript.id, sourceScript.languageId, sourceScript.snapshot) |
| 111 | + |
| 112 | + const [targetStart, targetEnd] = targetDependency.specRange |
| 113 | + const originStart = document.positionAt(dependency.specRange[0]) |
| 114 | + const originEnd = document.positionAt(dependency.specRange[1]) |
| 115 | + |
| 116 | + return [{ |
| 117 | + targetUri: workspaceFileUri.toString(), |
| 118 | + targetRange: { |
| 119 | + start: workspaceDocument.positionAt(targetStart), |
| 120 | + end: workspaceDocument.positionAt(targetEnd), |
| 121 | + }, |
| 122 | + targetSelectionRange: { |
| 123 | + start: workspaceDocument.positionAt(targetStart), |
| 124 | + end: workspaceDocument.positionAt(targetEnd), |
| 125 | + }, |
| 126 | + originSelectionRange: { |
| 127 | + start: originStart, |
| 128 | + end: originEnd, |
| 129 | + }, |
| 130 | + }] |
| 131 | + }, |
| 132 | + } |
| 133 | + }, |
| 134 | + } |
| 135 | +} |
0 commit comments