diff --git a/Sources/Ligature/MockTextTokenizer.swift b/Sources/Ligature/MockTextTokenizer.swift new file mode 100644 index 0000000..b5bc403 --- /dev/null +++ b/Sources/Ligature/MockTextTokenizer.swift @@ -0,0 +1,43 @@ +public final class MockTextTokenizer: TextTokenizer { + public enum Request: Hashable { + case position(Int, TextGranularity, TextDirection) + } + + public enum Response: Hashable { + case position(Int?) + case rangeEnclosingPosition(Range?) + } + + public private(set) var requests: [Request] = [] + public var responses: [Response] = [] + + public init() { + } + + public func closestMatchingVerticalLocation(to location: Int, above: Bool) -> Int? { + return nil + } + + public func position(from position: Int, toBoundary granularity: TextGranularity, inDirection direction: TextDirection) -> Int? { + requests.append(.position(position, granularity, direction)) + + switch responses.removeFirst() { + case let .position(value): + return value + default: + fatalError("wrong return type") + } + } + + public func rangeEnclosingPosition(_ position: Int, with granularity: TextGranularity, inDirection direction: TextDirection) -> Range? { + return nil + } + + public func isPosition(_ position: Int, atBoundary granularity: TextGranularity, inDirection direction: TextDirection) -> Bool { + return false + } + + public func isPosition(_ position: Int, withinTextUnit granularity: TextGranularity, inDirection direction: TextDirection) -> Bool { + return false + } +} diff --git a/Sources/Ligature/Platform.swift b/Sources/Ligature/Platform.swift index 62ad02d..8b36e5f 100644 --- a/Sources/Ligature/Platform.swift +++ b/Sources/Ligature/Platform.swift @@ -1,7 +1,6 @@ -#if canImport(AppKit) && !targetEnvironment(macCatalyst) +#if os(macOS) import AppKit -@MainActor open class TextPosition: NSObject { } @@ -191,6 +190,8 @@ extension NSTextSelection.Granularity { self = .line case .document: self = .paragraph + @unknown default: + self = .character } } } diff --git a/Sources/Ligature/SourceTokenizer.swift b/Sources/Ligature/SourceTokenizer.swift index c95f453..d5c6933 100644 --- a/Sources/Ligature/SourceTokenizer.swift +++ b/Sources/Ligature/SourceTokenizer.swift @@ -1,6 +1,6 @@ import Foundation -public struct SourceTokenizer where FallbackTokenzier.Position == Position { +public struct SourceTokenizer where FallbackTokenzier.Position == Position, FallbackTokenzier.TextRange == TextRange { private let fallbackTokenzier: FallbackTokenzier init(fallbackTokenzier: FallbackTokenzier) { @@ -13,7 +13,7 @@ extension SourceTokenizer : TextTokenizer { return fallbackTokenzier.position(from: position, toBoundary: granularity, inDirection: direction) } - public func rangeEnclosingPosition(_ position: Position, with granularity: TextGranularity, inDirection direction: TextDirection) -> TextRange? { + public func rangeEnclosingPosition(_ position: Position, with granularity: TextGranularity, inDirection direction: TextDirection) -> Ligature.TextRange? { return fallbackTokenzier.rangeEnclosingPosition(position, with: granularity, inDirection: direction) } diff --git a/Sources/Ligature/TextInputStringTokenizer.swift b/Sources/Ligature/TextInputStringTokenizer.swift index 9cdcc4d..80a11f9 100644 --- a/Sources/Ligature/TextInputStringTokenizer.swift +++ b/Sources/Ligature/TextInputStringTokenizer.swift @@ -1,5 +1,5 @@ import Foundation -#if canImport(AppKit) && !targetEnvironment(macCatalyst) +#if os(macOS) import AppKit extension NSTextView { diff --git a/Sources/Ligature/TextTokenizer.swift b/Sources/Ligature/TextTokenizer.swift index 51582fb..805eca9 100644 --- a/Sources/Ligature/TextTokenizer.swift +++ b/Sources/Ligature/TextTokenizer.swift @@ -1,8 +1,9 @@ import Foundation @MainActor -public protocol TextTokenizer { - associatedtype Position : TextPosition +public protocol TextTokenizer { + associatedtype Position + associatedtype TextRange func position(from position: Position, toBoundary granularity: TextGranularity, inDirection direction: TextDirection) -> Position? func rangeEnclosingPosition(_ position: Position, with granularity: TextGranularity, inDirection direction: TextDirection) -> TextRange?