8000 Add insertNewlineKeepIndent command · codemirror/commands@6a9d016 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a9d016

Browse files
committed
Add insertNewlineKeepIndent command
FEATURE: The `insertNewlineKeepIndent` command inserts a newline along with the same indentation as the line before. Issue codemirror/dev#1370
1 parent f999611 commit 6a9d016

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ with key bindings for a lot of them.
187187

188188
@insertNewlineAndIndent
189189

190+
@insertNewlineKeepIndent
191+
190192
@insertBlankLine
191193

192194
### Undo History

src/commands.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,19 @@ export const insertNewline: StateCommand = ({state, dispatch}) => {
697697
return true
698698
}
699699

700+
/// Replace the selection with a newline and the same amount of
701+
/// indentation as the line above.
702+
export const insertNewlineKeepIndent: StateCommand = ({state, dispatch}) => {
703+
dispatch(state.update(state.changeByRange(range => {
704+
let indent = /^\s*/.exec(state.doc.lineAt(range.from).text)![0]
705+
return {
706+
changes: {from: range.from, to: range.to, insert: state.lineBreak + indent},
707+
range: EditorSelection.cursor(range.from + indent.length + 1)
708+
}
709+
}), {scrollIntoView: true, userEvent: "input"}))
710+
return true
711+
}
712+
700713
function isBetweenBrackets(state: EditorState, pos: number): {from: number, to: number} | null {
701714
if (/\(\)|\[\]|\{\}/.test(state.sliceDoc(pos - 1, pos + 1))) return {from: pos, to: pos}
702715
let context = syntaxTree(state).resolveInner(pos)

test/test-commands.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {EditorState, StateCommand, Extension} from "@codemirror/state"
2-
import {indentMore, indentLess, indentSelection, insertNewlineAndIndent,
2+
import {indentMore, indentLess, indentSelection,
3+
insertNewlineAndIndent, insertNewlineKeepIndent,
34
deleteTrailingWhitespace, deleteGroupForward, deleteGroupBackward,
45
moveLineUp, moveLineDown} from "@codemirror/commands"
56
import {javascriptLanguage} from "@codemirror/lang-javascript"
@@ -66,6 +67,21 @@ describe("commands", () => {
6667
test("<{\n{\n{\n{}\n}\n}\n}>", "<{\n {\n {\n {}\n }\n }\n}>"))
6768
})
6869

70+
describe("insertNewlineKeepIndent", () => {
71+
function test(from: string, to: string) {
72+
ist(stateStr(cmd(mkState(from), insertNewlineKeepIndent)), to)
73+
}
74+
75+
it("keeps indentation", () =>
76+
test(" one|", " one\n |"))
77+
78+
it("keeps zero indentation", () =>
79+
test("one|two", "one\n|two"))
80+
81+
it("deletes the selection", () =>
82+
test("if x:\n one<two\n three>four", "if x:\n one\n |four"))
83+
})
84+
6985
describe("insertNewlineAndIndent", () => {
7086
function test(from: string, to: string) {
7187
ist(stateStr(cmd(mkState(from, javascriptLanguage), insertNewlineAndIndent)), to)

0 commit comments

Comments
 (0)
0