Skip to content

Commit 7ed3896

Browse files
authored
Merge pull request microsoft#31587 from microsoft/definitionSpan
Include declarationSpan as relevant declaration span when defintion or other places are declaration name
2 parents 1e6a6e9 + 73bf268 commit 7ed3896

File tree

355 files changed

+2131
-1539
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

355 files changed

+2131
-1539
lines changed

src/harness/client.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,15 @@ namespace ts.server {
395395
const locations: RenameLocation[] = [];
396396
for (const entry of body.locs) {
397397
const fileName = entry.file;
398-
for (const { start, end, ...prefixSuffixText } of entry.locs) {
399-
locations.push({ textSpan: this.decodeSpan({ start, end }, fileName), fileName, ...prefixSuffixText });
398+
for (const { start, end, contextStart, contextEnd, ...prefixSuffixText } of entry.locs) {
399+
locations.push({
400+
textSpan: this.decodeSpan({ start, end }, fileName),
401+
fileName,
402+
...(contextStart !== undefined ?
403+
{ contextSpan: this.decodeSpan({ start: contextStart, end: contextEnd! }, fileName) } :
404+
undefined),
405+
...prefixSuffixText
406+
});
400407
}
401408
}
402409

src/harness/fourslash.ts

+35-15
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace FourSlash {
4242
* is a range with `text in range` "selected".
4343
*/
4444
ranges: Range[];
45+
rangesByText?: ts.MultiMap<Range>;
4546
}
4647

4748
export interface Marker {
@@ -955,12 +956,15 @@ namespace FourSlash {
955956
const fullExpected = ts.map<FourSlashInterface.ReferenceGroup, ReferenceGroupJson>(parts, ({ definition, ranges }) => ({
956957
definition: typeof definition === "string" ? definition : { ...definition, range: ts.createTextSpanFromRange(definition.range) },
957958
references: ranges.map<ts.ReferenceEntry>(r => {
958-
const { isWriteAccess = false, isDefinition = false, isInString } = (r.marker && r.marker.data || {}) as { isWriteAccess?: boolean, isDefinition?: boolean, isInString?: true };
959+
const { isWriteAccess = false, isDefinition = false, isInString, contextRangeIndex } = (r.marker && r.marker.data || {}) as { isWriteAccess?: boolean, isDefinition?: boolean, isInString?: true, contextRangeIndex?: number };
959960
return {
960961
fileName: r.fileName,
961962
textSpan: ts.createTextSpanFromRange(r),
962963
isWriteAccess,
963964
isDefinition,
965+
...(contextRangeIndex !== undefined ?
966+
{ contextSpan: ts.createTextSpanFromRange(this.getRanges()[contextRangeIndex]) } :
967+
undefined),
964968
...(isInString ? { isInString: true } : undefined),
965969
};
966970
}),
@@ -997,8 +1001,8 @@ namespace FourSlash {
9971001
assert.deepEqual<ReadonlyArray<ts.ReferenceEntry> | undefined>(refs, expected);
9981002
}
9991003

1000-
public verifySingleReferenceGroup(definition: FourSlashInterface.ReferenceGroupDefinition, ranges?: Range[]) {
1001-
ranges = ranges || this.getRanges();
1004+
public verifySingleReferenceGroup(definition: FourSlashInterface.ReferenceGroupDefinition, ranges?: Range[] | string) {
1005+
ranges = ts.isString(ranges) ? this.rangesByText().get(ranges)! : ranges || this.getRanges();
10021006
this.verifyReferenceGroups(ranges, [{ definition, ranges }]);
10031007
}
10041008

@@ -1011,7 +1015,7 @@ Actual: ${stringify(fullActual)}`);
10111015
};
10121016

10131017
if ((actual === undefined) !== (expected === undefined)) {
1014-
fail(`Expected ${expected}, got ${actual}`);
1018+
fail(`Expected ${stringify(expected)}, got ${stringify(actual)}`);
10151019
}
10161020

10171021
for (const key in actual) {
@@ -1021,7 +1025,7 @@ Actual: ${stringify(fullActual)}`);
10211025
recur(ak, ek, path ? path + "." + key : key);
10221026
}
10231027
else if (ak !== ek) {
1024-
fail(`Expected '${key}' to be '${ek}', got '${ak}'`);
1028+
fail(`Expected '${key}' to be '${stringify(ek)}', got '${stringify(ak)}'`);
10251029
}
10261030
}
10271031
}
@@ -1189,7 +1193,15 @@ Actual: ${stringify(fullActual)}`);
11891193
locations && ts.sort(locations, (r1, r2) => ts.compareStringsCaseSensitive(r1.fileName, r2.fileName) || r1.textSpan.start - r2.textSpan.start);
11901194
assert.deepEqual(sort(references), sort(ranges.map((rangeOrOptions): ts.RenameLocation => {
11911195
const { range, ...prefixSuffixText } = "range" in rangeOrOptions ? rangeOrOptions : { range: rangeOrOptions };
1192-
return { fileName: range.fileName, textSpan: ts.createTextSpanFromRange(range), ...prefixSuffixText };
1196+
const { contextRangeIndex } = (range.marker && range.marker.data || {}) as { contextRangeIndex?: number; };
1197+
return {
1198+
fileName: range.fileName,
1199+
textSpan: ts.createTextSpanFromRange(range),
1200+
...(contextRangeIndex !== undefined ?
1201+
{ contextSpan: ts.createTextSpanFromRange(this.getRanges()[contextRangeIndex]) } :
1202+
undefined),
1203+
...prefixSuffixText
1204+
};
11931205
})));
11941206
}
11951207
}
@@ -1844,6 +1856,7 @@ Actual: ${stringify(fullActual)}`);
18441856
range.end = updatePosition(range.end, editStart, editEnd, newText);
18451857
}
18461858
}
1859+
this.testData.rangesByText = undefined;
18471860
}
18481861

