Skip to content

Instantly share code, notes, and snippets.

@erica
Last active October 11, 2017 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/2e6cecf5fae2a206faed440539c514c4 to your computer and use it in GitHub Desktop.
Save erica/2e6cecf5fae2a206faed440539c514c4 to your computer and use it in GitHub Desktop.
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