Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit e035c74

Browse files
committed
Support an assoc argument to EditorSelection.range and SelectionRange.extend
FEATURE: `EditorSelection.range` now takes an optional `assoc` argument. FEATURE: `SelectionRange.extend` can now be given a third argument to specify associativity. Issue codemirror/dev#1681
1 parent eef74db commit e035c74

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/selection.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ export class SelectionRange {
7878
}
7979

8080
/// Extend this range to cover at least `from` to `to`.
81-
extend(from: number, to: number = from) {
82-
if (from <= this.anchor && to >= this.anchor) return EditorSelection.range(from, to)
81+
extend(from: number, to: number = from, assoc = 0) {
82+
if (from <= this.anchor && to >= this.anchor) return EditorSelection.range(from, to, undefined, undefined, assoc)
8383
let head = Math.abs(from - this.anchor) > Math.abs(to - this.anchor) ? from : to
84-
return EditorSelection.range(this.anchor, head)
84+
return EditorSelection.range(this.anchor, head, undefined, undefined, assoc)
8585
}
8686

8787
/// Compare this range to another range.
@@ -201,11 +201,12 @@ export class EditorSelection {
201201
}
202202

203203
/// Create a selection range.
204-
static range(anchor: number, head: number, goalColumn?: number, bidiLevel?: number) {
204+
static range(anchor: number, head: number, goalColumn?: number, bidiLevel?: number, assoc?: number) {
205205
let flags = ((goalColumn ?? RangeFlag.NoGoalColumn) << RangeFlag.GoalColumnOffset) |
206206
(bidiLevel == null ? 7 : Math.min(6, bidiLevel))
207+
if (!assoc && anchor != head) assoc = head < anchor ? 1 : -1
207208
return head < anchor ? SelectionRange.create(head, anchor, RangeFlag.Inverted | RangeFlag.AssocAfter | flags)
208-
: SelectionRange.create(anchor, head, (head > anchor ? RangeFlag.AssocBefore : 0) | flags)
209+
: SelectionRange.create(anchor, head, (!assoc ? 0 : assoc < 0 ? RangeFlag.AssocBefore : RangeFlag.AssocAfter) | flags)
209210
}
210211

211212
/// @internal

0 commit comments

Comments
 (0)