Skip to content

Commit 99519da

Browse files
lufinkeylufinkey
lufinkey
authored and
lufinkey
committed
Syntax Highlighting!
Added syntax highlighting, which can be toggled from preferences. There is a small bug, where the last row of text disappears if it is slightly off of the screen. I will try to find a fix for this, but it's very unlikely (but I'm hopeful)
1 parent f269071 commit 99519da

32 files changed

+1305
-25
lines changed

Images/icons/icons.plist

+8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
<string>file_bundle.png</string>
1313
<key>c</key>
1414
<string>file_c.png</string>
15+
<key>cc</key>
16+
<string>file_cpp.png</string>
17+
<key>cp</key>
18+
<string>file_cpp.png</string>
1519
<key>cpp</key>
1620
<string>file_cpp.png</string>
21+
<key>cxx</key>
22+
<string>file_cpp.png</string>
23+
<key>c++</key>
24+
<string>file_cpp.png</string>
1725
<key>db</key>
1826
<string>file_sql.png</string>
1927
<key>deb</key>

Source/PreferencesView/GlobalPreferences.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ static String sdkFolder = "/var/stash/Developer/SDKs";
1616
static String bundleID = "com.BrokenPhysics.miniCode";
1717

1818
static String defaultSDK = "";
19-
static String codeEditorFont = "Helvetica";
19+
static String codeEditorFont = "Courier";
2020
static unsigned int codeEditorFontSize = 12;
21+
static bool syntaxHighlightingOn = true;
2122

2223
static double currentVersion = 1.030;
2324
static String versionMessage = "Welcome to miniCode! If you enjoy using this app, feel free to donate by "
@@ -64,6 +65,12 @@ bool GlobalPreferences_load()
6465
}
6566
}
6667

68+
void* syntaxHighlighting = NSDictionary_objectForKey(dict, "SyntaxHighlighting");
69+
if(syntaxHighlighting)
70+
{
71+
syntaxHighlightingOn = NSNumber_boolValue(syntaxHighlighting);
72+
}
73+
6774
void* installedAppsArray = NSDictionary_objectForKey(dict, "InstalledApps");
6875
if(installedAppsArray!=NULL)
6976
{
@@ -126,6 +133,9 @@ bool GlobalPreferences_save()
126133
void*editorFontSize = NSNumber_numberWithInt((int)codeEditorFontSize);
127134
NSMutableDictionary_setObjectForKey(dict, editorFontSize, "EditorFontSize");
128135

136+
void*syntaxHighlighting = NSNumber_numberWithBool(syntaxHighlightingOn);
137+
NSMutableDictionary_setObjectForKey(dict, syntaxHighlighting, "SyntaxHighlighting");
138+
129139
void*installedAppsArray = NSMutableArray_alloc_init();
130140
for(int i=0; i<installedApps.size(); i++)
131141
{
@@ -164,6 +174,11 @@ void GlobalPreferences_setCodeEditorFontSize(unsigned int size)
164174
}
165175
}
166176

177+
void GlobalPreferences_enableSyntaxHighlighting(bool toggle)
178+
{
179+
syntaxHighlightingOn = toggle;
180+
}
181+
167182
void GlobalPreferences_installedApps_add(const char* appName)
168183
{
169184
String appNameString = appName;
@@ -193,6 +208,11 @@ unsigned int GlobalPreferences_getCodeEditorFontSize()
193208
return codeEditorFontSize;
194209
}
195210

