forked from anthonyc-r/Gem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GemasEditorView.m
395 lines (331 loc) · 10.8 KB
/
GemasEditorView.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
/*
Copyright (C) 2010, 2011, 2012, 2013, 2014 German A. Arias <[email protected]>
Copyright (C) 2020 Anthony Cohn-Richardby <[email protected]>
This file is part of Gemas application
Gemas is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#import "GemasEditorView.h"
#import <Foundation/Foundation.h>
#import "GemasDocument.h"
#import <HighlighterKit/HighlighterKit.h>
#import "Preferences.h"
#import "InputModifiers/ObjcAutoIndenter.h"
#import "InputModifiers/InputModifiable.h"
@implementation GemasEditorView (Private)
- (void) updateVisibleHighlighting
{
NSLayoutManager *layoutManager;
NSTextContainer *textContainer;
NSRange drawnRange;
NSScrollView *scrollView = (NSScrollView*)[self superview];
NSRect visibleRect = [scrollView documentVisibleRect];
layoutManager = [self layoutManager];
textContainer = [self textContainer];
drawnRange = [layoutManager glyphRangeForBoundingRect: visibleRect
inTextContainer: textContainer];
[highlighter highlightRange: drawnRange];
}
@end
@implementation GemasEditorView
- (void) dealloc
{
TEST_RELEASE(highlighter);
TEST_RELEASE(openCharacters);
TEST_RELEASE(closeCharacters);
TEST_RELEASE(type);
RELEASE(autoIndenter);
RELEASE(openBrackets);
RELEASE(closeBrackets);
[super dealloc];
}
- (void) awakeFromNib
{
openCharacters = [NSCharacterSet characterSetWithCharactersInString: @"[{("];
RETAIN(openCharacters);
closeCharacters = [NSCharacterSet characterSetWithCharactersInString: @"]})"];
RETAIN(closeCharacters);
closeBrackets = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"[", @"]",
@"{", @"}",
@"(", @")",
NULL];
openBrackets = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"]", @"[",
@"}", @"{",
@")", @"(",
NULL];
autoIndenter = nil;
if ([Preferences autoIndentEnabled])
{
autoIndenter = [[ObjcAutoIndenter alloc] initWithFiletype: type];
}
}
- (void) highlightRange: (NSRange)range
{
[highlighter highlightRange: range];
}
// Find ":" for next parameter
- (void) nextParameter: (id)sender
{
NSRange range;
NSUInteger loc = [self selectedRange].location;
range = [[[self string] substringToIndex: loc] rangeOfString: @" "
options: NSBackwardsSearch];
if (range.location != NSNotFound)
{
NSString *line = nil;
line = [[[self string] substringToIndex: loc]
substringFromIndex: range.location];
range = [line rangeOfString: @":"];
if (range.location != NSNotFound &&
range.location < ([line length] - 1))
{
[self setSelectedRange: NSMakeRange(loc -
([line length] - range.location) + 1, 0)];
}
else
{
range = [[[self string] substringFromIndex: loc] rangeOfString: @":"];
if (range.location != NSNotFound)
{
[self setSelectedRange: NSMakeRange(range.location + loc + 1, 0)];
}
}
}
else
{
range = [[[self string] substringFromIndex: loc] rangeOfString: @":"];
if (range.location != NSNotFound)
{
[self setSelectedRange: NSMakeRange(range.location + loc + 1, 0)];
}
}
}
- (void) setAutoIndenter: (id)anAutoIndenter
{
ASSIGN(autoIndenter, anAutoIndenter);
}
//Insert the correponding indentation when the user press Tab key
- (void) insertTab: (id)sender
{
if ([autoIndenter modifyTab: self])
{
return;
}
[super insertText: [Preferences indentation]];
}
//If the new line need indentation, add it
- (void) insertNewline: (id)sender
{
if ([autoIndenter modifyNewline: self])
{
return;
}
[super insertNewline: sender];
}
- (void) insertText: (id)string
{
if ([autoIndenter modifyInput: string forModifiable: self])
{
return;
}
[super insertText: string];
}
- (void) didChangeText
{
[self updateVisibleHighlighting];
[super didChangeText];
}
// Delete from the beginning of line to current position
- (void) deleteToBeginningOfLine: (id)sender
{
NSUInteger start;
NSUInteger loc = [self selectedRange].location;
start = [[[self string] substringToIndex: loc]
rangeOfCharacterFromSet: [NSCharacterSet newlineCharacterSet]
options: NSBackwardsSearch].location;
if (start == NSNotFound)
{
start = -1;
}
if (loc > start + 1)
{
[self setSelectedRange: NSMakeRange(start + 1, loc - start - 1)];
[self delete: self];
}
}
// Delete from current position to end of line
- (void) deleteToEndOfLine: (id)sender
{
NSUInteger end;
NSUInteger loc = [self selectedRange].location;
end = [[[self string] substringFromIndex: loc]
rangeOfCharacterFromSet: [NSCharacterSet newlineCharacterSet]].location;
if (end == NSNotFound)
{
end = [[self string] length] - 1;
}
if (end > 1)
{
[self setSelectedRange: NSMakeRange(loc, end)];
[self delete: self];
}
}
/*Select the range of parentheses when the user do a double
click obove one*/
- (void) mouseDown: (NSEvent *)event
{
NSRange textRange;
NSString *selection;
[super mouseDown: event];
textRange = [self selectedRange];
selection = [[self string] substringFromRange: textRange];
if (textRange.length != 1)
{
return;
}
NSString *opositeMatch, *context;
NSRange matchRange;
if ((opositeMatch = [openBrackets objectForKey: selection]))
{
context = [[self string] substringFromIndex: textRange.location];
matchRange = [context rangeOfString: opositeMatch options: 0];
if (matchRange.location != NSNotFound)
{
[self setSelectedRange: NSMakeRange(textRange.location, matchRange.location + 1)];
}
}
else if ((opositeMatch = [closeBrackets objectForKey: selection]))
{
context = [[self string] substringToIndex: textRange.location];
matchRange = [context rangeOfString: opositeMatch options: NSBackwardsSearch];
if (matchRange.location != NSNotFound)
{
[self setSelectedRange: NSMakeRange(matchRange.location, (textRange.location -
matchRange.location) + 1)];
}
}
}
- (NSRange) rangeForUserCompletion
{
NSUInteger count = 0;
NSRange range = [super rangeForUserCompletion];
NSString *word = [[self string] substringWithRange: range];
while ([word hasPrefix: @"["] || [word hasPrefix: @"("])
{
word = [word substringFromIndex: 1];
count += 1;
}
return NSMakeRange(range.location + count, range.length - count);
}
//Creates a new syntax highlighter for the specified file type
- (void) createSyntaxHighlighterForFileType: (NSString *) fileType
{
NSString *name = [[editorDocument fileName] lastPathComponent];
type = [NSString stringWithString: fileType];
if ([type isEqualToString: @"GNUmakefile"] && ([name length] > 0))
{
if (![name hasPrefix: @"GNUmakefile"] &&
![name isEqualToString: @"ChangeLog"] &&
![name isEqualToString: @"Changelog"])
{
type = @"Generic";
}
else if ([name isEqualToString: @"ChangeLog"] ||
[name isEqualToString: @"Changelog"])
{
type = @"ChangeLog";
}
}
ASSIGN (highlighter, [[[HKSyntaxHighlighter alloc]
initWithHighlighterType: type
textStorage: [self textStorage]
defaultTextColor: [editorDocument textColor]]
autorelease]);
[type retain];
if ([type isEqualToString: @"C"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.c"]];
}
if ([type isEqualToString: @"C-Header"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.h"]];
}
if ([type isEqualToString: @"C++"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.cc"]];
}
if ([type isEqualToString: @"QuakeC"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.qc"]];
}
if ([type isEqualToString: @"Material"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.mat"]];
}
if ([type isEqualToString: @"Q3AShader"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.shader"]];
}
if ([type isEqualToString: @"GLSL"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.glsl"]];
}
if ([type isEqualToString: @"ObjC"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.m"]];
}
if ([type isEqualToString: @"ObjC++"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.mm"]];
}
if ([type isEqualToString: @"Plist"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.plist"]];
}
if ([type isEqualToString: @"Strings"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.strings"]];
}
if ([type isEqualToString: @"GSmarkup"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_.gsmarkup"]];
}
if ([type isEqualToString: @"GNUmakefile"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"FileIcon_makefile"]];
}
if ([type isEqualToString: @"Generic"] || [type isEqualToString: @"ChangeLog"])
{
[[self window] setMiniwindowImage: [NSImage imageNamed: @"common_Unknown"]];
}
}
- (void) modifyInputByReplacingRange: (NSRange)aRange withString: (NSString*)aString {
[self replaceCharactersInRange: aRange withString: aString];
}
- (void) modifyInputByInserting: (NSString*)aString {
[super insertText: aString];
}
- (void) modifyInputByInsertingTab {
[super insertText: [Preferences indentation]];
}
- (void) modifyInputByInsertingNewline {
[super insertNewline: self];
}
- (NSString*) inputModifiableString {
return [self string];
}
- (int) inputModifiableCursor {
return [self selectedRange].location;
}
@end