Skip to content

Commit 48c0af5

Browse files
author
Andy
authored
Support testing document highlights with "filesToSearch" (#21640)
* Support testing document highlights with "filesToSearch" * Fix lint
1 parent d584f4d commit 48c0af5

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

Diff for: src/harness/fourslash.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -2819,7 +2819,7 @@ Actual: ${stringify(fullActual)}`);
28192819
}
28202820
}
28212821

2822-
private getDocumentHighlightsAtCurrentPosition(fileNamesToSearch: string[]) {
2822+
private getDocumentHighlightsAtCurrentPosition(fileNamesToSearch: ReadonlyArray<string>) {
28232823
const filesToSearch = fileNamesToSearch.map(name => ts.combinePaths(this.basePath, name));
28242824
return this.languageService.getDocumentHighlights(this.activeFile.fileName, this.currentCaretPosition, filesToSearch);
28252825
}
@@ -2843,9 +2843,8 @@ Actual: ${stringify(fullActual)}`);
28432843
this.rangesByText().forEach(ranges => this.verifyRangesAreDocumentHighlights(ranges));
28442844
}
28452845

2846-
public verifyDocumentHighlightsOf(startRange: Range, ranges: Range[]) {
2847-
ts.Debug.assert(ts.contains(ranges, startRange));
2848-
const fileNames = unique(ranges, range => range.fileName);
2846+
public verifyDocumentHighlightsOf(startRange: Range, ranges: Range[], options: FourSlashInterface.VerifyDocumentHighlightsOptions | undefined) {
2847+
const fileNames = options && options.filesToSearch || unique(ranges, range => range.fileName);
28492848
this.goToRangeStart(startRange);
28502849
this.verifyDocumentHighlights(ranges, fileNames);
28512850
}
@@ -2868,7 +2867,7 @@ Actual: ${stringify(fullActual)}`);
28682867
}
28692868
}
28702869

2871-
private verifyDocumentHighlights(expectedRanges: Range[], fileNames: string[] = [this.activeFile.fileName]) {
2870+
private verifyDocumentHighlights(expectedRanges: Range[], fileNames: ReadonlyArray<string> = [this.activeFile.fileName]) {
28722871
const documentHighlights = this.getDocumentHighlightsAtCurrentPosition(fileNames) || [];
28732872

28742873
for (const dh of documentHighlights) {
@@ -2880,10 +2879,7 @@ Actual: ${stringify(fullActual)}`);
28802879
for (const fileName of fileNames) {
28812880
const expectedRangesInFile = expectedRanges.filter(r => r.fileName === fileName);
28822881
const highlights = ts.find(documentHighlights, dh => dh.fileName === fileName);
2883-
if (!highlights) {
2884-
this.raiseError(`verifyDocumentHighlights failed - found no highlights in ${fileName}`);
2885-
}
2886-
const spansInFile = highlights.highlightSpans.sort((s1, s2) => s1.textSpan.start - s2.textSpan.start);
2882+
const spansInFile = highlights ? highlights.highlightSpans.sort((s1, s2) => s1.textSpan.start - s2.textSpan.start) : [];
28872883

28882884
if (expectedRangesInFile.length !== spansInFile.length) {
28892885
this.raiseError(`verifyDocumentHighlights failed - In ${fileName}, expected ${expectedRangesInFile.length} highlights, got ${spansInFile.length}`);
@@ -4272,8 +4268,8 @@ namespace FourSlashInterface {
42724268
this.state.verifyRangesWithSameTextAreDocumentHighlights();
42734269
}
42744270

4275-
public documentHighlightsOf(startRange: FourSlash.Range, ranges: FourSlash.Range[]) {
4276-
this.state.verifyDocumentHighlightsOf(startRange, ranges);
4271+
public documentHighlightsOf(startRange: FourSlash.Range, ranges: FourSlash.Range[], options?: VerifyDocumentHighlightsOptions) {
4272+
this.state.verifyDocumentHighlightsOf(startRange, ranges, options);
42774273
}
42784274

42794275
public noDocumentHighlights(startRange: FourSlash.Range) {
@@ -4622,6 +4618,10 @@ namespace FourSlashInterface {
46224618
insertText?: string;
46234619
}
46244620

4621+
export interface VerifyDocumentHighlightsOptions {
4622+
filesToSearch?: ReadonlyArray<string>;
4623+
}
4624+
46254625
export interface NewContentOptions {
46264626
// Exactly one of these should be defined.
46274627
newFileContent?: string;

Diff for: src/services/services.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@ namespace ts {
15821582

15831583
function getDocumentHighlights(fileName: string, position: number, filesToSearch: string[]): DocumentHighlights[] {
15841584
synchronizeHostData();
1585-
const sourceFilesToSearch = map(filesToSearch, f => program.getSourceFile(f));
1585+
const sourceFilesToSearch = map(filesToSearch, f => Debug.assertDefined(program.getSourceFile(f)));
15861586
const sourceFile = getValidSourceFile(fileName);
15871587
return DocumentHighlights.getDocumentHighlights(program, cancellationToken, sourceFile, position, sourceFilesToSearch);
15881588
}

Diff for: tests/cases/fourslash/fourslash.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,9 @@ declare namespace FourSlashInterface {
310310
occurrencesAtPositionCount(expectedCount: number): void;
311311
rangesAreDocumentHighlights(ranges?: Range[]): void;
312312
rangesWithSameTextAreDocumentHighlights(): void;
313-
documentHighlightsOf(startRange: Range, ranges: Range[]): void;
313+
documentHighlightsOf(startRange: Range, ranges: Range[], options?: {
314+
filesToSearch?: ReadonlyArray<string>;
315+
}): void;
314316
completionEntryDetailIs(entryName: string, text: string, documentation?: string, kind?: string, tags?: ts.JSDocTagInfo[]): void;
315317
/**
316318
* This method *requires* a contiguous, complete, and ordered stream of classifications for a file.

0 commit comments

Comments
 (0)