feat(editor): Move Up / Down and Scroll actions (#6268)

Adds Move Up / Down and Scroll and their with-Selection variants as Editor
Actions (IDEA's EditorMoveUpAndScroll family, reachable from Find Action,
imported keymaps and IdeaVim :action). The carets move one line from the head,
dropping a selection unless extending, and the view scrolls by the distance the
primary caret moved (text line top below block widgets, or the visual row with
soft wrap), so the caret keeps its place on screen. Unbound by default, as in
IDEA's keymaps.

Closes #6267

Co-authored-by: mansur-akhmadi <[email protected]>
Цей коміт міститься в:
Mansur Akhmadiтаmansur-akhmadi authored and GitHub committed 2026-11-27 06:04:03 +08:00
1 джерело add23fe97f
коміт 031f1acd07
13 files changed
+235 -9

No files matched your search

+3 -1
Переглянути файл
@@ -156,10 +156,12 @@ With tabs placed left or right, the tab column is as wide as its longest name, u
| **Scroll Down** | — | `Ctrl+Down` |
| **Scroll Up and Move if Necessary** | — | — |
| **Scroll Down and Move if Necessary** | — | — |
| **Move Up and Scroll** | — | — |
| **Move Down and Scroll** | — | — |
| **Scroll to Top** | — | — |
| **Scroll to Bottom** | — | — |
**Scroll Up** and **Scroll Down** move the view by one line and leave the caret where it is, even when it scrolls out of sight. **Scroll Up and Move if Necessary** and **Scroll Down and Move if Necessary** also move the view by one line, but bring a caret the view doesn't show to its first or last line. **Scroll to Top** and **Scroll to Bottom** show the start and the end of the file; the caret stays where it is too. They have no default shortcut: run them from Find Action or assign one in **Settings › Keymap**.
**Scroll Up** and **Scroll Down** move the view by one line and leave the caret where it is, even when it scrolls out of sight. **Scroll Up and Move if Necessary** and **Scroll Down and Move if Necessary** also move the view by one line, but bring a caret the view doesn't show to its first or last line. **Move Up and Scroll** and **Move Down and Scroll** move the caret one line and the view with it, so the caret stays in the same place on screen; their **with Selection** variants select as they go. **Scroll to Top** and **Scroll to Bottom** show the start and the end of the file; the caret stays where it is too. They have no default shortcut: run them from Find Action or assign one in **Settings › Keymap**.
- **View › Active Editor › Soft-Wrap** wraps long lines in the current editor. To wrap some file types by default, use **Settings › Editor › General**, section **Soft Wraps**.
- **View › Reset Font Size** undoes font zoom in the active editor or focused surface (**View › Reset Font Size in All Editors** resets every open editor). Changing the font size with `⌘` / `Ctrl` + mouse wheel (scoped to **Active editor** by default, or **All editors**) can be turned on in **Settings › Editor › General**, section **Mouse Control**.
+3 -1
Переглянути файл
@@ -154,10 +154,12 @@ description: "多光标与选区、行编辑、剪贴板历史、代码折叠、
| **向下滚动** | — | `Ctrl+Down` |
| **向上滚动并在必要时移动光标** | — | — |
| **向下滚动并在必要时移动光标** | — | — |
| **向上移动并滚动** | — | — |
| **向下移动并滚动** | — | — |
| **滚动到顶部** | — | — |
| **滚动到底部** | — | — |
**向上滚动** 和 **向下滚动** 把视图移动一行,光标保持原位,即使它因此移出可见区域。**向上滚动并在必要时移动光标** 和 **向下滚动并在必要时移动光标** 同样把视图移动一行,但会把不在视图中的光标移到视图的第一行或最后一行。**滚动到顶部** 和 **滚动到底部** 显示文件的开头和结尾;光标同样保持原位。它们默认没有快捷键:在查找操作中运行,或在 **设置 › 按键映射** 中指定。
**向上滚动** 和 **向下滚动** 把视图移动一行,光标保持原位,即使它因此移出可见区域。**向上滚动并在必要时移动光标** 和 **向下滚动并在必要时移动光标** 同样把视图移动一行,但会把不在视图中的光标移到视图的第一行或最后一行。**向上移动并滚动** 和 **向下移动并滚动** 把光标移动一行,视图随之滚动同样的距离,光标在屏幕上的位置保持不变;对应的 **并选择** 版本边移动边选择。**滚动到顶部** 和 **滚动到底部** 显示文件的开头和结尾;光标同样保持原位。它们默认没有快捷键:在查找操作中运行,或在 **设置 › 按键映射** 中指定。
- **视图 › 活动编辑器 › 软换行** 让当前编辑器的长行自动折行。想让某些文件类型默认软换行,在 **设置 › 编辑器 › 常规** 的 **软换行** 一节中设置。
- **视图 › 重置字体大小** 撤销当前编辑器或焦点区域的字体缩放(**视图 › 重置所有编辑器中的字体大小** 会重置所有打开的编辑器)。用 `⌘` / `Ctrl` + 鼠标滚轮调整字号的功能默认关闭,可在 **设置 › 编辑器 › 常规** 的 **鼠标控制** 一节中打开,并可选择作用于 **当前编辑器**(默认)或 **所有编辑器**。
+24 -1
Переглянути файл
@@ -97,7 +97,14 @@ import {
} from "./cm/caretCommands";
import { isOverwrite, toggleOverwrite } from "./cm/overwrite";
import { isColumnMode, toggleColumnMode } from "./cm/columnMode";
import { cloneCaretAbove, cloneCaretBelow } from "./cm/verticalMotion";
import {
cloneCaretAbove,
cloneCaretBelow,
moveDownAndScroll,
moveDownAndScrollWithSelection,
moveUpAndScroll,
moveUpAndScrollWithSelection,
} from "./cm/verticalMotion";
import { pasteAsPlainText, pasteFromHistory, unwrapAtCaret } from "./extras";
import { cyclicExpandWord } from "./wordCompletion";
import { copyReferenceToClipboard, declaredName, qualifiedReference, referenceOffset } from "./copyReference";
@@ -1067,6 +1074,22 @@ export function registerEditorActions(): () => void {
caret("editor.scrollDownAndMove", "Scroll Down and Move if Necessary", (v) => scrollDownAndMove(v), {
category: "Editor Actions",
}),
caret("editor.moveUpAndScroll", "Move Up and Scroll", (v) => moveUpAndScroll(v), { category: "Editor Actions" }),
caret("editor.moveDownAndScroll", "Move Down and Scroll", (v) => moveDownAndScroll(v), {
category: "Editor Actions",
}),
caret(
"editor.moveUpAndScrollWithSelection",
"Move Up and Scroll with Selection",
(v) => moveUpAndScrollWithSelection(v),
{ category: "Editor Actions" },
),
caret(
"editor.moveDownAndScrollWithSelection",
"Move Down and Scroll with Selection",
(v) => moveDownAndScrollWithSelection(v),
{ category: "Editor Actions" },
),
caret("editor.scrollToTop", "Scroll to Top", (v) => scrollToTop(v), { category: "Editor Actions" }),
caret("editor.scrollToBottom", "Scroll to Bottom", (v) => scrollToBottom(v), { category: "Editor Actions" }),
caret("editor.moveToPageTop", "Move Caret to Page Top", (v) => cursorPageTop(v), { category: "Editor Actions" }),
+125 -2
Переглянути файл
@@ -1,12 +1,16 @@
// @vitest-environment jsdom
import { afterEach, describe, expect, it } from "vitest";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { EditorSelection, EditorState, StateEffect } from "@codemirror/state";
import { Decoration, EditorView, WidgetType } from "@codemirror/view";
import {
cloneCaretAbove,
cloneCaretBelow,
cursorLineDown,
cursorLineUp,
moveDownAndScroll,
moveDownAndScrollWithSelection,
moveUpAndScroll,
moveUpAndScrollWithSelection,
selectLineDown,
visibleGoalColumns,
} from "./verticalMotion";
@@ -106,3 +110,122 @@ describe("Clone Caret Above / Below in visible columns", () => {
expect(cloneCaretAbove(v)).toBe(false);
});
});
describe("Move Up / Down and Scroll", () => {
/** jsdom has no layout: record what the command asks the scroller for, and whether it scrolled to the caret. */
function track(v: EditorView, start: number) {
let top = start;
Object.defineProperty(v.scrollDOM, "scrollTop", {
configurable: true,
get: () => top,
set: (x: number) => (top = x),
});
const scrolledToCaret: boolean[] = [];
v.dispatch({
effects: StateEffect.appendConfig.of(
EditorView.updateListener.of((u) => u.transactions.forEach((tr) => scrolledToCaret.push(tr.scrollIntoView))),
),
});
scrolledToCaret.length = 0;
return { top: () => top, scrolledToCaret };
}
const rowGap = (v: EditorView, from: number, to: number) =>
v.lineBlockAt(v.state.doc.line(to).from).top - v.lineBlockAt(v.state.doc.line(from).from).top;
it("moves the caret one line and scrolls the view by the same amount", () => {
const v = make("abcdef\nabcdef\nabcdef\nabcdef", 7 + 3);
const t = track(v, 100);
const gap = rowGap(v, 2, 3);
expect(gap).toBeGreaterThan(0);
expect(moveDownAndScroll(v)).toBe(true);
expect(heads(v)).toEqual([14 + 3]);
expect(t.top()).toBe(100 + gap);
expect(moveUpAndScroll(v)).toBe(true);
expect(moveUpAndScroll(v)).toBe(true);
expect(heads(v)).toEqual([3]);
expect(t.top()).toBe(100 - rowGap(v, 1, 2));
expect(v.state.selection.main.empty).toBe(true);
});
it("keeps the goal column across a short line", () => {
const v = make("abcdefgh\n张\nabcdefgh", 6);
track(v, 0);
moveDownAndScroll(v);
moveDownAndScroll(v);
expect(heads(v)).toEqual([11 + 6]);
});
it("with Selection extends from the anchor", () => {
const v = make("abcd\nabcd\nabcd", 5 + 2);
const t = track(v, 40);
moveDownAndScrollWithSelection(v);
expect([v.state.selection.main.anchor, v.state.selection.main.head]).toEqual([7, 12]);
moveUpAndScrollWithSelection(v);
moveUpAndScrollWithSelection(v);
expect([v.state.selection.main.anchor, v.state.selection.main.head]).toEqual([7, 2]);
expect(t.top()).toBe(40 - rowGap(v, 1, 2));
});
it("moves every caret and scrolls with the primary one", () => {
const v = make("ab\nab\nab\nab", 0);
v.dispatch({ selection: EditorSelection.create([EditorSelection.cursor(1), EditorSelection.cursor(7)], 1) });
const t = track(v, 0);
moveDownAndScroll(v);
expect(heads(v)).toEqual([4, 10]);
expect(v.state.selection.mainIndex).toBe(1);
expect(t.top()).toBe(rowGap(v, 3, 4));
});
it("leaves scrolling to itself: never scrolls a caret out of view back into it", () => {
const v = make("ab\nab\nab", 1);
const t = track(v, 500);
moveDownAndScroll(v);
expect(t.scrolledToCaret).toEqual([false]);
expect(t.top()).toBe(500 + rowGap(v, 1, 2));
});
it("drops a selection and moves one line from its caret", () => {
const v = make("abcd\nabcd\nabcd\nabcd", 0);
v.dispatch({ selection: { anchor: 15 + 2, head: 2 } });
const t = track(v, 0);
moveDownAndScroll(v);
expect(v.state.selection.main.empty).toBe(true);
expect(heads(v)).toEqual([5 + 2]);
expect(t.top()).toBe(rowGap(v, 1, 2));
});
it("measures to the text of a line below a block widget (code lens)", () => {
class Lens extends WidgetType {
toDOM() {
return document.createElement("div");
}
get estimatedHeight() {
return 40;
}
}
const v = make("ab\nab\nab", 1);
const lens = Decoration.widget({ widget: new Lens(), block: true, side: -1 }).range(3);
v.dispatch({ effects: StateEffect.appendConfig.of(EditorView.decorations.of(Decoration.set([lens]))) });
const t = track(v, 0);
const line = v.lineBlockAt(0).height;
expect(v.lineBlockAt(3).height).toBeGreaterThan(line);
moveDownAndScroll(v);
expect(heads(v)).toEqual([4]);
expect(t.top()).toBe(v.lineBlockAt(3).bottom - line);
moveUpAndScroll(v);
expect(t.top()).toBe(0);
});
it("does not scroll at the document edge", () => {
const v = make("ab\ncd", 1);
const t = track(v, 0);
expect(moveUpAndScroll(v)).toBe(true);
expect(heads(v)).toEqual([0]);
expect(moveUpAndScroll(v)).toBe(false);
v.dispatch({ selection: { anchor: 4 } });
expect(moveDownAndScroll(v)).toBe(true);
expect(moveDownAndScroll(v)).toBe(false);
expect(heads(v)).toEqual([5]);
expect(t.top()).toBe(0);
});
});
+41 -4
Переглянути файл
@@ -6,7 +6,7 @@
* CodeMirror's pixel motion between their visual rows.
*/
import { EditorSelection, StateEffect, StateField, type EditorState, type SelectionRange } from "@codemirror/state";
import type { Command, EditorView } from "@codemirror/view";
import { BlockType, type BlockInfo, type Command, type EditorView } from "@codemirror/view";
import { offsetAtVisibleColumn, visibleColumn } from "./visibleColumn";
/** Goal visible column per caret head, kept while only these commands move the carets. */
@@ -52,11 +52,12 @@ export function verticalHead(view: EditorView, head: number, forward: boolean, g
return moved === head ? null : moved;
}
function moveCarets(view: EditorView, forward: boolean, extend: boolean): boolean {
/** `fromHead`: drop a selection and move its caret on from the head, as IDEA's caret-relative moves do. */
function moveCarets(view: EditorView, forward: boolean, extend: boolean, fromHead = false): boolean {
const { state } = view;
const goals = new Map<number, number>();
const ranges = state.selection.ranges.map((r): SelectionRange => {
if (!extend && !r.empty) return EditorSelection.cursor(forward ? r.to : r.from);
if (!extend && !fromHead && !r.empty) return EditorSelection.cursor(forward ? r.to : r.from);
const goal = goalOf(state, r.head);
let head = verticalHead(view, r.head, forward, goal);
if (head === null) head = forward ? state.doc.lineAt(r.head).to : state.doc.lineAt(r.head).from;
@@ -65,7 +66,8 @@ function moveCarets(view: EditorView, forward: boolean, extend: boolean): boolea
});
const selection = EditorSelection.create(ranges, state.selection.mainIndex);
if (selection.eq(state.selection)) return false;
view.dispatch({ selection, effects: setGoals.of(goals), scrollIntoView: true, userEvent: "select" });
// The caret-relative moves scroll the view themselves and may leave the caret out of view.
view.dispatch({ selection, effects: setGoals.of(goals), scrollIntoView: !fromHead, userEvent: "select" });
return true;
}
@@ -74,6 +76,41 @@ export const cursorLineDown: Command = (view) => moveCarets(view, true, false);
export const selectLineUp: Command = (view) => moveCarets(view, false, true);
export const selectLineDown: Command = (view) => moveCarets(view, true, true);
/** Top of the text of the line at `pos`, in document coordinates: below any block widgets (code lens, inline diff) joined to its line block. */
function lineTextTop(view: EditorView, pos: number): number {
const block = view.lineBlockAt(pos);
if (!Array.isArray(block.type)) return block.top;
const parts = block.type as readonly BlockInfo[];
return (parts.find((b) => b.type === BlockType.Text && b.from <= pos && pos <= b.to) ?? block).top;
}
/** Screen top of the caret's visual row when soft wrap splits lines into rows and the caret is drawn; otherwise null. */
function caretRowTop(view: EditorView): number | null {
if (!view.lineWrapping) return null;
const { head, assoc } = view.state.selection.main;
return view.coordsAtPos(head, assoc || 1)?.top ?? null;
}
/**
* Move Up / Down and Scroll (with Selection), as IDEA: the carets move one line and
* the view scrolls by as much, so the primary caret keeps its place on screen.
*/
function moveAndScroll(view: EditorView, forward: boolean, extend: boolean): boolean {
const before = { row: caretRowTop(view), line: lineTextTop(view, view.state.selection.main.head) };
if (!moveCarets(view, forward, extend, true)) return false;
const row = before.row === null ? null : caretRowTop(view);
view.scrollDOM.scrollTop +=
row !== null && before.row !== null
? row - before.row
: lineTextTop(view, view.state.selection.main.head) - before.line;
return true;
}
export const moveUpAndScroll: Command = (view) => moveAndScroll(view, false, false);
export const moveDownAndScroll: Command = (view) => moveAndScroll(view, true, false);
export const moveUpAndScrollWithSelection: Command = (view) => moveAndScroll(view, false, true);
export const moveDownAndScrollWithSelection: Command = (view) => moveAndScroll(view, true, true);
/** Clone Caret Above / Below: a caret on the next line at each caret's goal column. */
function cloneCaret(view: EditorView, forward: boolean): boolean {
const { state } = view;
+4
Переглянути файл
@@ -122,6 +122,10 @@ export const IDEA_ACTIONS: Readonly<Record<string, string>> = {
EditorScrollDown: "editor.scrollDown",
EditorScrollUpAndMove: "editor.scrollUpAndMove",
EditorScrollDownAndMove: "editor.scrollDownAndMove",
EditorMoveUpAndScroll: "editor.moveUpAndScroll",
EditorMoveDownAndScroll: "editor.moveDownAndScroll",
EditorMoveUpAndScrollWithSelection: "editor.moveUpAndScrollWithSelection",
EditorMoveDownAndScrollWithSelection: "editor.moveDownAndScrollWithSelection",
EditorScrollTop: "editor.scrollToTop",
EditorScrollBottom: "editor.scrollToBottom",
EditorMoveToPageTop: "editor.moveToPageTop",
+11
Переглянути файл
@@ -101,6 +101,17 @@ describe("IntelliJ IDEA keymap import", () => {
expect(k.unknownActions).toEqual([]);
});
it("maps Move Up / Down and Scroll (with Selection)", () => {
const k = parseIdeaKeymap(
`<keymap name="E" parent="$default"><action id="EditorMoveUpAndScroll"><keyboard-shortcut first-keystroke="ctrl alt UP"/></action><action id="EditorMoveDownAndScroll"><keyboard-shortcut first-keystroke="ctrl alt DOWN"/></action><action id="EditorMoveUpAndScrollWithSelection"><keyboard-shortcut first-keystroke="ctrl alt shift UP"/></action><action id="EditorMoveDownAndScrollWithSelection"><keyboard-shortcut first-keystroke="ctrl alt shift DOWN"/></action></keymap>`,
);
expect(k.overrides["editor.moveUpAndScroll"]).toEqual([normalizeBinding("Ctrl+Alt+Up")]);
expect(k.overrides["editor.moveDownAndScroll"]).toEqual([normalizeBinding("Ctrl+Alt+Down")]);
expect(k.overrides["editor.moveUpAndScrollWithSelection"]).toEqual([normalizeBinding("Ctrl+Alt+Shift+Up")]);
expect(k.overrides["editor.moveDownAndScrollWithSelection"]).toEqual([normalizeBinding("Ctrl+Alt+Shift+Down")]);
expect(k.unknownActions).toEqual([]);
});
it("maps Scroll to Top / Scroll to Bottom", () => {
const k = parseIdeaKeymap(
`<keymap name="X" parent="Mac OS X 10.5+"><action id="EditorScrollTop"><keyboard-shortcut first-keystroke="HOME"/></action><action id="EditorScrollBottom"><keyboard-shortcut first-keystroke="END"/></action></keymap>`,
+4
Переглянути файл
@@ -66,6 +66,10 @@ describe("IDEA keymap parity", () => {
["editor.scrollDown", [], ["Ctrl+Down"]],
["editor.scrollUpAndMove", [], []],
["editor.scrollDownAndMove", [], []],
["editor.moveUpAndScroll", [], []],
["editor.moveDownAndScroll", [], []],
["editor.moveUpAndScrollWithSelection", [], []],
["editor.moveDownAndScrollWithSelection", [], []],
// Only IDEA's Xcode keymap binds these (Home / End).
["editor.scrollToTop", [], []],
["editor.scrollToBottom", [], []],
+4
Переглянути файл
@@ -140,6 +140,10 @@ export const intellijMacos: Record<string, string[]> = {
"editor.scrollDown": [],
"editor.scrollUpAndMove": [],
"editor.scrollDownAndMove": [],
"editor.moveUpAndScroll": [],
"editor.moveDownAndScroll": [],
"editor.moveUpAndScrollWithSelection": [],
"editor.moveDownAndScrollWithSelection": [],
"editor.scrollToTop": [],
"editor.scrollToBottom": [],
"editor.moveToPageTop": ["Meta+PageUp"],
+4
Переглянути файл
@@ -141,6 +141,10 @@ export const intellijWindows: Record<string, string[]> = {
"editor.scrollDown": ["Ctrl+Down"],
"editor.scrollUpAndMove": [],
"editor.scrollDownAndMove": [],
"editor.moveUpAndScroll": [],
"editor.moveDownAndScroll": [],
"editor.moveUpAndScrollWithSelection": [],
"editor.moveDownAndScrollWithSelection": [],
"editor.scrollToTop": [],
"editor.scrollToBottom": [],
"editor.moveToPageTop": ["Ctrl+PageUp"],
+4
Переглянути файл
@@ -256,6 +256,10 @@ export const vscodeMacos: Record<string, string[]> = {
"editor.scrollDown": [],
"editor.scrollUpAndMove": [],
"editor.scrollDownAndMove": [],
"editor.moveUpAndScroll": [],
"editor.moveDownAndScroll": [],
"editor.moveUpAndScrollWithSelection": [],
"editor.moveDownAndScrollWithSelection": [],
"editor.scrollToTop": [],
"editor.scrollToBottom": [],
"editor.moveToPageTop": [],
+4
Переглянути файл
@@ -695,6 +695,10 @@
"Scroll Down": "向下捲動",
"Scroll Up and Move if Necessary": "向上捲動並在必要時移動游標",
"Scroll Down and Move if Necessary": "向下捲動並在必要時移動游標",
"Move Up and Scroll": "向上移動並捲動",
"Move Down and Scroll": "向下移動並捲動",
"Move Up and Scroll with Selection": "向上移動並捲動並選擇",
"Move Down and Scroll with Selection": "向下移動並捲動並選擇",
"Scroll to Top": "捲動到頂部",
"Scroll to Bottom": "捲動到底部",
"Move Caret to Page Top": "將游標移至頁面頂部",
+4
Переглянути файл
@@ -1056,6 +1056,10 @@
"Scroll Down": "向下滚动",
"Scroll Up and Move if Necessary": "向上滚动并在必要时移动光标",
"Scroll Down and Move if Necessary": "向下滚动并在必要时移动光标",
"Move Up and Scroll": "向上移动并滚动",
"Move Down and Scroll": "向下移动并滚动",
"Move Up and Scroll with Selection": "向上移动并滚动并选择",
"Move Down and Scroll with Selection": "向下移动并滚动并选择",
"Scroll to Top": "滚动到顶部",
"Scroll to Bottom": "滚动到底部",
"Move Caret to Page Top": "将光标移至页面顶部",