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

Added insertString and deleteString #3

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
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
49 changes: 49 additions & 0 deletions Sources/TextViewPlus/NSTextView+AttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public extension NSTextView {
return attributedSubstring(forProposedRange: range, actualRange: actualRange)?.copy() as? NSAttributedString
}

/// Undoable replacement of attributed string in specified range
func replaceString(in range: NSRange, with attributedString: NSAttributedString) {
if let manager = undoManager {
let originalString = safeAttributedSubstring(forProposedRange: range, actualRange: nil)
Expand All @@ -50,4 +51,52 @@ public extension NSTextView {

replaceCharacters(in: range, with: attributedString)
}

func insertCharacters(_ attributedString: NSAttributedString, at location: Int) {
guard let storage = textStorage else {
fatalError("Unable to replace characters in a textview without a backing NSTextStorage")
}

storage.insert(attributedString, at: location)

didChangeText()
}

/// Undoable insertion of attributed string at specified location
func insertString(_ attributedString: NSAttributedString, at location: Int) {
if let manager = undoManager {
let inverseLength = attributedString.length
let inverseRange = NSRange(location: location, length: inverseLength)

manager.registerUndo(withTarget: self, handler: { view in
view.deleteString(in: inverseRange)
})
}

insertCharacters(attributedString, at: location)
}

func deleteCharacters(in range: NSRange) {
guard let storage = textStorage else {
fatalError("Unable to replace characters in a textview without a backing NSTextStorage")
}

storage.deleteCharacters(in: range)

didChangeText()
}

/// Undoable deletion of string in specified range
func deleteString(in range: NSRange) {
if let manager = undoManager {
let originalString = safeAttributedSubstring(forProposedRange: range, actualRange: nil)
let usableReplacementString = originalString ?? NSAttributedString()

manager.registerUndo(withTarget: self, handler: { view in
view.insertString(usableReplacementString, at: range.location)
})
}

deleteCharacters(in: range)
}
}
38 changes: 38 additions & 0 deletions Tests/TextViewPlusTests/TextViewPlusTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,44 @@ final class TextViewPlusTests: XCTestCase {

XCTAssertEqual(textView.string, "abc")
}

func testProgrammaticInsertionOfAttributedString() {
let textView = TestableTextView(string: "abc")

let attrString = NSAttributedString(string: "z")
textView.insertString(attrString, at: 1)

XCTAssertEqual(textView.string, "azbc")

textView.undoManager!.undo()

XCTAssertEqual(textView.string, "abc")
}

func testProgrammaticDeletionOfAttributedString() {
let textView = TestableTextView(string: "abc")

textView.deleteString(in: NSRange(location: 1, length: 1))

XCTAssertEqual(textView.string, "ac")

textView.undoManager!.undo()

XCTAssertEqual(textView.string, "abc")
}

func testProgrammaticDeletionOfAttributedStringWithFullRange() {
let textView = TestableTextView(string: "abc")

textView.deleteString(in: NSRange(location: 0, length: 3))

XCTAssert(textView.string.isEmpty)

textView.undoManager!.undo()

XCTAssertEqual(textView.string, "abc")
}

}

extension TextViewPlusTests {
Expand Down
Loading