mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
feat(plugins): the editor's tab size in editor panels (#3906)
openidea.editor.get() includes tabSize, the editor state's tab size (Code Style, .editorconfig, indent detection or the status bar's indent picker), and onTarget fires again with the new get() value when it changes for the same file, at most once a frame. Documented under API 1.10; the minimap fixture expands tabs to that size. Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
This commit is contained in:
co-authored by
wildcatDownstairs
parent
449db6baa7
commit
577193d387
@@ -21,10 +21,11 @@
|
||||
<body>
|
||||
<canvas id="map"></canvas>
|
||||
<script>
|
||||
// A minimal minimap over `openidea.editor`: each line 2px high, each character 1px wide,
|
||||
// in its highlight colour; the visible area as a frame that can be dragged; the wheel
|
||||
// scrolls the editor; the marker bar's problems as bars at the right edge; folded lines left out;
|
||||
// the selected lines tinted and the caret's line marked; VCS changes at the left edge.
|
||||
// A minimal minimap over `openidea.editor`: each line 2px high, each character 1px wide (tabs
|
||||
// as wide as the editor draws them), in its highlight colour; the visible area as a frame that
|
||||
// can be dragged; the wheel scrolls the editor; the marker bar's problems as bars at the right
|
||||
// edge; folded lines left out; the selected lines tinted and the caret's line marked; VCS
|
||||
// changes at the left edge.
|
||||
const editor = window.openidea.editor;
|
||||
const canvas = document.getElementById("map");
|
||||
const ctx = canvas.getContext("2d");
|
||||
@@ -38,6 +39,7 @@
|
||||
let folds = [];
|
||||
let selections = [];
|
||||
let changes = [];
|
||||
let tabSize = 4;
|
||||
const PROBLEM_COLOR = { error: "--openidea-danger", warning: "--openidea-warning" };
|
||||
const PROBLEM_RANK = { error: 2, warning: 1 };
|
||||
let drag = null;
|
||||
@@ -47,6 +49,13 @@
|
||||
const m = /rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(color);
|
||||
return !!m && Math.max(m[1], m[2], m[3]) - Math.min(m[1], m[2], m[3]) < 24;
|
||||
};
|
||||
// Column → x, with tabs expanded to the editor's tab stops.
|
||||
const xOf = (line) => {
|
||||
if (!line.includes("\t")) return (c) => c;
|
||||
const xs = [0];
|
||||
for (let i = 0, x = 0; i < line.length; i++) xs.push((x += line[i] === "\t" ? tabSize - (x % tabSize) : 1));
|
||||
return (c) => xs[Math.min(c, line.length)];
|
||||
};
|
||||
const cssVar = (name) => (window.openidea.theme && window.openidea.theme.vars[name]) || "";
|
||||
|
||||
// Document lines ↔ minimap rows, without the lines folds hide (fractional within a fold's row).
|
||||
@@ -120,10 +129,11 @@
|
||||
const i = lineOfRow(row);
|
||||
const y = row * LINE - offset;
|
||||
const line = text[i] || "";
|
||||
const x = xOf(line);
|
||||
// Text without a syntax colour: faint, so the coloured spans stand out.
|
||||
ctx.globalAlpha = 0.2;
|
||||
ctx.fillStyle = muted;
|
||||
for (const m of line.matchAll(/\S+/g)) ctx.fillRect(m.index, y, m[0].length, LINE - 0.5);
|
||||
for (const m of line.matchAll(/\S+/g)) ctx.fillRect(x(m.index), y, m[0].length, LINE - 0.5);
|
||||
ctx.globalAlpha = 1;
|
||||
const spans = colors.get(i);
|
||||
if (!spans) missing.push(i);
|
||||
@@ -132,7 +142,7 @@
|
||||
// Greys (the plain text colour, comments) stay as faint as uncoloured text.
|
||||
if (grey(s.color)) continue;
|
||||
ctx.fillStyle = s.color;
|
||||
ctx.fillRect(s.from, y, s.to - s.from, LINE - 0.5);
|
||||
ctx.fillRect(x(s.from), y, x(s.to) - x(s.from), LINE - 0.5);
|
||||
}
|
||||
}
|
||||
// Changes in the gutter's colours: a bar over changed lines, a notch where lines were deleted.
|
||||
@@ -186,6 +196,8 @@
|
||||
view = null;
|
||||
return redraw();
|
||||
}
|
||||
// `tabSize` is API 1.10.
|
||||
tabSize = target.tabSize || 4;
|
||||
const doc = await editor.document.get();
|
||||
text = doc.text.split("\n");
|
||||
// `problems` is API 1.10.
|
||||
|
||||
+5
-2
@@ -467,6 +467,9 @@ matching SDK. Released 1.9 plugins keep working. New in 1.10:
|
||||
`openidea.editor.changes()` and hears them change through `onChanges`. Frames
|
||||
get the lane's colours as `--openidea-change-added`, `--openidea-change-modified`
|
||||
and `--openidea-change-deleted`. See _Editor panels_.
|
||||
- **The tab size in editor panels.** `openidea.editor.get()` includes the
|
||||
`tabSize` the editor expands tabs with for the group's file, and `onTarget`
|
||||
reports it again when it changes. See _Editor panels_.
|
||||
|
||||
### API 1.9
|
||||
|
||||
@@ -1935,8 +1938,8 @@ The page gets `openidea.editor` (only an editor panel's page has it). Lines are
|
||||
|
||||
| `openidea.editor` | What it does |
|
||||
| -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `get()` | `{ path, language, version, lineCount }` of the group's file, or `null` |
|
||||
| `onTarget(fn)` | Called with `get()` when the group shows another file, or none |
|
||||
| `get()` | `{ path, language, version, lineCount, tabSize }` of the group's file, or `null`; `tabSize` (API 1.10) is the columns a tab takes in the editor (Code Style, `.editorconfig`, indent detection) |
|
||||
| `onTarget(fn)` | Called with `get()` when the group shows another file, or none, and (API 1.10) when the file's tab size changes |
|
||||
| `document.get()` / `document.onChange` | The `openidea.document` of web views, on the group's file and read-only: `edit` and `replace` reject. Changes arrive at most once a frame |
|
||||
| `viewport()` / `onViewport(fn)` | `{ firstLine, lastLine, lineCount, scrollTop, scrollHeight, clientHeight }`; lines are fractional (the position within a line). Sent on scroll, resize and folding, at most once a frame |
|
||||
| `scrollTo(line)` | Scrolls so that `line` (fractional) is at the top of the visible area; call it on every move while dragging |
|
||||
|
||||
@@ -2,7 +2,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { render, waitFor } from "@testing-library/react";
|
||||
import { javascript } from "@codemirror/lang-javascript";
|
||||
import { codeFolding, foldEffect, syntaxHighlighting, unfoldEffect } from "@codemirror/language";
|
||||
import { EditorSelection, EditorState, StateEffect } from "@codemirror/state";
|
||||
import { Compartment, EditorSelection, EditorState, StateEffect } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import type { Diagnostic, LineHunk, PluginInfo } from "@/bindings";
|
||||
import type { TextEditorHandle } from "@/editor/api";
|
||||
@@ -163,7 +163,7 @@ describe("WebView as an editor panel", () => {
|
||||
const listeners = new Set<(e: FrameEditorEvent) => void>();
|
||||
const calls: unknown[] = [];
|
||||
const editor: FrameEditor & { fire(e: FrameEditorEvent): void } = {
|
||||
get: () => ({ path: "/p/a.ts", language: "typescript", version: 2, lineCount: 3 }),
|
||||
get: () => ({ path: "/p/a.ts", language: "typescript", version: 2, lineCount: 3, tabSize: 4 }),
|
||||
viewport: () => ({ firstLine: 0, lastLine: 2.5, lineCount: 3, scrollTop: 0, scrollHeight: 60, clientHeight: 50 }),
|
||||
scrollTo: (line) => void calls.push(["scrollTo", line]),
|
||||
scrollBy: (px) => void calls.push(["scrollBy", px]),
|
||||
@@ -212,7 +212,13 @@ describe("WebView as an editor panel", () => {
|
||||
});
|
||||
|
||||
send({ call: 1, op: "editor.get" });
|
||||
expect((await reply(1)).value).toEqual({ path: "/p/a.ts", language: "typescript", version: 2, lineCount: 3 });
|
||||
expect((await reply(1)).value).toEqual({
|
||||
path: "/p/a.ts",
|
||||
language: "typescript",
|
||||
version: 2,
|
||||
lineCount: 3,
|
||||
tabSize: 4,
|
||||
});
|
||||
send({ call: 2, op: "editor.highlights", args: { fromLine: 0, toLine: 1 } });
|
||||
expect((await reply(2)).value).toEqual([[{ from: 0, to: 5, color: "rgb(1, 2, 3)" }]]);
|
||||
send({ call: 3, op: "editor.highlights", args: { fromLine: 0, toLine: 2001 } });
|
||||
@@ -296,7 +302,7 @@ describe("PanelEditor", () => {
|
||||
panel.bind(handle);
|
||||
expect(events[0]).toEqual({
|
||||
editor: "target",
|
||||
value: { path: "/p/src/a.js", language: "javascript", version: 1, lineCount: 3 },
|
||||
value: { path: "/p/src/a.js", language: "javascript", version: 1, lineCount: 3, tabSize: 4 },
|
||||
});
|
||||
expect(panel.document.read()).toMatchObject({ readOnly: true, name: "a.js", text: view.state.doc.toString() });
|
||||
expect(() => panel.document.edit(0, [])).toThrow("read-only");
|
||||
@@ -733,6 +739,48 @@ describe("PanelEditor changes", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("PanelEditor tab size", () => {
|
||||
it("reports the editor's tab size and the new one when it changes", async () => {
|
||||
const parent = document.createElement("div");
|
||||
document.body.appendChild(parent);
|
||||
const indent = new Compartment();
|
||||
const view = new EditorView({
|
||||
parent,
|
||||
state: EditorState.create({ doc: "\tx", extensions: [indent.of(EditorState.tabSize.of(2)), editorPanelUpdates] }),
|
||||
});
|
||||
const handle: TextEditorHandle = {
|
||||
view,
|
||||
tabId: "t1",
|
||||
ctx: { doc: 1, path: "/p/src/a.go", language: "go", uiMode: "Go", readOnly: false },
|
||||
};
|
||||
const panel = new PanelEditor();
|
||||
const events: FrameEditorEvent[] = [];
|
||||
panel.subscribe((e) => events.push(e));
|
||||
panel.bind(handle);
|
||||
expect(panel.get()).toMatchObject({ path: "/p/src/a.go", tabSize: 2 });
|
||||
await nextFrame();
|
||||
|
||||
// A settings or `.editorconfig` change: `onTarget` again, once a frame.
|
||||
events.length = 0;
|
||||
view.dispatch({ effects: indent.reconfigure(EditorState.tabSize.of(8)) });
|
||||
view.dispatch({ effects: indent.reconfigure(EditorState.tabSize.of(3)) });
|
||||
await nextFrame();
|
||||
expect(events.filter((e) => e.editor === "target")).toEqual([
|
||||
{ editor: "target", value: { path: "/p/src/a.go", language: "go", version: 1, lineCount: 1, tabSize: 3 } },
|
||||
]);
|
||||
|
||||
// The same size again, or an edit: no new target.
|
||||
events.length = 0;
|
||||
view.dispatch({ effects: indent.reconfigure(EditorState.tabSize.of(3)) });
|
||||
view.dispatch({ changes: { from: 0, insert: "y" } });
|
||||
await nextFrame();
|
||||
expect(events.filter((e) => e.editor === "target")).toEqual([]);
|
||||
|
||||
panel.dispose();
|
||||
view.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("editorPanels projection", () => {
|
||||
it("shows the panel only while `editor.read` is granted and `when` holds", () => {
|
||||
const plugin = (granted: boolean) =>
|
||||
|
||||
@@ -73,6 +73,8 @@ export interface EditorTarget {
|
||||
language: string;
|
||||
version: number;
|
||||
lineCount: number;
|
||||
/** The columns a tab takes, as the editor expands it. */
|
||||
tabSize: number;
|
||||
}
|
||||
|
||||
/** What `openidea.editor.viewport()` returns: 0-based lines, fractional within a line. */
|
||||
|
||||
@@ -77,6 +77,7 @@ function lineAtHeight(view: EditorView, height: number): number {
|
||||
|
||||
/** What the next frame sends: nothing yet. */
|
||||
const nothingPending = () => ({
|
||||
target: false,
|
||||
viewport: false,
|
||||
document: false,
|
||||
problems: false,
|
||||
@@ -122,6 +123,8 @@ export class PanelEditor implements FrameEditor {
|
||||
this.unbind = () => {};
|
||||
this.handle = handle;
|
||||
this.version++;
|
||||
// Sent below.
|
||||
this.pending.target = false;
|
||||
if (handle) {
|
||||
const { view } = handle;
|
||||
const onScroll = () => this.schedule({ viewport: true });
|
||||
@@ -148,6 +151,8 @@ export class PanelEditor implements FrameEditor {
|
||||
// An edit can move a caret to another line without changing its offset.
|
||||
if (u.docChanged || !u.state.selection.eq(u.startState.selection)) this.schedule({ selections: true });
|
||||
if (u.startState.facet(lineChanges) !== u.state.facet(lineChanges)) this.schedule({ changes: true });
|
||||
// Settings or `.editorconfig` changed the file's indentation: `get()` changed.
|
||||
if (u.startState.tabSize !== u.state.tabSize) this.schedule({ target: true });
|
||||
// Wrapped rows follow the line blocks' heights.
|
||||
const wrap = u.view.lineWrapping;
|
||||
if (foldedRanges(u.startState) !== foldedRanges(u.state) || wrap !== softWrap || (wrap && u.heightChanged)) {
|
||||
@@ -200,7 +205,14 @@ export class PanelEditor implements FrameEditor {
|
||||
get(): EditorTarget | null {
|
||||
const h = this.handle;
|
||||
if (!h) return null;
|
||||
return { path: h.ctx.path, language: h.ctx.language, version: this.version, lineCount: h.view.state.doc.lines };
|
||||
const { state } = h.view;
|
||||
return {
|
||||
path: h.ctx.path,
|
||||
language: h.ctx.language,
|
||||
version: this.version,
|
||||
lineCount: state.doc.lines,
|
||||
tabSize: state.tabSize,
|
||||
};
|
||||
}
|
||||
|
||||
viewport(): EditorViewport | null {
|
||||
@@ -382,6 +394,7 @@ export class PanelEditor implements FrameEditor {
|
||||
}
|
||||
|
||||
private schedule(p: {
|
||||
target?: boolean;
|
||||
viewport?: boolean;
|
||||
document?: boolean;
|
||||
problems?: boolean;
|
||||
@@ -391,6 +404,7 @@ export class PanelEditor implements FrameEditor {
|
||||
lines?: [number, number];
|
||||
}) {
|
||||
const q = this.pending;
|
||||
q.target ||= !!p.target;
|
||||
q.viewport ||= !!p.viewport;
|
||||
q.document ||= !!p.document;
|
||||
q.problems ||= !!p.problems;
|
||||
@@ -407,6 +421,7 @@ export class PanelEditor implements FrameEditor {
|
||||
this.pending = nothingPending();
|
||||
const view = this.handle?.view;
|
||||
if (!view) return;
|
||||
if (q.target) this.emit({ editor: "target", value: this.get() });
|
||||
if (q.document && this.documentListeners.size) {
|
||||
const state = { version: this.version, text: view.state.doc.toString() };
|
||||
for (const l of this.documentListeners) l(state);
|
||||
|
||||
@@ -139,9 +139,9 @@
|
||||
};
|
||||
/** The text editor of the editor group the panel is in. */
|
||||
const editorApi = Object.freeze({
|
||||
/** `{ path, language, version, lineCount }` of the group's file, or null. */
|
||||
/** `{ path, language, version, lineCount, tabSize }` of the group's file, or null. */
|
||||
get: () => call("editor.get"),
|
||||
/** Called with the new `get()` value when the group shows another file, or none. */
|
||||
/** Called with the new `get()` value when the group shows another file, or none, or the file's tab size changes. */
|
||||
onTarget: listen(editorListeners.target),
|
||||
/** The group's document, read-only (`edit` / `replace` reject). */
|
||||
document: documentApi,
|
||||
|
||||
Reference in New Issue
Block a user