1919/// ```
2020///
2121/// Set the anchor to force selection of the caret represented by its index. This is required
22- /// when the snapshot contains only formatting characters, and you want the user's caret to appear
23- /// at a specific location. As an example, a pattern text style with an empty value may anchor at the
24- /// index of the first placeholder character.
22+ /// when a snapshot contains only formatting characters, and you want the caret to appear
23+ /// at a location. As an example, a pattern style bound to an enmpty value may want to set
24+ /// the anchor at the index of the first placeholder character.
2525///
2626public struct Snapshot : BidirectionalCollection , RangeReplaceableCollection {
2727 @usableFromInline typealias Target = ( Index ) -> Bool
@@ -158,7 +158,7 @@ public extension Snapshot {
158158 }
159159
160160 //=------------------------------------------------------------------------=
161- // MARK: Attributes
161+ // MARK: Update
162162 //=------------------------------------------------------------------------=
163163
164164 @inlinable mutating func update( attributes index: Index ,
@@ -181,7 +181,7 @@ public extension Snapshot {
181181 }
182182
183183 //=------------------------------------------------------------------------=
184- // MARK: Replacements
184+ // MARK: Replace
185185 //=------------------------------------------------------------------------=
186186
187187 @inlinable mutating func replaceSubrange< C: Collection > (
@@ -239,10 +239,41 @@ extension Snapshot: CustomStringConvertible {
239239 }
240240}
241241
242+ //=----------------------------------------------------------------------------=
243+ // All extensions below this line are internal implementation details.
242244//=----------------------------------------------------------------------------=
243245// MARK: + Accessors
244246//=----------------------------------------------------------------------------=
245- // All extensions below this line are internal implementation details.
247+
248+ extension Snapshot {
249+
250+ //=------------------------------------------------------------------------=
251+ // MARK: Attributes
252+ //=------------------------------------------------------------------------=
253+
254+ @inlinable func nonpassthrough( at index: Index ) -> Bool {
255+ !attributes[ index. attribute] . contains ( . passthrough)
256+ }
257+
258+ //=------------------------------------------------------------------------=
259+ // MARK: Peek
260+ //=------------------------------------------------------------------------=
261+
262+ @inlinable func peek( from index: Index , towards direction: Direction ) -> Index ? {
263+ direction == . forwards ? peek ( ahead: index) : peek ( behind: index)
264+ }
265+
266+ @inlinable func peek( ahead index: Index ) -> Index ? {
267+ index != endIndex ? index : nil
268+ }
269+
270+ @inlinable func peek( behind index: Index ) -> Index ? {
271+ index != startIndex ? self . index ( before: index) : nil
272+ }
273+ }
274+
275+ //=----------------------------------------------------------------------------=
276+ // MARK: + Positions
246277//=----------------------------------------------------------------------------=
247278
248279extension Snapshot {
@@ -256,27 +287,27 @@ extension Snapshot {
256287 }
257288
258289 //=------------------------------------------------------------------------=
259- // MARK: Index
290+ // MARK: Single
260291 //=------------------------------------------------------------------------=
261292
262293 @inlinable func index< T> ( at position: T . Position ) -> Index where T: Offset {
263294 T . index ( at: position, in: characters)
264295 }
265296
266- @inlinable func indices < T> ( at positions : Range < T . Position > ) -> Range < Index > where T: Offset {
267- Range . from ( positions , map : index ( at : ) )
297+ @inlinable func position < T> ( at index : Index ) -> T . Position where T: Offset {
298+ T . position ( at : index, in : characters )
268299 }
269300
270301 //=------------------------------------------------------------------------=
271- // MARK: Position
302+ // MARK: Double
272303 //=------------------------------------------------------------------------=
273-
274- @inlinable func position < T> ( at index : Index ) -> T . Position where T: Offset {
275- T . position ( at : index , in : characters )
304+
305+ @inlinable func indices < T> ( at positions : Range < T . Position > ) -> Range < Index > where T: Offset {
306+ Range . map ( positions , bound : index ( at : ) )
276307 }
277308
278309 @inlinable func positions< T> ( at indices: Range < Index > ) -> Range < T . Position > where T: Offset {
279- Range . from ( indices, map : position ( at: ) )
310+ Range . map ( indices, bound : position ( at: ) )
280311 }
281312}
282313
@@ -307,29 +338,28 @@ extension Snapshot {
307338 //=--------------------------------------=
308339 let direction = direction ?? preference
309340 //=--------------------------------------=
310- // MARK: Search In The Direction
341+ // MARK: Search In Direction
311342 //=--------------------------------------=
312343 if let caret = caret ( from: index,
313344 towards: direction,
314345 jumping: direction == preference ? . to : . through,
315346 targeting: nonpassthrough ( at: ) ) { return caret }
316347 //=--------------------------------------=
317- // MARK: Search In The Other Direction
348+ // MARK: Search In Other Direction
318349 //=--------------------------------------=
319350 if let caret = caret ( from: index,
320351 towards: direction. reversed ( ) ,
321352 jumping: Jump . to, // use Jump.to on each direction
322353 targeting: nonpassthrough ( at: ) ) { return caret }
323354 //=--------------------------------------=
324- // MARK: Default To Instance End Index
355+ // MARK: Default To Instance's End Index
325356 //=--------------------------------------=
326357 return self . endIndex
327358 }
328359
329360 //=--------------------------------------------------------------------=
330361 // MARK: Forwards / Backwards / To / Through
331362 //=--------------------------------------------------------------------=
332-
333363
334364 @inlinable func caret( from index: Index , towards direction: Direction ,
335365 jumping distance: Jump , targeting target: Target ) -> Index ? {
@@ -356,28 +386,4 @@ extension Snapshot {
356386 @inlinable func caret( from index: Index , backwardsThrough target: Target ) -> Index ? {
357387 indices [ ..< index] . last ( where: target)
358388 }
359-
360- //=------------------------------------------------------------------------=
361- // MARK: Peek
362- //=------------------------------------------------------------------------=
363-
364- @inlinable func peek( from index: Index , towards direction: Direction ) -> Index ? {
365- direction == . forwards ? peek ( ahead: index) : peek ( behind: index)
366- }
367-
368- @inlinable func peek( ahead index: Index ) -> Index ? {
369- index != endIndex ? index : nil
370- }
371-
372- @inlinable func peek( behind index: Index ) -> Index ? {
373- index != startIndex ? self . index ( before: index) : nil
374- }
375-
376- //=------------------------------------------------------------------------=
377- // MARK: Passthrough
378- //=------------------------------------------------------------------------=
379-
380- @inlinable func nonpassthrough( at index: Index ) -> Bool {
381- !attributes[ index. attribute] . contains ( . passthrough)
382- }
383389}
0 commit comments