|
| 1 | +import { LSPPlugin } from "@codemirror/lsp-client"; |
| 2 | +import type { Command, EditorView } from "@codemirror/view"; |
| 3 | +import { showReferencesPanel } from "components/referencesPanel"; |
| 4 | +import { navigateToReference } from "components/referencesPanel/utils"; |
| 5 | +import toast from "components/toast"; |
| 6 | +import type { ServerCapabilities } from "vscode-languageserver-protocol"; |
| 7 | +import { normalizeLocations, type LspLocationResult } from "./locationUtils"; |
| 8 | +import { addLspLogFor } from "./logs"; |
| 9 | +import { fetchLineText, getWordAtCursor } from "./references"; |
| 10 | + |
| 11 | +type DefinitionKind = |
| 12 | + | "definition" |
| 13 | + | "declaration" |
| 14 | + | "implementation" |
| 15 | + | "typeDefinition"; |
| 16 | + |
| 17 | +const CAPABILITY: Record<DefinitionKind, keyof ServerCapabilities> = { |
| 18 | + definition: "definitionProvider", |
| 19 | + declaration: "declarationProvider", |
| 20 | + implementation: "implementationProvider", |
| 21 | + typeDefinition: "typeDefinitionProvider", |
| 22 | +}; |
| 23 | + |
| 24 | +const LABEL: Record<DefinitionKind, string> = { |
| 25 | + definition: "definition", |
| 26 | + declaration: "declaration", |
| 27 | + implementation: "implementation", |
| 28 | + typeDefinition: "type definition", |
| 29 | +}; |
| 30 | + |
| 31 | +function locationKey(location: ReturnType<typeof normalizeLocations>[number]) { |
| 32 | + const { start, end } = location.range; |
| 33 | + return `${location.uri}:${start.line}:${start.character}:${end.line}:${end.character}`; |
| 34 | +} |
| 35 | + |
| 36 | +async function fetchLocations( |
| 37 | + view: EditorView, |
| 38 | + kind: DefinitionKind, |
| 39 | +): Promise<ReturnType<typeof normalizeLocations> | null> { |
| 40 | + const plugins = LSPPlugin.getAll(view, kind).filter( |
| 41 | + (plugin) => !!plugin.client.serverCapabilities?.[CAPABILITY[kind]], |
| 42 | + ); |
| 43 | + if (!plugins.length) { |
| 44 | + toast(`Language server does not support go to ${LABEL[kind]}`); |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + const position = view.state.selection.main.head; |
| 49 | + const settled = await Promise.allSettled( |
| 50 | + plugins.map(async (plugin) => { |
| 51 | + plugin.client.sync(); |
| 52 | + return plugin.client.request< |
| 53 | + { |
| 54 | + textDocument: { uri: string }; |
| 55 | + position: { line: number; character: number }; |
| 56 | + }, |
| 57 | + LspLocationResult |
| 58 | + >(`textDocument/${kind}`, { |
| 59 | + textDocument: { uri: plugin.uri }, |
| 60 | + position: plugin.toPosition(position), |
| 61 | + }); |
| 62 | + }), |
| 63 | + ); |
| 64 | + |
| 65 | + const locations: ReturnType<typeof normalizeLocations> = []; |
| 66 | + const seen = new Set<string>(); |
| 67 | + for (let index = 0; index < settled.length; index++) { |
| 68 | + const result = settled[index]; |
| 69 | + if (result.status === "rejected") { |
| 70 | + addLspLogFor( |
| 71 | + plugins[index], |
| 72 | + "warn", |
| 73 | + `Go to ${LABEL[kind]} failed`, |
| 74 | + result.reason, |
| 75 | + ); |
| 76 | + continue; |
| 77 | + } |
| 78 | + for (const location of normalizeLocations(result.value)) { |
| 79 | + const key = locationKey(location); |
| 80 | + if (seen.has(key)) continue; |
| 81 | + seen.add(key); |
| 82 | + locations.push(location); |
| 83 | + } |
| 84 | + } |
| 85 | + return locations; |
| 86 | +} |
| 87 | + |
| 88 | +async function goTo(view: EditorView, kind: DefinitionKind): Promise<boolean> { |
| 89 | + try { |
| 90 | + const locations = await fetchLocations(view, kind); |
| 91 | + if (locations === null) return false; |
| 92 | + if (!locations.length) { |
| 93 | + toast(`No ${LABEL[kind]} found`); |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + if (locations.length === 1) { |
| 98 | + await navigateToReference(locations[0]); |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + const symbolName = getWordAtCursor(view); |
| 103 | + const panel = showReferencesPanel({ symbolName }); |
| 104 | + panel.setReferences( |
| 105 | + await Promise.all( |
| 106 | + locations.map(async (location) => ({ |
| 107 | + ...location, |
| 108 | + lineText: await fetchLineText( |
| 109 | + location.uri, |
| 110 | + location.range.start.line, |
| 111 | + ), |
| 112 | + })), |
| 113 | + ), |
| 114 | + ); |
| 115 | + return true; |
| 116 | + } catch (error) { |
| 117 | + console.error(`[LSP:Definition] Go to ${LABEL[kind]} failed:`, error); |
| 118 | + return false; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export const jumpToDefinition: Command = (view) => { |
| 123 | + void goTo(view, "definition"); |
| 124 | + return true; |
| 125 | +}; |
| 126 | + |
| 127 | +export const jumpToDeclaration: Command = (view) => { |
| 128 | + void goTo(view, "declaration"); |
| 129 | + return true; |
| 130 | +}; |
| 131 | + |
| 132 | +export const jumpToImplementation: Command = (view) => { |
| 133 | + void goTo(view, "implementation"); |
| 134 | + return true; |
| 135 | +}; |
| 136 | + |
| 137 | +export const jumpToTypeDefinition: Command = (view) => { |
| 138 | + void goTo(view, "typeDefinition"); |
| 139 | + return true; |
| 140 | +}; |
| 141 | + |
| 142 | +export const goToDefinition = (view: EditorView) => goTo(view, "definition"); |
| 143 | +export const goToDeclaration = (view: EditorView) => goTo(view, "declaration"); |
| 144 | +export const goToImplementation = (view: EditorView) => |
| 145 | + goTo(view, "implementation"); |
| 146 | +export const goToTypeDefinition = (view: EditorView) => |
| 147 | + goTo(view, "typeDefinition"); |
0 commit comments