Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. //
  2. // SwiftVersionSourceEditorCommand.swift
  3. // WrapperExtension
  4. //
  5. // Created by Alan Zeino on 7/25/17.
  6. // Copyright © 2017 Alan Zeino. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import XcodeKit
  11.  
  12. class SwiftVersionSourceEditorCommand: NSObject, XCSourceEditorCommand {
  13.  
  14. func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void {
  15. guard invocation.buffer.contentUTI == "public.swift-source" else { completionHandler(nil); return }
  16.  
  17. let selections = invocation.buffer.selections as! [XCSourceTextRange]
  18. let selection = selections.first!
  19. let startIndex = selection.start.line
  20. let endIndex = selection.end.line
  21.  
  22. let selectedRange = NSRange(location: startIndex, length: endIndex - startIndex)
  23. let selectedLines = invocation.buffer.lines.subarray(with: selectedRange)
  24.  
  25. invocation.buffer.lines.insert("#if swift(>=3.2)", at: startIndex)
  26. for string in selectedLines.reversed() {
  27. invocation.buffer.lines.insert(string, at: startIndex + 1)
  28. }
  29. invocation.buffer.lines.insert("#else", at: startIndex + selectedLines.count + 1)
  30. invocation.buffer.lines.insert("#endif", at: endIndex + selectedLines.count + 2)
  31.  
  32. completionHandler(nil)
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement