-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSourceEditorCommand.swift
More file actions
75 lines (60 loc) · 2.35 KB
/
SourceEditorCommand.swift
File metadata and controls
75 lines (60 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// SourceEditorCommand.swift
// FastCommenter
//
// Created by Eric Baker on 28Sep2016.
// Copyright © 2016 DuneParkSoftware, LLC. All rights reserved.
//
import Foundation
import XcodeKit
class SourceEditorCommand: NSObject, XCSourceEditorCommand {
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void) -> Void {
defer {
completionHandler(nil)
}
func enumerateSelectedLines(_ block: (String, Int) -> Bool) -> Void {
var stop = false
invocation.buffer.selections.forEach { selection in
guard !stop else { return }
if let range = selection as? XCSourceTextRange {
(range.start.line ... range.end.line).forEach { lineIndex in
guard !stop, lineIndex < invocation.buffer.lines.count else { return }
if let line = invocation.buffer.lines[lineIndex] as? String {
stop = block(line, lineIndex)
}
}
}
}
}
let commentChars = "//"
// Examine all lines in the selection to see whether any need to be commented.
// If any need to be commented, then comment the whole selection.
// Otherwise, uncomment the whole selection.
var commentSelection = false
enumerateSelectedLines { line, lineIndex in
if !line.hasPrefix(commentChars) {
commentSelection = true
}
return commentSelection
}
var updatedLines = [Int]()
enumerateSelectedLines { line, lineIndex in
var updatedLine = line
if commentSelection {
updatedLine = "\(commentChars)\(line)"
} else {
updatedLine.removeFirst(commentChars.unicodeScalars.count)
}
invocation.buffer.lines[lineIndex] = updatedLine
updatedLines.append(lineIndex)
return false
}
if updatedLines.count > 0 {
let range = XCSourceTextRange()
let position = XCSourceTextPosition(line: updatedLines.last! + 1, column: 0)
range.start = position
range.end = position
invocation.buffer.selections.setArray([range])
}
}
}