1818/// |x|o|o|x|x|o|o|o|x|x|o|o|o|x|o|x|x|x|x|~
1919/// ```
2020///
21- /// **Anchor**
22- ///
23- /// Set the anchor index to select the caret represented by it. This can be
24- /// desirable on snapshots containing only formatting characters. A pattern
25- /// style may set the anchor at its first placeholder character, for example.
26- ///
27- /// ```
28- /// ↓ == anchor
29- /// |+|#|#|_|(|#|#|#|)|_|#|#|#|-|#|#|-|#|#|~
30- /// |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|~
31- /// ```
32- ///
3321/// **Attributes & Characters**
3422///
3523/// The number of attributes must equal the number of joint characters in the
4735/// |➖| + |➖| + |➖| + |➖| -> |➖|➖|➖|➖|~ (BAD)
4836/// ```
4937///
38+ /// **Selection**
39+ ///
40+ /// Selection is done by differentiation, but it can also be done manually. It
41+ /// can be appropriate on snapshots containing only formatting characters. A pattern
42+ /// text style may manually select its first placeholder character, for example.
43+ ///
44+ /// ```
45+ /// ↓ == selection
46+ /// |+|#|#|_|(|#|#|#|)|_|#|#|#|-|#|#|-|#|#|~
47+ /// |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|~
48+ /// ```
49+ ///
5050public struct Snapshot : BidirectionalCollection , CustomStringConvertible , Equatable ,
5151ExpressibleByArrayLiteral , ExpressibleByStringLiteral , RangeReplaceableCollection {
5252
@@ -57,9 +57,9 @@ ExpressibleByArrayLiteral, ExpressibleByStringLiteral, RangeReplaceableCollectio
5757 // MARK: State
5858 //=------------------------------------------------------------------------=
5959
60- @usableFromInline var _characters : String
61- @usableFromInline var _attributes : [ Attribute ]
62- @ usableFromInline var _anchor : Self . Index ?
60+ @usableFromInline private ( set ) var _characters : String
61+ @usableFromInline private ( set ) var _attributes : [ Attribute ]
62+ public var selection : Selection < Index > ?
6363
6464 //=------------------------------------------------------------------------=
6565 // MARK: Initializers
@@ -92,11 +92,6 @@ ExpressibleByArrayLiteral, ExpressibleByStringLiteral, RangeReplaceableCollectio
9292 _attributes
9393 }
9494
95- @inlinable @inline ( __always)
96- public var anchor : Index ? {
97- _anchor
98- }
99-
10095 public var description : String {
10196 " \( Self . self) ( \" \( _characters) \" , \( _attributes) ) "
10297 }
@@ -106,13 +101,13 @@ ExpressibleByArrayLiteral, ExpressibleByStringLiteral, RangeReplaceableCollectio
106101 //=------------------------------------------------------------------------=
107102
108103 @inlinable @inline ( __always)
109- public mutating func anchorAtEndIndex ( ) {
110- self . _anchor = endIndex
104+ public mutating func select ( _ position : Index ) {
105+ self . selection = Selection ( position )
111106 }
112107
113108 @inlinable @inline ( __always)
114- public mutating func anchor ( at index : Index ? ) {
115- self . _anchor = index
109+ public mutating func select ( _ positions : Range < Index > ) {
110+ self . selection = Selection ( positions )
116111 }
117112
118113 //=------------------------------------------------------------------------=
@@ -387,11 +382,11 @@ extension Snapshot {
387382 // MARK: Resolve
388383 //=------------------------------------------------------------------------=
389384
385+ @inlinable func resolve( _ selection: Selection < Caret < Index > > ) -> Selection < Index > {
386+ self . selection ?? selection. map ( resolve)
387+ }
388+
390389 @inlinable func resolve( _ caret: Caret < Index > ) -> Index {
391- //=--------------------------------------=
392- // Anchor
393- //=--------------------------------------=
394- if let anchor { return anchor }
395390 //=--------------------------------------=
396391 // Inspect Initial Index
397392 //=--------------------------------------=
0 commit comments