-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6278bde
commit 3c07ec9
Showing
10 changed files
with
548 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Foundation | ||
|
||
public final class MockRangeCalculator<R: Bounded & Equatable>: TextRangeCalculating where R.Bound == Int { | ||
public typealias TextRange = R | ||
|
||
private let textRangeBuilder: (Position, Position) -> TextRange? | ||
|
||
public init(textRangeBuilder: @escaping (Position, Position) -> TextRange?) { | ||
self.textRangeBuilder = textRangeBuilder | ||
} | ||
|
||
public var beginningOfDocument: Position { | ||
0 | ||
} | ||
|
||
public var endOfDocument: Position { | ||
0 | ||
} | ||
|
||
public func textRange(from fromPosition: Position, to toPosition: Position) -> TextRange? { | ||
textRangeBuilder(fromPosition, toPosition) | ||
} | ||
|
||
public func position(from position: Position, offset: Int) -> Position? { | ||
position + offset | ||
} | ||
|
||
public func position(from position: Position, in direction: Ligature.TextLayoutDirection, offset: Int) -> Position? { | ||
nil | ||
} | ||
|
||
public func offset(from: Position, to toPosition: Position) -> Int { | ||
toPosition - from | ||
} | ||
|
||
public func compare(_ position: Position, to other: Position) -> ComparisonResult { | ||
if position < other { | ||
return .orderedAscending | ||
} | ||
|
||
if other < position { | ||
return .orderedDescending | ||
} | ||
|
||
return .orderedSame | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#if os(macOS) | ||
import AppKit | ||
#elseif canImport(UIKit) | ||
import UIKit | ||
#endif | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *) | ||
@available(watchOS, unavailable) | ||
extension NSTextSelection.Granularity { | ||
public init(_ granularity: TextGranularity) { | ||
switch granularity { | ||
case .character: | ||
self = .character | ||
case .paragraph: | ||
self = .paragraph | ||
case .word: | ||
self = .word | ||
case .sentence: | ||
self = .sentence | ||
case .line: | ||
self = .line | ||
case .document: | ||
self = .paragraph | ||
@unknown default: | ||
self = .character | ||
} | ||
} | ||
|
||
public var textGranularity: TextGranularity { | ||
switch self { | ||
case .character: | ||
.character | ||
case .line: | ||
.line | ||
case .paragraph: | ||
.paragraph | ||
case .sentence: | ||
.sentence | ||
case .word: | ||
.word | ||
@unknown default: | ||
.character | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
public struct RangeTranslator<RangeA, RangeB> { | ||
public let to: (RangeA) -> RangeB? | ||
public let from: (RangeB) -> RangeA? | ||
|
||
public init( | ||
to: @escaping (RangeA) -> RangeB?, | ||
from: @escaping (RangeB) -> RangeA? | ||
) { | ||
self.to = to | ||
self.from = from | ||
} | ||
} | ||
|
||
#if os(macOS) | ||
import AppKit | ||
|
||
extension NSTextView { | ||
public var rangeTranslator: RangeTranslator<NSRange, TextRange> { | ||
.init( | ||
to: { [weak self] in | ||
self?.textRange(from: $0) | ||
}, | ||
from: { [weak self] textRange in | ||
guard let self else { return nil } | ||
|
||
return NSRange(textRange, textView: self) | ||
} | ||
) | ||
} | ||
} | ||
#endif |
Oops, something went wrong.