diff --git a/src/README.md b/src/README.md index 8c1ea4c..1ed2fa7 100644 --- a/src/README.md +++ b/src/README.md @@ -59,6 +59,10 @@ with key bindings for a lot of them. @selectGroupBackward +@cursorGroupForwardWin + +@selectGroupForwardWin + @cursorSubwordForward @selectSubwordForward diff --git a/src/commands.ts b/src/commands.ts index db152cd..4e1eb88 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -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" @@ -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 @@ -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)) }