Created
July 26, 2017 20:02
-
-
Save alanzeino/8d6800e64f670c8fb3607448d9f5a7e0 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// SwiftVersionSourceEditorCommand.swift | |
// WrapperExtension | |
// | |
// Created by Alan Zeino on 7/25/17. | |
// Copyright © 2017 Alan Zeino. All rights reserved. | |
// | |
import Foundation | |
import XcodeKit | |
class SwiftVersionSourceEditorCommand: NSObject, XCSourceEditorCommand { | |
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void { | |
guard invocation.buffer.contentUTI == "public.swift-source" else { completionHandler(nil); return } | |
let selections = invocation.buffer.selections as! [XCSourceTextRange] | |
let selection = selections.first! | |
let startIndex = selection.start.line | |
let endIndex = selection.end.line | |
let selectedRange = NSRange(location: startIndex, length: endIndex - startIndex) | |
let selectedLines = invocation.buffer.lines.subarray(with: selectedRange) | |
invocation.buffer.lines.insert("#if swift(>=3.2)", at: startIndex) | |
for string in selectedLines.reversed() { | |
invocation.buffer.lines.insert(string, at: startIndex + 1) | |
} | |
invocation.buffer.lines.insert("#else", at: startIndex + selectedLines.count + 1) | |
invocation.buffer.lines.insert("#endif", at: endIndex + selectedLines.count + 2) | |
completionHandler(nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment