-
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.
- Loading branch information
1 parent
80911be
commit e95fbfb
Showing
6 changed files
with
170 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// KillRing.swift | ||
// CodeEditTextView | ||
// | ||
// Created by Khan Winter on 6/13/24. | ||
// | ||
|
||
import Foundation | ||
|
||
// swiftlint:disable line_length | ||
|
||
/// A global kill ring similar to emacs. With support for killing and yanking multiple cursors. | ||
/// | ||
/// Documentation sources: | ||
/// - [Emacs kill ring](https://www.gnu.org/software/emacs/manual/html_node/emacs/Yanking.html) | ||
/// - [Cocoa Docs](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/TextDefaultsBindings/TextDefaultsBindings.html) | ||
class KillRing { | ||
static let shared: KillRing = KillRing() | ||
|
||
// swiftlint:enable line_length | ||
|
||
private static let bufferSizeKey = "NSTextKillRingSize" | ||
|
||
private var buffer: [[String]] | ||
private var index = 0 | ||
|
||
init(_ size: Int? = nil) { | ||
buffer = Array( | ||
repeating: [""], | ||
count: size ?? max(1, UserDefaults.standard.integer(forKey: Self.bufferSizeKey)) | ||
) | ||
} | ||
|
||
/// Performs the kill action in response to a delete action. Saving the deleted text to the kill ring. | ||
func kill(strings: [String]) { | ||
incrementIndex() | ||
buffer[index] = strings | ||
} | ||
|
||
/// Yanks the current item in the ring. | ||
func yank() -> [String] { | ||
return buffer[index] | ||
} | ||
|
||
/// Yanks an item from the ring, and selects the next one in the ring. | ||
func yankAndSelect() -> [String] { | ||
let retVal = buffer[index] | ||
incrementIndex() | ||
return retVal | ||
} | ||
|
||
private func incrementIndex() { | ||
index = (index + 1) % buffer.count | ||
} | ||
} |
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,73 @@ | ||
import XCTest | ||
@testable import CodeEditTextView | ||
|
||
class KillRingTests: XCTestCase { | ||
func test_killRingYank() { | ||
var ring = KillRing.shared | ||
ring.kill(strings: ["hello"]) | ||
for _ in 0..<100 { | ||
XCTAssertEqual(ring.yank(), ["hello"]) | ||
} | ||
|
||
ring.kill(strings: ["hello", "multiple", "strings"]) | ||
// should never change on yank | ||
for _ in 0..<100 { | ||
XCTAssertEqual(ring.yank(), ["hello", "multiple", "strings"]) | ||
} | ||
|
||
ring = KillRing(2) | ||
ring.kill(strings: ["hello"]) | ||
for _ in 0..<100 { | ||
XCTAssertEqual(ring.yank(), ["hello"]) | ||
} | ||
|
||
ring.kill(strings: ["hello", "multiple", "strings"]) | ||
// should never change on yank | ||
for _ in 0..<100 { | ||
XCTAssertEqual(ring.yank(), ["hello", "multiple", "strings"]) | ||
} | ||
} | ||
|
||
func test_killRingYankAndSelect() { | ||
let ring = KillRing(5) | ||
ring.kill(strings: ["1"]) | ||
ring.kill(strings: ["2"]) | ||
ring.kill(strings: ["3", "3", "3"]) | ||
ring.kill(strings: ["4", "4"]) | ||
ring.kill(strings: ["5"]) | ||
// should loop | ||
for _ in 0..<5 { | ||
XCTAssertEqual(ring.yankAndSelect(), ["5"]) | ||
XCTAssertEqual(ring.yankAndSelect(), ["1"]) | ||
XCTAssertEqual(ring.yankAndSelect(), ["2"]) | ||
XCTAssertEqual(ring.yankAndSelect(), ["3", "3", "3"]) | ||
XCTAssertEqual(ring.yankAndSelect(), ["4", "4"]) | ||
} | ||
} | ||
|
||
func test_textViewYank() { | ||
let view = TextView(string: "Hello World") | ||
view.selectionManager.setSelectedRange(NSRange(location: 0, length: 1)) | ||
view.delete(self) | ||
XCTAssertEqual(view.string, "ello World") | ||
|
||
view.yank(self) | ||
XCTAssertEqual(view.string, "Hello World") | ||
view.selectionManager.setSelectedRange(NSRange(location: 0, length: 0)) | ||
view.yank(self) | ||
XCTAssertEqual(view.string, "HHello World") | ||
} | ||
|
||
func test_textViewYankMultipleCursors() { | ||
let view = TextView(string: "Hello World") | ||
view.selectionManager.setSelectedRanges([NSRange(location: 1, length: 0), NSRange(location: 4, length: 0)]) | ||
view.delete(self) | ||
XCTAssertEqual(view.string, "elo World") | ||
|
||
view.yank(self) | ||
XCTAssertEqual(view.string, "Hello World") | ||
view.selectionManager.setSelectedRanges([NSRange(location: 0, length: 0)]) | ||
view.yank(self) | ||
XCTAssertEqual(view.string, "H\nlHello World") | ||
} | ||
} |