|
| 1 | +// |
| 2 | +// TextViewController+Shortcuts.swift |
| 3 | +// CodeEditSourceEditor |
| 4 | +// |
| 5 | +// Created by Sophia Hooley on 4/21/24. |
| 6 | +// |
| 7 | + |
| 8 | +import CodeEditTextView |
| 9 | +import AppKit |
| 10 | + |
| 11 | +extension TextViewController { |
| 12 | + /// Method called when CMD + / key sequence recognized, comments cursor's current line of code |
| 13 | + public func commandSlashCalled() { |
| 14 | + guard let cursorPosition = cursorPositions.first else { |
| 15 | + print("There is no cursor \(#function)") |
| 16 | + return |
| 17 | + } |
| 18 | + // Many languages require a character sequence at the beginning of the line to comment the line. |
| 19 | + // (ex. python #, C++ //) |
| 20 | + // If such a sequence exists, we will insert that sequence at the beginning of the line |
| 21 | + if !language.lineCommentString.isEmpty { |
| 22 | + toggleCharsAtBeginningOfLine(chars: language.lineCommentString, lineNumber: cursorPosition.line) |
| 23 | + } |
| 24 | + // In other cases, languages require a character sequence at beginning and end of a line, aka a range comment |
| 25 | + // (Ex. HTML <!--line here -->) |
| 26 | + // We treat the line as a one-line range to comment it out using rangeCommentStrings on both sides of the line |
| 27 | + else { |
| 28 | + let (openComment, closeComment) = language.rangeCommentStrings |
| 29 | + toggleCharsAtEndOfLine(chars: closeComment, lineNumber: cursorPosition.line) |
| 30 | + toggleCharsAtBeginningOfLine(chars: openComment, lineNumber: cursorPosition.line) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /// Toggles comment string at the beginning of a specified line (lineNumber is 1-indexed) |
| 35 | + private func toggleCharsAtBeginningOfLine(chars: String, lineNumber: Int) { |
| 36 | + guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { |
| 37 | + print("There are no characters/lineInfo \(#function)") |
| 38 | + return |
| 39 | + } |
| 40 | + guard let lineString = textView.textStorage.substring(from: lineInfo.range) else { |
| 41 | + print("There are no characters/lineString \(#function)") |
| 42 | + return |
| 43 | + } |
| 44 | + let firstNonWhiteSpaceCharIndex = lineString.firstIndex(where: {!$0.isWhitespace}) ?? lineString.startIndex |
| 45 | + let numWhitespaceChars = lineString.distance(from: lineString.startIndex, to: firstNonWhiteSpaceCharIndex) |
| 46 | + let firstCharsInLine = lineString.suffix(from: firstNonWhiteSpaceCharIndex).prefix(chars.count) |
| 47 | + // toggle comment off |
| 48 | + if firstCharsInLine == chars { |
| 49 | + textView.replaceCharacters(in: NSRange( |
| 50 | + location: lineInfo.range.location + numWhitespaceChars, |
| 51 | + length: chars.count |
| 52 | + ), with: "") |
| 53 | + } |
| 54 | + // toggle comment on |
| 55 | + else { |
| 56 | + textView.replaceCharacters(in: NSRange( |
| 57 | + location: lineInfo.range.location + numWhitespaceChars, |
| 58 | + length: 0 |
| 59 | + ), with: chars) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Toggles a specific string of characters at the end of a specified line. (lineNumber is 1-indexed) |
| 64 | + private func toggleCharsAtEndOfLine(chars: String, lineNumber: Int) { |
| 65 | + guard let lineInfo = textView.layoutManager.textLineForIndex(lineNumber - 1) else { |
| 66 | + print("There are no characters/lineInfo \(#function)") |
| 67 | + return |
| 68 | + } |
| 69 | + guard let lineString = textView.textStorage.substring(from: lineInfo.range) else { |
| 70 | + print("There are no characters/lineString \(#function)") |
| 71 | + return |
| 72 | + } |
| 73 | + let lineLastCharIndex = lineInfo.range.location + lineInfo.range.length - 1 |
| 74 | + let closeCommentLength = chars.count |
| 75 | + let closeCommentRange = NSRange( |
| 76 | + location: lineLastCharIndex - closeCommentLength, |
| 77 | + length: closeCommentLength |
| 78 | + ) |
| 79 | + let lastCharsInLine = textView.textStorage.substring(from: closeCommentRange) |
| 80 | + // toggle comment off |
| 81 | + if lastCharsInLine == chars { |
| 82 | + textView.replaceCharacters(in: NSRange( |
| 83 | + location: lineLastCharIndex - closeCommentLength, |
| 84 | + length: closeCommentLength |
| 85 | + ), with: "") |
| 86 | + } |
| 87 | + // toggle comment on |
| 88 | + else { |
| 89 | + textView.replaceCharacters(in: NSRange(location: lineLastCharIndex, length: 0), with: chars) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments