Skip to content

Commit dbeae5d

Browse files
authoredOct 7, 2022
fix(51017): Make lineText in the references response opt-out (#51081)
* add option to exclude lineText from the response * add comments * update baseline
1 parent d06a592 commit dbeae5d

File tree

5 files changed

+58
-16
lines changed

5 files changed

+58
-16
lines changed
 

‎src/server/protocol.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1158,12 +1158,14 @@ namespace ts.server.protocol {
11581158
}
11591159

11601160
export interface ReferencesResponseItem extends FileSpanWithContext {
1161-
/** Text of line containing the reference. Including this
1162-
* with the response avoids latency of editor loading files
1163-
* to show text of reference line (the server already has
1164-
* loaded the referencing files).
1161+
/**
1162+
* Text of line containing the reference. Including this
1163+
* with the response avoids latency of editor loading files
1164+
* to show text of reference line (the server already has loaded the referencing files).
1165+
*
1166+
* If {@link UserPreferences.disableLineTextInReferences} is enabled, the property won't be filled
11651167
*/
1166-
lineText: string;
1168+
lineText?: string;
11671169

11681170
/**
11691171
* True if reference is a write location, false otherwise.
@@ -3478,6 +3480,11 @@ namespace ts.server.protocol {
34783480
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
34793481
readonly includeInlayEnumMemberValueHints?: boolean;
34803482
readonly autoImportFileExcludePatterns?: string[];
3483+
3484+
/**
3485+
* Indicates whether {@link ReferencesResponseItem.lineText} is supported.
3486+
*/
3487+
readonly disableLineTextInReferences?: boolean;
34813488
}
34823489

34833490
export interface CompilerOptions {

‎src/server/session.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,7 @@ namespace ts.server {
18071807

18081808
if (!simplifiedResult) return references;
18091809

1810+
const preferences = this.getPreferences(file);
18101811
const defaultProject = this.getDefaultProject(args);
18111812
const scriptInfo = defaultProject.getScriptInfoForNormalizedPath(file)!;
18121813
const nameInfo = defaultProject.getLanguageService().getQuickInfoAtPosition(file, position);
@@ -1815,14 +1816,15 @@ namespace ts.server {
18151816
const symbolStartOffset = nameSpan ? scriptInfo.positionToLineOffset(nameSpan.start).offset : 0;
18161817
const symbolName = nameSpan ? scriptInfo.getSnapshot().getText(nameSpan.start, textSpanEnd(nameSpan)) : "";
18171818
const refs: readonly protocol.ReferencesResponseItem[] = flatMap(references, referencedSymbol => {
1818-
return referencedSymbol.references.map(entry => referenceEntryToReferencesResponseItem(this.projectService, entry));
1819+
return referencedSymbol.references.map(entry => referenceEntryToReferencesResponseItem(this.projectService, entry, preferences));
18191820
});
18201821
return { refs, symbolName, symbolStartOffset, symbolDisplayString };
18211822
}
18221823

18231824
private getFileReferences(args: protocol.FileRequestArgs, simplifiedResult: boolean): protocol.FileReferencesResponseBody | readonly ReferenceEntry[] {
18241825
const projects = this.getProjects(args);
18251826
const fileName = args.file;
1827+
const preferences = this.getPreferences(toNormalizedPath(fileName));
18261828

18271829
const references: ReferenceEntry[] = [];
18281830
const seen = createDocumentSpanSet();
@@ -1842,7 +1844,7 @@ namespace ts.server {
18421844
});
18431845

18441846
if (!simplifiedResult) return references;
1845-
const refs = references.map(entry => referenceEntryToReferencesResponseItem(this.projectService, entry));
1847+
const refs = references.map(entry => referenceEntryToReferencesResponseItem(this.projectService, entry, preferences));
18461848
return {
18471849
refs,
18481850
symbolName: `"${args.file}"`
@@ -3516,11 +3518,10 @@ namespace ts.server {
35163518
return text;
35173519
}
35183520

3519-
function referenceEntryToReferencesResponseItem(projectService: ProjectService, { fileName, textSpan, contextSpan, isWriteAccess, isDefinition }: ReferencedSymbolEntry): protocol.ReferencesResponseItem {
3521+
function referenceEntryToReferencesResponseItem(projectService: ProjectService, { fileName, textSpan, contextSpan, isWriteAccess, isDefinition }: ReferencedSymbolEntry, { disableLineTextInReferences }: protocol.UserPreferences): protocol.ReferencesResponseItem {
35203522
const scriptInfo = Debug.checkDefined(projectService.getScriptInfo(fileName));
35213523
const span = toProtocolTextSpanWithContext(textSpan, contextSpan, scriptInfo);
3522-
const lineSpan = scriptInfo.lineToTextSpan(span.start.line - 1);
3523-
const lineText = scriptInfo.getSnapshot().getText(lineSpan.start, textSpanEnd(lineSpan)).replace(/\r|\n/g, "");
3524+
const lineText = disableLineTextInReferences ? undefined : getLineText(scriptInfo, span);
35243525
return {
35253526
file: fileName,
35263527
...span,
@@ -3530,6 +3531,11 @@ namespace ts.server {
35303531
};
35313532
}
35323533

3534+
function getLineText(scriptInfo: ScriptInfo, span: protocol.TextSpanWithContext) {
3535+
const lineSpan = scriptInfo.lineToTextSpan(span.start.line - 1);
3536+
return scriptInfo.getSnapshot().getText(lineSpan.start, textSpanEnd(lineSpan)).replace(/\r|\n/g, "");
3537+
}
3538+
35333539
function isCompletionEntryData(data: any): data is CompletionEntryData {
35343540
return data === undefined || data && typeof data === "object"
35353541
&& typeof data.exportName === "string"

‎src/testRunner/unittests/tsserver/getFileReferences.ts

+23
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,28 @@ namespace ts.projectSystem {
5454

5555
assert.deepEqual(response, expectResponse);
5656
});
57+
58+
it("should skip lineText from file references", () => {
59+
const session = makeSampleSession();
60+
session.getProjectService().setHostConfiguration({ preferences: { disableLineTextInReferences: true } });
61+
62+
const response = executeSessionRequest<protocol.FileReferencesRequest, protocol.FileReferencesResponse>(
63+
session,
64+
protocol.CommandTypes.FileReferences,
65+
{ file: aTs.path },
66+
);
67+
68+
const expectResponse: protocol.FileReferencesResponseBody = {
69+
refs: [
70+
makeReferenceItem({ file: bTs, text: "./a", lineText: undefined, contextText: importA, isWriteAccess: false }),
71+
makeReferenceItem({ file: cTs, text: "./a", lineText: undefined, contextText: importCurlyFromA, isWriteAccess: false }),
72+
makeReferenceItem({ file: dTs, text: "/project/a", lineText: undefined, contextText: importAFromA, isWriteAccess: false }),
73+
makeReferenceItem({ file: dTs, text: "./a", lineText: undefined, contextText: typeofImportA, isWriteAccess: false }),
74+
],
75+
symbolName: `"${aTs.path}"`,
76+
};
77+
78+
assert.deepEqual(response, expectResponse);
79+
});
5780
});
5881
}

‎src/testRunner/unittests/tsserver/helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ namespace ts.projectSystem {
810810
export interface MakeReferenceItem extends DocumentSpanFromSubstring {
811811
isDefinition?: boolean;
812812
isWriteAccess?: boolean;
813-
lineText: string;
813+
lineText?: string;
814814
}
815815

816816
export function makeReferenceItem({ isDefinition, isWriteAccess, lineText, ...rest }: MakeReferenceItem): protocol.ReferencesResponseItem {

‎tests/baselines/reference/api/tsserverlibrary.d.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -7982,12 +7982,14 @@ declare namespace ts.server.protocol {
79827982
command: CommandTypes.References;
79837983
}
79847984
interface ReferencesResponseItem extends FileSpanWithContext {
7985-
/** Text of line containing the reference. Including this
7986-
* with the response avoids latency of editor loading files
7987-
* to show text of reference line (the server already has
7988-
* loaded the referencing files).
7985+
/**
7986+
* Text of line containing the reference. Including this
7987+
* with the response avoids latency of editor loading files
7988+
* to show text of reference line (the server already has loaded the referencing files).
7989+
*
7990+
* If {@link UserPreferences.disableLineTextInReferences} is enabled, the property won't be filled
79897991
*/
7990-
lineText: string;
7992+
lineText?: string;
79917993
/**
79927994
* True if reference is a write location, false otherwise.
79937995
*/
@@ -9833,6 +9835,10 @@ declare namespace ts.server.protocol {
98339835
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
98349836
readonly includeInlayEnumMemberValueHints?: boolean;
98359837
readonly autoImportFileExcludePatterns?: string[];
9838+
/**
9839+
* Indicates whether {@link ReferencesResponseItem.lineText} is supported.
9840+
*/
9841+
readonly disableLineTextInReferences?: boolean;
98369842
}
98379843
interface CompilerOptions {
98389844
allowJs?: boolean;

0 commit comments

Comments
 (0)
Please sign in to comment.