Skip to content

Commit 2ff2e3d

Browse files
committed
Make implementation of addCursorAbove/Below a bit more efficient
1 parent ec02f63 commit 2ff2e3d

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/commands.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,13 @@ export const selectParentSyntax: StateCommand = ({state, dispatch}) => {
476476
}
477477

478478
function addCursorVertically(view: EditorView, forward: boolean) {
479-
let {state} = view, sel = state.selection
479+
let {state} = view, sel = state.selection, ranges: SelectionRange[] = state.selection.ranges.slice()
480480
for (let range of state.selection.ranges) {
481481
let line = state.doc.lineAt(range.head)
482482
if (forward ? line.to < view.state.doc.length : line.from > 0) for (let cur = range;;) {
483483
let next = view.moveVertically(cur, forward)
484484
if (next.head < line.from || next.head > line.to) {
485-
sel = sel.addRange(next)
485+
if (!ranges.some(r => r.head == next.head)) ranges.push(next)
486486
break
487487
} else if (next.head == cur.head) {
488488
break
@@ -491,8 +491,8 @@ function addCursorVertically(view: EditorView, forward: boolean) {
491491
}
492492
}
493493
}
494-
if (sel == state.selection) return false
495-
view.dispatch(setSel(view.state, sel))
494+
if (ranges.length == sel.ranges.length) return false
495+
view.dispatch(setSel(state, EditorSelection.create(ranges, ranges.length - 1)))
496496
return true
497497
}
498498

0 commit comments

Comments
 (0)