Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
for selection in invocation.buffer.selections {
for lineIndex in selection.start.line...selection.end.line {
guard let line = invocation.buffer.lines[lineIndex] as? String else { continue }
guard !line.hasPrefix("/// ") else { continue }
let newLine = "/// " + line
invocation.buffer.lines[lineIndex] = newLine
updatedSelections.append(
{
$0.start = XCSourceTextPosition(line: lineIndex, column: 0)
$0.end = XCSourceTextPosition(line: lineIndex + 1, column: 0)
return $0
}(XCSourceTextRange()))
}
}
if !updatedSelections.isEmpty {
invocation.buffer.selections.setArray(updatedSelections)
}
completionHandler(nil)
}
func getSelection(_ index: Int, _ invocation: XCSourceEditorCommandInvocation) -> String? {
guard index < invocation.buffer.selections.count else { return nil }
let selection = invocation.buffer.selections[index]
let lines = invocation.buffer.lines
var output = ""
for lineIndex in selection.start.line...selection.end.line {
var line = lines[lineIndex]
if lineIndex == selection.end.line {
line = line.substring(to: selection.end.column)
}
if lineIndex == selection.start.line {
line = line.substring(to: selection.start.column)
}
print(line, terminator: "", to: &output)
}
return output
}
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void {
var updatedSelections: [XCSourceTextRange] = []
for selection in invocation.buffer.selections {
for lineIndex in selection.start.line...selection.end.line {
guard let line = invocation.buffer.lines[lineIndex] as? String else { continue }
guard line.hasPrefix("/// ") else { continue }
let index = line.index(line.startIndex, offsetBy: 4)
let newLine = line.substring(from: index)
invocation.buffer.lines[lineIndex] = newLine
updatedSelections.append(
{
$0.start = XCSourceTextPosition(line: lineIndex, column: 0)
$0.end = XCSourceTextPosition(line: lineIndex + 1, column: 0)
return $0
}(XCSourceTextRange()))
}
}
if !updatedSelections.isEmpty {
invocation.buffer.selections.setArray(updatedSelections)
}
completionHandler(nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment