Skip to content

Commit

Permalink
Generic over TextRange, MockTextTokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Aug 31, 2024
1 parent 036f19e commit b184b82
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
43 changes: 43 additions & 0 deletions Sources/Ligature/MockTextTokenizer.swift
Original file line number Diff line number Diff line change
@@ -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<Int>?)
}

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<Int>? {
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
}
}
5 changes: 3 additions & 2 deletions Sources/Ligature/Platform.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
#if os(macOS)
import AppKit

@MainActor
open class TextPosition: NSObject {

}
Expand Down Expand Up @@ -191,6 +190,8 @@ extension NSTextSelection.Granularity {
self = .line
case .document:
self = .paragraph
@unknown default:

Check warning on line 193 in Sources/Ligature/Platform.swift

View workflow job for this annotation

GitHub Actions / Test (platform=macOS)

default will never be executed

Check warning on line 193 in Sources/Ligature/Platform.swift

View workflow job for this annotation

GitHub Actions / Test (platform=iOS Simulator,name=iPhone 12)

default will never be executed
self = .character
}
}
}
4 changes: 2 additions & 2 deletions Sources/Ligature/SourceTokenizer.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct SourceTokenizer<Position, FallbackTokenzier: TextTokenizer> where FallbackTokenzier.Position == Position {
public struct SourceTokenizer<Position, FallbackTokenzier: TextTokenizer> where FallbackTokenzier.Position == Position, FallbackTokenzier.TextRange == TextRange {
private let fallbackTokenzier: FallbackTokenzier

init(fallbackTokenzier: FallbackTokenzier) {
Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Ligature/TextInputStringTokenizer.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
#if os(macOS)
import AppKit

extension NSTextView {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Ligature/TextTokenizer.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Foundation

@MainActor
public protocol TextTokenizer<Position> {
associatedtype Position : TextPosition
public protocol TextTokenizer<Position, TextRange> {
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?
Expand Down

0 comments on commit b184b82

Please sign in to comment.