Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Toggle comment for current line via CMD + / #241

Merged
merged 37 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b8acee0
add random comment to test git syncing with package
Sophiahooley Apr 14, 2024
9cecfa8
add key down event monitor
Sophiahooley Apr 14, 2024
7882267
create basic function to understand functionality of keyDown
Sophiahooley Apr 15, 2024
fca0329
edit basic function to include viewDidLoad call
Sophiahooley Apr 15, 2024
0787b98
changes to basic function
Sophiahooley Apr 15, 2024
614ce7e
add local monitoring functionality
Sophiahooley Apr 15, 2024
6879299
debug output for characters ignoring modifiers
Sophiahooley Apr 15, 2024
85e7c18
add check to see if cmd / has been pressed
Sophiahooley Apr 15, 2024
802815d
change function output to match what it is doing
Sophiahooley Apr 15, 2024
fd1ace9
print out cursor positions when cmd/ pressed
Sophiahooley Apr 15, 2024
8698fb7
include debug statement for what string is when cmd/ is pressed
Sophiahooley Apr 15, 2024
87511ba
attempt to insert text when cmd/ pressed
Sophiahooley Apr 15, 2024
2167053
insert text at beginning of line
Sophiahooley Apr 15, 2024
8d40d08
print out current cursor line
Sophiahooley Apr 16, 2024
1c5e334
fix line indexing
Sophiahooley Apr 16, 2024
f231eca
fix indexing again
Sophiahooley Apr 16, 2024
b692a31
insert comment at beginning of line using line char index
Sophiahooley Apr 16, 2024
507247b
add debug to see if code reaches inner if statement
Sophiahooley Apr 16, 2024
ecfc976
use lineCommentString instead of hardcoded string for comment
Sophiahooley Apr 17, 2024
412c327
add space after comment insert
Sophiahooley Apr 18, 2024
6c86617
print lineInfo data
Sophiahooley Apr 18, 2024
555ed63
print line info line fragments
Sophiahooley Apr 18, 2024
2498247
print line info
Sophiahooley Apr 19, 2024
a23c0aa
get substring of current line
Sophiahooley Apr 19, 2024
1a5870c
toggle commenting
Sophiahooley Apr 19, 2024
0b288ad
finish single line implementation, clean up code, add case for no lin…
Sophiahooley Apr 19, 2024
0eeddec
rollback
Sophiahooley Apr 19, 2024
acbbec5
make requested changes from PR (more comments, delete debug line)
Sophiahooley Apr 21, 2024
3bf438a
refactor keyboard shortcuts logic, more changes requested from PR
Sophiahooley Apr 21, 2024
664e375
fix swiftlint errors
Sophiahooley Apr 21, 2024
723761c
get rid of extra line
Sophiahooley Apr 21, 2024
acb3bb8
fix more lint issues
Sophiahooley Apr 22, 2024
a10b2f7
make new file for keyboard shortcuts
Sophiahooley Apr 22, 2024
e520e6c
more linter errors fixed
Sophiahooley Apr 22, 2024
4a4306e
lastfix of lint errors
Sophiahooley Apr 22, 2024
fab9079
get rid of blank line
Sophiahooley Apr 22, 2024
15a6e33
Merge branch 'main' into main
FastestMolasses Apr 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,18 @@ 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
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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 <!--line here -->)
// 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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading