Skip to content

Commit 502de14

Browse files
authored
Disregard semantic classification tokens from <template> portions of .gts files (#873)
* add comment about volar labs * disable semantic tokens on gts via ts plugin * something wokring
1 parent e5adbce commit 502de14

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

packages/core/src/transform/template/map-template-contents.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,12 @@ export function mapTemplateContents(
232232
hbsRange,
233233
mappings,
234234
source,
235-
codeFeaturesForNode ?? codeFeaturesProxy.all,
235+
236+
// Prevent TS's semantic classifications (used for semantic highlighting, see
237+
// https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide) from being
238+
// source-mapped back to the glimmer template. These might be useful to reinstate at some
239+
// point in the future but by default tends to make the highlighting in gts files look wrong.
240+
codeFeaturesForNode ?? codeFeaturesProxy.withoutHighlight,
236241
),
237242
);
238243
segmentsStack[0].push(...segments);

packages/core/src/volar/language-server.ts

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ connection.onInitialize((params) => {
7575
return ls;
7676
}
7777
}
78+
// TODO: this branch is hit when running Volar Labs and currently breaks. Figure out
79+
// how to reinstate a "simple" LS without a tsconfig.
7880
return (simpleLs ??= createLs(server, undefined));
7981
},
8082
getExistingLanguageServices() {

packages/tsserver-plugin/src/typescript-server-plugin.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ function proxyLanguageServiceForGlint<T>(
100100
// case 'getDefinitionAndBoundSpan': return getDefinitionAndBoundSpan(ts, language, languageService, glintOptions, asScriptId, target[p]);
101101
// case 'getQuickInfoAtPosition': return getQuickInfoAtPosition(ts, target, target[p]);
102102
// TS plugin only
103-
// case 'getEncodedSemanticClassifications': return getEncodedSemanticClassifications(ts, language, target, asScriptId, target[p]);
103+
104+
// Left as an example in case we want to augment semantic classification in .gts files.
105+
// e.g. Vue does this to semantically classify Component names as `class` tokens.
106+
// case 'getEncodedSemanticClassifications':
107+
// return getEncodedSemanticClassifications(ts, language, target, asScriptId, target[p]);
104108

105109
case 'getSemanticDiagnostics':
106110
return getSemanticDiagnostics(ts, language, languageService, asScriptId, target[p]);
@@ -203,3 +207,37 @@ function getSemanticDiagnostics<T>(
203207
return augmentedTsDiagnostics;
204208
};
205209
}
210+
211+
const windowsPathReg = /\\/g;
212+
213+
/**
214+
* Return semantic tokens for semantic highlighting:
215+
* https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide
216+
function getEncodedSemanticClassifications<T>(
217+
ts: typeof import('typescript'),
218+
language: any, // Language<T>,
219+
languageService: ts.LanguageService,
220+
asScriptId: (fileName: string) => T,
221+
getEncodedSemanticClassifications: ts.LanguageService['getEncodedSemanticClassifications'],
222+
): ts.LanguageService['getEncodedSemanticClassifications'] {
223+
return (filePath, span, format) => {
224+
const fileName = filePath.replace(windowsPathReg, '/');
225+
const result = getEncodedSemanticClassifications(fileName, span, format);
226+
const sourceScript = language.scripts.get(asScriptId(fileName));
227+
const root = sourceScript?.generated?.root;
228+
if (root instanceof VirtualGtsCode) {
229+
// This would remove all semantic highlighting from .gts files, including the TS parts
230+
// outside of the `<template>` tags, which is probably undesirable.
231+
// result.spans = [];
232+
233+
// We can push span to the end of the array to override previous entries.
234+
// result.spans.push(
235+
// 0,
236+
// 100,
237+
// 256, // class
238+
// );
239+
}
240+
return result;
241+
};
242+
}
243+
*/

0 commit comments

Comments
 (0)