Skip to content

Commit

Permalink
Adding sorting extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Sarrazin committed Oct 30, 2016
1 parent 7f35e07 commit 2919fc3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Soothe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
98111A371DC655AA00905480 /* InterpolateSourceEditorCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98111A361DC655AA00905480 /* InterpolateSourceEditorCommand.swift */; };
98111A3B1DC655AA00905480 /* Soothing.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 98111A2D1DC655AA00905480 /* Soothing.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
98688BAC1DC665B1005A6D13 /* AlignSourceEditorCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98688BAB1DC665B1005A6D13 /* AlignSourceEditorCommand.swift */; };
98688BAE1DC667F3005A6D13 /* SortSourceEditorCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98688BAD1DC667F3005A6D13 /* SortSourceEditorCommand.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -56,6 +57,7 @@
98111A361DC655AA00905480 /* InterpolateSourceEditorCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterpolateSourceEditorCommand.swift; sourceTree = "<group>"; };
98111A381DC655AA00905480 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
98688BAB1DC665B1005A6D13 /* AlignSourceEditorCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlignSourceEditorCommand.swift; sourceTree = "<group>"; };
98688BAD1DC667F3005A6D13 /* SortSourceEditorCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SortSourceEditorCommand.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -180,6 +182,7 @@
children = (
98688BAB1DC665B1005A6D13 /* AlignSourceEditorCommand.swift */,
98111A361DC655AA00905480 /* InterpolateSourceEditorCommand.swift */,
98688BAD1DC667F3005A6D13 /* SortSourceEditorCommand.swift */,
);
name = Commands;
sourceTree = "<group>";
Expand Down Expand Up @@ -300,6 +303,7 @@
98111A351DC655AA00905480 /* SourceEditorExtension.swift in Sources */,
98688BAC1DC665B1005A6D13 /* AlignSourceEditorCommand.swift in Sources */,
98111A371DC655AA00905480 /* InterpolateSourceEditorCommand.swift in Sources */,
98688BAE1DC667F3005A6D13 /* SortSourceEditorCommand.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
8 changes: 8 additions & 0 deletions Soothing/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
<key>XCSourceEditorCommandName</key>
<string>Interpolate</string>
</dict>
<dict>
<key>XCSourceEditorCommandClassName</key>
<string>$(PRODUCT_MODULE_NAME).SortSourceEditorCommand</string>
<key>XCSourceEditorCommandIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).SortSourceEditorCommand</string>
<key>XCSourceEditorCommandName</key>
<string>Sort</string>
</dict>
</array>
<key>XCSourceEditorExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).SourceEditorExtension</string>
Expand Down
29 changes: 29 additions & 0 deletions Soothing/SortSourceEditorCommand.swift
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)
}

}

0 comments on commit 2919fc3

Please sign in to comment.