Skip to content

Commit 6bb481c

Browse files
author
Orta Therox
authored
Support semantic highlights for JS files (#43992)
* Switches from never allowing semantic highlight on JS to only doing it if we have a valid source file * Adds a way to test and validate that an arbitrary JS file gets semantic classification results * Revert to just dropping the if statement
1 parent cfed79b commit 6bb481c

File tree

7 files changed

+26
-26
lines changed

7 files changed

+26
-26
lines changed

src/harness/client.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,10 @@ namespace ts.server {
772772
return notImplemented();
773773
}
774774

775-
getEncodedSemanticClassifications(_fileName: string, _span: TextSpan, _format?: SemanticClassificationFormat): Classifications {
776-
return notImplemented();
775+
getEncodedSemanticClassifications(file: string, span: TextSpan, format?: SemanticClassificationFormat): Classifications {
776+
const request = this.processRequest<protocol.EncodedSemanticClassificationsRequest>(protocol.CommandTypes.EncodedSemanticClassificationsFull, { file, start: span.start, length: span.length, format });
777+
const r = this.processResponse<protocol.EncodedSemanticClassificationsResponse>(request);
778+
return r.body!;
777779
}
778780

779781
private convertCallHierarchyItem(item: protocol.CallHierarchyItem): CallHierarchyItem {

src/harness/fourslashImpl.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,6 +2746,12 @@ namespace FourSlash {
27462746
// fs.writeFileSync(testfilePath, newfile);
27472747
}
27482748

2749+
public verifyEncodedSemanticClassificationsLength(format: ts.SemanticClassificationFormat, expected: number) {
2750+
const actual = this.languageService.getEncodedSemanticClassifications(this.activeFile.fileName, ts.createTextSpan(0, this.activeFile.content.length), format);
2751+
if (actual.spans.length !== expected) {
2752+
this.raiseError(`encodedSemanticClassificationsLength failed - expected total spans to be ${expected} got ${actual.spans.length}`);
2753+
}
2754+
}
27492755

27502756
public verifySemanticClassifications(format: ts.SemanticClassificationFormat, expected: { classificationType: string | number; text?: string }[]) {
27512757
const actual = this.languageService.getSemanticClassifications(this.activeFile.fileName,

src/harness/fourslashInterfaceImpl.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ namespace FourSlashInterface {
537537
this.state.verifySyntacticClassifications(classifications);
538538
}
539539

540+
public encodedSemanticClassificationsLength(format: ts.SemanticClassificationFormat, length: number) {
541+
this.state.verifyEncodedSemanticClassificationsLength(format, length);
542+
}
543+
540544
/**
541545
* This method *requires* an ordered stream of classifications for a file, and spans are highly recommended.
542546
*/

src/services/services.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,17 +1882,8 @@ namespace ts {
18821882
return NavigationBar.getNavigationTree(syntaxTreeCache.getCurrentSourceFile(fileName), cancellationToken);
18831883
}
18841884

1885-
function isTsOrTsxFile(fileName: string): boolean {
1886-
const kind = getScriptKind(fileName, host);
1887-
return kind === ScriptKind.TS || kind === ScriptKind.TSX;
1888-
}
1889-
18901885
function getSemanticClassifications(fileName: string, span: TextSpan): ClassifiedSpan[];
18911886
function getSemanticClassifications(fileName: string, span: TextSpan, format?: SemanticClassificationFormat): ClassifiedSpan[] | ClassifiedSpan2020[] {
1892-
if (!isTsOrTsxFile(fileName)) {
1893-
// do not run semantic classification on non-ts-or-tsx files
1894-
return [];
1895-
}
18961887
synchronizeHostData();
18971888

18981889
const responseFormat = format || SemanticClassificationFormat.Original;
@@ -1905,10 +1896,6 @@ namespace ts {
19051896
}
19061897

19071898
function getEncodedSemanticClassifications(fileName: string, span: TextSpan, format?: SemanticClassificationFormat): Classifications {
1908-
if (!isTsOrTsxFile(fileName)) {
1909-
// do not run semantic classification on non-ts-or-tsx files
1910-
return { spans: [], endOfLineState: EndOfLineState.None };
1911-
}
19121899
synchronizeHostData();
19131900

19141901
const responseFormat = format || SemanticClassificationFormat.Original;

tests/cases/fourslash/fourslash.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ declare namespace FourSlashInterface {
358358
rangesAreDocumentHighlights(ranges?: Range[], options?: VerifyDocumentHighlightsOptions): void;
359359
rangesWithSameTextAreDocumentHighlights(): void;
360360
documentHighlightsOf(startRange: Range, ranges: Range[], options?: VerifyDocumentHighlightsOptions): void;
361+
/** Prefer semanticClassificationsAre for more descriptive tests */
362+
encodedSemanticClassificationsLength(format: "original" | "2020", length: number)
361363
/**
362364
* This method *requires* a contiguous, complete, and ordered stream of classifications for a file.
363365
*/

tests/cases/fourslash/semanticClassificationJs.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="../fourslash.ts"/>
2+
3+
//// @Filename: index.js
4+
////
5+
//// var Thing = 0;
6+
//// Thing.toExponential();
7+
8+
// This test validates that an arbitrary JS file gets
9+
// encoded semantic classifications when requested
10+
verify.encodedSemanticClassificationsLength("2020", 9)

0 commit comments

Comments
 (0)