211+
bool GlobalPreferences_syntaxHighlightingEnabled()
212+
{
213+
return syntaxHighlightingOn;
214+
}
215+
196216
unsigned int GlobalPreferences_installedApps_size()
197217
{
198218
return (unsigned int)installedApps.size();

Source/PreferencesView/GlobalPreferences.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ bool GlobalPreferences_save();
1010
void GlobalPreferences_setDefaultSDK(const char*sdk);
1111
void GlobalPreferences_setCodeEditorFont(const char*font);
1212
void GlobalPreferences_setCodeEditorFontSize(unsigned int size);
13+
void GlobalPreferences_enableSyntaxHighlighting(bool toggle);
1314
void GlobalPreferences_installedApps_add(const char* appName);
1415

1516
const char* GlobalPreferences_getDefaultSDK();
1617
const char* GlobalPreferences_getCodeEditorFont();
1718
unsigned int GlobalPreferences_getCodeEditorFontSize();
19+
bool GlobalPreferences_syntaxHighlightingEnabled();
1820
unsigned int GlobalPreferences_installedApps_size();
1921
const char* GlobalPreferences_installedApps_get(unsigned int index);
2022
bool GlobalPreferences_installedApps_contains(const char* appName);

Source/PreferencesView/PreferencesViewController.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
#import <Foundation/Foundation.h>
44
#import "../UIFileBrowserViewController/UIFileBrowserViewController.h"
55
#import "../Navigation/NavigatedViewController.h"
6+
#import "../Util/UIDictionaryTableViewCell.h"
67
#import <MessageUI/MessageUI.h>
78

8-
@interface PreferencesViewController : NavigatedViewController <UITableViewDelegate, UITableViewDataSource, UIFileBrowserDelegate, MFMailComposeViewControllerDelegate>
9+
@interface PreferencesViewController : NavigatedViewController <UITableViewDelegate, UITableViewDataSource, UIDictionaryTableViewCellDelegate, UIFileBrowserDelegate, MFMailComposeViewControllerDelegate>
910
{
1011
UITableView*preferences;
1112
UIFileBrowserViewController* fileExplorer;

Source/PreferencesView/PreferencesViewController.mm

+25-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)
8888
{
8989
if(section==0)
9090
{
91-
return 3;
91+
return 4;
9292
}
9393
else if(section==1)
9494
{
@@ -115,6 +115,7 @@ - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSI
115115
if(indexPath.section==0)
116116
{
117117
NSString*cellID = nil;
118+
id obj = nil;
118119

119120
switch(indexPath.row)
120121
{
@@ -123,21 +124,34 @@ - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSI
123124

124125
case 0:
125126
cellID = @"Default SDK";
127+
obj = cellID;
126128
break;
127129

128130
case 1:
129131
cellID = @"Code Editor Font";
132+
obj = cellID;
130133
break;
131134

132135
case 2:
133136
cellID = @"Code Editor Font Size";
137+
obj = cellID;
138+
break;
139+
140+
case 3:
141+
cellID = @"Syntax Highlighting";
142+
obj = [NSNumber numberWithBool:GlobalPreferences_syntaxHighlightingEnabled()];
134143
break;
135144
}
136145

137-
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
146+
UIDictionaryTableViewCell* cell = (UIDictionaryTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
138147
if(cell==nil)
139148
{
140-
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID] autorelease];
149+
cell = [[[UIDictionaryTableViewCell alloc] initForObject:obj label:cellID reuseIdentifier:cellID] autorelease];
150+
cell.delegate = self;
151+
}
152+
else
153+
{
154+
[cell reloadForObject:obj label:cellID];
141155
}
142156

143157
NSMutableString* title = [[NSMutableString alloc] initWithString:cellID];
@@ -372,6 +386,14 @@ - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)
372386
[tableView deselectRowAtIndexPath:indexPath animated:YES];
373387
}
374388

389+
- (void)dictionaryTableViewCell:(UIDictionaryTableViewCell*)cell didToggleSwitch:(BOOL)toggle
390+
{
391+
if([cell.reuseIdentifier isEqual:@"Syntax Highlighting"])
392+
{
393+
GlobalPreferences_enableSyntaxHighlighting(toggle);
394+
}
395+
}
396+
375397
- (BOOL)fileBrowser:(UIFileBrowserViewController*)fileBrowser shouldOpenFolder:(NSString*)folder
376398
{
377399
return NO;

Source/ProjectView/CodeEditorViewController.h

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
@private
1919
UICodeEditorView* codeArea;
20-
BOOL returning;
2120
NSMutableArray* returnPoints;
2221
BOOL insertingText;
2322
BOOL codeEditing;

Source/ProjectView/CodeEditorViewController.mm

+16-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "ProjectTreeViewController.h"
1111
#import "../CompilerView/BuildOptionsActionSheet.h"
1212
#import "../UIFileBrowserViewController/NSFilePath.h"
13+
#import "../RegexHighlightView/SyntaxDefinitionManager.h"
1314

1415
@interface CodeEditorViewController()
1516
- (void)overwriteRange:(NSRange)range withText:(NSString*)text;
@@ -64,7 +65,6 @@ - (id)init
6465
fileEdited = NO;
6566
locked = NO;
6667
isOnScreen = NO;
67-
returning = NO;
6868
codeEditing = NO;
6969
currentFilePath = nil;
7070
insertingText = NO;
@@ -132,6 +132,11 @@ - (void)viewWillAppear:(BOOL)animated
132132
[codeArea setFont:[UIFont fontWithName:@"Helvetica" size:GlobalPreferences_getCodeEditorFontSize()]];
133133
}
134134
[fontName release];
135+
136+
BOOL highlighting = GlobalPreferences_syntaxHighlightingEnabled();
137+
[codeArea setHighlightingEnabled:highlighting];
138+
139+
[codeArea setNeedsDisplay];
135140
}
136141

137142
- (void)viewDidAppear:(BOOL)animated
@@ -296,6 +301,10 @@ - (BOOL)loadWithFile:(NSString*)filePath
296301
codeEditing = NO;
297302
}
298303
locked = NO;
304+
305+
NSDictionary* syntaxDefinitions = [SyntaxDefinitionManager loadSyntaxDefinitionsForFile:filePath];
306+
[codeArea setHighlightDefinition:syntaxDefinitions];
307+
299308
return YES;
300309
}
301310
else
@@ -587,7 +596,12 @@ - (void)textViewDidChange:(UITextView*)textView
587596
[tabText release];
588597
}
589598
}
590-
returning = NO;
599+
[textView setNeedsDisplay];
600+
}
601+
602+
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
603+
{
604+
[scrollView setNeedsDisplay];
591605
}
592606

