Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 9e3909a

Browse files
committed
fix(code): pressing tab while multiple lines are selected would replace with tab
1 parent 03de472 commit 9e3909a

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

packages/codemirror/src/extensions/custom_tab.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,38 @@ const smartIndentWithTab: KeyBinding[] = [
2424

2525
// Step 1: Handle non-empty selections → replace with tab
2626
if (selection.ranges.some(range => !range.empty)) {
27-
for (let range of selection.ranges) {
28-
changes.push({ from: range.from, to: range.to, insert: "\t" });
29-
newSelections.push(EditorSelection.cursor(range.from + 1));
27+
// If multiple lines are selected, insert a tab character at the start of each line
28+
// and move the cursor to the position after the tab character.
29+
const linesCovered = new Set<number>();
30+
for (const range of selection.ranges) {
31+
const startLine = state.doc.lineAt(range.from);
32+
const endLine = state.doc.lineAt(range.to);
33+
34+
for (let lineNumber = startLine.number; lineNumber <= endLine.number; lineNumber++) {
35+
linesCovered.add(lineNumber);
36+
}
37+
}
38+
39+
if (linesCovered.size > 1) {
40+
// Multiple lines are selected, indent each line.
41+
return indentMore({ state, dispatch });
42+
} else {
43+
// Single line selection, replace with tab.
44+
for (let range of selection.ranges) {
45+
changes.push({ from: range.from, to: range.to, insert: "\t" });
46+
newSelections.push(EditorSelection.cursor(range.from + 1));
47+
}
48+
49+
dispatch(
50+
state.update({
51+
changes,
52+
selection: EditorSelection.create(newSelections),
53+
scrollIntoView: true,
54+
userEvent: "input"
55+
})
56+
);
3057
}
3158

32-
dispatch(
33-
state.update({
34-
changes,
35-
selection: EditorSelection.create(newSelections),
36-
scrollIntoView: true,
37-
userEvent: "input"
38-
})
39-
);
4059
return true;
4160
}
4261

0 commit comments

Comments
 (0)