Skip to content

Commit

Permalink
Add deleteCharBackwardStrict commands
Browse files Browse the repository at this point in the history
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
  • Loading branch information
marijnh committed Apr 17, 2024
1 parent 19491af commit 8c7d3d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ with key bindings for a lot of them.

@deleteCharBackward

@deleteCharBackwardStrict

@deleteCharForward

@deleteGroupBackward
Expand Down
18 changes: 12 additions & 6 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,9 @@ function skipAtomic(target: CommandTarget, pos: number, forward: boolean) {
return pos
}

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

/// Delete the selection, or, for cursor selections, the character
/// before the cursor.
export const deleteCharBackward: Command = view => deleteByChar(view, false)
/// Delete the selection, or, for cursor selections, the character or
/// indentation unit before the cursor.
export const deleteCharBackward: Command = view => deleteByChar(view, false, true)

/// Delete the selection or the character before the cursor. Does not
/// implement any extended behavior like deleting whole indentation
/// units in one go.
export const deleteCharBackwardStrict: Command = view => deleteByChar(view, false, false)

/// Delete the selection or the character after the cursor.
export const deleteCharForward: Command = view => deleteByChar(view, true)
export const deleteCharForward: Command = view => deleteByChar(view, true, false)

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

0 comments on commit 8c7d3d9

Please sign in to comment.