Last active
April 21, 2020 03:10
-
-
Save hlung/d5d8e1067e759921492fafd60db8cac5 to your computer and use it in GitHub Desktop.
A simple Xcode extension that reverses code on selected lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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