-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement support for spec version of Call Hierarchy (#649)
- Loading branch information
Showing
12 changed files
with
305 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright (C) 2022 TypeFox and others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
import * as chai from 'chai'; | ||
import * as lsp from 'vscode-languageserver'; | ||
import { uri, createServer, TestLspServer, positionAfter, documentFromFile } from './../test-utils.js'; | ||
|
||
const assert = chai.assert; | ||
|
||
const diagnostics: Map<string, lsp.PublishDiagnosticsParams> = new Map(); | ||
let server: TestLspServer; | ||
|
||
interface CallHierarchyItemWithChildren extends lsp.CallHierarchyItem { | ||
children: CallHierarchyItemWithChildren[]; | ||
} | ||
|
||
async function getIncomingCalls(server: TestLspServer, item: lsp.CallHierarchyItem): Promise<CallHierarchyItemWithChildren> { | ||
const incomingCalls = await server.callHierarchyIncomingCalls({ item }); | ||
const children = await Promise.all((incomingCalls || []).map(incomingCall => getIncomingCalls(server, incomingCall.from))); | ||
return { | ||
...item, | ||
children, | ||
}; | ||
} | ||
|
||
async function getOutgoingCalls(server: TestLspServer, item: lsp.CallHierarchyItem): Promise<CallHierarchyItemWithChildren> { | ||
const outgoingCalls = await server.callHierarchyOutgoingCalls({ item }); | ||
const children = await Promise.all((outgoingCalls || []).map(outgoingCall => getOutgoingCalls(server, outgoingCall.to))); | ||
return { | ||
...item, | ||
children, | ||
}; | ||
} | ||
|
||
function itemToString(item: lsp.CallHierarchyItem | null, indentLevel: number): string { | ||
if (!item) { | ||
return '<not found>'; | ||
} | ||
return `${new Array(indentLevel * 2 + 1).join(' ')}-|> ${item.name} (symbol: ${item.uri.split('/').pop()}#${item.selectionRange.start.line})`; | ||
} | ||
|
||
function callsToString(item: CallHierarchyItemWithChildren, indentLevel: number, lines: string[]): void { | ||
for (const child of item.children) { | ||
lines.push(itemToString(child, indentLevel + 1)); | ||
callsToString(child, indentLevel + 1, lines); | ||
} | ||
} | ||
|
||
before(async () => { | ||
server = await createServer({ | ||
rootUri: uri(), | ||
publishDiagnostics: args => diagnostics.set(args.uri, args), | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
server.closeAll(); | ||
// "closeAll" triggers final publishDiagnostics with an empty list so clear last. | ||
diagnostics.clear(); | ||
}); | ||
|
||
after(() => { | ||
server.closeAll(); | ||
server.shutdown(); | ||
}); | ||
|
||
describe.only('call hierarchy', () => { | ||
const oneDoc = documentFromFile({ path: 'call-hierarchy/one.ts' }); | ||
const twoDoc = documentFromFile({ path: 'call-hierarchy/two.ts' }); | ||
const threeDoc = documentFromFile({ path: 'call-hierarchy/three.ts' }); | ||
|
||
function openDocuments() { | ||
for (const textDocument of [oneDoc, twoDoc, threeDoc]) { | ||
server.didOpenTextDocument({ textDocument }); | ||
} | ||
} | ||
|
||
it('incoming calls', async () => { | ||
openDocuments(); | ||
const items = await server.prepareCallHierarchy({ | ||
textDocument: twoDoc, | ||
position: positionAfter(twoDoc, 'new Three().tada'), | ||
}); | ||
assert.isNotNull(items); | ||
assert.lengthOf(items!, 1); | ||
const lines: string[] = []; | ||
for (const item of items!) { | ||
lines.push(itemToString(item, 0)); | ||
const incomingCalls = await getIncomingCalls(server, item); | ||
callsToString(incomingCalls, 0, lines); | ||
} | ||
assert.equal(lines.join('\n'), | ||
` | ||
-|> tada (symbol: three.ts#2) | ||
-|> callThreeTwice (symbol: two.ts#3) | ||
-|> main (symbol: one.ts#2) | ||
`.trim(), | ||
); | ||
}); | ||
|
||
it('outgoing calls', async () => { | ||
openDocuments(); | ||
const items = await server.prepareCallHierarchy({ | ||
textDocument: oneDoc, | ||
position: positionAfter(oneDoc, 'new Two().callThreeTwice'), | ||
}); | ||
assert.isNotNull(items); | ||
assert.lengthOf(items!, 1); | ||
const lines: string[] = []; | ||
for (const item of items!) { | ||
lines.push(itemToString(item, 0)); | ||
const outgoingCalls = await getOutgoingCalls(server, item); | ||
callsToString(outgoingCalls, 0, lines); | ||
} | ||
assert.equal(lines.join('\n'), | ||
` | ||
-|> callThreeTwice (symbol: two.ts#3) | ||
-|> tada (symbol: three.ts#2) | ||
-|> print (symbol: three.ts#6) | ||
-|> log (symbol: console.d.ts#220) | ||
-|> Three (symbol: three.ts#1) | ||
`.trim(), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (C) 2022 TypeFox and others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import path from 'node:path'; | ||
import * as lsp from 'vscode-languageserver'; | ||
import type { LspDocuments } from '../document.js'; | ||
import { pathToUri } from '../protocol-translation.js'; | ||
import { tslib, tsp } from '../ts-protocol.js'; | ||
import { Range } from '../utils/typeConverters.js'; | ||
|
||
export function fromProtocolCallHierarchyItem(item: tsp.CallHierarchyItem, documents: LspDocuments, workspaceRoot: string | undefined): lsp.CallHierarchyItem { | ||
const useFileName = isSourceFileItem(item); | ||
const name = useFileName ? path.basename(item.file) : item.name; | ||
const detail = useFileName | ||
? workspaceRoot ? path.relative(workspaceRoot, path.dirname(item.file)) : path.dirname(item.file) | ||
: item.containerName ?? ''; | ||
const result: lsp.CallHierarchyItem = { | ||
kind: fromProtocolScriptElementKind(item.kind), | ||
name, | ||
detail, | ||
uri: pathToUri(item.file, documents), | ||
range: Range.fromTextSpan(item.span), | ||
selectionRange: Range.fromTextSpan(item.selectionSpan), | ||
}; | ||
|
||
const kindModifiers = item.kindModifiers ? parseKindModifier(item.kindModifiers) : undefined; | ||
if (kindModifiers?.has(tslib.ScriptElementKindModifier.deprecatedModifier)) { | ||
result.tags = [lsp.SymbolTag.Deprecated]; | ||
} | ||
return result; | ||
} | ||
|
||
export function fromProtocolCallHierarchyIncomingCall(item: tsp.CallHierarchyIncomingCall, documents: LspDocuments, workspaceRoot: string | undefined): lsp.CallHierarchyIncomingCall { | ||
return { | ||
from: fromProtocolCallHierarchyItem(item.from, documents, workspaceRoot), | ||
fromRanges: item.fromSpans.map(Range.fromTextSpan), | ||
}; | ||
} | ||
|
||
export function fromProtocolCallHierarchyOutgoingCall(item: tsp.CallHierarchyOutgoingCall, documents: LspDocuments, workspaceRoot: string | undefined): lsp.CallHierarchyOutgoingCall { | ||
return { | ||
to: fromProtocolCallHierarchyItem(item.to, documents, workspaceRoot), | ||
fromRanges: item.fromSpans.map(Range.fromTextSpan), | ||
}; | ||
} | ||
|
||
function isSourceFileItem(item: tsp.CallHierarchyItem) { | ||
return item.kind === tslib.ScriptElementKind.scriptElement || item.kind === tslib.ScriptElementKind.moduleElement && item.selectionSpan.start.line === 1 && item.selectionSpan.start.offset === 1; | ||
} | ||
|
||
function fromProtocolScriptElementKind(kind: tslib.ScriptElementKind): lsp.SymbolKind { | ||
switch (kind) { | ||
case tslib.ScriptElementKind.moduleElement:return lsp.SymbolKind.Module; | ||
case tslib.ScriptElementKind.classElement:return lsp.SymbolKind.Class; | ||
case tslib.ScriptElementKind.enumElement:return lsp.SymbolKind.Enum; | ||
case tslib.ScriptElementKind.enumMemberElement:return lsp.SymbolKind.EnumMember; | ||
case tslib.ScriptElementKind.interfaceElement:return lsp.SymbolKind.Interface; | ||
case tslib.ScriptElementKind.indexSignatureElement:return lsp.SymbolKind.Method; | ||
case tslib.ScriptElementKind.callSignatureElement:return lsp.SymbolKind.Method; | ||
case tslib.ScriptElementKind.memberFunctionElement:return lsp.SymbolKind.Method; | ||
case tslib.ScriptElementKind.memberVariableElement:return lsp.SymbolKind.Property; | ||
case tslib.ScriptElementKind.memberGetAccessorElement:return lsp.SymbolKind.Property; | ||
case tslib.ScriptElementKind.memberSetAccessorElement:return lsp.SymbolKind.Property; | ||
case tslib.ScriptElementKind.variableElement:return lsp.SymbolKind.Variable; | ||
case tslib.ScriptElementKind.letElement:return lsp.SymbolKind.Variable; | ||
case tslib.ScriptElementKind.constElement:return lsp.SymbolKind.Variable; | ||
case tslib.ScriptElementKind.localVariableElement:return lsp.SymbolKind.Variable; | ||
case tslib.ScriptElementKind.alias:return lsp.SymbolKind.Variable; | ||
case tslib.ScriptElementKind.functionElement:return lsp.SymbolKind.Function; | ||
case tslib.ScriptElementKind.localFunctionElement:return lsp.SymbolKind.Function; | ||
case tslib.ScriptElementKind.constructSignatureElement:return lsp.SymbolKind.Constructor; | ||
case tslib.ScriptElementKind.constructorImplementationElement:return lsp.SymbolKind.Constructor; | ||
case tslib.ScriptElementKind.typeParameterElement:return lsp.SymbolKind.TypeParameter; | ||
case tslib.ScriptElementKind.string:return lsp.SymbolKind.String; | ||
default: return lsp.SymbolKind.Variable; | ||
} | ||
} | ||
|
||
function parseKindModifier(kindModifiers: string): Set<string> { | ||
return new Set(kindModifiers.split(/,|\s+/g)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// comment line | ||
import { Two } from './two' | ||
export function main() { | ||
new Two().callThreeTwice(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// comment line | ||
export class Three { | ||
tada() { | ||
print('🎉'); | ||
} | ||
} | ||
export function print(s: string) { | ||
console.log(s); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// comment line | ||
import { Three } from "./three"; | ||
export class Two { | ||
callThreeTwice() { | ||
new Three().tada(); | ||
new Three().tada(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ | |
"allowSyntheticDefaultImports": true, | ||
"resolveJsonModule": true, | ||
"moduleResolution": "node", | ||
"noLib": true | ||
}, | ||
} |