From b8acee06dc46b5b6b1aa9fb350a6f64932cda20a Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 14 Apr 2024 17:35:42 -0500 Subject: [PATCH 01/36] add random comment to test git syncing with package --- .../CodeEditSourceEditor/Controller/TextViewController.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index d2ae830f1..d96005cb9 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -20,6 +20,8 @@ import TextFormation public class TextViewController: NSViewController { // swiftlint:disable:next line_length public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification") + + // this is a random comment var scrollView: NSScrollView! var textView: TextView! From 9cecfa82a34a76347f1e9dfa4723818fac7cc910 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 14 Apr 2024 17:41:04 -0500 Subject: [PATCH 02/36] add key down event monitor --- .../Controller/TextViewController.swift | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index d96005cb9..1d394115f 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -18,10 +18,47 @@ import TextFormation /// tree-sitter for syntax highlighting, and TextFormation for live editing completions. /// public class TextViewController: NSViewController { + + @IBOutlet var textField: NSTextField! + + override public func viewDidLoad() { + super.viewDidLoad() + NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) { + self.flagsChanged(with: $0) + return $0 + } + NSEvent.addLocalMonitorForEvents(matching: .keyDown) { + self.keyDown(with: $0) + return $0 + } + } + + override public func keyDown(with event: NSEvent) { + switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { + case [.command] where event.characters == "l", + [.command, .shift] where event.characters == "l": + print("command-l or command-shift-l") + default: + break + } + textField.stringValue = "key = " + (event.charactersIgnoringModifiers + ?? "") + textField.stringValue += "\ncharacter = " + (event.characters ?? "") + } + + override public func flagsChanged(with event: NSEvent) { + switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { + case [.shift]: + print("shift key is pressed") + case [.control]: + print("control key is pressed") + default: + print("no modifier keys are pressed") + } + } + // swiftlint:disable:next line_length public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification") - - // this is a random comment var scrollView: NSScrollView! var textView: TextView! From 7882267ac83c50f253aee552918309b1bff99da0 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 14 Apr 2024 20:21:39 -0500 Subject: [PATCH 03/36] create basic function to understand functionality of keyDown --- .../Controller/TextViewController.swift | 81 ++++++++++++------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 1d394115f..c2b98e786 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -19,43 +19,62 @@ import TextFormation /// public class TextViewController: NSViewController { - @IBOutlet var textField: NSTextField! - - override public func viewDidLoad() { - super.viewDidLoad() - NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) { - self.flagsChanged(with: $0) - return $0 - } - NSEvent.addLocalMonitorForEvents(matching: .keyDown) { - self.keyDown(with: $0) - return $0 - } +// @IBOutlet var textField: NSTextField! +// +// override public func viewDidLoad() { +// super.viewDidLoad() +// NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) { +// self.flagsChanged(with: $0) +// return $0 +// } +// NSEvent.addLocalMonitorForEvents(matching: .keyDown) { +// self.keyDown(with: $0) +// return $0 +// } +// } +// +// override public func keyDown(with event: NSEvent) { +// switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { +// case [.command] where event.characters == "l", +// [.command, .shift] where event.characters == "l": +// print("command-l or command-shift-l") +// default: +// break +// } +// textField.stringValue = "key = " + (event.charactersIgnoringModifiers +// ?? "") +// textField.stringValue += "\ncharacter = " + (event.characters ?? "") +// } +// +// override public func flagsChanged(with event: NSEvent) { +// switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { +// case [.shift]: +// print("shift key is pressed") +// case [.control]: +// print("control key is pressed") +// default: +// print("no modifier keys are pressed") +// } +// } + + func myCustomFunction() { + print("Shortcut used!") } override public func keyDown(with event: NSEvent) { - switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { - case [.command] where event.characters == "l", - [.command, .shift] where event.characters == "l": - print("command-l or command-shift-l") - default: - break + super.keyDown(with: event) + + let commandKey = NSEvent.ModifierFlags.command.rawValue + let shiftKey = NSEvent.ModifierFlags.shift.rawValue + let desiredKey = 0x01 // Using 's' key as an example, you can find other key codes + + if event.modifierFlags.rawValue & commandKey != 0 && + event.modifierFlags.rawValue & shiftKey != 0 && + event.keyCode == desiredKey { + myCustomFunction() } - textField.stringValue = "key = " + (event.charactersIgnoringModifiers - ?? "") - textField.stringValue += "\ncharacter = " + (event.characters ?? "") } - override public func flagsChanged(with event: NSEvent) { - switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { - case [.shift]: - print("shift key is pressed") - case [.control]: - print("control key is pressed") - default: - print("no modifier keys are pressed") - } - } // swiftlint:disable:next line_length public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification") From fca032904e2b97e4b0adf11fbb1c4d9287d59a06 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 14 Apr 2024 20:26:09 -0500 Subject: [PATCH 04/36] edit basic function to include viewDidLoad call --- .../CodeEditSourceEditor/Controller/TextViewController.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index c2b98e786..eb2c8379e 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -61,6 +61,11 @@ public class TextViewController: NSViewController { print("Shortcut used!") } + override public func viewDidLoad() { + super.viewDidLoad() + // Any additional setup after your view loads + } + override public func keyDown(with event: NSEvent) { super.keyDown(with: event) From 0787b98efebacf050dc45405ce9bbd8f0595f887 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 14 Apr 2024 20:30:20 -0500 Subject: [PATCH 05/36] changes to basic function --- .../Controller/TextViewController.swift | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index eb2c8379e..f96ef97e9 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -57,30 +57,29 @@ public class TextViewController: NSViewController { // } // } - func myCustomFunction() { - print("Shortcut used!") - } - override public func viewDidLoad() { super.viewDidLoad() // Any additional setup after your view loads } override public func keyDown(with event: NSEvent) { - super.keyDown(with: event) - - let commandKey = NSEvent.ModifierFlags.command.rawValue - let shiftKey = NSEvent.ModifierFlags.shift.rawValue - let desiredKey = 0x01 // Using 's' key as an example, you can find other key codes - - if event.modifierFlags.rawValue & commandKey != 0 && - event.modifierFlags.rawValue & shiftKey != 0 && - event.keyCode == desiredKey { - myCustomFunction() + print("key pressed"); + let commandShiftKey = NSEvent.ModifierFlags.command.rawValue | NSEvent.ModifierFlags.shift.rawValue + let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue + + if modifierFlags == commandShiftKey && event.charactersIgnoringModifiers == "l" { + // Call your custom function here + customFunction() + } else { + super.keyDown(with: event) // It's important to call super for unhandled events } } - + func customFunction() { + print("Command-Shift-L has been pressed!") + // Implement what this function should do + } + // swiftlint:disable:next line_length public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification") From 614ce7e2d6e35b06c8a56a5e7d208271ef232a4b Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 14 Apr 2024 20:34:43 -0500 Subject: [PATCH 06/36] add local monitoring functionality --- .../Controller/TextViewController.swift | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index f96ef97e9..c59257883 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -21,17 +21,17 @@ public class TextViewController: NSViewController { // @IBOutlet var textField: NSTextField! // -// override public func viewDidLoad() { -// super.viewDidLoad() + override public func viewDidLoad() { + super.viewDidLoad() // NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) { // self.flagsChanged(with: $0) // return $0 // } -// NSEvent.addLocalMonitorForEvents(matching: .keyDown) { -// self.keyDown(with: $0) -// return $0 -// } -// } + NSEvent.addLocalMonitorForEvents(matching: .keyDown) { + self.keyDown(with: $0) + return $0 + } + } // // override public func keyDown(with event: NSEvent) { // switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { @@ -57,11 +57,6 @@ public class TextViewController: NSViewController { // } // } - override public func viewDidLoad() { - super.viewDidLoad() - // Any additional setup after your view loads - } - override public func keyDown(with event: NSEvent) { print("key pressed"); let commandShiftKey = NSEvent.ModifierFlags.command.rawValue | NSEvent.ModifierFlags.shift.rawValue From 6879299b2bdae3a2842255a62fc6f9a7985f400e Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 14 Apr 2024 20:54:17 -0500 Subject: [PATCH 07/36] debug output for characters ignoring modifiers --- .../Controller/TextViewController.swift | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index c59257883..a6006acfe 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -20,19 +20,15 @@ import TextFormation public class TextViewController: NSViewController { // @IBOutlet var textField: NSTextField! -// + override public func viewDidLoad() { super.viewDidLoad() -// NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) { -// self.flagsChanged(with: $0) -// return $0 -// } NSEvent.addLocalMonitorForEvents(matching: .keyDown) { self.keyDown(with: $0) return $0 } } -// + // override public func keyDown(with event: NSEvent) { // switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { // case [.command] where event.characters == "l", @@ -59,6 +55,7 @@ public class TextViewController: NSViewController { override public func keyDown(with event: NSEvent) { print("key pressed"); + let charactersIgnoringModifiers = event.charactersIgnoringModifiers; let commandShiftKey = NSEvent.ModifierFlags.command.rawValue | NSEvent.ModifierFlags.shift.rawValue let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue From 85e7c180f85a1d135d98b0c89f5b05f8ba7f50ef Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 14 Apr 2024 20:58:18 -0500 Subject: [PATCH 08/36] add check to see if cmd / has been pressed --- .../CodeEditSourceEditor/Controller/TextViewController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index a6006acfe..3fe68b87f 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -56,10 +56,10 @@ public class TextViewController: NSViewController { override public func keyDown(with event: NSEvent) { print("key pressed"); let charactersIgnoringModifiers = event.charactersIgnoringModifiers; - let commandShiftKey = NSEvent.ModifierFlags.command.rawValue | NSEvent.ModifierFlags.shift.rawValue + let commandKey = NSEvent.ModifierFlags.command.rawValue let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue - if modifierFlags == commandShiftKey && event.charactersIgnoringModifiers == "l" { + if modifierFlags == commandKey && event.charactersIgnoringModifiers == "/" { // Call your custom function here customFunction() } else { From 802815dfd9c2d6af4240e3cb0e16ad8b597b7b4d Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 14 Apr 2024 20:59:57 -0500 Subject: [PATCH 09/36] change function output to match what it is doing --- .../CodeEditSourceEditor/Controller/TextViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 3fe68b87f..79bb47b56 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -68,7 +68,7 @@ public class TextViewController: NSViewController { } func customFunction() { - print("Command-Shift-L has been pressed!") + print("Command-/ has been pressed!") // Implement what this function should do } From fd1ace9a51a793b8125c8804639e61fcc75ecf83 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Mon, 15 Apr 2024 17:19:34 -0500 Subject: [PATCH 10/36] print out cursor positions when cmd/ pressed --- .../Controller/TextViewController.swift | 36 +++---------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 79bb47b56..7ad152756 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -18,9 +18,6 @@ import TextFormation /// tree-sitter for syntax highlighting, and TextFormation for live editing completions. /// public class TextViewController: NSViewController { - -// @IBOutlet var textField: NSTextField! - override public func viewDidLoad() { super.viewDidLoad() NSEvent.addLocalMonitorForEvents(matching: .keyDown) { @@ -28,48 +25,23 @@ public class TextViewController: NSViewController { return $0 } } - -// override public func keyDown(with event: NSEvent) { -// switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { -// case [.command] where event.characters == "l", -// [.command, .shift] where event.characters == "l": -// print("command-l or command-shift-l") -// default: -// break -// } -// textField.stringValue = "key = " + (event.charactersIgnoringModifiers -// ?? "") -// textField.stringValue += "\ncharacter = " + (event.characters ?? "") -// } -// -// override public func flagsChanged(with event: NSEvent) { -// switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { -// case [.shift]: -// print("shift key is pressed") -// case [.control]: -// print("control key is pressed") -// default: -// print("no modifier keys are pressed") -// } -// } override public func keyDown(with event: NSEvent) { print("key pressed"); let charactersIgnoringModifiers = event.charactersIgnoringModifiers; let commandKey = NSEvent.ModifierFlags.command.rawValue let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue - if modifierFlags == commandKey && event.charactersIgnoringModifiers == "/" { - // Call your custom function here - customFunction() + commandSlashCalled() } else { - super.keyDown(with: event) // It's important to call super for unhandled events + super.keyDown(with: event) } } - func customFunction() { + func commandSlashCalled() { print("Command-/ has been pressed!") // Implement what this function should do + print(cursorPositions) } // swiftlint:disable:next line_length From 8698fb7069c1efa17ed057c21d1e3b372d6567c0 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Mon, 15 Apr 2024 17:34:51 -0500 Subject: [PATCH 11/36] include debug statement for what string is when cmd/ is pressed --- Sources/CodeEditSourceEditor/Controller/TextViewController.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 7ad152756..6c501c95c 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -42,6 +42,7 @@ public class TextViewController: NSViewController { print("Command-/ has been pressed!") // Implement what this function should do print(cursorPositions) + print(textView.string) } // swiftlint:disable:next line_length From 87511ba10f646420e0b4650d7592d33d0effd7ee Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Mon, 15 Apr 2024 17:49:17 -0500 Subject: [PATCH 12/36] attempt to insert text when cmd/ pressed --- .../CodeEditSourceEditor/Controller/TextViewController.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 6c501c95c..bf02bd6c7 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -42,7 +42,9 @@ public class TextViewController: NSViewController { print("Command-/ has been pressed!") // Implement what this function should do print(cursorPositions) - print(textView.string) + // print(textView.string) + let stringContents = "hi" + textView.insertText(stringContents, replacementRange: NSRange(location: NSNotFound, length: 0)) } // swiftlint:disable:next line_length From 216705341d56ec00e881ce13431d85c5cc696eea Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Mon, 15 Apr 2024 18:18:24 -0500 Subject: [PATCH 13/36] insert text at beginning of line --- .../Controller/TextViewController.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index bf02bd6c7..07af7df13 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -27,7 +27,6 @@ public class TextViewController: NSViewController { } override public func keyDown(with event: NSEvent) { - print("key pressed"); let charactersIgnoringModifiers = event.charactersIgnoringModifiers; let commandKey = NSEvent.ModifierFlags.command.rawValue let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue @@ -40,11 +39,13 @@ public class TextViewController: NSViewController { func commandSlashCalled() { print("Command-/ has been pressed!") - // Implement what this function should do print(cursorPositions) // print(textView.string) - let stringContents = "hi" - textView.insertText(stringContents, replacementRange: NSRange(location: NSNotFound, length: 0)) + let stringContents = "//" + if let cursorPosition = cursorPositions.first { + let lineNumber = cursorPosition.line + textView.insertText(stringContents, replacementRange: NSRange(location: lineNumber, length: 0)) + } } // swiftlint:disable:next line_length From 8d40d08720707c9b99ae7738b2328bd86d141604 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Tue, 16 Apr 2024 10:23:20 -0500 Subject: [PATCH 14/36] print out current cursor line --- .../Controller/TextViewController.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 07af7df13..55415e9b0 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -25,9 +25,9 @@ public class TextViewController: NSViewController { return $0 } } - + override public func keyDown(with event: NSEvent) { - let charactersIgnoringModifiers = event.charactersIgnoringModifiers; + let charactersIgnoringModifiers = event.charactersIgnoringModifiers let commandKey = NSEvent.ModifierFlags.command.rawValue let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue if modifierFlags == commandKey && event.charactersIgnoringModifiers == "/" { @@ -43,8 +43,9 @@ public class TextViewController: NSViewController { // print(textView.string) let stringContents = "//" if let cursorPosition = cursorPositions.first { - let lineNumber = cursorPosition.line - textView.insertText(stringContents, replacementRange: NSRange(location: lineNumber, length: 0)) + print(textView.layoutManager.textLineForIndex(cursorPosition.line) ?? 0) +// let lineNumber = cursorPosition.line +// textView.insertText(stringContents, replacementRange: NSRange(location: lineNumber, length: 0)) } } From 1c5e334f53559271ca88d73ae906c49aa783aca0 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Tue, 16 Apr 2024 10:30:44 -0500 Subject: [PATCH 15/36] fix line indexing --- .../CodeEditSourceEditor/Controller/TextViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 55415e9b0..c6e476df5 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -43,7 +43,7 @@ public class TextViewController: NSViewController { // print(textView.string) let stringContents = "//" if let cursorPosition = cursorPositions.first { - print(textView.layoutManager.textLineForIndex(cursorPosition.line) ?? 0) + print(textView.layoutManager.textLineForIndex(cursorPosition.line + 1) ?? 0) // let lineNumber = cursorPosition.line // textView.insertText(stringContents, replacementRange: NSRange(location: lineNumber, length: 0)) } From f231ecaaee50644dec5cc67dc5a0f13f35983c05 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Tue, 16 Apr 2024 10:34:18 -0500 Subject: [PATCH 16/36] fix indexing again --- .../CodeEditSourceEditor/Controller/TextViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index c6e476df5..bc37c09b1 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -43,7 +43,7 @@ public class TextViewController: NSViewController { // print(textView.string) let stringContents = "//" if let cursorPosition = cursorPositions.first { - print(textView.layoutManager.textLineForIndex(cursorPosition.line + 1) ?? 0) + print(textView.layoutManager.textLineForIndex(cursorPosition.line - 1) ?? 0) // let lineNumber = cursorPosition.line // textView.insertText(stringContents, replacementRange: NSRange(location: lineNumber, length: 0)) } From b692a31b01776d3ad0aa68691f967079aae79561 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Tue, 16 Apr 2024 10:53:27 -0500 Subject: [PATCH 17/36] insert comment at beginning of line using line char index --- .../Controller/TextViewController.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index bc37c09b1..2439603f6 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -44,8 +44,10 @@ public class TextViewController: NSViewController { let stringContents = "//" if let cursorPosition = cursorPositions.first { print(textView.layoutManager.textLineForIndex(cursorPosition.line - 1) ?? 0) -// let lineNumber = cursorPosition.line -// textView.insertText(stringContents, replacementRange: NSRange(location: lineNumber, length: 0)) + if let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) { + let lineFirstCharIndex = lineInfo.range.location + textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: "//") + } } } From 507247b43a68969fdb4c787bb79de46f96d3f12f Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Tue, 16 Apr 2024 10:54:50 -0500 Subject: [PATCH 18/36] add debug to see if code reaches inner if statement --- Sources/CodeEditSourceEditor/Controller/TextViewController.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 2439603f6..5fe4f3c3a 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -45,6 +45,7 @@ public class TextViewController: NSViewController { if let cursorPosition = cursorPositions.first { print(textView.layoutManager.textLineForIndex(cursorPosition.line - 1) ?? 0) if let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) { + print("reached inner if") let lineFirstCharIndex = lineInfo.range.location textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: "//") } From ecfc976c30d049663b993ebda45a4a4487521547 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Wed, 17 Apr 2024 16:51:16 -0500 Subject: [PATCH 19/36] use lineCommentString instead of hardcoded string for comment --- Package.resolved | 12 ++++++------ .../Controller/TextViewController.swift | 7 +++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Package.resolved b/Package.resolved index 08f1665e7..9be37aec1 100644 --- a/Package.resolved +++ b/Package.resolved @@ -23,8 +23,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/mattmassicotte/MainOffender", "state" : { - "revision" : "343cc3797618c29b48b037b4e2beea0664e75315", - "version" : "0.1.0" + "revision" : "8de872d9256ff7f9913cbc5dd560568ab164be45", + "version" : "0.2.1" } }, { @@ -32,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/ChimeHQ/Rearrange", "state" : { - "revision" : "8f97f721d8a08c6e01ab9f7460e53819bef72dfa", - "version" : "1.5.3" + "revision" : "5ff7f3363f7a08f77e0d761e38e6add31c2136e1", + "version" : "1.8.1" } }, { @@ -50,8 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/lukepistrol/SwiftLintPlugin", "state" : { - "revision" : "f69b412a765396d44dc9f4788a5b79919c1ca9e3", - "version" : "0.2.2" + "revision" : "ea6d3ca895b49910f790e98e4b4ca658e0fe490e", + "version" : "0.54.0" } }, { diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 5fe4f3c3a..06b7ff424 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -26,6 +26,7 @@ public class TextViewController: NSViewController { } } + // key Down event recognizer, currently only set up to recognize CMD + / override public func keyDown(with event: NSEvent) { let charactersIgnoringModifiers = event.charactersIgnoringModifiers let commandKey = NSEvent.ModifierFlags.command.rawValue @@ -37,6 +38,7 @@ public class TextViewController: NSViewController { } } + // Method called when CMD + / key sequence recognized, comments cursor's current line of code func commandSlashCalled() { print("Command-/ has been pressed!") print(cursorPositions) @@ -45,9 +47,10 @@ public class TextViewController: NSViewController { if let cursorPosition = cursorPositions.first { print(textView.layoutManager.textLineForIndex(cursorPosition.line - 1) ?? 0) if let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) { - print("reached inner if") let lineFirstCharIndex = lineInfo.range.location - textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: "//") + let languageCommentStr = language.lineCommentString +// let lengthOfCommentStr = languageCommentStr.count + textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: languageCommentStr) } } } From 412c32769b72821073a1e2286b26e1f9969256d3 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Thu, 18 Apr 2024 10:55:37 -0500 Subject: [PATCH 20/36] add space after comment insert --- .../Controller/TextViewController.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 06b7ff424..d2cdd0af2 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -43,14 +43,14 @@ public class TextViewController: NSViewController { print("Command-/ has been pressed!") print(cursorPositions) // print(textView.string) - let stringContents = "//" if let cursorPosition = cursorPositions.first { print(textView.layoutManager.textLineForIndex(cursorPosition.line - 1) ?? 0) if let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) { let lineFirstCharIndex = lineInfo.range.location + // let currentLinePosition = textView.layoutManager.lineStorage.getLine(atIndex: lineFirstCharIndex) let languageCommentStr = language.lineCommentString -// let lengthOfCommentStr = languageCommentStr.count - textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: languageCommentStr) + // let lengthOfCommentStr = languageCommentStr.count + textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: languageCommentStr + " ") } } } From 6c866179be389cbbb1c9e84ac2ea2b60d77cde11 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Thu, 18 Apr 2024 10:57:33 -0500 Subject: [PATCH 21/36] print lineInfo data --- Sources/CodeEditSourceEditor/Controller/TextViewController.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index d2cdd0af2..631473c92 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -46,6 +46,7 @@ public class TextViewController: NSViewController { if let cursorPosition = cursorPositions.first { print(textView.layoutManager.textLineForIndex(cursorPosition.line - 1) ?? 0) if let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) { + print(lineInfo.data) let lineFirstCharIndex = lineInfo.range.location // let currentLinePosition = textView.layoutManager.lineStorage.getLine(atIndex: lineFirstCharIndex) let languageCommentStr = language.lineCommentString From 555ed63766e0e840d34db47f7900e87175ea70b8 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Thu, 18 Apr 2024 10:59:42 -0500 Subject: [PATCH 22/36] print line info line fragments --- .../CodeEditSourceEditor/Controller/TextViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 631473c92..f6d1b533c 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -46,7 +46,7 @@ public class TextViewController: NSViewController { if let cursorPosition = cursorPositions.first { print(textView.layoutManager.textLineForIndex(cursorPosition.line - 1) ?? 0) if let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) { - print(lineInfo.data) + print(lineInfo.data.lineFragments) let lineFirstCharIndex = lineInfo.range.location // let currentLinePosition = textView.layoutManager.lineStorage.getLine(atIndex: lineFirstCharIndex) let languageCommentStr = language.lineCommentString From 2498247f302eb00ba29a0701c5dd7333dd753afc Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Fri, 19 Apr 2024 09:17:32 -0500 Subject: [PATCH 23/36] print line info --- .../CodeEditSourceEditor/Controller/TextViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index f6d1b533c..a563a6598 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -46,8 +46,8 @@ public class TextViewController: NSViewController { if let cursorPosition = cursorPositions.first { print(textView.layoutManager.textLineForIndex(cursorPosition.line - 1) ?? 0) if let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) { - print(lineInfo.data.lineFragments) let lineFirstCharIndex = lineInfo.range.location + print(textView.layoutManager.textLineForOffset(lineFirstCharIndex)) // let currentLinePosition = textView.layoutManager.lineStorage.getLine(atIndex: lineFirstCharIndex) let languageCommentStr = language.lineCommentString // let lengthOfCommentStr = languageCommentStr.count From a23c0aa8b07225d9affad77eb4aa0c92f1132ed8 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Fri, 19 Apr 2024 09:41:48 -0500 Subject: [PATCH 24/36] get substring of current line --- .../Controller/TextViewController.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index a563a6598..9615eda3f 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -41,16 +41,17 @@ public class TextViewController: NSViewController { // Method called when CMD + / key sequence recognized, comments cursor's current line of code func commandSlashCalled() { print("Command-/ has been pressed!") - print(cursorPositions) + // print(cursorPositions) // print(textView.string) if let cursorPosition = cursorPositions.first { print(textView.layoutManager.textLineForIndex(cursorPosition.line - 1) ?? 0) if let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) { let lineFirstCharIndex = lineInfo.range.location - print(textView.layoutManager.textLineForOffset(lineFirstCharIndex)) - // let currentLinePosition = textView.layoutManager.lineStorage.getLine(atIndex: lineFirstCharIndex) let languageCommentStr = language.lineCommentString - // let lengthOfCommentStr = languageCommentStr.count + let lengthOfCommentStr = languageCommentStr.count + let range = NSRange(location: lineFirstCharIndex, length: lengthOfCommentStr) + print(textView.textStorage.substring(from: range)) + // let currentLinePosition = textView.layoutManager.lineStorage.getLine(atIndex: lineFirstCharIndex) textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: languageCommentStr + " ") } } From 1a5870c9b57810ce07c9d3d96cae6a4b5cfc346c Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Fri, 19 Apr 2024 09:51:43 -0500 Subject: [PATCH 25/36] toggle commenting --- .../Controller/TextViewController.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 9615eda3f..70a44fa6e 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -50,9 +50,15 @@ public class TextViewController: NSViewController { let languageCommentStr = language.lineCommentString let lengthOfCommentStr = languageCommentStr.count let range = NSRange(location: lineFirstCharIndex, length: lengthOfCommentStr) + // equal to length of language's comment string + let firstCharsInLine = textView.textStorage.substring(from: range) print(textView.textStorage.substring(from: range)) - // let currentLinePosition = textView.layoutManager.lineStorage.getLine(atIndex: lineFirstCharIndex) - textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: languageCommentStr + " ") + // toggle comments depending on if comment already there + if firstCharsInLine == languageCommentStr { + textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: lengthOfCommentStr), with: "") + } else { + textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: languageCommentStr + " ") + } } } } From 0b288adad655a3db17858826e4af3507bcaae247 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Fri, 19 Apr 2024 17:06:19 -0500 Subject: [PATCH 26/36] finish single line implementation, clean up code, add case for no lineComment --- .../Controller/TextViewController.swift | 67 +++++++++++++------ 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 70a44fa6e..17a67d2f7 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -26,7 +26,7 @@ public class TextViewController: NSViewController { } } - // key Down event recognizer, currently only set up to recognize CMD + / + /// key Down event recognizer, currently only set up to recognize CMD + / override public func keyDown(with event: NSEvent) { let charactersIgnoringModifiers = event.charactersIgnoringModifiers let commandKey = NSEvent.ModifierFlags.command.rawValue @@ -38,27 +38,52 @@ public class TextViewController: NSViewController { } } - // Method called when CMD + / key sequence recognized, comments cursor's current line of code + /// Method called when CMD + / key sequence recognized, comments cursor's current line of code func commandSlashCalled() { - print("Command-/ has been pressed!") - // print(cursorPositions) - // print(textView.string) - if let cursorPosition = cursorPositions.first { - print(textView.layoutManager.textLineForIndex(cursorPosition.line - 1) ?? 0) - if let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) { - let lineFirstCharIndex = lineInfo.range.location - let languageCommentStr = language.lineCommentString - let lengthOfCommentStr = languageCommentStr.count - let range = NSRange(location: lineFirstCharIndex, length: lengthOfCommentStr) - // equal to length of language's comment string - let firstCharsInLine = textView.textStorage.substring(from: range) - print(textView.textStorage.substring(from: range)) - // toggle comments depending on if comment already there - if firstCharsInLine == languageCommentStr { - textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: lengthOfCommentStr), with: "") - } else { - textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: languageCommentStr + " ") - } + print(cursorPositions) + guard let cursorPosition = cursorPositions.first else { + print("There is no cursor \(#function)") + return + } + guard let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) else { + print("There are no characters/lineInfo \(#function)") + return + } + let lineFirstCharIndex = lineInfo.range.location + // lineComment only required at front of line + if !language.lineCommentString.isEmpty { + let languageCommentStr = language.lineCommentString + let lengthOfCommentStr = languageCommentStr.count + let commentRange = NSRange(location: lineFirstCharIndex, length: lengthOfCommentStr) + // equal to length of language's comment string + let firstCharsInLine = textView.textStorage.substring(from: commentRange) + // toggle comment off + if firstCharsInLine == languageCommentStr { + textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: lengthOfCommentStr), with: "") + } + // toggle comment on + else { + textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: languageCommentStr) + } + } + // commenting line requires one-line rangeComment + else { + let (openComment,closeComment) = language.rangeCommentStrings + let lineLastCharIndex = lineFirstCharIndex + lineInfo.range.length - 1 + let openCommentLength = openComment.count + let openCommentRange = NSRange(location: lineFirstCharIndex, length: openCommentLength) + let closeCommentLength = closeComment.count + let closeCommentRange = NSRange(location: lineLastCharIndex - closeCommentLength + 1, length: closeCommentLength) + let firstCharsInLine = textView.textStorage.substring(from: openCommentRange) + // toggle comment off + if firstCharsInLine == openComment { + textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: openCommentLength), with: "") + textView.replaceCharacters(in:NSRange(location: lineLastCharIndex - closeCommentLength - openCommentLength, length: closeCommentLength), with: "") + } + // toggle comment on + else { + textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: openComment) + textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex + lineInfo.range.length + openCommentLength - 1, length: 0), with: closeComment) } } } From 0eeddec10b02fd49c00f34afe3fca57240cc7f81 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Fri, 19 Apr 2024 17:21:44 -0500 Subject: [PATCH 27/36] rollback --- Package.resolved | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Package.resolved b/Package.resolved index 9be37aec1..08f1665e7 100644 --- a/Package.resolved +++ b/Package.resolved @@ -23,8 +23,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/mattmassicotte/MainOffender", "state" : { - "revision" : "8de872d9256ff7f9913cbc5dd560568ab164be45", - "version" : "0.2.1" + "revision" : "343cc3797618c29b48b037b4e2beea0664e75315", + "version" : "0.1.0" } }, { @@ -32,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/ChimeHQ/Rearrange", "state" : { - "revision" : "5ff7f3363f7a08f77e0d761e38e6add31c2136e1", - "version" : "1.8.1" + "revision" : "8f97f721d8a08c6e01ab9f7460e53819bef72dfa", + "version" : "1.5.3" } }, { @@ -50,8 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/lukepistrol/SwiftLintPlugin", "state" : { - "revision" : "ea6d3ca895b49910f790e98e4b4ca658e0fe490e", - "version" : "0.54.0" + "revision" : "f69b412a765396d44dc9f4788a5b79919c1ca9e3", + "version" : "0.2.2" } }, { From acbbec537a398c807bf6e34b67a0bd4cfed6212b Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 21 Apr 2024 09:19:14 -0500 Subject: [PATCH 28/36] make requested changes from PR (more comments, delete debug line) --- .../Controller/TextViewController.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 17a67d2f7..82e4723e9 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -37,10 +37,9 @@ public class TextViewController: NSViewController { super.keyDown(with: event) } } - + /// Method called when CMD + / key sequence recognized, comments cursor's current line of code func commandSlashCalled() { - print(cursorPositions) guard let cursorPosition = cursorPositions.first else { print("There is no cursor \(#function)") return @@ -50,7 +49,9 @@ public class TextViewController: NSViewController { return } let lineFirstCharIndex = lineInfo.range.location - // lineComment only required at front of line + // Many languages require a character sequence at the beginning of the line to comment the line. + // (ex. python #, C++ //) + // If such a sequence exists, we will insert that sequence at the beginning of the line if !language.lineCommentString.isEmpty { let languageCommentStr = language.lineCommentString let lengthOfCommentStr = languageCommentStr.count @@ -66,7 +67,9 @@ public class TextViewController: NSViewController { textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: languageCommentStr) } } - // commenting line requires one-line rangeComment + // In other cases, there are languages that require a character sequence at both the beginning and end of a line, aka a range comment + // (Ex. HTML ) + // We can treat the line as a one-line range to comment it out using the language's rangeCommentStrings on both sides of the line else { let (openComment,closeComment) = language.rangeCommentStrings let lineLastCharIndex = lineFirstCharIndex + lineInfo.range.length - 1 From 3bf438a6bfccc1ad4912efa4283e4e55eaeea347 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 21 Apr 2024 10:58:28 -0500 Subject: [PATCH 29/36] refactor keyboard shortcuts logic, more changes requested from PR --- .../Controller/TextViewController.swift | 160 ++++++++++-------- 1 file changed, 88 insertions(+), 72 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 82e4723e9..5bc2a3ca1 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -18,78 +18,6 @@ import TextFormation /// tree-sitter for syntax highlighting, and TextFormation for live editing completions. /// public class TextViewController: NSViewController { - override public func viewDidLoad() { - super.viewDidLoad() - NSEvent.addLocalMonitorForEvents(matching: .keyDown) { - self.keyDown(with: $0) - return $0 - } - } - - /// key Down event recognizer, currently only set up to recognize CMD + / - override public func keyDown(with event: NSEvent) { - let charactersIgnoringModifiers = event.charactersIgnoringModifiers - let commandKey = NSEvent.ModifierFlags.command.rawValue - let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue - if modifierFlags == commandKey && event.charactersIgnoringModifiers == "/" { - commandSlashCalled() - } else { - super.keyDown(with: event) - } - } - - /// Method called when CMD + / key sequence recognized, comments cursor's current line of code - func commandSlashCalled() { - guard let cursorPosition = cursorPositions.first else { - print("There is no cursor \(#function)") - return - } - guard let lineInfo = textView.layoutManager.textLineForIndex(cursorPosition.line - 1) else { - print("There are no characters/lineInfo \(#function)") - return - } - let lineFirstCharIndex = lineInfo.range.location - // Many languages require a character sequence at the beginning of the line to comment the line. - // (ex. python #, C++ //) - // If such a sequence exists, we will insert that sequence at the beginning of the line - if !language.lineCommentString.isEmpty { - let languageCommentStr = language.lineCommentString - let lengthOfCommentStr = languageCommentStr.count - let commentRange = NSRange(location: lineFirstCharIndex, length: lengthOfCommentStr) - // equal to length of language's comment string - let firstCharsInLine = textView.textStorage.substring(from: commentRange) - // toggle comment off - if firstCharsInLine == languageCommentStr { - textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: lengthOfCommentStr), with: "") - } - // toggle comment on - else { - textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: languageCommentStr) - } - } - // In other cases, there are languages that require a character sequence at both the beginning and end of a line, aka a range comment - // (Ex. HTML ) - // We can treat the line as a one-line range to comment it out using the language's rangeCommentStrings on both sides of the line - else { - let (openComment,closeComment) = language.rangeCommentStrings - let lineLastCharIndex = lineFirstCharIndex + lineInfo.range.length - 1 - let openCommentLength = openComment.count - let openCommentRange = NSRange(location: lineFirstCharIndex, length: openCommentLength) - let closeCommentLength = closeComment.count - let closeCommentRange = NSRange(location: lineLastCharIndex - closeCommentLength + 1, length: closeCommentLength) - let firstCharsInLine = textView.textStorage.substring(from: openCommentRange) - // toggle comment off - if firstCharsInLine == openComment { - textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: openCommentLength), with: "") - textView.replaceCharacters(in:NSRange(location: lineLastCharIndex - closeCommentLength - openCommentLength, length: closeCommentLength), with: "") - } - // toggle comment on - else { - textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex, length: 0), with: openComment) - textView.replaceCharacters(in:NSRange(location: lineFirstCharIndex + lineInfo.range.length + openCommentLength - 1, length: 0), with: closeComment) - } - } - } // swiftlint:disable:next line_length public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification") @@ -309,6 +237,94 @@ public class TextViewController: NSViewController { required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } + + // MARK: Keyboard Shortcuts + + override public func viewDidLoad() { + super.viewDidLoad() + NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event in + guard self.view.window?.firstResponder == self.textView else { return event } + let charactersIgnoringModifiers = event.charactersIgnoringModifiers + let commandKey = NSEvent.ModifierFlags.command.rawValue + let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue + if modifierFlags == commandKey && event.charactersIgnoringModifiers == "/" { + self.commandSlashCalled() + return nil + } else { + super.keyDown(with: event) + return event + } + } + } + + /// Method called when CMD + / key sequence recognized, comments cursor's current line of code + func commandSlashCalled() { + guard let cursorPosition = cursorPositions.first else { + print("There is no cursor \(#function)") + return + } + // Many languages require a character sequence at the beginning of the line to comment the line. + // (ex. python #, C++ //) + // If such a sequence exists, we will insert that sequence at the beginning of the line + if !language.lineCommentString.isEmpty { + toggleCharsAtBeginningOfLine(chars: language.lineCommentString, lineNumber: cursorPosition.line) + } + // In other cases, there are languages that require a character sequence at both the beginning and end of a line, aka a range comment + // (Ex. HTML ) + // We can treat the line as a one-line range to comment it out using the language's rangeCommentStrings on both sides of the line + else { + let (openComment,closeComment) = language.rangeCommentStrings + toggleCharsAtEndOfLine(chars: closeComment, lineNumber: cursorPosition.line) + toggleCharsAtBeginningOfLine(chars: openComment, lineNumber: cursorPosition.line) + } + } + + /// Toggles a specific string of characters at the beginning of a specified line in the textView's text storage. (lineNumber is 1-indexed) + private func toggleCharsAtBeginningOfLine(chars: String, lineNumber: Int){ + guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { + print("There are no characters/lineInfo \(#function)") + return + } + guard let lineString = textView.textStorage.substring(from: lineInfo.range) else { + print("There are no characters/lineString \(#function)") + return + } + let firstNonWhiteSpaceCharIndex = lineString.firstIndex(where: {!$0.isWhitespace}) ?? lineString.startIndex + let numWhitespaceChars = lineString.distance(from: lineString.startIndex, to: firstNonWhiteSpaceCharIndex) + let firstCharsInLine = lineString.suffix(from: firstNonWhiteSpaceCharIndex).prefix(chars.count) + // toggle comment off + if firstCharsInLine == chars { + textView.replaceCharacters(in:NSRange(location: lineInfo.range.location + numWhitespaceChars, length: chars.count), with: "") + } + // toggle comment on + else { + textView.replaceCharacters(in:NSRange(location: lineInfo.range.location + numWhitespaceChars, length: 0), with: chars) + } + } + + /// Toggles a specific string of characters at the end of a specified line in the textView's text storage. (lineNumber is 1-indexed) + private func toggleCharsAtEndOfLine(chars: String, lineNumber: Int){ + guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { + print("There are no characters/lineInfo \(#function)") + return + } + guard let lineString = textView.textStorage.substring(from: lineInfo.range) else { + print("There are no characters/lineString \(#function)") + return + } + let lineLastCharIndex = lineInfo.range.location + lineInfo.range.length - 1 + let closeCommentLength = chars.count + let closeCommentRange = NSRange(location: lineLastCharIndex - closeCommentLength, length: closeCommentLength) + let lastCharsInLine = textView.textStorage.substring(from: closeCommentRange) + // toggle comment off + if lastCharsInLine == chars { + textView.replaceCharacters(in:NSRange(location: lineLastCharIndex - closeCommentLength, length: closeCommentLength), with: "") + } + // toggle comment on + else { + textView.replaceCharacters(in:NSRange(location: lineLastCharIndex, length: 0), with: chars) + } + } /// Set the contents of the editor. /// - Parameter text: The new contents of the editor. From 664e37555743d7694edb28aea7d6fac490a29bf7 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 21 Apr 2024 13:38:47 -0500 Subject: [PATCH 30/36] fix swiftlint errors --- .../Controller/TextViewController.swift | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 5bc2a3ca1..b32803772 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -237,9 +237,9 @@ public class TextViewController: NSViewController { required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + // MARK: Keyboard Shortcuts - + override public func viewDidLoad() { super.viewDidLoad() NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event in @@ -256,7 +256,7 @@ public class TextViewController: NSViewController { } } } - + /// Method called when CMD + / key sequence recognized, comments cursor's current line of code func commandSlashCalled() { guard let cursorPosition = cursorPositions.first else { @@ -269,18 +269,18 @@ public class TextViewController: NSViewController { if !language.lineCommentString.isEmpty { toggleCharsAtBeginningOfLine(chars: language.lineCommentString, lineNumber: cursorPosition.line) } - // In other cases, there are languages that require a character sequence at both the beginning and end of a line, aka a range comment + // In other cases, languages require a character sequence at beginning and end of a line, aka a range comment // (Ex. HTML ) - // We can treat the line as a one-line range to comment it out using the language's rangeCommentStrings on both sides of the line + // We treat the line as a one-line range to comment it out using rangeCommentStrings on both sides of the line else { - let (openComment,closeComment) = language.rangeCommentStrings + let (openComment, closeComment) = language.rangeCommentStrings toggleCharsAtEndOfLine(chars: closeComment, lineNumber: cursorPosition.line) toggleCharsAtBeginningOfLine(chars: openComment, lineNumber: cursorPosition.line) } } - - /// Toggles a specific string of characters at the beginning of a specified line in the textView's text storage. (lineNumber is 1-indexed) - private func toggleCharsAtBeginningOfLine(chars: String, lineNumber: Int){ + + /// Toggles comment string at the beginning of a specified line (lineNumber is 1-indexed) + private func toggleCharsAtBeginningOfLine(chars: String, lineNumber: Int) { guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { print("There are no characters/lineInfo \(#function)") return @@ -294,15 +294,21 @@ public class TextViewController: NSViewController { let firstCharsInLine = lineString.suffix(from: firstNonWhiteSpaceCharIndex).prefix(chars.count) // toggle comment off if firstCharsInLine == chars { - textView.replaceCharacters(in:NSRange(location: lineInfo.range.location + numWhitespaceChars, length: chars.count), with: "") + textView.replaceCharacters(in: NSRange( + location: lineInfo.range.location + numWhitespaceChars, + length: chars.count + ), with: "") } // toggle comment on else { - textView.replaceCharacters(in:NSRange(location: lineInfo.range.location + numWhitespaceChars, length: 0), with: chars) + textView.replaceCharacters(in: NSRange( + location: lineInfo.range.location + numWhitespaceChars, + length: 0 + ), with: chars) } } - /// Toggles a specific string of characters at the end of a specified line in the textView's text storage. (lineNumber is 1-indexed) + /// Toggles a specific string of characters at the end of a specified line. (lineNumber is 1-indexed) private func toggleCharsAtEndOfLine(chars: String, lineNumber: Int){ guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { print("There are no characters/lineInfo \(#function)") @@ -314,15 +320,18 @@ public class TextViewController: NSViewController { } let lineLastCharIndex = lineInfo.range.location + lineInfo.range.length - 1 let closeCommentLength = chars.count - let closeCommentRange = NSRange(location: lineLastCharIndex - closeCommentLength, length: closeCommentLength) + let closeCommentRange = NSRange( + location: lineLastCharIndex - closeCommentLength, + length: closeCommentLength + ) let lastCharsInLine = textView.textStorage.substring(from: closeCommentRange) // toggle comment off if lastCharsInLine == chars { - textView.replaceCharacters(in:NSRange(location: lineLastCharIndex - closeCommentLength, length: closeCommentLength), with: "") + textView.replaceCharacters(in: NSRange(location: lineLastCharIndex - closeCommentLength, length: closeCommentLength), with: "") } // toggle comment on else { - textView.replaceCharacters(in:NSRange(location: lineLastCharIndex, length: 0), with: chars) + textView.replaceCharacters(in: NSRange(location: lineLastCharIndex, length: 0), with: chars) } } From 723761c8623550ae19a738dc91838590d4326621 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 21 Apr 2024 13:53:57 -0500 Subject: [PATCH 31/36] get rid of extra line --- Sources/CodeEditSourceEditor/Controller/TextViewController.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index b32803772..fe2055729 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -18,7 +18,6 @@ import TextFormation /// tree-sitter for syntax highlighting, and TextFormation for live editing completions. /// public class TextViewController: NSViewController { - // swiftlint:disable:next line_length public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification") From acb3bb8231923abe7823bae449ee4d5b150acedf Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 21 Apr 2024 20:54:02 -0500 Subject: [PATCH 32/36] fix more lint issues --- .../Controller/TextViewController.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index fe2055729..832373d92 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -16,7 +16,7 @@ import TextFormation /// /// A view controller class for managing a source editor. Uses ``CodeEditTextView/TextView`` for input and rendering, /// tree-sitter for syntax highlighting, and TextFormation for live editing completions. -/// +/// // swiftlint:disable file_length public class TextViewController: NSViewController { // swiftlint:disable:next line_length public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification") @@ -306,9 +306,9 @@ public class TextViewController: NSViewController { ), with: chars) } } - + /// Toggles a specific string of characters at the end of a specified line. (lineNumber is 1-indexed) - private func toggleCharsAtEndOfLine(chars: String, lineNumber: Int){ + private func toggleCharsAtEndOfLine(chars: String, lineNumber: Int) { guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { print("There are no characters/lineInfo \(#function)") return @@ -326,7 +326,10 @@ public class TextViewController: NSViewController { let lastCharsInLine = textView.textStorage.substring(from: closeCommentRange) // toggle comment off if lastCharsInLine == chars { - textView.replaceCharacters(in: NSRange(location: lineLastCharIndex - closeCommentLength, length: closeCommentLength), with: "") + textView.replaceCharacters(in: NSRange( + location: lineLastCharIndex - closeCommentLength, + length: closeCommentLength + ), with: "") } // toggle comment on else { @@ -438,3 +441,5 @@ extension TextViewController: GutterViewDelegate { textView?.edgeInsets = HorizontalEdgeInsets(left: newWidth, right: 0) } } + +// swiftlint:enable file_length From a10b2f7a5547b3d4da040b086ba7e70e0c657e16 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 21 Apr 2024 22:22:00 -0500 Subject: [PATCH 33/36] make new file for keyboard shortcuts --- .../TextViewController+LoadView.swift | 14 +++ .../TextViewController+Shortcuts.swift | 92 ++++++++++++++++ .../Controller/TextViewController.swift | 100 ------------------ 3 files changed, 106 insertions(+), 100 deletions(-) create mode 100644 Sources/CodeEditSourceEditor/Controller/TextViewController+Shortcuts.swift diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift index 598359647..39648063b 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift @@ -109,5 +109,19 @@ extension TextViewController { } } .store(in: &cancellables) + + + NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event in + guard self.view.window?.firstResponder == self.textView else { return event } + let charactersIgnoringModifiers = event.charactersIgnoringModifiers + let commandKey = NSEvent.ModifierFlags.command.rawValue + let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue + if modifierFlags == commandKey && event.charactersIgnoringModifiers == "/" { + self.commandSlashCalled() + return nil + } else { + return event + } + } } } diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController+Shortcuts.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController+Shortcuts.swift new file mode 100644 index 000000000..24fe58263 --- /dev/null +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController+Shortcuts.swift @@ -0,0 +1,92 @@ +// +// TextViewController+Shortcuts.swift +// CodeEditSourceEditor +// +// Created by Sophia Hooley on 4/21/24. +// + +import CodeEditTextView +import AppKit + +extension TextViewController { + /// Method called when CMD + / key sequence recognized, comments cursor's current line of code + public func commandSlashCalled() { + guard let cursorPosition = cursorPositions.first else { + print("There is no cursor \(#function)") + return + } + // Many languages require a character sequence at the beginning of the line to comment the line. + // (ex. python #, C++ //) + // If such a sequence exists, we will insert that sequence at the beginning of the line + if !language.lineCommentString.isEmpty { + toggleCharsAtBeginningOfLine(chars: language.lineCommentString, lineNumber: cursorPosition.line) + } + // In other cases, languages require a character sequence at beginning and end of a line, aka a range comment + // (Ex. HTML ) + // We treat the line as a one-line range to comment it out using rangeCommentStrings on both sides of the line + else { + let (openComment, closeComment) = language.rangeCommentStrings + toggleCharsAtEndOfLine(chars: closeComment, lineNumber: cursorPosition.line) + toggleCharsAtBeginningOfLine(chars: openComment, lineNumber: cursorPosition.line) + } + } + + /// Toggles comment string at the beginning of a specified line (lineNumber is 1-indexed) + private func toggleCharsAtBeginningOfLine(chars: String, lineNumber: Int) { + guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { + print("There are no characters/lineInfo \(#function)") + return + } + guard let lineString = textView.textStorage.substring(from: lineInfo.range) else { + print("There are no characters/lineString \(#function)") + return + } + let firstNonWhiteSpaceCharIndex = lineString.firstIndex(where: {!$0.isWhitespace}) ?? lineString.startIndex + let numWhitespaceChars = lineString.distance(from: lineString.startIndex, to: firstNonWhiteSpaceCharIndex) + let firstCharsInLine = lineString.suffix(from: firstNonWhiteSpaceCharIndex).prefix(chars.count) + // toggle comment off + if firstCharsInLine == chars { + textView.replaceCharacters(in: NSRange( + location: lineInfo.range.location + numWhitespaceChars, + length: chars.count + ), with: "") + } + // toggle comment on + else { + textView.replaceCharacters(in: NSRange( + location: lineInfo.range.location + numWhitespaceChars, + length: 0 + ), with: chars) + } + } + + /// Toggles a specific string of characters at the end of a specified line. (lineNumber is 1-indexed) + private func toggleCharsAtEndOfLine(chars: String, lineNumber: Int) { + guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { + print("There are no characters/lineInfo \(#function)") + return + } + guard let lineString = textView.textStorage.substring(from: lineInfo.range) else { + print("There are no characters/lineString \(#function)") + return + } + let lineLastCharIndex = lineInfo.range.location + lineInfo.range.length - 1 + let closeCommentLength = chars.count + let closeCommentRange = NSRange( + location: lineLastCharIndex - closeCommentLength, + length: closeCommentLength + ) + let lastCharsInLine = textView.textStorage.substring(from: closeCommentRange) + // toggle comment off + if lastCharsInLine == chars { + textView.replaceCharacters(in: NSRange( + location: lineLastCharIndex - closeCommentLength, + length: closeCommentLength + ), with: "") + } + // toggle comment on + else { + textView.replaceCharacters(in: NSRange(location: lineLastCharIndex, length: 0), with: chars) + } + } +} diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 832373d92..b9e6d872b 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -237,106 +237,6 @@ public class TextViewController: NSViewController { fatalError("init(coder:) has not been implemented") } - // MARK: Keyboard Shortcuts - - override public func viewDidLoad() { - super.viewDidLoad() - NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event in - guard self.view.window?.firstResponder == self.textView else { return event } - let charactersIgnoringModifiers = event.charactersIgnoringModifiers - let commandKey = NSEvent.ModifierFlags.command.rawValue - let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue - if modifierFlags == commandKey && event.charactersIgnoringModifiers == "/" { - self.commandSlashCalled() - return nil - } else { - super.keyDown(with: event) - return event - } - } - } - - /// Method called when CMD + / key sequence recognized, comments cursor's current line of code - func commandSlashCalled() { - guard let cursorPosition = cursorPositions.first else { - print("There is no cursor \(#function)") - return - } - // Many languages require a character sequence at the beginning of the line to comment the line. - // (ex. python #, C++ //) - // If such a sequence exists, we will insert that sequence at the beginning of the line - if !language.lineCommentString.isEmpty { - toggleCharsAtBeginningOfLine(chars: language.lineCommentString, lineNumber: cursorPosition.line) - } - // In other cases, languages require a character sequence at beginning and end of a line, aka a range comment - // (Ex. HTML ) - // We treat the line as a one-line range to comment it out using rangeCommentStrings on both sides of the line - else { - let (openComment, closeComment) = language.rangeCommentStrings - toggleCharsAtEndOfLine(chars: closeComment, lineNumber: cursorPosition.line) - toggleCharsAtBeginningOfLine(chars: openComment, lineNumber: cursorPosition.line) - } - } - - /// Toggles comment string at the beginning of a specified line (lineNumber is 1-indexed) - private func toggleCharsAtBeginningOfLine(chars: String, lineNumber: Int) { - guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { - print("There are no characters/lineInfo \(#function)") - return - } - guard let lineString = textView.textStorage.substring(from: lineInfo.range) else { - print("There are no characters/lineString \(#function)") - return - } - let firstNonWhiteSpaceCharIndex = lineString.firstIndex(where: {!$0.isWhitespace}) ?? lineString.startIndex - let numWhitespaceChars = lineString.distance(from: lineString.startIndex, to: firstNonWhiteSpaceCharIndex) - let firstCharsInLine = lineString.suffix(from: firstNonWhiteSpaceCharIndex).prefix(chars.count) - // toggle comment off - if firstCharsInLine == chars { - textView.replaceCharacters(in: NSRange( - location: lineInfo.range.location + numWhitespaceChars, - length: chars.count - ), with: "") - } - // toggle comment on - else { - textView.replaceCharacters(in: NSRange( - location: lineInfo.range.location + numWhitespaceChars, - length: 0 - ), with: chars) - } - } - - /// Toggles a specific string of characters at the end of a specified line. (lineNumber is 1-indexed) - private func toggleCharsAtEndOfLine(chars: String, lineNumber: Int) { - guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { - print("There are no characters/lineInfo \(#function)") - return - } - guard let lineString = textView.textStorage.substring(from: lineInfo.range) else { - print("There are no characters/lineString \(#function)") - return - } - let lineLastCharIndex = lineInfo.range.location + lineInfo.range.length - 1 - let closeCommentLength = chars.count - let closeCommentRange = NSRange( - location: lineLastCharIndex - closeCommentLength, - length: closeCommentLength - ) - let lastCharsInLine = textView.textStorage.substring(from: closeCommentRange) - // toggle comment off - if lastCharsInLine == chars { - textView.replaceCharacters(in: NSRange( - location: lineLastCharIndex - closeCommentLength, - length: closeCommentLength - ), with: "") - } - // toggle comment on - else { - textView.replaceCharacters(in: NSRange(location: lineLastCharIndex, length: 0), with: chars) - } - } - /// Set the contents of the editor. /// - Parameter text: The new contents of the editor. public func setText(_ text: String) { From e520e6c0772a6518cc269394a82205a6acd003cc Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 21 Apr 2024 23:09:45 -0500 Subject: [PATCH 34/36] more linter errors fixed --- .../Controller/TextViewController+LoadView.swift | 1 - .../CodeEditSourceEditor/Controller/TextViewController.swift | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift index 39648063b..9750bd1eb 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift @@ -110,7 +110,6 @@ extension TextViewController { } .store(in: &cancellables) - NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event in guard self.view.window?.firstResponder == self.textView else { return event } let charactersIgnoringModifiers = event.charactersIgnoringModifiers diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index b9e6d872b..3f37b84f9 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -16,7 +16,7 @@ import TextFormation /// /// A view controller class for managing a source editor. Uses ``CodeEditTextView/TextView`` for input and rendering, /// tree-sitter for syntax highlighting, and TextFormation for live editing completions. -/// // swiftlint:disable file_length +// swiftlint:disable file_length public class TextViewController: NSViewController { // swiftlint:disable:next line_length public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification") From 4a4306e6cc677e8d0f2597f7c68c4d6971df653e Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 21 Apr 2024 23:40:19 -0500 Subject: [PATCH 35/36] lastfix of lint errors --- .../CodeEditSourceEditor/Controller/TextViewController.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 3f37b84f9..e85f14ca5 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -16,7 +16,7 @@ import TextFormation /// /// A view controller class for managing a source editor. Uses ``CodeEditTextView/TextView`` for input and rendering, /// tree-sitter for syntax highlighting, and TextFormation for live editing completions. -// swiftlint:disable file_length + public class TextViewController: NSViewController { // swiftlint:disable:next line_length public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification") @@ -341,5 +341,3 @@ extension TextViewController: GutterViewDelegate { textView?.edgeInsets = HorizontalEdgeInsets(left: newWidth, right: 0) } } - -// swiftlint:enable file_length From fab9079bb59018e66f3409e8a8c0a52d187dd9e6 Mon Sep 17 00:00:00 2001 From: sophiahooley Date: Sun, 21 Apr 2024 23:41:48 -0500 Subject: [PATCH 36/36] get rid of blank line --- Sources/CodeEditSourceEditor/Controller/TextViewController.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index e85f14ca5..32a73f6fd 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -16,7 +16,6 @@ import TextFormation /// /// A view controller class for managing a source editor. Uses ``CodeEditTextView/TextView`` for input and rendering, /// tree-sitter for syntax highlighting, and TextFormation for live editing completions. - public class TextViewController: NSViewController { // swiftlint:disable:next line_length public static let cursorPositionUpdatedNotification: Notification.Name = .init("TextViewController.cursorPositionNotification")