Skip to content

Commit

Permalink
Add insertNewlineKeepIndent command
Browse files Browse the repository at this point in the history
FEATURE: The `insertNewlineKeepIndent` command inserts a newline along
with the same indentation as the line before.

Issue codemirror/dev#1370
  • Loading branch information
marijnh committed Apr 19, 2024
1 parent f999611 commit 6a9d016
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ with key bindings for a lot of them.

@insertNewlineAndIndent

@insertNewlineKeepIndent

@insertBlankLine

### Undo History
Expand Down
13 changes: 13 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,19 @@ export const insertNewline: StateCommand = ({state, dispatch}) => {
return true
}

/// Replace the selection with a newline and the same amount of
/// indentation as the line above.
export const insertNewlineKeepIndent: StateCommand = ({state, dispatch}) => {
dispatch(state.update(state.changeByRange(range => {
let indent = /^\s*/.exec(state.doc.lineAt(range.from).text)![0]
return {
changes: {from: range.from, to: range.to, insert: state.lineBreak + indent},
range: EditorSelection.cursor(range.from + indent.length + 1)
}
}), {scrollIntoView: true, userEvent: "input"}))
return true
}

function isBetweenBrackets(state: EditorState, pos: number): {from: number, to: number} | null {
if (/\(\)|\[\]|\{\}/.test(state.sliceDoc(pos - 1, pos + 1))) return {from: pos, to: pos}
let context = syntaxTree(state).resolveInner(pos)
Expand Down
18 changes: 17 additions & 1 deletion test/test-commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {EditorState, StateCommand, Extension} from "@codemirror/state"
import {indentMore, indentLess, indentSelection, insertNewlineAndIndent,
import {indentMore, indentLess, indentSelection,
insertNewlineAndIndent, insertNewlineKeepIndent,
deleteTrailingWhitespace, deleteGroupForward, deleteGroupBackward,
moveLineUp, moveLineDown} from "@codemirror/commands"
import {javascriptLanguage} from "@codemirror/lang-javascript"
Expand Down Expand Up @@ -66,6 +67,21 @@ describe("commands", () => {
test("<{\n{\n{\n{}\n}\n}\n}>", "<{\n {\n {\n {}\n }\n }\n}>"))
})

describe("insertNewlineKeepIndent", () => {
function test(from: string, to: string) {
ist(stateStr(cmd(mkState(from), insertNewlineKeepIndent)), to)
}

it("keeps indentation", () =>
test(" one|", " one\n |"))

it("keeps zero indentation", () =>
test("one|two", "one\n|two"))

it("deletes the selection", () =>
test("if x:\n one<two\n three>four", "if x:\n one\n |four"))
})

describe("insertNewlineAndIndent", () => {
function test(from: string, to: string) {
ist(stateStr(cmd(mkState(from, javascriptLanguage), insertNewlineAndIndent)), to)
Expand Down

0 comments on commit 6a9d016

Please sign in to comment.