forked from kinwahlai/XcodeRefactoringPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXcodeRefactoringPlus.m
More file actions
186 lines (159 loc) · 7.22 KB
/
XcodeRefactoringPlus.m
File metadata and controls
186 lines (159 loc) · 7.22 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2014 KinWah
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "XcodeRefactoringPlus.h"
#import "RefactoringLogic.h"
#import "DVTAdapterFactory.h"
#import <DVTSourceTextView.h>
static XcodeRefactoringPlus *sharedPlugin;
@interface XcodeRefactoringPlus()
{
RefactoringLogic *logic;
}
@property (nonatomic, strong) NSBundle *bundle;
@end
@implementation XcodeRefactoringPlus
+ (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 = [self initWithLogic:[[RefactoringLogic alloc] initWithAdapterFactory:[[DVTAdapterFactoryImpl alloc] init]]]) {
// reference to plugin's bundle, for resource acccess
self.bundle = plugin;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self setupSubMenu:[[[[NSApp mainMenu] itemWithTitle:@"Edit"] submenu] itemWithTitle:@"Refactor"]];
}];
}
return self;
}
- (instancetype)initWithLogic:(RefactoringLogic*)refactoringLogic
{
self = [super init];
if (self) {
logic = refactoringLogic;
}
return self;
}
-(void)setupSubMenu:(NSMenuItem*)menuItem
{
if(menuItem)
{
unichar arrowKeyDown = NSDownArrowFunctionKey;
unichar arrowKeyUp = NSUpArrowFunctionKey;
[[menuItem submenu] addItem:[NSMenuItem separatorItem]];
NSMenuItem *deleteLineMenuItem = [[NSMenuItem alloc] initWithTitle:@"Delete Line" action:@selector(deleteLine) keyEquivalent:@"d"];
[deleteLineMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
[deleteLineMenuItem setTarget:self];
[[menuItem submenu] addItem:deleteLineMenuItem];
NSMenuItem *duplicateLineMenuItem = [[NSMenuItem alloc] initWithTitle:@"Duplicate Line" action:@selector(duplicateLine) keyEquivalent:[NSString stringWithCharacters:&arrowKeyDown length:1]];
[duplicateLineMenuItem setTarget:self];
[duplicateLineMenuItem setKeyEquivalentModifierMask:(NSCommandKeyMask | NSAlternateKeyMask)];
[[menuItem submenu] addItem:duplicateLineMenuItem];
NSMenuItem *moveLineUpMenuItem = [[NSMenuItem alloc] initWithTitle:@"Move Line Up" action:@selector(moveLineUp) keyEquivalent:[NSString stringWithCharacters:&arrowKeyUp length:1]];
[moveLineUpMenuItem setTarget:self];
[moveLineUpMenuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask)];
[[menuItem submenu] addItem:moveLineUpMenuItem];
NSMenuItem *moveLineDownMenuItem = [[NSMenuItem alloc] initWithTitle:@"Move Line Down" action:@selector(moveLineDown) keyEquivalent:[NSString stringWithCharacters:&arrowKeyDown length:1]];
[moveLineDownMenuItem setTarget:self];
[moveLineDownMenuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask)];
[[menuItem submenu] addItem:moveLineDownMenuItem];
NSMenuItem *extractLocalVarMenuItem = [[NSMenuItem alloc] initWithTitle:@"Extract Local Variable" action:@selector(extractLocalVariable) keyEquivalent:@"l"];
[extractLocalVarMenuItem setTarget:self];
[extractLocalVarMenuItem setKeyEquivalentModifierMask:(NSCommandKeyMask | NSControlKeyMask)];
[[menuItem submenu] addItem:extractLocalVarMenuItem];
NSMenuItem *inlineLocalVarMenuItem = [[NSMenuItem alloc] initWithTitle:@"Inline Local Variable" action:@selector(inlineLocalVariable) keyEquivalent:@"i"];
[inlineLocalVarMenuItem setTarget:self];
[inlineLocalVarMenuItem setKeyEquivalentModifierMask:(NSCommandKeyMask | NSControlKeyMask)];
[[menuItem submenu] addItem:inlineLocalVarMenuItem];
}
}
-(BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if([self respondsToSelector:[menuItem action]])
{
return [self isFirstResponderATextView:[self getFirstResponder]];
}
return NO;
}
-(BOOL)isFirstResponderATextView:(NSResponder*)firstResponder
{
return ([firstResponder isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && [firstResponder isKindOfClass:[NSTextView class]]);
}
- (void)dealloc
{
[super dealloc];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSResponder *)getFirstResponder
{
NSResponder *firstResponder = [[NSApp keyWindow] firstResponder];
return firstResponder;
}
-(void)deleteLine
{
if ([self isFirstResponderATextView:[self getFirstResponder]]) {
DVTSourceTextView *dvtSourceTextView = (DVTSourceTextView*) [self getFirstResponder];
[logic deleteLineWithRange:dvtSourceTextView.selectedRange inTextView:dvtSourceTextView];
}
}
-(void)duplicateLine
{
if ([self isFirstResponderATextView:[self getFirstResponder]]) {
DVTSourceTextView *dvtSourceTextView = (DVTSourceTextView*) [self getFirstResponder];
[logic duplicateLineWithRange:dvtSourceTextView.selectedRange inTextView:dvtSourceTextView];
}
}
-(void)moveLineDown
{
if ([self isFirstResponderATextView:[self getFirstResponder]]) {
DVTSourceTextView *dvtSourceTextView = (DVTSourceTextView*) [self getFirstResponder];
[logic moveDownLineWithRange:dvtSourceTextView.selectedRange inTextView:dvtSourceTextView];
}
}
-(void)moveLineUp
{
if ([self isFirstResponderATextView:[self getFirstResponder]]) {
DVTSourceTextView *dvtSourceTextView = (DVTSourceTextView*) [self getFirstResponder];
[logic moveUpLineWithRange:dvtSourceTextView.selectedRange inTextView:dvtSourceTextView];
}
}
-(void)extractLocalVariable
{
if ([self isFirstResponderATextView:[self getFirstResponder]]) {
DVTSourceTextView *dvtSourceTextView = (DVTSourceTextView*) [self getFirstResponder];
[logic extractLocalVariableWithRange:dvtSourceTextView.selectedRange inTextView:dvtSourceTextView];
}
}
-(void)inlineLocalVariable
{
if ([self isFirstResponderATextView:[self getFirstResponder]]) {
DVTSourceTextView *dvtSourceTextView = (DVTSourceTextView*) [self getFirstResponder];
[logic inlineLocalVariableWithRange:dvtSourceTextView.selectedRange inTextView:dvtSourceTextView];
}
}
@end