-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathXcode_beginning_of_line.m
executable file
·450 lines (383 loc) · 23 KB
/
Xcode_beginning_of_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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#import <Cocoa/Cocoa.h>
#import <objc/runtime.h>
static IMP original_doCommandBySelector = nil;
@interface Xcode_beginning_of_line : NSObject
@end
@implementation Xcode_beginning_of_line
NSUInteger matchingBracketPosition(NSString * text, NSRange range, bool isForward, unichar bracket) {
unichar pair = 0;
switch (bracket) {
case '}':
pair = '{';
break;
case '{':
pair = '}';
break;
case '[':
pair = ']';
break;
case ']':
pair = '[';
break;
case '(':
pair = ')';
break;
case ')':
pair = '(';
break;
}
NSString * searchStr = [NSString stringWithCharacters:&pair length:1];
NSRange r = range;
int level = 1;
NSCharacterSet * searchset = [NSCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:@"%c%c", bracket, pair]];
while(true) {
r = [text rangeOfCharacterFromSet:searchset options:isForward?0:NSBackwardsSearch range:r];
if (r.location == NSNotFound) return NSNotFound;
if ([[text substringWithRange:r] isEqualToString:searchStr]) {
--level;
if (level == 0) {
return r.location;
}
} else {
++level;
}
r = isForward ? NSMakeRange(r.location + 1, range.location + range.length - r.location - 1) : NSMakeRange(range.location, r.location - range.location);
}
}
NSUInteger matchingQuotePosition(NSString * text, NSUInteger position, bool isDouble) {
int count = 0;
NSString * searchStr = isDouble ? @"\"" : @"'";
NSCharacterSet * searchSet = [NSCharacterSet characterSetWithCharactersInString:searchStr];
bool countForward = position > (text.length - position);
NSUInteger currPosition = countForward ? position : position;
while (true) {
currPosition = symbolsFromSetPosition(text, countForward ? NSMakeRange(currPosition + 1, text.length - currPosition - 1) : NSMakeRange(0, currPosition), countForward, searchSet);
if (currPosition == NSNotFound) break;
if (currPosition == 0 || (![[text substringWithRange:NSMakeRange(currPosition - 1, 1)] isEqualToString:@"\\"])) count++;
}
bool searchForward = countForward ^ (count%2 == 0);
currPosition = searchForward ? position : position;
while (true) {
currPosition = symbolsFromSetPosition(text, searchForward ? NSMakeRange(currPosition + 1, text.length - currPosition - 1) : NSMakeRange(0, currPosition), searchForward, searchSet);
if (currPosition == NSNotFound) return NSNotFound;
if (currPosition == 0 || (![[text substringWithRange:NSMakeRange(currPosition - 1, 1)] isEqualToString:@"\\"])) return currPosition;
}
}
NSUInteger symbolsFromSetPosition(NSString * text, NSRange range, bool isForward, NSCharacterSet * set) {
NSRange r = [text rangeOfCharacterFromSet:set options:isForward ? 0 : NSBackwardsSearch range:range];
return r.location;
}
NSRange extendRangeToMatchingQuote(NSString * text, NSRange range, NSUInteger position, bool isDouble) {
NSUInteger bPos = matchingQuotePosition(text, position, isDouble);
if (bPos == NSNotFound || (bPos > range.location && bPos < (range.location + range.length)))
return position < range.location ? NSMakeRange(position, range.location + range.length - position) : NSMakeRange(range.location, position + 1 - range.location);
if (bPos < position) return NSMakeRange(bPos, position + 1 - bPos);
return NSMakeRange(position, bPos + 1 - position);
}
NSRange shrinkRangeToMatchingQuote(NSString * text, NSRange range, NSUInteger position, bool isDouble) {
bool begin = (position == range.location);
NSUInteger bPos = matchingQuotePosition(text, position, isDouble);
if (bPos == NSNotFound || bPos < range.location || bPos > (range.location + range.length))
return begin ? NSMakeRange(position, range.location + range.length - position) : NSMakeRange(range.location, position + 1 - range.location);
if (bPos < position) return NSMakeRange(bPos, position + 1 - bPos);
return NSMakeRange(position, bPos + 1- position);
}
NSRange extendRangeToMatchingBracket(NSString * text, NSRange range, NSRange lineRange, NSUInteger position, unichar bracket, bool isForward) {
NSRange searchRange = isForward ? NSMakeRange(position + 1, lineRange.location + lineRange.length - position - 1) : NSMakeRange(lineRange.location, position - lineRange.location);
NSUInteger bPos = matchingBracketPosition(text, searchRange, isForward, bracket);
if (bPos == NSNotFound || (bPos > range.location && bPos < (range.location + range.length)))
return isForward ? NSMakeRange(position, range.location + range.length - position) : NSMakeRange(range.location, position - range.location);
if (isForward) return NSMakeRange(range.location, bPos - range.location);
return NSMakeRange(bPos, range.location + range.length + 1 - bPos);
}
NSRange shrinkRangeToMatchingBracket(NSString * text, NSRange range, NSUInteger position, unichar bracket, bool isForward) {
NSRange searchRange = isForward ? NSMakeRange(position + 1, range.location + range.length - position - 1) : NSMakeRange(range.location, position - range.location);
NSUInteger bPos = matchingBracketPosition(text, searchRange, isForward, bracket);
if (bPos == NSNotFound)
return isForward ? NSMakeRange(position + 1, range.location + range.length - position - 1) : NSMakeRange(range.location, position - 1 - range.location);
if (isForward) return NSMakeRange(range.location, bPos + 1 - range.location);
return NSMakeRange(bPos, range.location + range.length - bPos);
}
NSRange extendRangeToSymbolFromSet(NSString * text, NSRange range, NSRange lineRange, NSCharacterSet * charset, bool isForward) {
NSRange searchRange = isForward ? NSMakeRange(range.location + range.length, lineRange.location + lineRange.length - range.location - range.length) : NSMakeRange(lineRange.location, range.location - lineRange.location);
NSUInteger sPos = symbolsFromSetPosition(text, searchRange, isForward, charset);
if (sPos == NSNotFound) return isForward ? NSMakeRange(range.location, lineRange.location + lineRange.length - range.location) : NSMakeRange(lineRange.location, range.location + range.length - lineRange.location);
if (isForward) return NSMakeRange(range.location, sPos - range.location);
return NSMakeRange(sPos + 1, range.location + range.length - sPos - 1);
}
NSRange shrinkRangeToSymbolFromSet(NSString * text, NSRange range, NSCharacterSet * charset, bool isForward) {
NSRange searchRange = isForward ? NSMakeRange(range.location + 1, range.length - 1) : NSMakeRange(range.location, range.length - 1);
NSUInteger sPos = symbolsFromSetPosition(text, searchRange, isForward, charset);
if (sPos == NSNotFound) return searchRange;
if (isForward) return NSMakeRange(sPos, range.location + range.length - sPos);
return NSMakeRange(range.location, sPos + 1 - range.location);
}
NSRange extendRange(NSString * text, NSRange range) {
NSUInteger end = range.location + range.length;
NSRange lineRange = [text lineRangeForRange:range];
NSCharacterSet * alpha = [NSCharacterSet alphanumericCharacterSet];
NSCharacterSet * spaces = [NSCharacterSet whitespaceCharacterSet];
NSCharacterSet * nonAlpha = [alpha invertedSet];
NSCharacterSet * nonSpaces = [spaces invertedSet];
NSCharacterSet * numeric = [NSCharacterSet decimalDigitCharacterSet];
//NSCharacterSet * nonNumeric = [numeric invertedSet];
bool hasBefore = (range.location > lineRange.location && symbolsFromSetPosition(text, NSMakeRange(lineRange.location, range.location - lineRange.location), false, nonSpaces) != NSNotFound);
bool hasAfter = ((lineRange.location + lineRange.length) > end && symbolsFromSetPosition(text, NSMakeRange(end, lineRange.location + lineRange.length - end), true, nonSpaces) != NSNotFound);
if (range.location > 0 && end < text.length) {
unichar begChar = [text characterAtIndex:range.location - 1];
unichar endChar = [text characterAtIndex:range.location + range.length];
if (begChar == '"' && endChar != '"') {
NSRange nRange = extendRangeToMatchingQuote(text, range, range.location - 1, true);
if (nRange.location == (range.location - 1)) return nRange;
}
if (endChar == '"' && begChar != '"') {
NSRange nRange = extendRangeToMatchingQuote(text, range, range.location + range.length, true);
if ((nRange.location + nRange.length) == (range.location + range.length + 1)) return nRange;
}
if (begChar == '\'' && endChar != '\'') {
NSRange nRange = extendRangeToMatchingQuote(text, range, range.location - 1, false);
if (nRange.location == range.location) return nRange;
}
if (endChar == '\'' && begChar != '\'') {
NSRange nRange = extendRangeToMatchingQuote(text, range, range.location + range.length, false);
if ((nRange.location + nRange.length) == (range.location + range.length)) return nRange;
}
if (begChar == '.' && endChar != '.') {
//return extendRangeToSymbolFromSet(text, NSMakeRange(range.location - 1, range.length + 1), lineRange, nonAlpha, false);
if ([numeric characterIsMember:[text characterAtIndex:range.location - 2]] && [numeric characterIsMember:endChar]) {
range = extendRangeToSymbolFromSet(text, NSMakeRange(range.location, range.length), lineRange, nonAlpha, true);
}
return extendRangeToSymbolFromSet(text, NSMakeRange(range.location - 1, range.length + 1), lineRange, nonAlpha, false);
}
if (endChar == '.' && begChar != '.') {
//return extendRangeToSymbolFromSet(text, NSMakeRange(range.location, range.length + 1), lineRange, nonAlpha, true);
if ([numeric characterIsMember:[text characterAtIndex:end - 1]] && [numeric characterIsMember:[text characterAtIndex:end + 1]]) {
range = extendRangeToSymbolFromSet(text, NSMakeRange(range.location, range.length), lineRange, nonAlpha, false);
}
return extendRangeToSymbolFromSet(text, NSMakeRange(range.location, range.length + 1), lineRange, nonAlpha, true);
}
if (begChar == '(') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(range.location, lineRange.location + lineRange.length - range.location), true, begChar);
if (pos != NSNotFound && pos != end)
return extendRangeToMatchingBracket(text, range, lineRange, range.location - 1, begChar, true);
}
if (endChar == ')') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(lineRange.location, range.location + range.length - lineRange.location), false, endChar);
if (pos != NSNotFound && pos != (range.location - 1))
return extendRangeToMatchingBracket(text, range, lineRange, range.location + range.length, endChar, false);
}
if (endChar == '(') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(end + 1, lineRange.location + lineRange.length - end - 1), true, endChar);
if (pos != NSNotFound) {
NSRange r = extendRangeToMatchingBracket(text, range, lineRange, range.location + range.length, endChar, true);
return NSMakeRange(r.location, r.length + 1);
}
}
if (begChar == ')') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(lineRange.location, range.location - lineRange.location - 1), false, begChar);
if (pos != NSNotFound) {
NSRange r = extendRangeToMatchingBracket(text, range, lineRange, range.location - 1, begChar, false);
return NSMakeRange(r.location - 1, r.length + 1);
}
}
if (begChar == '[') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(range.location, lineRange.location + lineRange.length - range.location), true, begChar);
if (pos != NSNotFound && pos != end)
return extendRangeToMatchingBracket(text, range, lineRange, range.location - 1, begChar, true);
}
if (endChar == ']') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(lineRange.location, range.location + range.length - lineRange.location), false, endChar);
if (pos != NSNotFound && pos != (range.location - 1))
return extendRangeToMatchingBracket(text, range, lineRange, range.location + range.length, endChar, false);
}
if (endChar == '[') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(end + 1, lineRange.location + lineRange.length - end - 1), true, endChar);
if (pos != NSNotFound) {
NSRange r = extendRangeToMatchingBracket(text, range, lineRange, range.location + range.length, endChar, true);
return NSMakeRange(r.location, r.length + 1);
}
}
if (begChar == ']') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(lineRange.location, range.location - lineRange.location - 1), false, begChar);
if (pos != NSNotFound) {
NSRange r = extendRangeToMatchingBracket(text, range, lineRange, range.location - 1, begChar, false);
return NSMakeRange(r.location - 1, r.length + 1);
}
}
if ([alpha characterIsMember:begChar] && ![alpha characterIsMember:endChar]) {
return extendRangeToSymbolFromSet(text, range, lineRange, nonAlpha, false);
}
if (([spaces characterIsMember:begChar] && range.location > lineRange.location) || ([spaces characterIsMember:endChar] && end < (lineRange.location + lineRange.length))) {
bool changes = false;
if ([spaces characterIsMember:begChar] && range.location > lineRange.location && hasBefore) {
range = extendRangeToSymbolFromSet(text, range, lineRange, nonSpaces, false);
changes = true;
}
if ([spaces characterIsMember:endChar] && end < (lineRange.location + lineRange.length) && hasAfter) {
range = extendRangeToSymbolFromSet(text, range, lineRange, nonSpaces, true);
changes = true;
}
if (changes) return extendRange(text, range);
//return range;
}
}
NSUInteger backwardTo = range.location;
if (hasBefore) {
backwardTo = symbolsFromSetPosition(text, NSMakeRange(lineRange.location, range.location - lineRange.location), false, nonAlpha);
if (backwardTo == NSNotFound) backwardTo = lineRange.location;
else backwardTo++;
if (backwardTo == range.location && backwardTo > lineRange.location) backwardTo--;
}
NSUInteger forwardTo = end - 1;
if (hasAfter) {
forwardTo = symbolsFromSetPosition(text, NSMakeRange(range.location + range.length, lineRange.location + lineRange.length - range.location - range.length), true, nonAlpha);
if (forwardTo == NSNotFound) forwardTo = lineRange.location + lineRange.length - 1;
else forwardTo--;
if (forwardTo == (range.location + range.length - 1) && forwardTo < (lineRange.location + lineRange.length - 1)) forwardTo++;
}
NSUInteger calculatedEnd = forwardTo - backwardTo + 1;
return NSMakeRange(backwardTo, calculatedEnd);
}
NSRange shrinkRange(NSString * text, NSRange range) {
if (range.length < 2) return range;
NSUInteger end = range.location + range.length;
NSCharacterSet * spaces = [NSCharacterSet whitespaceCharacterSet];
NSCharacterSet * nonSpaces = [spaces invertedSet];
NSCharacterSet * nonAlpha = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
unichar begChar = [text characterAtIndex:range.location];
unichar endChar = [text characterAtIndex:range.location + range.length - 1];
if (begChar == '"' && endChar != '"') {
return shrinkRangeToMatchingQuote(text, range, range.location, true);
}
if (endChar == '"' && begChar != '"') {
return shrinkRangeToMatchingQuote(text, range, range.location + range.length - 1, true);
}
if (begChar == '\'' && endChar != '\'') {
return shrinkRangeToMatchingQuote(text, range, range.location, false);
}
if (endChar == '\'' && begChar != '\'') {
return shrinkRangeToMatchingQuote(text, range, range.location + range.length - 1, false);
}
if (begChar == '(') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(range.location + 1, range.length - 1), true, begChar);
if (pos != NSNotFound && pos != (end - 1))
return shrinkRangeToMatchingBracket(text, range, range.location, begChar, true);
if (pos != NSNotFound) return NSMakeRange(range.location + 1, range.length - 2);
}
if (endChar == ')') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(range.location, range.length - 1), false, endChar);
if (pos != NSNotFound && pos != (range.location - 1))
return shrinkRangeToMatchingBracket(text, range, range.location + range.length - 1, endChar, false);
if (pos != NSNotFound) return NSMakeRange(range.location + 1, range.length - 2);
}
if (begChar == '[') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(range.location + 1, range.length - 1), true, begChar);
if (pos != NSNotFound && pos != (end - 1))
return shrinkRangeToMatchingBracket(text, range, range.location, begChar, true);
if (pos != NSNotFound) return NSMakeRange(range.location + 1, range.length - 2);
}
if (endChar == ']') {
NSUInteger pos = matchingBracketPosition(text, NSMakeRange(range.location, range.length - 1), false, endChar);
if (pos != NSNotFound && pos != (range.location - 1))
return shrinkRangeToMatchingBracket(text, range, range.location + range.length - 1, endChar, false);
if (pos != NSNotFound) return NSMakeRange(range.location + 1, range.length - 2);
}
if ([spaces characterIsMember:begChar] || [spaces characterIsMember:endChar]) {
if ([spaces characterIsMember:begChar]) {
range = shrinkRangeToSymbolFromSet(text, range, nonSpaces, true);
}
if ([spaces characterIsMember:endChar]) {
range = shrinkRangeToSymbolFromSet(text, range, nonSpaces, false);
}
return range;
}
NSUInteger backwardTo = symbolsFromSetPosition(text, range, true, nonAlpha);
if (backwardTo == NSNotFound) backwardTo = range.location + 1;
NSUInteger forwardTo = symbolsFromSetPosition(text, range, false, nonAlpha);
if (forwardTo == NSNotFound) forwardTo = range.location + range.length - 2;
if (backwardTo < forwardTo) return NSMakeRange(backwardTo, forwardTo - backwardTo);
return range;
}
void wrapper(NSTextView * textView, SEL _cmd, SEL selector) {
doCommandBySelector(textView, _cmd, selector);
}
static void doCommandBySelector( id self_, SEL _cmd, SEL selector )
{
do {
bool selectionModified = selector == @selector(moveToBeginningOfLineAndModifySelection:) ||
selector == @selector(moveToLeftEndOfLineAndModifySelection:) ||
selector == @selector(moveToBeginningOfParagraphAndModifySelection:);
if (selector == @selector(deleteToBeginningOfLine:) ||
selector == @selector(moveToBeginningOfLine:) ||
selector == @selector(moveToBeginningOfParagraph:) ||
selector == @selector(moveToLeftEndOfLine:) || selectionModified ||
selector == @selector(moveParagraphBackwardAndModifySelection:) ||
selector == @selector(moveParagraphForwardAndModifySelection:))
{
NSTextView *self = (NSTextView *)self_;
NSString *text = self.string;
NSRange selectedRange = self.selectedRange;
NSRange lineRange = [text lineRangeForRange:selectedRange];
if (lineRange.length == 0)
break;
NSString *line = [text substringWithRange:lineRange];
NSRange codeStartRange = [line rangeOfCharacterFromSet:[[NSCharacterSet whitespaceCharacterSet] invertedSet]];
if (codeStartRange.location == NSNotFound)
break;
NSUInteger caretLocation = selectedRange.location - lineRange.location;
/*if (caretLocation < codeStartRange.location && caretLocation != 0)
break;*/
NSUInteger start = lineRange.location;
if (selectedRange.location != (lineRange.location + codeStartRange.location))
start += codeStartRange.location;
NSUInteger end = selectionModified ? (selectedRange.location + selectedRange.length) : start;
if (end < start)
break;
NSRange range = NSMakeRange(start, end - start);
if (selector == @selector(deleteToBeginningOfLine:)) {
// handle deleteToBeginningOfLine: method
NSRange deleteRange;
if (caretLocation <= codeStartRange.location) {
// we are already at the beginnig of code, delete all the way to start of line
deleteRange = NSMakeRange(lineRange.location, caretLocation);
}
else {
// delete from caret to code start
deleteRange = NSMakeRange(lineRange.location+codeStartRange.location, caretLocation-codeStartRange.location);
}
[self setSelectedRange:deleteRange];
// We cannot undo if we use -replaceCharactersInRange:withString:,
// we need to use -insertText: to delete the text instead.
[self insertText:@""];
}
else {
// handle other methods
if (selector == @selector(moveParagraphBackwardAndModifySelection:)) {
range = extendRange(text, selectedRange);
}
if (selector == @selector(moveParagraphForwardAndModifySelection:)) {
range = shrinkRange(text, selectedRange);
}
[self setSelectedRange:range];
[self scrollRangeToVisible:range];
}
return;
}
} while (0);
return ((void (*)(id, SEL, SEL))original_doCommandBySelector)(self_, _cmd, selector);
}
+ (void) pluginDidLoad:(NSBundle *)plugin
{
Class class = nil;
Method originalMethod = nil;
if (!(class = NSClassFromString(@"DVTSourceTextView")))
goto failed;
if (!(originalMethod = class_getInstanceMethod(class, @selector(doCommandBySelector:))))
goto failed;
if (!(original_doCommandBySelector = method_setImplementation(originalMethod, (IMP)&doCommandBySelector)))
goto failed;
return;
failed:
return;
}
@end