|
| 1 | +import type * as atomIde from "atom-ide-base" |
| 2 | +import Convert from "../convert" |
| 3 | +import * as Utils from "../utils" |
| 4 | +import { SymbolTag } from "../languageclient" |
| 5 | +import type { LanguageClientConnection, ServerCapabilities, CallHierarchyItem } from "../languageclient" |
| 6 | +import type { CancellationTokenSource } from "vscode-jsonrpc" |
| 7 | +import type { Point, TextEditor } from "atom" |
| 8 | + |
| 9 | +import OutlineViewAdapter from "./outline-view-adapter" |
| 10 | + |
| 11 | +/** Public: Adapts the documentSymbolProvider of the language server to the Outline View supplied by Atom IDE UI. */ |
| 12 | +export default class CallHierarchyAdapter { |
| 13 | + private _cancellationTokens: WeakMap<LanguageClientConnection, CancellationTokenSource> = new WeakMap() |
| 14 | + |
| 15 | + /** |
| 16 | + * Public: Determine whether this adapter can be used to adapt a language server based on the serverCapabilities |
| 17 | + * matrix containing a callHierarchyProvider. |
| 18 | + * |
| 19 | + * @param serverCapabilities The {ServerCapabilities} of the language server to consider. |
| 20 | + * @returns A {Boolean} indicating adapter can adapt the server based on the given serverCapabilities. |
| 21 | + */ |
| 22 | + public static canAdapt(serverCapabilities: ServerCapabilities): boolean { |
| 23 | + return !!serverCapabilities.callHierarchyProvider |
| 24 | + } |
| 25 | + |
| 26 | + /** Corresponds to lsp's CallHierarchyPrepareRequest */ |
| 27 | + |
| 28 | + /** |
| 29 | + * Public: Obtain the relationship between calling and called functions hierarchically. Corresponds to lsp's |
| 30 | + * CallHierarchyPrepareRequest. |
| 31 | + * |
| 32 | + * @param connection A {LanguageClientConnection} to the language server that provides highlights. |
| 33 | + * @param editor The Atom {TextEditor} containing the text associated with the calling. |
| 34 | + * @param position The Atom {Point} associated with the calling. |
| 35 | + * @param type The hierarchy type either incoming or outgoing. |
| 36 | + * @returns A {Promise} of an {CallHierarchy}. |
| 37 | + */ |
| 38 | + async getCallHierarchy<T extends atomIde.CallHierarchyType>( |
| 39 | + connection: LanguageClientConnection, |
| 40 | + editor: TextEditor, |
| 41 | + point: Point, |
| 42 | + type: T |
| 43 | + ): Promise<atomIde.CallHierarchy<T>> { |
| 44 | + const results = await Utils.doWithCancellationToken(connection, this._cancellationTokens, (cancellationToken) => |
| 45 | + connection.prepareCallHierarchy( |
| 46 | + { |
| 47 | + textDocument: Convert.editorToTextDocumentIdentifier(editor), |
| 48 | + position: Convert.pointToPosition(point), |
| 49 | + }, |
| 50 | + cancellationToken |
| 51 | + ) |
| 52 | + ) |
| 53 | + return <CallHierarchyForAdapter<T>>{ |
| 54 | + type, |
| 55 | + data: results?.map(convertCallHierarchyItem) ?? [], |
| 56 | + itemAt(n: number) { |
| 57 | + if (type === "incoming") { |
| 58 | + return <Promise<atomIde.CallHierarchy<T>>>this.adapter.getIncoming(this.connection, this.data[n].rawData) |
| 59 | + } else { |
| 60 | + return <Promise<atomIde.CallHierarchy<T>>>this.adapter.getOutgoing(this.connection, this.data[n].rawData) |
| 61 | + } |
| 62 | + }, |
| 63 | + connection, |
| 64 | + adapter: this, |
| 65 | + } |
| 66 | + } |
| 67 | + /** Corresponds to lsp's CallHierarchyIncomingCallsRequest. */ |
| 68 | + async getIncoming( |
| 69 | + connection: LanguageClientConnection, |
| 70 | + item: CallHierarchyItem |
| 71 | + ): Promise<atomIde.CallHierarchy<"incoming">> { |
| 72 | + const results = await Utils.doWithCancellationToken(connection, this._cancellationTokens, (_cancellationToken) => |
| 73 | + connection.callHierarchyIncomingCalls({ item }) |
| 74 | + ) |
| 75 | + return <CallHierarchyForAdapter<"incoming">>{ |
| 76 | + type: "incoming", |
| 77 | + data: results?.map?.((l) => convertCallHierarchyItem(l.from)) || [], |
| 78 | + itemAt(n: number) { |
| 79 | + return this.adapter.getIncoming(this.connection, this.data[n].rawData) |
| 80 | + }, |
| 81 | + connection, |
| 82 | + adapter: this, |
| 83 | + } |
| 84 | + } |
| 85 | + /** Corresponds to lsp's CallHierarchyOutgoingCallsRequest. */ |
| 86 | + async getOutgoing( |
| 87 | + connection: LanguageClientConnection, |
| 88 | + item: CallHierarchyItem |
| 89 | + ): Promise<atomIde.CallHierarchy<"outgoing">> { |
| 90 | + const results = await Utils.doWithCancellationToken(connection, this._cancellationTokens, (_cancellationToken) => |
| 91 | + connection.callHierarchyOutgoingCalls({ item }) |
| 92 | + ) |
| 93 | + return <CallHierarchyForAdapter<"outgoing">>{ |
| 94 | + type: "outgoing", |
| 95 | + data: results?.map((l) => convertCallHierarchyItem(l.to)) || [], |
| 96 | + itemAt(n: number) { |
| 97 | + return this.adapter.getOutgoing(this.connection, this.data[n].rawData) |
| 98 | + }, |
| 99 | + connection, |
| 100 | + adapter: this, |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +function convertCallHierarchyItem(rawData: CallHierarchyItem): CallHierarchyItemForAdapter { |
| 106 | + return { |
| 107 | + path: Convert.uriToPath(rawData.uri), |
| 108 | + name: rawData.name, |
| 109 | + icon: OutlineViewAdapter.symbolKindToEntityKind(rawData.kind) ?? undefined, |
| 110 | + tags: rawData.tags |
| 111 | + ? [ |
| 112 | + ...rawData.tags.reduce((set, tag) => { |
| 113 | + // filter out null and remove duplicates |
| 114 | + const entity = symbolTagToEntityKind(tag) |
| 115 | + return entity == null ? set : set.add(entity) |
| 116 | + }, new Set<atomIde.SymbolTagKind>()), |
| 117 | + ] |
| 118 | + : [], |
| 119 | + detail: rawData.detail, |
| 120 | + range: Convert.lsRangeToAtomRange(rawData.range), |
| 121 | + selectionRange: Convert.lsRangeToAtomRange(rawData.selectionRange), |
| 122 | + rawData, |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function symbolTagToEntityKind(symbol: number): atomIde.SymbolTagKind | null { |
| 127 | + switch (symbol) { |
| 128 | + case SymbolTag.Deprecated: |
| 129 | + return "deprecated" |
| 130 | + default: |
| 131 | + return null |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/** Extend CallHierarchy to include properties used inside the adapter */ |
| 136 | +interface CallHierarchyForAdapter<T extends atomIde.CallHierarchyType> extends atomIde.CallHierarchy<T> { |
| 137 | + data: CallHierarchyItemForAdapter[] |
| 138 | + adapter: CallHierarchyAdapter |
| 139 | + connection: LanguageClientConnection |
| 140 | +} |
| 141 | + |
| 142 | +/** Extend CallHierarchyItem to include properties used inside the adapter */ |
| 143 | +interface CallHierarchyItemForAdapter extends atomIde.CallHierarchyItem { |
| 144 | + rawData: CallHierarchyItem |
| 145 | +} |
0 commit comments