From 19491af5ddb8909442e078566c9b69f95885fcb8 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Fri, 12 Apr 2024 18:54:32 +0200 Subject: [PATCH] Fix cursor behavior for deleteLine FIX: Fix an issue where `deleteLine` sometimes leaves the cursor on the wrong line. See https://discuss.codemirror.net/t/inconsistent-behaviour-when-deleting-wrapped-lines/8119 --- src/commands.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/commands.ts b/src/commands.ts index 3f762ce..167e043 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -673,7 +673,14 @@ export const deleteLine: Command = view => { else if (to < state.doc.length) to++ return {from, to} })) - let selection = updateSel(state.selection, range => view.moveVertically(range, true)).map(changes) + let selection = updateSel(state.selection, range => { + let dist: number | undefined = undefined + if (view.lineWrapping) { + let block = view.lineBlockAt(range.head), pos = view.coordsAtPos(range.head, range.assoc || 1) + if (pos) dist = (block.bottom + view.documentTop) - pos.bottom + view.defaultLineHeight / 2 + } + return view.moveVertically(range, true, dist) + }).map(changes) view.dispatch({changes, selection, scrollIntoView: true, userEvent: "delete.line"}) return true }