Snippet Language Bundle Development
What will you need? For creating a Snippet language bundle, all you need is to have the Developer tools installed (Xcode) and some Objective-C (thus C) knowledge.
Download CMHighlighter.h from Our GitHub Project
Getting Started
-
To create a new bundle, open Xcode, in the templates, choose Bundle and then Cocoa Bundle.
-
In the Info.plist file (under Resources folder), add a new line with a key NSPrincipalClass and value should be the name of a class that will highlight the text. The name should be unique, so restrain from creating classes called 'Highlighter', something like 'HighlighterForCPlusPlusByJoe' is more suitable (remember there can be more than one plugin for the same language).
-
Add a new line with a CMLanguageName key and the language name for the value, and CMHighlighterCreator with your name as the value.
-
Don't forget to change the CFBundleVersion and CFBundleShortVersionString line whenever you're submitting a new version of your plugin.
-
Once you're done with this, include the CMHighlighter.h header file that you can download from our website and create a subclass of NSObject and override a method called +(void)highlight:(NSDictionary*)aDict; (note it's a class method, not an instance method!)
In the aDict, there are a few objects:
Key Value
CMHighlightedTextViewKey the actual NSTextView object
CMHighlightedRangeKey a NSValue object with the range, in which you
should highlight the text
Now it's easy. Go through the text and using the NSTextView's layout manager, you're able to do something like:
[[textView layoutManager]
addTemporaryAttributes:[CMHighlighter stringAttributes]
forCharacterRange: NSMakeRange(start, i - start)]; CMHighlighter
class
The class has several methods that you might need:
+(NSDictionary*)stringAttributes; //Used for "strings"
+(NSDictionary*)cStringAttributes; //Used for 'strings'
+(NSDictionary*)keywordAttributes; //Used for keywords
+(NSDictionary*)commentAttributes; //Used for comments
+(NSDictionary*)macroAttributes; //Used for different things like
macros in C +(NSDictionary*)foreignLanguageAttributes; //Used for
foreign languages like PHP in HTML
+(Class)classForLanguage:(NSString*)languageName;
Gives you access to a different class that will highlight specified language. Note that the languageName is case sensitive and you can have nil returned if the language isn't installed.
Example:
[[CMHighlighter classForLanguage:@"PHP"]
highlight:dict];
Assuming you're highlighting syntax for HTML and you hit beginning of PHP and want Snippet to highlight the range of PHP (the range must be defined in dict).