Skip to content

Commit

Permalink
Export cursorGroupForwardWin and selectGroupForwardWin
Browse files Browse the repository at this point in the history
FEATURE: The new `cursorGroupForwardWin` and `selectGroupForwardWin` commands implement
Windows-style forward motion by group.

Issue codemirror/dev#1500
  • Loading branch information
marijnh committed Jan 8, 2025
1 parent f57dcff commit c9b8340
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ with key bindings for a lot of them.

@selectGroupBackward

@cursorGroupForwardWin

@selectGroupForwardWin

@cursorSubwordForward

@selectSubwordForward
Expand Down
27 changes: 26 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {EditorState, StateCommand, EditorSelection, SelectionRange,
StateEffect, ChangeSpec, Transaction, CharCategory,
findClusterBreak, Text, Line, countColumn} from "@codemirror/state"
findClusterBreak, Text, Line, countColumn, CharCategory} from "@codemirror/state"
import {EditorView, Command, Direction, KeyBinding} from "@codemirror/view"
import {syntaxTree, IndentContext, getIndentUnit, indentUnit, indentString,
getIndentation, matchBrackets} from "@codemirror/language"
Expand Down Expand Up @@ -89,6 +89,25 @@ export const cursorGroupForward: Command = view => cursorByGroup(view, true)
/// Move the selection one group backward.
export const cursorGroupBackward: Command = view => cursorByGroup(view, false)

function toGroupStart(view: EditorView, pos: number, start: string) {
let categorize = view.state.charCategorizer(pos)
let cat = categorize(start), initial = cat != CharCategory.Space
return (next: string) => {
let nextCat = categorize(next)
if (nextCat != CharCategory.Space) return initial && nextCat == cat
initial = false
return true
}
}

/// Move the cursor one group forward in the default Windows style,
/// where it moves to the start of the next group.
export const cursorGroupForwardWin: Command = view => {
return moveSel(view, range => range.empty
? view.moveByChar(range, true, start => toGroupStart(view, range.head, start))
: rangeEnd(range, true))
}

const segmenter = typeof Intl != "undefined" && (Intl as any).Segmenter ?
new ((Intl as any).Segmenter)(undefined, {granularity: "word"}) : null

Expand Down Expand Up @@ -338,6 +357,12 @@ export const selectGroupForward: Command = view => selectByGroup(view, true)
/// Move the selection head one group backward.
export const selectGroupBackward: Command = view => selectByGroup(view, false)

/// Move the selection head one group forward in the default Windows
/// style, skipping to the start of the next group.
export const selectGroupForwardWin: Command = view => {
return extendSel(view, range => view.moveByChar(range, true, start => toGroupStart(view, range.head, start)))
}

function selectBySubword(view: EditorView, forward: boolean) {
return extendSel(view, range => moveBySubword(view, range, forward))
}
Expand Down

0 comments on commit c9b8340

Please sign in to comment.