Skip to content

Commit 8c7d3d9

Browse files
committed
Add deleteCharBackwardStrict commands
FEATURE: The new `deleteCharBackwardStrict` command just deletes a character, without further smart behavior around indentation. See https://discuss.codemirror.net/t/indentation-using-spaces-should-remove-single-space-on-backspace/8072
1 parent 19491af commit 8c7d3d9

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ with key bindings for a lot of them.
137137

138138
@deleteCharBackward
139139

140+
@deleteCharBackwardStrict
141+
140142
@deleteCharForward
141143

142144
@deleteGroupBackward

src/commands.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ function skipAtomic(target: CommandTarget, pos: number, forward: boolean) {
466466
return pos
467467
}
468468

469-
const deleteByChar = (target: CommandTarget, forward: boolean) => deleteBy(target, range => {
469+
const deleteByChar = (target: CommandTarget, forward: boolean, byIndentUnit: boolean) => deleteBy(target, range => {
470470
let pos = range.from, {state} = target, line = state.doc.lineAt(pos), before, targetPos: number
471-
if (!forward && pos > line.from && pos < line.from + 200 &&
471+
if (byIndentUnit && !forward && pos > line.from && pos < line.from + 200 &&
472472
!/[^ \t]/.test(before = line.text.slice(0, pos - line.from))) {
473473
if (before[before.length - 1] == "\t") return pos - 1
474474
let col = countColumn(before, state.tabSize), drop = col % getIndentUnit(state) || getIndentUnit(state)
@@ -484,11 +484,17 @@ const deleteByChar = (target: CommandTarget, forward: boolean) => deleteBy(targe
484484
return targetPos
485485
})
486486

487-
/// Delete the selection, or, for cursor selections, the character
488-
/// before the cursor.
489-
export const deleteCharBackward: Command = view => deleteByChar(view, false)
487+
/// Delete the selection, or, for cursor selections, the character or
488+
/// indentation unit before the cursor.
489+
export const deleteCharBackward: Command = view => deleteByChar(view, false, true)
490+
491+
/// Delete the selection or the character before the cursor. Does not
492+
/// implement any extended behavior like deleting whole indentation
493+
/// units in one go.
494+
export const deleteCharBackwardStrict: Command = view => deleteByChar(view, false, false)
495+
490496
/// Delete the selection or the character after the cursor.
491-
export const deleteCharForward: Command = view => deleteByChar(view, true)
497+
export const deleteCharForward: Command = view => deleteByChar(view, true, false)
492498

493499
const deleteByGroup = (target: CommandTarget, forward: boolean) => deleteBy(target, range => {
494500
let pos = range.head, {state} = target, line = state.doc.lineAt(pos)

0 commit comments

Comments
 (0)