593607
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text

Source/ProjectView/ProjectTreeViewController.h

+5-11
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@
1111
#import "PathListTableViewController.h"
1212
#import "ProjectSettingsViewController.h"
1313

14-
static NSArray* EXTENSIONS_CODEEDITOR = [[NSArray alloc]
15-
initWithObjects:@"c", @"cc", @"cpp", @"h", @"m", @"mm",
16-
@"txt", @"strings", nil];
17-
static NSArray* EXTENSIONS_TEXTEDITOR = [[NSArray alloc]
18-
initWithObjects:@"txt", @"list", @"strings", nil];
19-
static NSArray* EXTENSIONS_IMAGEVIEWER = [[NSArray alloc]
20-
initWithObjects:@"jpg", @"jpeg", @"png", @"gif", @"bmp", @"tif", nil];
21-
static NSArray* EXTENSIONS_AUDIOPLAYER = [[NSArray alloc]
22-
initWithObjects:@"mp3", @"wav", @"aac", @"m4a", nil];
23-
static NSArray* EXTENSIONS_PLISTEDITOR = [[NSArray alloc]
24-
initWithObjects:@"plist", nil];
14+
#define EXTENSIONS_CODEEDITOR [[[NSArray alloc] initWithObjects:@"c", @"cc", @"cp", @"cpp", @"cxx", @"c++", @"h", @"m", @"mm", @"txt", @"strings", nil] autorelease]
15+
#define EXTENSIONS_TEXTEDITOR [[[NSArray alloc] initWithObjects:@"txt", @"list", @"strings", nil] autorelease]
16+
#define EXTENSIONS_IMAGEVIEWER [[[NSArray alloc] initWithObjects:@"jpg", @"jpeg", @"png", @"gif", @"bmp", @"tif", nil] autorelease]
17+
#define EXTENSIONS_AUDIOPLAYER [[[NSArray alloc] initWithObjects:@"mp3", @"wav", @"aac", @"m4a", nil] autorelease]
18+
#define EXTENSIONS_PLISTEDITOR [[[NSArray alloc] initWithObjects:@"plist", nil] autorelease]
2519

2620
@class CellHoldAction;
2721

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// RegexHighlightView.h
3+
// iOS Syntax Highlighter
4+
//
5+
// Created by Kristian Kraljic on 30/8/12.
6+
// Copyright (c) 2012 Kristian Kraljic (dikrypt.com, ksquared.de). All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person
9+
// obtaining a copy of this software and associated documentation
10+
// files (the "Software"), to deal in the Software without
11+
// restriction, including without limitation the rights to use,
12+
// copy, modify, merge, publish, distribute, sublicense, and/or
13+
// sell copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following
15+
// conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be
18+
// included in all copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
// OTHER DEALINGS IN THE SOFTWARE.
28+
//
29+
30+
#import <UIKit/UIKit.h>
31+
#import <CoreText/CoreText.h>
32+
33+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeText;
34+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeBackground;
35+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeComment;
36+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeDocumentationComment;
37+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeString;
38+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeCharacter;
39+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeNumber;
40+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeKeyword;
41+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypePreprocessor;
42+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeURL;
43+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeAttribute;
44+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeProject;
45+
FOUNDATION_EXPORT NSString *const kRegexHighlightViewTypeOther;
46+
47+
typedef enum
48+
{
49+
kRegexHighlightViewThemeNone,
50+
kRegexHighlightViewThemeBasic,
51+
kRegexHighlightViewThemeDefault,
52+
kRegexHighlightViewThemeDusk,
53+
kRegexHighlightViewThemeLowKey,
54+
kRegexHighlightViewThemeMidnight,
55+
kRegexHighlightViewThemePresentation,
56+
kRegexHighlightViewThemePrinting,
57+
kRegexHighlightViewThemeSunset
58+
} RegexHighlightViewTheme;
59+
60+
@interface RegexHighlightView : UITextView
61+
{
62+
BOOL highlightingEnabled;
63+
64+
NSDictionary* highlightColor;
65+
NSDictionary* highlightDefinition;
66+
67+
@private
68+
id internalDelegate;
69+
}
70+
71+
@property (nonatomic) BOOL highlightingEnabled;
72+
@property (nonatomic, retain) NSDictionary* highlightColor;
73+
@property (nonatomic, retain) NSDictionary* highlightDefinition;
74+
75+
-(void)setHighlightDefinition:(NSDictionary*)highlightDefinition;
76+
-(void)setHighlightDefinitionWithContentsOfFile:(NSString*)path;
77+
78+
-(void)setHighlightTheme:(RegexHighlightViewTheme)theme;
79+
+(NSDictionary*)highlightTheme:(RegexHighlightViewTheme)theme;
80+
81+
@end

0 commit comments

Comments
 (0)