Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(51017): Make lineText in the references response opt-out #51081

Merged
merged 3 commits into from Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/server/protocol.ts
Expand Up @@ -1163,7 +1163,7 @@ namespace ts.server.protocol {
* to show text of reference line (the server already has
* loaded the referencing files).
*/
lineText: string;
lineText?: string;
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved

/**
* True if reference is a write location, false otherwise.
Expand Down Expand Up @@ -3478,6 +3478,8 @@ namespace ts.server.protocol {
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
readonly includeInlayEnumMemberValueHints?: boolean;
readonly autoImportFileExcludePatterns?: string[];

readonly disableLineTextInReferences?: boolean;
amcasey marked this conversation as resolved.
Show resolved Hide resolved
}

export interface CompilerOptions {
Expand Down
16 changes: 11 additions & 5 deletions src/server/session.ts
Expand Up @@ -1807,6 +1807,7 @@ namespace ts.server {

if (!simplifiedResult) return references;

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

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

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

if (!simplifiedResult) return references;
const refs = references.map(entry => referenceEntryToReferencesResponseItem(this.projectService, entry));
const refs = references.map(entry => referenceEntryToReferencesResponseItem(this.projectService, entry, preferences));
return {
refs,
symbolName: `"${args.file}"`
Expand Down Expand Up @@ -3516,11 +3518,10 @@ namespace ts.server {
return text;
}

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

function getLineText(scriptInfo: ScriptInfo, span: protocol.TextSpanWithContext) {
const lineSpan = scriptInfo.lineToTextSpan(span.start.line - 1);
return scriptInfo.getSnapshot().getText(lineSpan.start, textSpanEnd(lineSpan)).replace(/\r|\n/g, "");
}

function isCompletionEntryData(data: any): data is CompletionEntryData {
return data === undefined || data && typeof data === "object"
&& typeof data.exportName === "string"
Expand Down
23 changes: 23 additions & 0 deletions src/testRunner/unittests/tsserver/getFileReferences.ts
Expand Up @@ -54,5 +54,28 @@ namespace ts.projectSystem {

assert.deepEqual(response, expectResponse);
});

amcasey marked this conversation as resolved.
Show resolved Hide resolved
it("should skip lineText from file references", () => {
const session = makeSampleSession();
session.getProjectService().setHostConfiguration({ preferences: { disableLineTextInReferences: true } });

const response = executeSessionRequest<protocol.FileReferencesRequest, protocol.FileReferencesResponse>(
session,
protocol.CommandTypes.FileReferences,
{ file: aTs.path },
);

const expectResponse: protocol.FileReferencesResponseBody = {
refs: [
makeReferenceItem({ file: bTs, text: "./a", lineText: undefined, contextText: importA, isWriteAccess: false }),
makeReferenceItem({ file: cTs, text: "./a", lineText: undefined, contextText: importCurlyFromA, isWriteAccess: false }),
makeReferenceItem({ file: dTs, text: "/project/a", lineText: undefined, contextText: importAFromA, isWriteAccess: false }),
makeReferenceItem({ file: dTs, text: "./a", lineText: undefined, contextText: typeofImportA, isWriteAccess: false }),
],
symbolName: `"${aTs.path}"`,
};

assert.deepEqual(response, expectResponse);
});
});
}
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserver/helpers.ts
Expand Up @@ -810,7 +810,7 @@ namespace ts.projectSystem {
export interface MakeReferenceItem extends DocumentSpanFromSubstring {
isDefinition?: boolean;
isWriteAccess?: boolean;
lineText: string;
lineText?: string;
}

export function makeReferenceItem({ isDefinition, isWriteAccess, lineText, ...rest }: MakeReferenceItem): protocol.ReferencesResponseItem {
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Expand Up @@ -7987,7 +7987,7 @@ declare namespace ts.server.protocol {
* to show text of reference line (the server already has
* loaded the referencing files).
*/
lineText: string;
lineText?: string;
/**
* True if reference is a write location, false otherwise.
*/
Expand Down Expand Up @@ -9833,6 +9833,7 @@ declare namespace ts.server.protocol {
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
readonly includeInlayEnumMemberValueHints?: boolean;
readonly autoImportFileExcludePatterns?: string[];
readonly disableLineTextInReferences?: boolean;
}
interface CompilerOptions {
allowJs?: boolean;
Expand Down