Skip to content

Commit

Permalink
Add visualLineSide helper
Browse files Browse the repository at this point in the history
FEATURE: `EditorView.visualLineSide` can be used to find the visual end or start of
a line with bidirectional text.
  • Loading branch information
marijnh committed Dec 28, 2023
1 parent fae01eb commit 9d2c195
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export function moveByChar(view: EditorView, start: SelectionRange, forward: boo
char = "\n"
line = view.state.doc.line(line.number + (forward ? 1 : -1))
spans = view.bidiSpans(line)
next = EditorSelection.cursor(forward ? line.from : line.to)
next = view.visualLineSide(line, !forward)
}
if (!check) {
if (!by) return next
Expand Down
10 changes: 10 additions & 0 deletions src/editorview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,16 @@ export class EditorView {
return skipAtoms(this, start, moveByChar(this, start, forward, initial => byGroup(this, start.head, initial)))
}

/// Get the cursor position visually at the start or end of a line.
/// Note that this may differ from the _logical_ position at its
/// start or end (which is simply at `line.from`/`line.to`) if text
/// at the start or end goes against the line's base text direction.
visualLineSide(line: Line, end: boolean) {
let order = this.bidiSpans(line), dir = this.textDirectionAt(line.from)
let span = order[end ? order.length - 1 : 0]
return EditorSelection.cursor(span.side(end, dir) + line.from, span.forward(!end, dir) ? 1 : -1)
}

/// Move to the next line boundary in the given direction. If
/// `includeWrap` is true, line wrapping is on, and there is a
/// further wrap point on the current line, the wrap point will be
Expand Down

0 comments on commit 9d2c195

Please sign in to comment.