Toggle comments on multiple lines
@@ -1259,18 +1259,47 @@ fileprivate func parseArgs(_ args: inout [String]) {
/// Comments / Uncomments line.
@objc func toggleComment() {
- guard var line = textView.contentTextView.currentLine else {
+
+ guard textView.contentTextView.isFirstResponder else {
return
}
- let currentLine = line
+ guard let selected = textView.contentTextView.selectedTextRange else {
+ return
+ }
- line = line.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "\t", with: "")
+ if (textView.contentTextView.text(in: selected)?.components(separatedBy: "\n").count ?? 0) > 1 { // Multiple lines
+ guard let line = textView.contentTextView.selectedTextRange else {
+ return
+ }
- guard textView.contentTextView.isFirstResponder else {
+ guard let text = textView.contentTextView.text(in: line) else {
+ return
+ }
+
+ var newText = [String]()
+
+ for line in text.components(separatedBy: "\n") {
+ let _line = line.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "\t", with: "")
+ if _line == "" {
+ newText.append(line)
+ } else if _line.hasPrefix("#") {
+ newText.append(line.replacingFirstOccurrence(of: "#", with: ""))
+ } else {
+ newText.append("#"+line)
+ }
+ }
+
+ textView.contentTextView.replace(line, withText: newText.joined(separator: "\n"))
+ } else { // Current line
+ guard var line = textView.contentTextView.currentLine else {
return
}
+ let currentLine = line
+
+ line = line.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "\t", with: "")
+
if line.hasPrefix("#") {
line = currentLine.replacingFirstOccurrence(of: "#", with: "")
} else {
@@ -1280,6 +1309,7 @@ fileprivate func parseArgs(_ args: inout [String]) {
textView.contentTextView.replace(lineRange, withText: line)
}
}
+ }
/// Undo.
@objc func undo() {
