-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathULISyntaxColoredTextView.m
120 lines (103 loc) · 2.73 KB
/
ULISyntaxColoredTextView.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
//
// ULISyntaxColoredTextView.m
// UKSyntaxColoredDocument
//
// Created by Uli Kusterer on 17/06/16.
//
//
#import "ULISyntaxColoredTextView.h"
#import "UKSyntaxColoredTextViewController.h"
@implementation ULISyntaxColoredTextView
-(void) keyDown:(NSEvent *)event
{
NSString * pressedChars = [event charactersIgnoringModifiers];
if( pressedChars.length == 0 )
{
[super keyDown: event];
return;
}
unichar theCh = [pressedChars characterAtIndex: 0];
if( theCh == 0x03 ) // Enter key.
{
id<ULISyntaxColoredTextViewDelegate> theDelegate = (id<ULISyntaxColoredTextViewDelegate>)self.delegate;
if( [theDelegate respondsToSelector: @selector(syntaxColoredTextViewHandleEnterKey:)] )
{
if( [theDelegate respondsToSelector: @selector(syntaxColoredTextViewShouldHandleEnterKey:)]
&& [theDelegate syntaxColoredTextViewShouldHandleEnterKey: self] )
{
[theDelegate syntaxColoredTextViewHandleEnterKey: self];
return;
}
}
}
[super keyDown: event];
}
-(void) insertNewline:(id)sender
{
if( ![(UKSyntaxColoredTextViewController*)self.delegate maintainIndentation] )
{
[super insertNewline: sender];
return;
}
NSRange affectedCharRange = self.selectedRange;
NSMutableAttributedString* textStore = [self textStorage];
BOOL hadSpaces = NO;
NSUInteger lastSpace = affectedCharRange.location,
prevLineBreak = 0;
NSRange spacesRange = { 0, 0 };
unichar theChar = 0;
NSUInteger x = (affectedCharRange.location == 0) ? 0 : affectedCharRange.location -1;
NSString* tsString = [textStore string];
if( tsString.length == 0 )
{
[super insertNewline: sender];
return;
}
while( YES )
{
if( x > ([tsString length] -1) )
break;
theChar = [tsString characterAtIndex: x];
switch( theChar )
{
case '\n':
case '\r':
prevLineBreak = x +1;
x = 0; // Terminate the loop.
break;
case ' ':
case '\t':
if( !hadSpaces )
{
lastSpace = x;
hadSpaces = YES;
}
break;
default:
hadSpaces = NO;
break;
}
if( x == 0 )
break;
x--;
}
[self.undoManager beginUndoGrouping];
NSRange newlineAndSpacesRange = self.selectedRange;
newlineAndSpacesRange.length = 1;
NSRange replacementRange = self.selectedRange;
[self insertText: @"\n" replacementRange: replacementRange];
replacementRange.location += newlineAndSpacesRange.length;
replacementRange.length = 0;
if( hadSpaces )
{
spacesRange.location = prevLineBreak;
spacesRange.length = lastSpace -prevLineBreak +1;
if( spacesRange.length > 0 )
{
newlineAndSpacesRange.length += spacesRange.length;
[self insertText: [tsString substringWithRange:spacesRange] replacementRange: replacementRange];
}
}
[self.undoManager endUndoGrouping];
}
@end