feat(editor): highlighting level of the current file's editor (#7805)

Add Configure Current File Analysis… (editor.highlightingLevel, IDEA's PopupHector, Cmd+Alt+Shift+H / Ctrl+Alt+Shift+H) with the three levels None, Syntax and All Problems:

- All Problems (default): everything as before.
- Syntax: syntax and semantic colouring stay; diagnostics and their scrollbar marks leave the editor (Problems still lists them).
- None: plain text; the lezer highlighter is reconfigured out and semantic tokens are dropped.

The level lives in the editor state next to the other per-editor display overrides (cm/localAppearance.ts) and is not saved.

Co-authored-by: muutot <[email protected]>
Цей коміт міститься в:
Muuтаmuutot authored and GitHub committed 2026-12-04 01:41:21 +08:00
1 джерело 703c33a089
коміт f1876f1260
14 files changed
+218 -1

No files matched your search

+27
Переглянути файл
@@ -163,6 +163,8 @@ import { setLineSeparators, toggleReadOnly } from "./fileAttributes";
import { codeVisionShown, toggleCodeVision } from "./codeVision";
import { breadcrumbsShown, toggleBreadcrumbs } from "./breadcrumbs";
import { appearanceSwitches, localAppearanceSwitches, toggleAppearance, type AppearanceSwitch } from "./gutterMenu";
import { HIGHLIGHTING_LEVEL_ITEMS } from "./cm/localAppearance";
import { showContextMenu } from "@/ui";
import { openNavBarPopup } from "./view/NavBarPopup";
import {
cachedStructureQueries,
@@ -998,6 +1000,31 @@ export function registerEditorActions(): () => void {
localAppearanceSwitch("editor.toggleLocalWhitespaces", "whitespaces"),
localAppearanceSwitch("editor.toggleLocalGutterIcons", "gutterIcons"),
localAppearanceSwitch("editor.toggleLocalIndentGuides", "indentGuides"),
{
// IDEA's PopupHector: the highlighting level of the current file's editor.
id: "editor.highlightingLevel",
title: "Configure Current File Analysis…",
description:
"Choose what the active editor highlights: None, Syntax (colouring without problems) or All Problems.",
category: "View",
keywords: ["highlighting level", "inspections", "syntax", "none", "problems", "hector"],
when: "textEditorActive",
enabled: () => !focusedCellEditor() && !!activeSlot(),
run: () => {
const slot = activeSlot();
if (!slot) return;
const current = slot.highlightingLevel();
showContextMenu(
"center",
HIGHLIGHTING_LEVEL_ITEMS.map((item) => ({
label: item.label,
checked: item.id === current,
run: () => slot.setHighlightingLevel(item.id),
})),
{ title: "Highlighting Level" },
);
},
},
{
id: "editor.toggleBreadcrumbs",
title: "Show Breadcrumbs",
+31
Переглянути файл
@@ -0,0 +1,31 @@
import { afterEach, describe, expect, it } from "vitest";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import {
HIGHLIGHTING_LEVEL_ITEMS,
highlightingLevelField,
highlightingLevelOf,
setHighlightingLevel,
} from "./localAppearance";
const views: EditorView[] = [];
afterEach(() => views.splice(0).forEach((view) => view.destroy()));
describe("highlightingLevelField", () => {
it("starts at All Problems and switches through the three levels", () => {
let state = EditorState.create({ doc: "const x = 1;", extensions: [highlightingLevelField] });
expect(highlightingLevelOf(state)).toBe("inspections");
for (const level of ["syntax", "none", "inspections"] as const) {
state = state.update({ effects: setHighlightingLevel.of(level) }).state;
expect(highlightingLevelOf(state)).toBe(level);
}
});
it("labels the three levels in IDEA's order and wording", () => {
expect(HIGHLIGHTING_LEVEL_ITEMS.map((item) => item.label)).toEqual(["None", "Syntax", "All Problems"]);
});
it("reports All Problems for a state without the field", () => {
expect(highlightingLevelOf(EditorState.create({ doc: "x" }))).toBe("inspections");
});
});
+28
Переглянути файл
@@ -51,3 +51,31 @@ export function gutterIconsOn(
const local = state?.field(localAppearanceField, false)?.gutterIcons;
return (local ?? prefs.showGutterIcons) && (!group || !prefs.gutterIconsDisabled.includes(group));
}
/**
* The editor's highlighting level (IDEA's Configure Current File Analysis): All Problems
* (the default), Syntax (colouring without problems) and None (plain text). It lives in
* the editor state and is not saved.
*/
export type HighlightingLevel = "inspections" | "syntax" | "none";
/** The chooser's items in IDEA's order and wording. */
export const HIGHLIGHTING_LEVEL_ITEMS: readonly { id: HighlightingLevel; label: string }[] = [
{ id: "none", label: "None" },
{ id: "syntax", label: "Syntax" },
{ id: "inspections", label: "All Problems" },
];
export const setHighlightingLevel = StateEffect.define<HighlightingLevel>();
export const highlightingLevelField = StateField.define<HighlightingLevel>({
create: () => "inspections",
update(value, tr) {
for (const effect of tr.effects) if (effect.is(setHighlightingLevel)) value = effect.value;
return value;
},
});
/** The level of the editor `state` belongs to; anything without the field highlights everything. */
export function highlightingLevelOf(state: EditorState | undefined): HighlightingLevel {
return state?.field(highlightingLevelField, false) ?? "inspections";
}
+5 -1
Переглянути файл
@@ -37,6 +37,7 @@ import { changeLane, overviewLane } from "./lanes";
import { cornerInset } from "./cornerInset";
import { foldingExtension } from "./folding";
import { syntaxHighlightField } from "./syntaxHighlight";
import { highlightingLevelField } from "./localAppearance";
import { indentGuides, rainbowIndentGuides } from "./indentGuides";
import { rainbowBrackets } from "./rainbowBrackets";
import { scopeContext } from "./scopeContext";
@@ -91,6 +92,8 @@ export const compartments = {
caret: new Compartment(),
prefs: new Compartment(),
distractionFree: new Compartment(),
/** The lezer highlighter, so the None highlighting level can take all syntax colouring out. */
highlighting: new Compartment(),
};
/** Editor-group settings of an `editor` settings object (validated, with defaults). */
@@ -460,7 +463,8 @@ export function baseExtensions(o: SetupOptions): Extension {
primarySelectionYield,
indentOnInput(),
syntaxHighlightField,
syntaxHighlighting(openideaHighlightStyle),
highlightingLevelField,
compartments.highlighting.of(syntaxHighlighting(openideaHighlightStyle)),
closeBrackets(),
caretRowHighlight,
fontZoom,
+16
Переглянути файл
@@ -34,6 +34,9 @@ import {
import { showContextMenu } from "@/ui";
import { appearanceEffects, baseExtensions, compartments, contributionExtensions, prefsOf } from "../cm/setup";
import { effectiveAppearance, localAppearanceField, setLocalAppearance, type LocalAppearanceKey } from "../cm/prefs";
import { highlightingLevelOf, setHighlightingLevel, type HighlightingLevel } from "../cm/localAppearance";
import { openideaHighlightStyle } from "../cm/highlight";
import { syntaxHighlighting } from "@codemirror/language";
import { markdownImageFolds } from "../cm/folding";
import { gutterMenuItems, inGutterMenuArea } from "../gutterMenu";
import { offerMakeWritable } from "../fileAttributes";
@@ -198,6 +201,19 @@ export class EditorSlot {
return this.view ?? this.kept;
}
/** The per-editor highlighting level (IDEA's HighlightingLevelManager), defaulting to All Problems. */
highlightingLevel(): HighlightingLevel {
return highlightingLevelOf(this.currentState());
}
/** Switch the highlighting level and take the lezer highlighter out at None. */
setHighlightingLevel(level: HighlightingLevel): void {
this.reconfigure([
setHighlightingLevel.of(level),
compartments.highlighting.reconfigure(level === "none" ? [] : syntaxHighlighting(openideaHighlightStyle)),
]);
}
get state(): EditorState {
return this.live?.state ?? this.stored;
}
+11
Переглянути файл
@@ -16,6 +16,7 @@ import { intentionsChanged, isLibraryPath, scrollbarMarks, shownAgain, type Scro
import { inlineBackground } from "@/editor/core/cm/inlineBg";
import { rangeToOffsets } from "./positions";
import { lspDoc } from "./lsp";
import { highlightingLevelOf, setHighlightingLevel } from "@/editor/core/cm/localAppearance";
import { showHint } from "./hint";
import { editorPrefs } from "@/platform/editorSettings";
import {
@@ -280,6 +281,15 @@ const diagnosticsSync = ViewPlugin.fromClass(
const path = this.view.state.facet(lspDoc)?.path;
this.skipped = !this.view.dom.isConnected;
if (!path || this.skipped) return;
// IDEA's Syntax and None levels show no diagnostics in the editor; Problems keeps counting.
if (highlightingLevelOf(this.view.state) !== "inspections") {
const tr = setDiagnostics(this.view.state, []);
this.view.dispatch({
...tr,
effects: [...[tr.effects ?? []].flat(), setMarks.of(NO_MARKS)],
});
return;
}
const profile = inspectionProfile();
const sources = [...(byPath.get(path) ?? [])];
const items = sources.flatMap(([source, list]) =>
@@ -300,6 +310,7 @@ const diagnosticsSync = ViewPlugin.fromClass(
}
update(u: ViewUpdate) {
if (u.docChanged) for (const anchor of this.anchors.values()) anchor.changes = anchor.changes.compose(u.changes);
if (u.transactions.some((tr) => tr.effects.some((e) => e.is(setHighlightingLevel)))) this.schedule();
if (this.skipped && shownAgain(u)) this.schedule();
}
destroy() {
+20
Переглянути файл
@@ -23,6 +23,7 @@ import { debounce, docOf, isUnavailable, lspRequest } from "../lsp";
import { featureBackoff, onLspRefresh } from "../availability";
import { languageSemantics, type SemanticRange } from "./languages";
import { previewHighlights } from "@/editor/core/cm/renderLineContent";
import { highlightingLevelOf, setHighlightingLevel } from "@/editor/core/cm/localAppearance";
// Language folders register their semantics by importing this module; lookups
// elsewhere go through the re-exported `languageSemantics`.
@@ -258,6 +259,17 @@ const semanticPlugin = ViewPlugin.fromClass(
}
const rainbow = editorPrefs().semanticHighlighting;
const docstringFormat = editorPrefs().docstringFormat;
// The None highlighting level takes the semantic colouring out too; coming back
// re-decorates from the cached answer without a new request.
if (u.transactions.some((tr) => tr.effects.some((e) => e.is(setHighlightingLevel)))) {
const last = this.last;
queueMicrotask(() => {
if (this.destroyed) return;
if (last && last.doc === this.view.state.doc) this.decorate(last.doc, last.tokens);
else this.refresh();
});
return;
}
if (rainbow === this.rainbow && docstringFormat === this.docstringFormat) return;
this.rainbow = rainbow;
this.docstringFormat = docstringFormat;
@@ -270,6 +282,10 @@ const semanticPlugin = ViewPlugin.fromClass(
else this.refresh();
}
refreshSyntax(doc: Text) {
if (highlightingLevelOf(this.view.state) === "none") {
this.view.dispatch({ effects: setSemantic.of(Decoration.none) });
return;
}
const tree = syntaxTree(this.view.state);
const semantics = languageSemantics(docOf(this.view)?.language);
const symbols = semantics?.symbolKinds ? semantics.symbolKinds(tree, doc) : undefined;
@@ -304,6 +320,10 @@ const semanticPlugin = ViewPlugin.fromClass(
}
decorate(doc: Text, tokens: SemanticTokens | null) {
this.last = { doc, tokens };
if (highlightingLevelOf(this.view.state) === "none") {
this.view.dispatch({ effects: setSemantic.of(Decoration.none) });
return;
}
const ranges = semanticRanges(
doc,
tokens ?? { data: [], tokenTypes: [], tokenModifiers: [] },