-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benoit Sarrazin
committed
Oct 30, 2016
1 parent
7f35e07
commit 2919fc3
Showing
3 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// SortSourceEditorCommand.swift | ||
// Soothe | ||
// | ||
// Created by Benoit Sarrazin on 2016-10-30. | ||
// Copyright © 2016 Berzerker IO. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import XcodeKit | ||
|
||
class SortSourceEditorCommand: NSObject, XCSourceEditorCommand { | ||
|
||
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void { | ||
guard let selection = invocation.buffer.selections.firstObject as? XCSourceTextRange else { | ||
let userInfo = [NSLocalizedDescriptionKey: "This extension only works with a selection."] | ||
let error = NSError(domain: "\(#file) -> \(#function)", code: #line, userInfo: userInfo) | ||
completionHandler(error) | ||
return | ||
} | ||
|
||
let allLines = invocation.buffer.lines.flatMap { $0 as? String } | ||
let lines = allLines[selection.start.line...selection.end.line].sorted() | ||
let range = NSMakeRange(selection.start.line, selection.end.line - selection.start.line + 1) | ||
invocation.buffer.lines.replaceObjects(in: range, withObjectsFrom: lines) | ||
completionHandler(nil) | ||
} | ||
|
||
} |