-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description Fixes marked text input for sequences longer than one marked character. Also ensures marked text works consistently with multiple cursors and adds testing for the marked text functionality. Also: - Fixes a few lint markers in test files that have caused issues for others. - Adds a public `TextView.markedTextAttributes` property for modifying the marked text attributes if desired. ### Related Issues * closes #37 * closes #36 * closes #26 * closes CodeEditApp/CodeEditSourceEditor#188 ### Screenshots https://github.com/CodeEditApp/CodeEditTextView/assets/35942988/9f6eb84b-c668-45a4-9d30-75cbd5d4fccd
- Loading branch information
1 parent
40458fe
commit eb1d382
Showing
7 changed files
with
211 additions
and
59 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
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,127 @@ | ||
import XCTest | ||
@testable import CodeEditTextView | ||
|
||
class MarkedTextTests: XCTestCase { | ||
func test_markedTextSingleChar() { | ||
let textView = TextView(string: "") | ||
textView.selectionManager.setSelectedRange(.zero) | ||
|
||
textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "´") | ||
|
||
textView.insertText("é", replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "é") | ||
XCTAssertEqual(textView.selectionManager.textSelections.map(\.range), [NSRange(location: 1, length: 0)]) | ||
} | ||
|
||
func test_markedTextSingleCharInStrings() { | ||
let textView = TextView(string: "Lorem Ipsum") | ||
textView.selectionManager.setSelectedRange(NSRange(location: 5, length: 0)) | ||
|
||
textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "Lorem´ Ipsum") | ||
|
||
textView.insertText("é", replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "Loremé Ipsum") | ||
XCTAssertEqual(textView.selectionManager.textSelections.map(\.range), [NSRange(location: 6, length: 0)]) | ||
} | ||
|
||
func test_markedTextReplaceSelection() { | ||
let textView = TextView(string: "ABCDE") | ||
textView.selectionManager.setSelectedRange(NSRange(location: 4, length: 1)) | ||
|
||
textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "ABCD´") | ||
|
||
textView.insertText("é", replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "ABCDé") | ||
XCTAssertEqual(textView.selectionManager.textSelections.map(\.range), [NSRange(location: 5, length: 0)]) | ||
} | ||
|
||
func test_markedTextMultipleSelection() { | ||
let textView = TextView(string: "ABC") | ||
textView.selectionManager.setSelectedRanges([NSRange(location: 1, length: 0), NSRange(location: 2, length: 0)]) | ||
|
||
textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "A´B´C") | ||
|
||
textView.insertText("é", replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "AéBéC") | ||
XCTAssertEqual( | ||
textView.selectionManager.textSelections.map(\.range).sorted(by: { $0.location < $1.location }), | ||
[NSRange(location: 2, length: 0), NSRange(location: 4, length: 0)] | ||
) | ||
} | ||
|
||
func test_markedTextMultipleSelectionReplaceSelection() { | ||
let textView = TextView(string: "ABCDE") | ||
textView.selectionManager.setSelectedRanges([NSRange(location: 0, length: 1), NSRange(location: 4, length: 1)]) | ||
|
||
textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "´BCD´") | ||
|
||
textView.insertText("é", replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "éBCDé") | ||
XCTAssertEqual( | ||
textView.selectionManager.textSelections.map(\.range).sorted(by: { $0.location < $1.location }), | ||
[NSRange(location: 1, length: 0), NSRange(location: 5, length: 0)] | ||
) | ||
} | ||
|
||
func test_markedTextMultipleSelectionMultipleChar() { | ||
let textView = TextView(string: "ABCDE") | ||
textView.selectionManager.setSelectedRanges([NSRange(location: 0, length: 1), NSRange(location: 4, length: 1)]) | ||
|
||
textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "´BCD´") | ||
|
||
textView.setMarkedText("´´´", selectedRange: .notFound, replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "´´´BCD´´´") | ||
XCTAssertEqual( | ||
textView.selectionManager.textSelections.map(\.range).sorted(by: { $0.location < $1.location }), | ||
[NSRange(location: 3, length: 0), NSRange(location: 9, length: 0)] | ||
) | ||
|
||
textView.insertText("é", replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "éBCDé") | ||
XCTAssertEqual( | ||
textView.selectionManager.textSelections.map(\.range).sorted(by: { $0.location < $1.location }), | ||
[NSRange(location: 1, length: 0), NSRange(location: 5, length: 0)] | ||
) | ||
} | ||
|
||
func test_cancelMarkedText() { | ||
let textView = TextView(string: "") | ||
textView.selectionManager.setSelectedRange(.zero) | ||
|
||
textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "´") | ||
|
||
// The NSTextInputContext performs the following actions when a marked text segment is ended w/o replacing the | ||
// marked text: | ||
textView.insertText("´", replacementRange: .notFound) | ||
textView.insertText("4", replacementRange: .notFound) | ||
|
||
XCTAssertEqual(textView.string, "´4") | ||
XCTAssertEqual(textView.selectionManager.textSelections.map(\.range), [NSRange(location: 2, length: 0)]) | ||
} | ||
|
||
func test_cancelMarkedTextMultipleCursor() { | ||
let textView = TextView(string: "ABC") | ||
textView.selectionManager.setSelectedRanges([NSRange(location: 1, length: 0), NSRange(location: 2, length: 0)]) | ||
|
||
textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) | ||
XCTAssertEqual(textView.string, "A´B´C") | ||
|
||
// The NSTextInputContext performs the following actions when a marked text segment is ended w/o replacing the | ||
// marked text: | ||
textView.insertText("´", replacementRange: .notFound) | ||
textView.insertText("4", replacementRange: .notFound) | ||
|
||
XCTAssertEqual(textView.string, "A´4B´4C") | ||
XCTAssertEqual( | ||
textView.selectionManager.textSelections.map(\.range).sorted(by: { $0.location < $1.location }), | ||
[NSRange(location: 3, length: 0), NSRange(location: 6, length: 0)] | ||
) | ||
} | ||
} |
Oops, something went wrong.