diff --git a/e2e/highlightingLevel.spec.ts b/e2e/highlightingLevel.spec.ts new file mode 100644 index 0000000000..b8f6a36aaf --- /dev/null +++ b/e2e/highlightingLevel.spec.ts @@ -0,0 +1,68 @@ +import { expect, openFromTree, stubDesktop, test } from "./fixtures"; + +const FILE = "/dev/acme-api/build.gradle.kts"; + +test("highlighting level: Configure Current File Analysis switches what the editor highlights", async ({ + workbench: page, +}) => { + const editor = await openFromTree(page, "build.gradle.kts"); + await stubDesktop(page); + await editor.locator(".cm-line", { hasText: "val order: Order" }).click(); + await editor.locator(".cm-content").focus(); + await page.evaluate((file) => { + window.__ipc.emit({ + type: "diagnostics", + path: file, + source: "kotlin", + items: [ + { + range: { start: { line: 20, col: 19 }, end: { line: 20, col: 24 } }, + severity: "error", + message: "Unresolved reference: Order", + source: "kotlin", + code: null, + }, + ], + }); + }, FILE); + const squiggle = editor.locator(".cm-lintRange-error"); + await expect(squiggle).toHaveText("Order", { timeout: 15000 }); + + const menu = page.getByRole("menu", { name: /^Highlighting Level$/ }); + const choose = async (level: string) => { + await page.evaluate(async () => { + const path = "/src/platform/actions.ts"; + const { executeAction } = await import(/* @vite-ignore */ path); + await executeAction("editor.highlightingLevel"); + }); + await menu.getByRole("menuitemcheckbox", { name: level, exact: true }).click(); + }; + + // The chooser starts at IDEA's All Problems. + await page.evaluate(async () => { + const path = "/src/platform/actions.ts"; + const { executeAction } = await import(/* @vite-ignore */ path); + await executeAction("editor.highlightingLevel"); + }); + await expect(menu.getByRole("menuitemcheckbox", { name: "All Problems", exact: true })).toHaveAttribute( + "aria-checked", + "true", + ); + await page.keyboard.press("Escape"); + + // Syntax keeps the colouring and takes the diagnostics out of the editor. + await choose("Syntax"); + await expect(squiggle).toHaveCount(0); + + // None also reconfigures the lezer highlighter out: a line without the caret keeps no spans. + await choose("None"); + await editor.locator(".cm-content").focus(); + await page.keyboard.press("ArrowUp"); + const plainLine = editor.locator(".cm-content .cm-line").filter({ hasText: "// Paged order lookup" }).first(); + await expect(plainLine).toBeVisible(); + await expect(plainLine.locator("span")).toHaveCount(0); + + // Back to All Problems: the squiggle returns without a new diagnostics event. + await choose("All Problems"); + await expect(squiggle).toHaveText("Order"); +}); diff --git a/src/editor/core/actions.ts b/src/editor/core/actions.ts index 81912451f4..bdc48b9427 100644 --- a/src/editor/core/actions.ts +++ b/src/editor/core/actions.ts @@ -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", diff --git a/src/editor/core/cm/highlightingLevel.test.ts b/src/editor/core/cm/highlightingLevel.test.ts new file mode 100644 index 0000000000..74d9156ec2 --- /dev/null +++ b/src/editor/core/cm/highlightingLevel.test.ts @@ -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"); + }); +}); diff --git a/src/editor/core/cm/localAppearance.ts b/src/editor/core/cm/localAppearance.ts index 908b9e9f6a..c2edb695d3 100644 --- a/src/editor/core/cm/localAppearance.ts +++ b/src/editor/core/cm/localAppearance.ts @@ -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(); +export const highlightingLevelField = StateField.define({ + 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"; +} diff --git a/src/editor/core/cm/setup.ts b/src/editor/core/cm/setup.ts index 95fa53f8ef..deb020bfa0 100644 --- a/src/editor/core/cm/setup.ts +++ b/src/editor/core/cm/setup.ts @@ -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, diff --git a/src/editor/core/model/slot.ts b/src/editor/core/model/slot.ts index ebf78ceaf3..647fc73916 100644 --- a/src/editor/core/model/slot.ts +++ b/src/editor/core/model/slot.ts @@ -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; } diff --git a/src/editor/lsp/diagnostics.ts b/src/editor/lsp/diagnostics.ts index 04739da48f..578dbc41b0 100644 --- a/src/editor/lsp/diagnostics.ts +++ b/src/editor/lsp/diagnostics.ts @@ -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() { diff --git a/src/editor/lsp/semantic/index.ts b/src/editor/lsp/semantic/index.ts index 7d256d78de..39091bdb43 100644 --- a/src/editor/lsp/semantic/index.ts +++ b/src/editor/lsp/semantic/index.ts @@ -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: [] }, diff --git a/src/platform/ideaActions.ts b/src/platform/ideaActions.ts index 74814b13f3..7e3a8867e4 100644 --- a/src/platform/ideaActions.ts +++ b/src/platform/ideaActions.ts @@ -3,6 +3,7 @@ * `()`, and imported IDEA keymaps. */ export const IDEA_ACTIONS: Readonly> = { + PopupHector: "editor.highlightingLevel", GotoDeclaration: "navigate.declaration", GotoImplementation: "navigate.implementation", GotoTypeDeclaration: "navigate.typeDeclaration", diff --git a/src/platform/keymaps/ideaParity.test.ts b/src/platform/keymaps/ideaParity.test.ts index c22a2015b8..80042e671a 100644 --- a/src/platform/keymaps/ideaParity.test.ts +++ b/src/platform/keymaps/ideaParity.test.ts @@ -114,6 +114,8 @@ describe("IDEA keymap parity", () => { ["editor.toggleLocalGutterIcons", [], []], ["editor.toggleLocalIndentGuides", [], []], ["editor.toggleBreadcrumbs", [], []], + // PopupHector: Configure Current File Analysis. + ["editor.highlightingLevel", ["Meta+Alt+Shift+H"], ["Ctrl+Alt+Shift+H"]], // CopyAsPlainText / CopyAsRichText have no shortcut. ["code.copyAsPlainText", [], []], ["code.copyAsRichText", [], []], diff --git a/src/platform/keymaps/intellij-macos.ts b/src/platform/keymaps/intellij-macos.ts index cdb2924a08..cdb44f83a1 100644 --- a/src/platform/keymaps/intellij-macos.ts +++ b/src/platform/keymaps/intellij-macos.ts @@ -350,6 +350,7 @@ export const intellijMacos: Record = { "editor.toggleLocalGutterIcons": [], "editor.toggleLocalIndentGuides": [], "editor.toggleBreadcrumbs": [], + "editor.highlightingLevel": ["Meta+Alt+Shift+H"], // Build "build.buildProject": ["Meta+F9"], diff --git a/src/platform/keymaps/intellij-windows.ts b/src/platform/keymaps/intellij-windows.ts index a07e26730a..77d52e5c90 100644 --- a/src/platform/keymaps/intellij-windows.ts +++ b/src/platform/keymaps/intellij-windows.ts @@ -344,6 +344,7 @@ export const intellijWindows: Record = { "editor.toggleLocalGutterIcons": [], "editor.toggleLocalIndentGuides": [], "editor.toggleBreadcrumbs": [], + "editor.highlightingLevel": ["Ctrl+Alt+Shift+H"], // Build "build.buildProject": ["Ctrl+F9"], diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index b771a9d37b..7b05676410 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -1151,6 +1151,9 @@ "Attribute": "Attribute", "Contents differ only in line separators": "Contents differ only in line separators", "Line Separator": "Line Separator", + "Highlighting Level": "Highlighting Level", + "All Problems": "All Problems", + "Syntax": "Syntax", "Windows (\\r\\n)": "Windows (\\r\\n)", "Unix and macOS (\\n)": "Unix and macOS (\\n)", "Classic Mac OS (\\r)": "Classic Mac OS (\\r)", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 8992a15161..a4e53d157c 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -2514,6 +2514,10 @@ "Line Separator": "行分隔符", "Line and column": "行和列", "Line height": "行高", + "Highlighting Level": "高亮级别", + "All Problems": "所有问题", + "Configure Current File Analysis…": "配置当前文件分析…", + "Choose what the active editor highlights: None, Syntax (colouring without problems) or All Problems.": "选择当前编辑器高亮的内容:无、语法(着色但不显示问题)或所有问题。", "Line separator": "行分隔符", "Line, %": "行,%", "Line:Column Number": "行:列号",