From 73c3961355322515567875286141c4da723fb8ae Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Wed, 24 Aug 2016 18:50:04 -0700 Subject: [PATCH 1/4] Adding display parts to definition items to support FindAllReferences --- src/services/services.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index babb5cf4574..7718be5a3d4 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -300,8 +300,8 @@ namespace ts { } } // For syntactic classifications, all trivia are classcified together, including jsdoc comments. - // For that to work, the jsdoc comments should still be the leading trivia of the first child. - // Restoring the scanner position ensures that. + // For that to work, the jsdoc comments should still be the leading trivia of the first child. + // Restoring the scanner position ensures that. pos = this.pos; forEachChild(this, processNode, processNodes); if (pos < this.end) { @@ -1374,8 +1374,13 @@ namespace ts { containerName: string; } + export interface ReferencedSymbolDefinitionInfo extends DefinitionInfo { + // For more complex definitions where kind and name are insufficient to properly colorize the text + displayParts?: SymbolDisplayPart[]; + } + export interface ReferencedSymbol { - definition: DefinitionInfo; + definition: ReferencedSymbolDefinitionInfo; references: ReferenceEntry[]; } @@ -6108,7 +6113,7 @@ namespace ts { return result; - function getDefinition(symbol: Symbol): DefinitionInfo { + function getDefinition(symbol: Symbol): ReferencedSymbolDefinitionInfo { const info = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, node.getSourceFile(), getContainerNode(node), node); const name = map(info.displayParts, p => p.text).join(""); const declarations = symbol.declarations; @@ -6122,7 +6127,8 @@ namespace ts { name, kind: info.symbolKind, fileName: declarations[0].getSourceFile().fileName, - textSpan: createTextSpan(declarations[0].getStart(), 0) + textSpan: createTextSpan(declarations[0].getStart(), 0), + displayParts: info.displayParts }; } @@ -6317,7 +6323,7 @@ namespace ts { } }); - const definition: DefinitionInfo = { + const definition: ReferencedSymbolDefinitionInfo = { containerKind: "", containerName: "", fileName: targetLabel.getSourceFile().fileName, @@ -6634,6 +6640,11 @@ namespace ts { getReferencesForStringLiteralInFile(sourceFile, type, possiblePositions, references); } + const symbol = typeChecker.getSymbolAtLocation(node); + + const displayParts = symbol ? getSymbolDisplayPartsDocumentationAndSymbolKind( + symbol, node.getSourceFile(), getContainerNode(node), node).displayParts : undefined; + return [{ definition: { containerKind: "", @@ -6641,7 +6652,8 @@ namespace ts { fileName: node.getSourceFile().fileName, kind: ScriptElementKind.variableElement, name: type.text, - textSpan: createTextSpanFromBounds(node.getStart(), node.getEnd()) + textSpan: createTextSpanFromBounds(node.getStart(), node.getEnd()), + displayParts }, references: references }]; From e0a36840c058210d7d29cea9e9bd039e4cf4210a Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Thu, 25 Aug 2016 11:53:44 -0700 Subject: [PATCH 2/4] PR feedback --- src/services/services.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index 7718be5a3d4..6da12696937 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1375,8 +1375,7 @@ namespace ts { } export interface ReferencedSymbolDefinitionInfo extends DefinitionInfo { - // For more complex definitions where kind and name are insufficient to properly colorize the text - displayParts?: SymbolDisplayPart[]; + displayParts: SymbolDisplayPart[]; } export interface ReferencedSymbol { @@ -6640,11 +6639,6 @@ namespace ts { getReferencesForStringLiteralInFile(sourceFile, type, possiblePositions, references); } - const symbol = typeChecker.getSymbolAtLocation(node); - - const displayParts = symbol ? getSymbolDisplayPartsDocumentationAndSymbolKind( - symbol, node.getSourceFile(), getContainerNode(node), node).displayParts : undefined; - return [{ definition: { containerKind: "", @@ -6653,7 +6647,7 @@ namespace ts { kind: ScriptElementKind.variableElement, name: type.text, textSpan: createTextSpanFromBounds(node.getStart(), node.getEnd()), - displayParts + displayParts: [displayPart(getTextOfNode(node), SymbolDisplayPartKind.stringLiteral)] }, references: references }]; From 4c847eb257434ac129a8a7b62bdb9a17a272028e Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Fri, 26 Aug 2016 15:04:08 -0700 Subject: [PATCH 3/4] Fixing errors and adding a fourslash test --- src/harness/fourslash.ts | 62 +++++++++++++------ src/services/services.ts | 11 +++- .../findReferencesDefinitionDisplayParts.ts | 23 +++++++ tests/cases/fourslash/fourslash.ts | 1 + 4 files changed, 75 insertions(+), 22 deletions(-) create mode 100644 tests/cases/fourslash/findReferencesDefinitionDisplayParts.ts diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 63731417f48..694dcf6394e 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -206,6 +206,24 @@ namespace FourSlash { private inputFiles = ts.createMap(); // Map between inputFile's fileName and its content for easily looking up when resolving references + private static getDisplayPartsJson(displayParts: ts.SymbolDisplayPart[]) { + let result = ""; + ts.forEach(displayParts, part => { + if (result) { + result += ",\n "; + } + else { + result = "[\n "; + } + result += JSON.stringify(part); + }); + if (result) { + result += "\n]"; + } + + return result; + } + // Add input file which has matched file name with the given reference-file path. // This is necessary when resolveReference flag is specified private addMatchedInputFile(referenceFilePath: string, extensions: string[]) { @@ -777,6 +795,20 @@ namespace FourSlash { ts.forEachProperty(this.rangesByText(), ranges => this.verifyRangesReferenceEachOther(ranges)); } + public verifyDisplayPartsOfReferencedSymbol(expected: ts.SymbolDisplayPart[]) { + const referencedSymbols = this.findReferencesAtCaret(); + + if (referencedSymbols.length === 0) { + this.raiseError("No referenced symbols found at current caret position"); + } + else if (referencedSymbols.length > 1) { + this.raiseError("More than one referenced symbol found"); + } + + assert.equal(TestState.getDisplayPartsJson(referencedSymbols[0].definition.displayParts), + TestState.getDisplayPartsJson(expected), this.messageAtLastKnownMarker("referenced symbol definition display parts")); + } + private verifyReferencesWorker(references: ts.ReferenceEntry[], fileName: string, start: number, end: number, isWriteAccess?: boolean, isDefinition?: boolean) { for (let i = 0; i < references.length; i++) { const reference = references[i]; @@ -811,6 +843,10 @@ namespace FourSlash { return this.languageService.getReferencesAtPosition(this.activeFile.fileName, this.currentCaretPosition); } + private findReferencesAtCaret() { + return this.languageService.findReferences(this.activeFile.fileName, this.currentCaretPosition); + } + public getSyntacticDiagnostics(expected: string) { const diagnostics = this.languageService.getSyntacticDiagnostics(this.activeFile.fileName); this.testDiagnostics(expected, diagnostics); @@ -856,30 +892,12 @@ namespace FourSlash { displayParts: ts.SymbolDisplayPart[], documentation: ts.SymbolDisplayPart[]) { - function getDisplayPartsJson(displayParts: ts.SymbolDisplayPart[]) { - let result = ""; - ts.forEach(displayParts, part => { - if (result) { - result += ",\n "; - } - else { - result = "[\n "; - } - result += JSON.stringify(part); - }); - if (result) { - result += "\n]"; - } - - return result; - } - const actualQuickInfo = this.languageService.getQuickInfoAtPosition(this.activeFile.fileName, this.currentCaretPosition); assert.equal(actualQuickInfo.kind, kind, this.messageAtLastKnownMarker("QuickInfo kind")); assert.equal(actualQuickInfo.kindModifiers, kindModifiers, this.messageAtLastKnownMarker("QuickInfo kindModifiers")); assert.equal(JSON.stringify(actualQuickInfo.textSpan), JSON.stringify(textSpan), this.messageAtLastKnownMarker("QuickInfo textSpan")); - assert.equal(getDisplayPartsJson(actualQuickInfo.displayParts), getDisplayPartsJson(displayParts), this.messageAtLastKnownMarker("QuickInfo displayParts")); - assert.equal(getDisplayPartsJson(actualQuickInfo.documentation), getDisplayPartsJson(documentation), this.messageAtLastKnownMarker("QuickInfo documentation")); + assert.equal(TestState.getDisplayPartsJson(actualQuickInfo.displayParts), TestState.getDisplayPartsJson(displayParts), this.messageAtLastKnownMarker("QuickInfo displayParts")); + assert.equal(TestState.getDisplayPartsJson(actualQuickInfo.documentation), TestState.getDisplayPartsJson(documentation), this.messageAtLastKnownMarker("QuickInfo documentation")); } public verifyRenameLocations(findInStrings: boolean, findInComments: boolean, ranges?: Range[]) { @@ -2947,6 +2965,10 @@ namespace FourSlashInterface { this.state.verifyRangesReferenceEachOther(ranges); } + public findReferencesDefinitionDisplayPartsAtCaretAre(expected: ts.SymbolDisplayPart[]) { + this.state.verifyDisplayPartsOfReferencedSymbol(expected); + } + public rangesWithSameTextReferenceEachOther() { this.state.verifyRangesWithSameTextReferenceEachOther(); } diff --git a/src/services/services.ts b/src/services/services.ts index 6da12696937..8794446e83e 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -6328,7 +6328,8 @@ namespace ts { fileName: targetLabel.getSourceFile().fileName, kind: ScriptElementKind.label, name: labelName, - textSpan: createTextSpanFromBounds(targetLabel.getStart(), targetLabel.getEnd()) + textSpan: createTextSpanFromBounds(targetLabel.getStart(), targetLabel.getEnd()), + displayParts: [displayPart(labelName, SymbolDisplayPartKind.text)] }; return [{ definition, references }]; @@ -6568,6 +6569,11 @@ namespace ts { getThisReferencesInFile(sourceFile, searchSpaceNode, possiblePositions, references); } + const thisOrSuperSymbol = typeChecker.getSymbolAtLocation(thisOrSuperKeyword); + + const { displayParts } = getSymbolDisplayPartsDocumentationAndSymbolKind( + thisOrSuperSymbol, thisOrSuperKeyword.getSourceFile(), getContainerNode(thisOrSuperKeyword), thisOrSuperKeyword); + return [{ definition: { containerKind: "", @@ -6575,7 +6581,8 @@ namespace ts { fileName: node.getSourceFile().fileName, kind: ScriptElementKind.variableElement, name: "this", - textSpan: createTextSpanFromBounds(node.getStart(), node.getEnd()) + textSpan: createTextSpanFromBounds(node.getStart(), node.getEnd()), + displayParts }, references: references }]; diff --git a/tests/cases/fourslash/findReferencesDefinitionDisplayParts.ts b/tests/cases/fourslash/findReferencesDefinitionDisplayParts.ts new file mode 100644 index 00000000000..d424bd0c500 --- /dev/null +++ b/tests/cases/fourslash/findReferencesDefinitionDisplayParts.ts @@ -0,0 +1,23 @@ +/// + +//// class Gre/*1*/eter { +//// someFunction() { th/*2*/is; } +//// } +//// +//// type Options = "opt/*3*/ion 1" | "option 2"; +//// let myOption: Options = "option 1"; +//// +//// some/*4*/Label: +//// break someLabel; + +goTo.marker("1"); +verify.findReferencesDefinitionDisplayPartsAtCaretAre([{ text: "class", kind: "keyword" }, { text: " ", kind: "space" }, { text: "Greeter", kind: "className" }]); + +goTo.marker("2"); +verify.findReferencesDefinitionDisplayPartsAtCaretAre([{ text: "this", kind: "keyword" }, { text: ":", kind: "punctuation" }, { text: " ", kind: "space" }, { text: "this", kind: "keyword" }]); + +goTo.marker("3"); +verify.findReferencesDefinitionDisplayPartsAtCaretAre([{ text: "\"option 1\"", kind: "stringLiteral" }]); + +goTo.marker("4"); +verify.findReferencesDefinitionDisplayPartsAtCaretAre([{ text: "someLabel", kind: "text" }]); \ No newline at end of file diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 9ce3197a46f..59037e0de67 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -171,6 +171,7 @@ declare namespace FourSlashInterface { * If `ranges` is omitted, this is `test.ranges()`. */ rangesReferenceEachOther(ranges?: Range[]): void; + findReferencesDefinitionDisplayPartsAtCaretAre(expected: ts.SymbolDisplayPart[]): void; rangesWithSameTextReferenceEachOther(): void; currentParameterHelpArgumentNameIs(name: string): void; currentParameterSpanIs(parameter: string): void; From d72ed3c0b0fcf1f3c5f2b06d430c5e9c2ea6ca6f Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Fri, 26 Aug 2016 16:52:25 -0700 Subject: [PATCH 4/4] Check for this symbol before getting display parts --- src/services/services.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index dd99c0dba52..c19eb487d75 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -6570,8 +6570,8 @@ namespace ts { const thisOrSuperSymbol = typeChecker.getSymbolAtLocation(thisOrSuperKeyword); - const { displayParts } = getSymbolDisplayPartsDocumentationAndSymbolKind( - thisOrSuperSymbol, thisOrSuperKeyword.getSourceFile(), getContainerNode(thisOrSuperKeyword), thisOrSuperKeyword); + const displayParts = thisOrSuperSymbol && getSymbolDisplayPartsDocumentationAndSymbolKind( + thisOrSuperSymbol, thisOrSuperKeyword.getSourceFile(), getContainerNode(thisOrSuperKeyword), thisOrSuperKeyword).displayParts; return [{ definition: {