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
This commit is contained in:
Orta Therox
2021-05-07 20:02:26 +01:00
committed by GitHub
parent cfed79b7a2
commit 6bb481c9d3
7 changed files with 26 additions and 26 deletions
+4 -2
View File
@@ -772,8 +772,10 @@ namespace ts.server {
return notImplemented();
}
getEncodedSemanticClassifications(_fileName: string, _span: TextSpan, _format?: SemanticClassificationFormat): Classifications {
return notImplemented();
getEncodedSemanticClassifications(file: string, span: TextSpan, format?: SemanticClassificationFormat): Classifications {
const request = this.processRequest<protocol.EncodedSemanticClassificationsRequest>(protocol.CommandTypes.EncodedSemanticClassificationsFull, { file, start: span.start, length: span.length, format });
const r = this.processResponse<protocol.EncodedSemanticClassificationsResponse>(request);
return r.body!;
}
private convertCallHierarchyItem(item: protocol.CallHierarchyItem): CallHierarchyItem {
+6
View File
@@ -2746,6 +2746,12 @@ namespace FourSlash {
// fs.writeFileSync(testfilePath, newfile);
}
public verifyEncodedSemanticClassificationsLength(format: ts.SemanticClassificationFormat, expected: number) {
const actual = this.languageService.getEncodedSemanticClassifications(this.activeFile.fileName, ts.createTextSpan(0, this.activeFile.content.length), format);
if (actual.spans.length !== expected) {
this.raiseError(`encodedSemanticClassificationsLength failed - expected total spans to be ${expected} got ${actual.spans.length}`);
}
}
public verifySemanticClassifications(format: ts.SemanticClassificationFormat, expected: { classificationType: string | number; text?: string }[]) {
const actual = this.languageService.getSemanticClassifications(this.activeFile.fileName,
+4
View File
@@ -537,6 +537,10 @@ namespace FourSlashInterface {
this.state.verifySyntacticClassifications(classifications);
}
public encodedSemanticClassificationsLength(format: ts.SemanticClassificationFormat, length: number) {
this.state.verifyEncodedSemanticClassificationsLength(format, length);
}
/**
* This method *requires* an ordered stream of classifications for a file, and spans are highly recommended.
*/
-13
View File
@@ -1882,17 +1882,8 @@ namespace ts {
return NavigationBar.getNavigationTree(syntaxTreeCache.getCurrentSourceFile(fileName), cancellationToken);
}
function isTsOrTsxFile(fileName: string): boolean {
const kind = getScriptKind(fileName, host);
return kind === ScriptKind.TS || kind === ScriptKind.TSX;
}
function getSemanticClassifications(fileName: string, span: TextSpan): ClassifiedSpan[];
function getSemanticClassifications(fileName: string, span: TextSpan, format?: SemanticClassificationFormat): ClassifiedSpan[] | ClassifiedSpan2020[] {
if (!isTsOrTsxFile(fileName)) {
// do not run semantic classification on non-ts-or-tsx files
return [];
}
synchronizeHostData();
const responseFormat = format || SemanticClassificationFormat.Original;
@@ -1905,10 +1896,6 @@ namespace ts {
}
function getEncodedSemanticClassifications(fileName: string, span: TextSpan, format?: SemanticClassificationFormat): Classifications {
if (!isTsOrTsxFile(fileName)) {
// do not run semantic classification on non-ts-or-tsx files
return { spans: [], endOfLineState: EndOfLineState.None };
}
synchronizeHostData();
const responseFormat = format || SemanticClassificationFormat.Original;
+2
View File
@@ -358,6 +358,8 @@ declare namespace FourSlashInterface {
rangesAreDocumentHighlights(ranges?: Range[], options?: VerifyDocumentHighlightsOptions): void;
rangesWithSameTextAreDocumentHighlights(): void;
documentHighlightsOf(startRange: Range, ranges: Range[], options?: VerifyDocumentHighlightsOptions): void;
/** Prefer semanticClassificationsAre for more descriptive tests */
encodedSemanticClassificationsLength(format: "original" | "2020", length: number)
/**
* This method *requires* a contiguous, complete, and ordered stream of classifications for a file.
*/
@@ -1,11 +0,0 @@
/// <reference path="fourslash.ts"/>
// @Filename: app.js
//// function foo() {
//// }
//// let x = 1;
// no semantic classification in js file
verify.semanticClassificationsAre("original", );
@@ -0,0 +1,10 @@
/// <reference path="../fourslash.ts"/>
//// @Filename: index.js
////
//// var Thing = 0;
//// Thing.toExponential();
// This test validates that an arbitrary JS file gets
// encoded semantic classifications when requested
verify.encodedSemanticClassificationsLength("2020", 9)