-
Notifications
You must be signed in to change notification settings - Fork 4
/
FormatImportDeclarationsCommand.swift
35 lines (28 loc) · 1.18 KB
/
FormatImportDeclarationsCommand.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//
// SourceEditorCommand.swift
// Snowonder Extension
//
// Created by Aliaksei Karetski on 19.06.17.
// Copyright © 2017 Karetski. All rights reserved.
//
import Foundation
import ImportArtisan
import XcodeKit
class FormatImportDeclarationsCommand: NSObject, XCSourceEditorCommand {
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void {
var error: Error? = nil
if let lines = invocation.buffer.lines as? [String] {
do {
let detector = ImportBlockDetector()
let importBlock = try detector.importBlock(from: lines)
let formatter = ImportBlockFormatter()
let formattedImportLines = formatter.lines(for: importBlock, using: ImportBlockFormatter.Operation.all)
let bufferEditor = SourceTextBufferEditor(buffer: invocation.buffer)
bufferEditor.replace(lines: importBlock.declarations, with: formattedImportLines, using: .top)
} catch let catchedError {
error = catchedError
}
}
completionHandler(error)
}
}