-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathOROpenInAppCode.m
More file actions
executable file
·110 lines (91 loc) · 3.5 KB
/
OROpenInAppCode.m
File metadata and controls
executable file
·110 lines (91 loc) · 3.5 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// OROpenInAppCode.m
// OROpenInAppCode
//
// Created by Orta on 30/01/2014.
// Copyright (c) 2014 Orta Therox. All rights reserved.
//
// Totally inspired by https://github.com/inquisitiveSoft/AJKExtendedOpening/blob/master/Classes/AJKExtendedOpening.m
#import "OROpenInAppCode.h"
static OROpenInAppCode *sharedPlugin;
@interface OROpenInAppCode()
@end
@implementation OROpenInAppCode
+ (void)pluginDidLoad:(NSBundle *)plugin
{
static id sharedPlugin = nil;
static dispatch_once_t onceToken;
NSString *currentApplicationName = [[NSBundle mainBundle] infoDictionary][@"CFBundleName"];
if ([currentApplicationName isEqual:@"Xcode"]) {
dispatch_once(&onceToken, ^{
sharedPlugin = [[self alloc] initWithBundle:plugin];
});
}
}
- (id)initWithBundle:(NSBundle *)plugin
{
if (self = [super init])
if(!self) return nil;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(setup)
name:NSApplicationDidFinishLaunchingNotification
object:nil];
return self;
}
- (void)setup
{
// Add menu bar items for the 'Show Project in Finder' and 'Open Project in Terminal' actions
NSMenu *fileMenu = [[[NSApp mainMenu] itemWithTitle:@"File"] submenu];
NSInteger desiredMenuItemIndex = [fileMenu indexOfItemWithTitle:@"Open with External Editor"];
if(fileMenu && (desiredMenuItemIndex >= 0)) {
NSMenuItem *openWithExternalEditorMenuItem = [[NSMenuItem alloc] initWithTitle:@"Open Project in AppCode" action:@selector(openInAppCode:) keyEquivalent:@"a"];
[openWithExternalEditorMenuItem setTarget:self];
[openWithExternalEditorMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask | NSControlKeyMask];
[fileMenu insertItem:openWithExternalEditorMenuItem atIndex:desiredMenuItemIndex];
} else if([NSApp mainMenu]) {
NSLog(@"OROpenInAppCode Xcode plugin: Couldn't find an 'Open with External Editor' item in the File menu");
}
else{
NSLog(@"OROpenInAppCode Xcode plugin: Couldn't get menu");
}
}
- (void)openInAppCode:(id)sender
{
NSURL *currentFileURL = [self currentProjectURL];
if(currentFileURL) {
NSArray *bundleIds = @[@"com.jetbrains.AppCode-EAP", @"com.jetbrains.AppCode"];
for (NSString *bundleIdentifier in bundleIds)
{
if ([[NSWorkspace sharedWorkspace] openURLs:@[currentFileURL]
withAppBundleIdentifier:bundleIdentifier
options:0
additionalEventParamDescriptor:nil
launchIdentifiers:nil])
{
break;
}
}
}
}
- (NSURL *)currentProjectURL
{
for (NSDocument *document in [NSApp orderedDocuments]) {
@try {
// _workspace(IDEWorkspace) -> representingFilePath(DVTFilePath) -> relativePathOnVolume(NSString)
NSURL *workspaceDirectoryURL = [[[document valueForKeyPath:@"_workspace.representingFilePath.fileURL"] URLByDeletingLastPathComponent] filePathURL];
if(workspaceDirectoryURL) {
return workspaceDirectoryURL;
}
}
@catch (NSException *exception) {
NSLog(@"OROpenInAppCode Xcode plugin: Raised an exception while asking for the documents '_workspace.representingFilePath.relativePathOnVolume' key path: %@", exception);
}
}
return nil;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end