Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Newline String Option #8

Merged
merged 1 commit into from
May 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/TextFormation/NewlineProcessingFilter.swift
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ import TextStory
public class NewlineProcessingFilter {
private let recognizer: ConsecutiveCharacterRecognizer

public init() {
self.recognizer = ConsecutiveCharacterRecognizer(matching: "\n")
public init(newline: String = "\n") {
self.recognizer = ConsecutiveCharacterRecognizer(matching: newline)
}

private func filterHandler(_ mutation: TextMutation, in interface: TextInterface, with providers: WhitespaceProviders) -> FilterAction {
37 changes: 37 additions & 0 deletions Tests/TextFormationTests/NewlineProcessingFilterTests.swift
Original file line number Diff line number Diff line change
@@ -68,4 +68,41 @@ final class NewlineProcessingFilterTests: XCTestCase {
XCTAssertEqual(interface.string, "\t\n\t\n\t")
XCTAssertEqual(interface.insertionLocation, 5)
}

func testMatchingWithUnknownNewline() {
let interface = TextInterfaceAdapter("")
let filter = NewlineProcessingFilter(newline: "crlf")

let mutation = TextMutation(insert: "crlf", at: 0, limit: 0)

XCTAssertEqual(filter.processMutation(mutation, in: interface, with: Self.providers), .discard)

XCTAssertEqual(interface.string, "crlf\t")
XCTAssertEqual(interface.insertionLocation, 5)
}

func testMatchingWithUnknownNewlineAndTrailingWhitespace() {
let interface = TextInterfaceAdapter(" ")
let filter = NewlineProcessingFilter(newline: "crlf")

let mutation = TextMutation(insert: "crlf", at: 1, limit: 1)

XCTAssertEqual(interface.insertionLocation, 1)
XCTAssertEqual(filter.processMutation(mutation, in: interface, with: Self.providers), .discard)

XCTAssertEqual(interface.string, " crlf\t")
XCTAssertEqual(interface.insertionLocation, 6)
}

func testMatchingWithUnknownNewlineAndTrailingTab() {
let interface = TextInterfaceAdapter("abc\t")
let filter = NewlineProcessingFilter(newline: "crlf")

let mutation = TextMutation(insert: "crlf", at: 4, limit: 4)

XCTAssertEqual(filter.processMutation(mutation, in: interface, with: Self.providers), .discard)

XCTAssertEqual(interface.string, "abc crlf\t")
XCTAssertEqual(interface.insertionLocation, 9)
}
}
Loading