-
Notifications
You must be signed in to change notification settings - Fork 8
/
Xcode_copy_line.m
executable file
·348 lines (276 loc) · 13.8 KB
/
Xcode_copy_line.m
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// The MIT License (MIT)
//
// Copyright (c) 2014 Malte Thiesen
//
// 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 <Cocoa/Cocoa.h>
#import <objc/runtime.h>
// -----------------------------------------------------------------------------
// Pasteboard helpers
// -----------------------------------------------------------------------------
static id fullLinePasteboardItem = nil;
static void rememberCopiedPasteboardItem() {
fullLinePasteboardItem = nil;
if ([[NSPasteboard generalPasteboard] pasteboardItems].count == 1)
fullLinePasteboardItem = [[[NSPasteboard generalPasteboard] pasteboardItems] objectAtIndex:0];
}
static BOOL isPasteboardItemFullLine() {
if ([[NSPasteboard generalPasteboard] pasteboardItems].count == 1)
return [[[NSPasteboard generalPasteboard] pasteboardItems] objectAtIndex:0] == fullLinePasteboardItem;
else
return NO;
}
// -----------------------------------------------------------------------------
// NSTextView extensions
// -----------------------------------------------------------------------------
@implementation NSTextView (XcodeCopyLineExtensions)
// Returns the column the cursor currently is on.
- (NSUInteger)xcl_cursorColumn {
const NSUInteger beginningOfCurrentLine = [[self string] lineRangeForRange:[self selectedRange]].location;
const NSUInteger cursorColumn = MAX(self.selectedRange.location - beginningOfCurrentLine, 0);
return cursorColumn;
}
// Places the cursor an a specified colum in the current line.
// If the line is too short the cursor is placed at the end of the line.
- (void)xcl_setCursorColumn:(NSUInteger)column {
const NSRange lineRange = [[self string] lineRangeForRange:[self selectedRange]];
const NSUInteger lastCharacterOnLinePos = MAX(lineRange.location + lineRange.length - 1, lineRange.location);
const NSUInteger newCursorPos = MIN(lineRange.location + column, lastCharacterOnLinePos);
const NSRange newSelection = NSMakeRange(newCursorPos, 0);
[self setSelectedRange:newSelection];
[self scrollRangeToVisible:newSelection];
}
// If this is called at the beginning of an undo group the current cursor position
// is restored if the undo group is undone.
- (void)xcl_markCursorPositionForUndo {
// The cursor position is normally not considered when an undo is perfomed.
// We force the desired behaviour by inserting a dummy character an deleting
// it immediately afterwards.
[self insertText:@"x"];
[self doCommandBySelector:@selector(deleteBackward:)];
}
@end
// -----------------------------------------------------------------------------
// DVTSourceTextView hooks
// -----------------------------------------------------------------------------
static NSMenuItem * cutMenuItem = nil;
static NSMenuItem * copyMenuItem = nil;
static NSTextView * activeTextView = nil;
static BOOL performingHookedCommand = NO;
typedef BOOL (*DVTSourceTextViewResponderMethodPtr)(id, SEL);
typedef void (*DVTSourceTextViewEditMethodPtr)(id, SEL, id);
static DVTSourceTextViewResponderMethodPtr originalBecomeFirstResponderMethod = NULL;
static BOOL becomeFirstResponderHook(id self_, SEL selector) {
activeTextView = self_;
// Enable the Cut and Copy menu items in case they were disabled.
[cutMenuItem setEnabled:YES];
[copyMenuItem setEnabled:YES];
return originalBecomeFirstResponderMethod(self_, selector);
}
static DVTSourceTextViewResponderMethodPtr originalResignFirstResponderMethod = NULL;
static BOOL resignFirstResponderHook(id self_, SEL selector) {
activeTextView = nil;
return originalResignFirstResponderMethod(self_, selector);
}
static DVTSourceTextViewEditMethodPtr originalCutMethod = NULL;
static void cutHook(id self_, SEL selector, id sender) {
NSTextView * self = (NSTextView *)self_;
if (self.selectedRange.length == 0 && !performingHookedCommand) {
performingHookedCommand = YES;
const NSUInteger cursorColumn = [self xcl_cursorColumn];
// Cut the current line.
[self.undoManager beginUndoGrouping];
[self xcl_markCursorPositionForUndo];
[self doCommandBySelector:@selector(selectLine:)];
[self doCommandBySelector:@selector(cut:)];
[self.undoManager endUndoGrouping];
// Place the cursor on the same column as before on the line below the cut one.
[self xcl_setCursorColumn:cursorColumn];
// Remember the item that was placed in the NSUndoManager.
// If this item is pasted we need to perform a special full line paste.
rememberCopiedPasteboardItem();
performingHookedCommand = NO;
} else if (self.selectedRange.length > 0) {
originalCutMethod(self_, selector, sender);
fullLinePasteboardItem = nil;
}
}
static DVTSourceTextViewEditMethodPtr originalCopyMethod = NULL;
static void copyHook(id self_, SEL selector, id sender) {
NSTextView * self = (NSTextView *)self_;
if (self.selectedRange.length == 0 && !performingHookedCommand) {
performingHookedCommand = YES;
// Copy the current line.
const NSUInteger cursorPos = self.selectedRange.location;
[self doCommandBySelector:@selector(selectLine:)];
[self doCommandBySelector:@selector(copy:)];
[self setSelectedRange:NSMakeRange(cursorPos, 0)];
// Remember the item that was placed in the NSUndoManager.
// If this item is pasted we need to perform a special full line paste.
rememberCopiedPasteboardItem();
performingHookedCommand = NO;
} else if (self.selectedRange.length > 0) {
originalCopyMethod(self_, selector, sender);
fullLinePasteboardItem = nil;
}
}
static DVTSourceTextViewEditMethodPtr originalPasteMethod = NULL;
static void pasteHook(id self_, SEL selector, id sender) {
NSTextView * self = (NSTextView *)self_;
if (isPasteboardItemFullLine() && !performingHookedCommand) {
performingHookedCommand = YES;
const NSUInteger cursorColumn = [self xcl_cursorColumn];
// We need a slightly differend behaviour when the cursor is on the first line.
const BOOL cursorIsInFirstLine = [[self string] lineRangeForRange:[self selectedRange]].location == 0;
[self.undoManager beginUndoGrouping];
[self xcl_markCursorPositionForUndo];
if (cursorIsInFirstLine) {
[self doCommandBySelector:@selector(moveToBeginningOfLine:)];
} else {
[self doCommandBySelector:@selector(moveUp:)];
[self doCommandBySelector:@selector(moveToEndOfLine:)];
[self doCommandBySelector:@selector(insertNewline:)];
}
[self doCommandBySelector:@selector(paste:)];
if (!cursorIsInFirstLine) {
[self doCommandBySelector:@selector(deleteBackward:)];
[self doCommandBySelector:@selector(moveRight:)];
}
// Place the cursor on the same column as before on the line below the pasted one.
[self xcl_setCursorColumn:cursorColumn];
[self.undoManager endUndoGrouping];
performingHookedCommand = NO;
} else {
originalPasteMethod(self_, selector, sender);
}
}
// -----------------------------------------------------------------------------
// NSMenuItem hooks
// -----------------------------------------------------------------------------
typedef void (*NSMenuItemSetEnabledMethodPtr)(id, SEL, BOOL);
static NSMenuItemSetEnabledMethodPtr originalSetEnabledMethod = NULL;
static void setEnabledHook(id self_, SEL selector, BOOL flag) {
// Don't allow the Cut and Copy menu items to be disabled if there is a text
// view active. These are normally disabled when there is no selection.
// Now cut and copy are valid operations even without a selection.
if (flag == NO && activeTextView != nil && (self_ == cutMenuItem || self_ == copyMenuItem))
return;
else
originalSetEnabledMethod(self_, selector, flag);
}
// -----------------------------------------------------------------------------
// Xcode_copy_line
// -----------------------------------------------------------------------------
static struct {
const char * className;
const char * selectorName;
IMP replacementMethod;
IMP * originalMethod;
} methodHooks[] = {
{ "DVTSourceTextView", "becomeFirstResponder", (IMP)becomeFirstResponderHook, (IMP *)&originalBecomeFirstResponderMethod },
{ "DVTSourceTextView", "resignFirstResponder", (IMP)resignFirstResponderHook, (IMP *)&originalResignFirstResponderMethod },
{ "DVTSourceTextView", "cut:", (IMP)cutHook, (IMP *)&originalCutMethod },
{ "DVTSourceTextView", "copy:", (IMP)copyHook, (IMP *)&originalCopyMethod },
{ "DVTSourceTextView", "paste:", (IMP)pasteHook, (IMP *)&originalPasteMethod },
{ "NSMenuItem", "setEnabled:", (IMP)setEnabledHook, (IMP *)&originalSetEnabledMethod },
};
const int kHookCount = sizeof(methodHooks) / sizeof(methodHooks[0]);
@interface Xcode_copy_line : NSObject
@end
@implementation Xcode_copy_line
- (id)init {
// Hook into this notification in order to check menu items, since Xcode plugins get loaded in
// applicationWillFinishLaunching as of 6.4.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidFinishLaunching:)
name:NSApplicationDidFinishLaunchingNotification
object:nil];
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
const BOOL hookingSuccessful = (self = [super init]) && [self hookMethods];
if (!hookingSuccessful)
[self unhookMethods];
NSLog(@"%@ %@", [self className], hookingSuccessful ? @"initialized" : @"failed to initialize");
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSApplicationDidFinishLaunchingNotification
object:nil];
}
- (BOOL)hookMethods {
NSMenuItem * editMenu = [[NSApp mainMenu] itemWithTitle:@"Edit"];
if (editMenu != nil && [editMenu submenu] != nil) {
cutMenuItem = [[editMenu submenu] itemWithTitle:@"Cut"];
copyMenuItem = [[editMenu submenu] itemWithTitle:@"Copy"];
}
if (!cutMenuItem) {
NSLog(@"%@ ERROR: Unable to find Cut menu item", [self className]);
return NO;
}
if (!copyMenuItem) {
NSLog(@"%@ ERROR: Unable to find Copy menu item", [self className]);
return NO;
}
for (int i = 0; i < kHookCount; ++i) {
Class cls = NSClassFromString([NSString stringWithCString:methodHooks[i].className encoding:NSASCIIStringEncoding]);
if (!cls) {
NSLog(@"%@ ERROR: Unable to find class %s", [self className], methodHooks[i].className);
return NO;
}
SEL selector = NSSelectorFromString([NSString stringWithCString:methodHooks[i].selectorName
encoding:NSASCIIStringEncoding]);
Method method = class_getInstanceMethod(cls, selector);
if (!method) {
NSLog(@"%@ ERROR: Unable to find method %s of class %s",
[self className],
methodHooks[i].selectorName,
methodHooks[i].className);
return NO;
}
*methodHooks[i].originalMethod = (IMP)method_setImplementation(method, methodHooks[i].replacementMethod);
}
return YES;
}
- (void)unhookMethods {
for (int i = 0; i < kHookCount; ++i) {
if (*methodHooks[i].originalMethod == NULL)
continue;
Class cls = NSClassFromString([NSString stringWithCString:methodHooks[i].className encoding:NSASCIIStringEncoding]);
if (!cls)
continue;
SEL selector = NSSelectorFromString([NSString stringWithCString:methodHooks[i].selectorName
encoding:NSASCIIStringEncoding]);
Method method = class_getInstanceMethod(cls, selector);
if (!method)
continue;
method_setImplementation(method, *methodHooks[i].originalMethod);
}
}
- (void)dealloc {
[self unhookMethods];
[super dealloc];
}
+ (void)pluginDidLoad:(NSBundle *)plugin {
static dispatch_once_t onceToken;
static Xcode_copy_line * instance;
NSString *currentApplicationName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
if ([currentApplicationName isEqual:@"Xcode"])
dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; });
}
@end
// -----------------------------------------------------------------------------