Skip to content

Instantly share code, notes, and snippets.

@hlung
Last active April 21, 2020 03:10
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 hlung/d5d8e1067e759921492fafd60db8cac5 to your computer and use it in GitHub Desktop.
Save hlung/d5d8e1067e759921492fafd60db8cac5 to your computer and use it in GitHub Desktop.
A simple Xcode extension that reverses code on selected lines
import Foundation
import XcodeKit
class SourceEditorCommand: NSObject, XCSourceEditorCommand {
// Reverses code on selected lines
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void {
let lines = invocation.buffer.lines as? [String] ?? []
let selections = invocation.buffer.selections as? [XCSourceTextRange] ?? []
for selection in selections {
let indices: [Int] = Array(selection.start.line...selection.end.line)
let reversedIndices: [Int] = indices.reversed()
for i in 0..<indices.count {
// Reverse the lines
let from = reversedIndices[i]
let to = indices[i]
invocation.buffer.lines[to] = lines[from]
}
}
// Signal to Xcode that the command has completed.
completionHandler(nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment