diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 79dd86ccb092d..4b9816f9fcb05 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -2819,7 +2819,7 @@ Actual: ${stringify(fullActual)}`); } } - private getDocumentHighlightsAtCurrentPosition(fileNamesToSearch: string[]) { + private getDocumentHighlightsAtCurrentPosition(fileNamesToSearch: ReadonlyArray) { const filesToSearch = fileNamesToSearch.map(name => ts.combinePaths(this.basePath, name)); return this.languageService.getDocumentHighlights(this.activeFile.fileName, this.currentCaretPosition, filesToSearch); } @@ -2843,9 +2843,8 @@ Actual: ${stringify(fullActual)}`); this.rangesByText().forEach(ranges => this.verifyRangesAreDocumentHighlights(ranges)); } - public verifyDocumentHighlightsOf(startRange: Range, ranges: Range[]) { - ts.Debug.assert(ts.contains(ranges, startRange)); - const fileNames = unique(ranges, range => range.fileName); + public verifyDocumentHighlightsOf(startRange: Range, ranges: Range[], options: FourSlashInterface.VerifyDocumentHighlightsOptions | undefined) { + const fileNames = options && options.filesToSearch || unique(ranges, range => range.fileName); this.goToRangeStart(startRange); this.verifyDocumentHighlights(ranges, fileNames); } @@ -2868,7 +2867,7 @@ Actual: ${stringify(fullActual)}`); } } - private verifyDocumentHighlights(expectedRanges: Range[], fileNames: string[] = [this.activeFile.fileName]) { + private verifyDocumentHighlights(expectedRanges: Range[], fileNames: ReadonlyArray = [this.activeFile.fileName]) { const documentHighlights = this.getDocumentHighlightsAtCurrentPosition(fileNames) || []; for (const dh of documentHighlights) { @@ -2880,10 +2879,7 @@ Actual: ${stringify(fullActual)}`); for (const fileName of fileNames) { const expectedRangesInFile = expectedRanges.filter(r => r.fileName === fileName); const highlights = ts.find(documentHighlights, dh => dh.fileName === fileName); - if (!highlights) { - this.raiseError(`verifyDocumentHighlights failed - found no highlights in ${fileName}`); - } - const spansInFile = highlights.highlightSpans.sort((s1, s2) => s1.textSpan.start - s2.textSpan.start); + const spansInFile = highlights ? highlights.highlightSpans.sort((s1, s2) => s1.textSpan.start - s2.textSpan.start) : []; if (expectedRangesInFile.length !== spansInFile.length) { this.raiseError(`verifyDocumentHighlights failed - In ${fileName}, expected ${expectedRangesInFile.length} highlights, got ${spansInFile.length}`); @@ -4272,8 +4268,8 @@ namespace FourSlashInterface { this.state.verifyRangesWithSameTextAreDocumentHighlights(); } - public documentHighlightsOf(startRange: FourSlash.Range, ranges: FourSlash.Range[]) { - this.state.verifyDocumentHighlightsOf(startRange, ranges); + public documentHighlightsOf(startRange: FourSlash.Range, ranges: FourSlash.Range[], options?: VerifyDocumentHighlightsOptions) { + this.state.verifyDocumentHighlightsOf(startRange, ranges, options); } public noDocumentHighlights(startRange: FourSlash.Range) { @@ -4622,6 +4618,10 @@ namespace FourSlashInterface { insertText?: string; } + export interface VerifyDocumentHighlightsOptions { + filesToSearch?: ReadonlyArray; + } + export interface NewContentOptions { // Exactly one of these should be defined. newFileContent?: string; diff --git a/src/services/services.ts b/src/services/services.ts index ddb869624a858..422248465c9d9 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1582,7 +1582,7 @@ namespace ts { function getDocumentHighlights(fileName: string, position: number, filesToSearch: string[]): DocumentHighlights[] { synchronizeHostData(); - const sourceFilesToSearch = map(filesToSearch, f => program.getSourceFile(f)); + const sourceFilesToSearch = map(filesToSearch, f => Debug.assertDefined(program.getSourceFile(f))); const sourceFile = getValidSourceFile(fileName); return DocumentHighlights.getDocumentHighlights(program, cancellationToken, sourceFile, position, sourceFilesToSearch); } diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 774c2191fd069..1d7cb0810f4de 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -310,7 +310,9 @@ declare namespace FourSlashInterface { occurrencesAtPositionCount(expectedCount: number): void; rangesAreDocumentHighlights(ranges?: Range[]): void; rangesWithSameTextAreDocumentHighlights(): void; - documentHighlightsOf(startRange: Range, ranges: Range[]): void; + documentHighlightsOf(startRange: Range, ranges: Range[], options?: { + filesToSearch?: ReadonlyArray; + }): void; completionEntryDetailIs(entryName: string, text: string, documentation?: string, kind?: string, tags?: ts.JSDocTagInfo[]): void; /** * This method *requires* a contiguous, complete, and ordered stream of classifications for a file.