Skip to content

Commit cb1ce16

Browse files
committed
Fixes microsoft#94211: Disable semantic highlighting when the setting asks to disable it or when a theme did not opt in
1 parent ee0ef37 commit cb1ce16

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

src/vs/editor/common/services/modelServiceImpl.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,17 @@ export interface ILineSequence {
577577
getLineContent(lineNumber: number): string;
578578
}
579579

580-
class SemanticColoringFeature extends Disposable {
580+
export const SEMANTIC_HIGHLIGHTING_SETTING_ID = 'editor.semanticHighlighting';
581+
582+
export function isSemanticColoringEnabled(model: ITextModel, themeService: IThemeService, configurationService: IConfigurationService): boolean {
583+
if (!themeService.getColorTheme().semanticHighlighting) {
584+
return false;
585+
}
586+
const options = configurationService.getValue<IEditorSemanticHighlightingOptions>(SEMANTIC_HIGHLIGHTING_SETTING_ID, { overrideIdentifier: model.getLanguageIdentifier().language, resource: model.uri });
587+
return Boolean(options && options.enabled);
588+
}
581589

582-
private static readonly SETTING_ID = 'editor.semanticHighlighting';
590+
class SemanticColoringFeature extends Disposable {
583591

584592
private readonly _watchers: Record<string, ModelSemanticColoring>;
585593
private readonly _semanticStyling: SemanticStyling;
@@ -589,13 +597,6 @@ class SemanticColoringFeature extends Disposable {
589597
this._watchers = Object.create(null);
590598
this._semanticStyling = semanticStyling;
591599

592-
const isSemanticColoringEnabled = (model: ITextModel) => {
593-
if (!themeService.getColorTheme().semanticHighlighting) {
594-
return false;
595-
}
596-
const options = configurationService.getValue<IEditorSemanticHighlightingOptions>(SemanticColoringFeature.SETTING_ID, { overrideIdentifier: model.getLanguageIdentifier().language, resource: model.uri });
597-
return options && options.enabled;
598-
};
599600
const register = (model: ITextModel) => {
600601
this._watchers[model.uri.toString()] = new ModelSemanticColoring(model, themeService, this._semanticStyling);
601602
};
@@ -606,7 +607,7 @@ class SemanticColoringFeature extends Disposable {
606607
const handleSettingOrThemeChange = () => {
607608
for (let model of modelService.getModels()) {
608609
const curr = this._watchers[model.uri.toString()];
609-
if (isSemanticColoringEnabled(model)) {
610+
if (isSemanticColoringEnabled(model, themeService, configurationService)) {
610611
if (!curr) {
611612
register(model);
612613
}
@@ -618,7 +619,7 @@ class SemanticColoringFeature extends Disposable {
618619
}
619620
};
620621
this._register(modelService.onModelAdded((model) => {
621-
if (isSemanticColoringEnabled(model)) {
622+
if (isSemanticColoringEnabled(model, themeService, configurationService)) {
622623
register(model);
623624
}
624625
}));
@@ -629,7 +630,7 @@ class SemanticColoringFeature extends Disposable {
629630
}
630631
}));
631632
this._register(configurationService.onDidChangeConfiguration(e => {
632-
if (e.affectsConfiguration(SemanticColoringFeature.SETTING_ID)) {
633+
if (e.affectsConfiguration(SEMANTIC_HIGHLIGHTING_SETTING_ID)) {
633634
handleSettingOrThemeChange();
634635
}
635636
}));

src/vs/editor/contrib/viewportSemanticTokens/viewportSemanticTokens.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import { ITextModel } from 'vs/editor/common/model';
1313
import { DocumentRangeSemanticTokensProviderRegistry, DocumentRangeSemanticTokensProvider, SemanticTokens } from 'vs/editor/common/modes';
1414
import { IModelService } from 'vs/editor/common/services/modelService';
1515
import { toMultilineTokens2, SemanticTokensProviderStyling } from 'vs/editor/common/services/semanticTokensProviderStyling';
16+
import { IThemeService } from 'vs/platform/theme/common/themeService';
17+
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
18+
import { isSemanticColoringEnabled, SEMANTIC_HIGHLIGHTING_SETTING_ID } from 'vs/editor/common/services/modelServiceImpl';
1619

1720
class ViewportSemanticTokensContribution extends Disposable implements IEditorContribution {
1821

@@ -28,7 +31,9 @@ class ViewportSemanticTokensContribution extends Disposable implements IEditorCo
2831

2932
constructor(
3033
editor: ICodeEditor,
31-
@IModelService private readonly _modelService: IModelService
34+
@IModelService private readonly _modelService: IModelService,
35+
@IThemeService private readonly _themeService: IThemeService,
36+
@IConfigurationService private readonly _configurationService: IConfigurationService
3237
) {
3338
super();
3439
this._editor = editor;
@@ -49,6 +54,16 @@ class ViewportSemanticTokensContribution extends Disposable implements IEditorCo
4954
this._cancelAll();
5055
this._tokenizeViewport.schedule();
5156
}));
57+
this._register(this._configurationService.onDidChangeConfiguration(e => {
58+
if (e.affectsConfiguration(SEMANTIC_HIGHLIGHTING_SETTING_ID)) {
59+
this._cancelAll();
60+
this._tokenizeViewport.schedule();
61+
}
62+
}));
63+
this._register(this._themeService.onDidColorThemeChange(() => {
64+
this._cancelAll();
65+
this._tokenizeViewport.schedule();
66+
}));
5267
}
5368

5469
private static _getSemanticColoringProvider(model: ITextModel): DocumentRangeSemanticTokensProvider | null {
@@ -80,6 +95,9 @@ class ViewportSemanticTokensContribution extends Disposable implements IEditorCo
8095
if (model.hasSemanticTokens()) {
8196
return;
8297
}
98+
if (!isSemanticColoringEnabled(model, this._themeService, this._configurationService)) {
99+
return;
100+
}
83101
const provider = ViewportSemanticTokensContribution._getSemanticColoringProvider(model);
84102
if (!provider) {
85103
return;

0 commit comments

Comments
 (0)