18491862
private removeWhitespace(text: string): string {
@@ -2026,7 +2039,9 @@ Actual: ${stringify(fullActual)}`);
20262039
}
20272040

20282041
public rangesByText(): ts.Map<Range[]> {
2042+
if (this.testData.rangesByText) return this.testData.rangesByText;
20292043
const result = ts.createMultiMap<Range>();
2044+
this.testData.rangesByText = result;
20302045
for (const range of this.getRanges()) {
20312046
const text = this.rangeText(range);
20322047
result.add(text, range);
@@ -2714,8 +2729,8 @@ Actual: ${stringify(fullActual)}`);
27142729
return this.languageService.getDocumentHighlights(this.activeFile.fileName, this.currentCaretPosition, filesToSearch);
27152730
}
27162731

2717-
public verifyRangesAreOccurrences(isWriteAccess?: boolean) {
2718-
const ranges = this.getRanges();
2732+
public verifyRangesAreOccurrences(isWriteAccess?: boolean, ranges?: Range[]) {
2733+
ranges = ranges || this.getRanges();
27192734
for (const r of ranges) {
27202735
this.goToRangeStart(r);
27212736
this.verifyOccurrencesAtPositionListCount(ranges.length);
@@ -2725,8 +2740,13 @@ Actual: ${stringify(fullActual)}`);
27252740
}
27262741
}
27272742

2728-
public verifyRangesWithSameTextAreRenameLocations() {
2729-
this.rangesByText().forEach(ranges => this.verifyRangesAreRenameLocations(ranges));
2743+
public verifyRangesWithSameTextAreRenameLocations(...texts: string[]) {
2744+
if (texts.length) {
2745+
texts.forEach(text => this.verifyRangesAreRenameLocations(this.rangesByText().get(text)!));
2746+
}
2747+
else {
2748+
this.rangesByText().forEach(ranges => this.verifyRangesAreRenameLocations(ranges));
2749+
}
27302750
}
27312751

27322752
public verifyRangesWithSameTextAreDocumentHighlights() {
@@ -3971,7 +3991,7 @@ namespace FourSlashInterface {
39713991
this.state.verifyGetReferencesForServerTest(expected);
39723992
}
39733993

3974-
public singleReferenceGroup(definition: ReferenceGroupDefinition, ranges?: FourSlash.Range[]) {
3994+
public singleReferenceGroup(definition: ReferenceGroupDefinition, ranges?: FourSlash.Range[] | string) {
39753995
this.state.verifySingleReferenceGroup(definition, ranges);
39763996
}
39773997

@@ -4093,12 +4113,12 @@ namespace FourSlashInterface {
40934113
this.state.verifyOccurrencesAtPositionListCount(expectedCount);
40944114
}
40954115

4096-
public rangesAreOccurrences(isWriteAccess?: boolean) {
4097-
this.state.verifyRangesAreOccurrences(isWriteAccess);
4116+
public rangesAreOccurrences(isWriteAccess?: boolean, ranges?: FourSlash.Range[]) {
4117+
this.state.verifyRangesAreOccurrences(isWriteAccess, ranges);
40984118
}
40994119

4100-
public rangesWithSameTextAreRenameLocations() {
4101-
this.state.verifyRangesWithSameTextAreRenameLocations();
4120+
public rangesWithSameTextAreRenameLocations(...texts: string[]) {
4121+
this.state.verifyRangesWithSameTextAreRenameLocations(...texts);
41024122
}
41034123

41044124
public rangesAreRenameLocations(options?: FourSlash.Range[] | { findInStrings?: boolean, findInComments?: boolean, ranges?: FourSlash.Range[] }) {

src/server/protocol.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -872,16 +872,24 @@ namespace ts.server.protocol {
872872
file: string;
873873
}
874874

875+
export interface TextSpanWithContext extends TextSpan {
876+
contextStart?: Location;
877+
contextEnd?: Location;
878+
}
879+
880+
export interface FileSpanWithContext extends FileSpan, TextSpanWithContext {
881+
}
882+
875883
export interface DefinitionInfoAndBoundSpan {
876-
definitions: ReadonlyArray<FileSpan>;
884+
definitions: ReadonlyArray<FileSpanWithContext>;
877885
textSpan: TextSpan;
878886
}
879887

880888
/**
881889
* Definition response message. Gives text range for definition.
882890
*/
883891
export interface DefinitionResponse extends Response {
884-
body?: FileSpan[];
892+
body?: FileSpanWithContext[];
885893
}
886894

887895
export interface DefinitionInfoAndBoundSpanReponse extends Response {
@@ -892,14 +900,14 @@ namespace ts.server.protocol {
892900
* Definition response message. Gives text range for definition.
893901
*/
894902
export interface TypeDefinitionResponse extends Response {
895-
body?: FileSpan[];
903+
body?: FileSpanWithContext[];
896904
}
897905

898906
/**
899907
* Implementation response message. Gives text range for implementations.
900908
*/
901909
export interface ImplementationResponse extends Response {
902-
body?: FileSpan[];
910+
body?: FileSpanWithContext[];
903911
}
904912

905913
/**
@@ -942,7 +950,7 @@ namespace ts.server.protocol {
942950
}
943951

944952
/** @deprecated */
945-
export interface OccurrencesResponseItem extends FileSpan {
953+
export interface OccurrencesResponseItem extends FileSpanWithContext {
946954
/**
947955
* True if the occurrence is a write location, false otherwise.
948956
*/
@@ -972,7 +980,7 @@ namespace ts.server.protocol {
972980
/**
973981
* Span augmented with extra information that denotes the kind of the highlighting to be used for span.
974982
*/
975-
export interface HighlightSpan extends TextSpan {
983+
export interface HighlightSpan extends TextSpanWithContext {
976984
kind: HighlightSpanKind;
977985
}
978986

@@ -1007,7 +1015,7 @@ namespace ts.server.protocol {
10071015
command: CommandTypes.References;
10081016
}
10091017

1010-
export interface ReferencesResponseItem extends FileSpan {
1018+
export interface ReferencesResponseItem extends FileSpanWithContext {
10111019
/** Text of line containing the reference. Including this
10121020
* with the response avoids latency of editor loading files
10131021
* to show text of reference line (the server already has
@@ -1150,7 +1158,7 @@ namespace ts.server.protocol {
11501158
locs: RenameTextSpan[];
11511159
}
11521160

1153-
export interface RenameTextSpan extends TextSpan {
1161+
export interface RenameTextSpan extends TextSpanWithContext {
11541162
readonly prefixText?: string;
11551163
readonly suffixText?: string;
11561164
}

0 commit comments

Comments
 (0)