Skip to content

Commit 5040e1d

Browse files
authored
Mark occurence item in string with a special property (microsoft#14677)
* mark occurence item in string with a special property * Adding trailing commas
1 parent 4b3cd6a commit 5040e1d

File tree

10 files changed

+76
-10
lines changed

10 files changed

+76
-10
lines changed

src/harness/fourslash.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,9 +928,13 @@ namespace FourSlash {
928928
}
929929

930930
function rangeToReferenceEntry(r: Range) {
931-
let { isWriteAccess, isDefinition } = (r.marker && r.marker.data) || { isWriteAccess: false, isDefinition: false };
931+
let { isWriteAccess, isDefinition, isInString } = (r.marker && r.marker.data) || { isWriteAccess: false, isDefinition: false, isInString: undefined };
932932
isWriteAccess = !!isWriteAccess; isDefinition = !!isDefinition;
933-
return { fileName: r.fileName, textSpan: { start: r.start, length: r.end - r.start }, isWriteAccess, isDefinition };
933+
const result: any = { fileName: r.fileName, textSpan: { start: r.start, length: r.end - r.start }, isWriteAccess, isDefinition };
934+
if (isInString !== undefined) {
935+
result.isInString = isInString;
936+
}
937+
return result;
934938
}
935939
}
936940

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3489,6 +3489,52 @@ namespace ts.projectSystem {
34893489
});
34903490
});
34913491

3492+
describe("occurence highlight on string", () => {
3493+
it("should be marked if only on string values", () => {
3494+
const file1: FileOrFolder = {
3495+
path: "/a/b/file1.ts",
3496+
content: `let t1 = "div";\nlet t2 = "div";\nlet t3 = { "div": 123 };\nlet t4 = t3["div"];`
3497+
};
3498+
3499+
const host = createServerHost([file1]);
3500+
const session = createSession(host);
3501+
const projectService = session.getProjectService();
3502+
3503+
projectService.openClientFile(file1.path);
3504+
{
3505+
const highlightRequest = makeSessionRequest<protocol.FileLocationRequestArgs>(
3506+
CommandNames.Occurrences,
3507+
{ file: file1.path, line: 1, offset: 11 }
3508+
);
3509+
const highlightResponse = session.executeCommand(highlightRequest).response as protocol.OccurrencesResponseItem[];
3510+
const firstOccurence = highlightResponse[0];
3511+
assert.isTrue(firstOccurence.isInString, "Highlights should be marked with isInString");
3512+
}
3513+
3514+
{
3515+
const highlightRequest = makeSessionRequest<protocol.FileLocationRequestArgs>(
3516+
CommandNames.Occurrences,
3517+
{ file: file1.path, line: 3, offset: 13 }
3518+
);
3519+
const highlightResponse = session.executeCommand(highlightRequest).response as protocol.OccurrencesResponseItem[];
3520+
assert.isTrue(highlightResponse.length === 2);
3521+
const firstOccurence = highlightResponse[0];
3522+
assert.isUndefined(firstOccurence.isInString, "Highlights should not be marked with isInString if on property name");
3523+
}
3524+
3525+
{
3526+
const highlightRequest = makeSessionRequest<protocol.FileLocationRequestArgs>(
3527+
CommandNames.Occurrences,
3528+
{ file: file1.path, line: 4, offset: 14 }
3529+
);
3530+
const highlightResponse = session.executeCommand(highlightRequest).response as protocol.OccurrencesResponseItem[];
3531+
assert.isTrue(highlightResponse.length === 2);
3532+
const firstOccurence = highlightResponse[0];
3533+
assert.isUndefined(firstOccurence.isInString, "Highlights should not be marked with isInString if on indexer");
3534+
}
3535+
});
3536+
});
3537+
34923538
describe("maxNodeModuleJsDepth for inferred projects", () => {
34933539
it("should be set to 2 if the project has js root files", () => {
34943540
const file1: FileOrFolder = {

src/server/protocol.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,11 @@ namespace ts.server.protocol {
621621
* True if the occurrence is a write location, false otherwise.
622622
*/
623623
isWriteAccess: boolean;
624+
625+
/**
626+
* True if the occurrence is in a string, undefined otherwise;
627+
*/
628+
isInString?: true;
624629
}
625630

626631
export interface OccurrencesResponse extends Response {

src/server/session.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,16 +651,21 @@ namespace ts.server {
651651
}
652652

653653
return occurrences.map(occurrence => {
654-
const { fileName, isWriteAccess, textSpan } = occurrence;
654+
const { fileName, isWriteAccess, textSpan, isInString } = occurrence;
655655
const scriptInfo = project.getScriptInfo(fileName);
656656
const start = scriptInfo.positionToLineOffset(textSpan.start);
657657
const end = scriptInfo.positionToLineOffset(ts.textSpanEnd(textSpan));
658-
return {
658+
const result: protocol.OccurrencesResponseItem = {
659659
start,
660660
end,
661661
file: fileName,
662662
isWriteAccess,
663663
};
664+
// no need to serialize the property if it is not true
665+
if (isInString) {
666+
result.isInString = isInString;
667+
}
668+
return result;
664669
});
665670
}
666671

src/services/documentHighlights.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ namespace ts.DocumentHighlights {
3737

3838
documentHighlights.highlightSpans.push({
3939
textSpan: referenceEntry.textSpan,
40-
kind: referenceEntry.isWriteAccess ? HighlightSpanKind.writtenReference : HighlightSpanKind.reference
40+
kind: referenceEntry.isWriteAccess ? HighlightSpanKind.writtenReference : HighlightSpanKind.reference,
41+
isInString: referenceEntry.isInString
4142
});
4243
}
4344
}

src/services/findAllReferences.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,9 @@ namespace ts.FindAllReferences {
10441044

10451045
const type = getStringLiteralTypeForNode(<StringLiteral>node, typeChecker);
10461046
if (type === searchType) {
1047-
references.push(getReferenceEntryFromNode(node));
1047+
const reference = getReferenceEntryFromNode(node);
1048+
reference.isInString = true;
1049+
references.push(reference);
10481050
}
10491051
}
10501052
}

src/services/services.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,8 @@ namespace ts {
13961396
fileName: entry.fileName,
13971397
textSpan: highlightSpan.textSpan,
13981398
isWriteAccess: highlightSpan.kind === HighlightSpanKind.writtenReference,
1399-
isDefinition: false
1399+
isDefinition: false,
1400+
isInString: highlightSpan.isInString,
14001401
});
14011402
}
14021403
}

src/services/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ namespace ts {
364364
fileName: string;
365365
isWriteAccess: boolean;
366366
isDefinition: boolean;
367+
isInString?: true;
367368
}
368369

369370
export interface ImplementationLocation {
@@ -385,6 +386,7 @@ namespace ts {
385386

386387
export interface HighlightSpan {
387388
fileName?: string;
389+
isInString?: true;
388390
textSpan: TextSpan;
389391
kind: string;
390392
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference path='fourslash.ts'/>
22

3-
////type Options = "[|option 1|]" | "option 2";
4-
////let myOption: Options = "[|option 1|]";
3+
////type Options = "[|{| "isInString": true |}option 1|]" | "option 2";
4+
////let myOption: Options = "[|{| "isInString": true |}option 1|]";
55

66
verify.singleReferenceGroup('"option 1"');

tests/cases/fourslash/referencesForIndexProperty2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
// References to a unknown index property
44

55
////var a;
6-
////a["[|blah|]"];
6+
////a["[|{| "isInString": true |}blah|]"];
77

88
verify.singleReferenceGroup('"blah"');

0 commit comments

Comments
 (0)