Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment