Skip to content

Instantly share code, notes, and snippets.

@alanzeino
Created July 26, 2017 20:02
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 alanzeino/8d6800e64f670c8fb3607448d9f5a7e0 to your computer and use it in GitHub Desktop.
Save alanzeino/8d6800e64f670c8fb3607448d9f5a7e0 to your computer and use it in GitHub Desktop.
//
// 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