Sublime Text - C++ Highlight - syntax-highlighting

How can i make my class be highlighted as a normal primitive type like the int or double?
See a exemple:
I want that when i declare the Test aloha; Test be highlighted of the same way that int a.

The syntax highlighting is controlled by the tmLanguage file. In ST3 these are held in .cache files.
So follow these steps.
Open Tools -> Command Pallet and using Package Manager install PackageResourceViewer
In the Command Pallet select PackageResourceViewer: Open Resource
In the presented panel choose C++
In the presented panel choose C++.tmLanguage
Now you have the language definition file displayed for you.
Have a look at the form of how the parts of a language are defined.
If we search for class we find the following as one of the matches:
<dict>
<key>match</key>
<string>\b(class|struct)\s+([_A-Za-z][_A-Za-z0-9]*\b);</string>
<key>name</key>
<string>meta.class-struct-block.c++</string>
<key>captures</key>
<dict>
. . .
. . .
</dict>
I chose that one because it involves some regular expresssion (regex) matching and that's the hint to do what you want to do.
Because ST has a lovely open framework any of us who know a little programming can add our own language component definitions to these .tmLanguage files. This flexibility is part of why ST is my choice.
So let's see how to do that.
First thing is to save the C++.tmLanguage file we have open in the editor into our User folder.
We do this both to keep the original as a rollback if things go wrong and because it means that our version in User will not get overwritten each time ST updates. The way ST loads files means that duplicates in the User folder overwrite values in the default location so our User versions take precedence in what is running when we use ST. Cool hey?
So now we have our own version of the C++.tmLanguage file let's play with it.
Find a definition that looks a bit like what we want to do and duplicate it.
I am going to copy this:
<dict>
<key>match</key>
<string>\b(class|wchar_t|nullptr_t)\b</string>
<key>name</key>
<string>storage.type.c++</string>
</dict>
First thing I am going to do is decide what I am going to name my new language component.
I could simply give it the name of an existing language component that has the colouring I want to have. This has the advantage that if that name is already in place in the existing colour schemes I will get the highlighting for free. Feel free to make this choice.
But to explore more of the system I am going to invent my own name and scope it to myself. So I will call it duncan.name.class. You can use any name you like that does not clash with an existing name.
So I will make that change in the duplicate:
<dict>
<key>match</key>
<string>\b(class|wchar_t|nullptr_t)\b</string>
<key>name</key>
<string>duncan.name.class</string>
</dict>
Now I need to write the regex to identify my new language component. This is pretty easy since I am going to assume we are all following the fairly standard practice of naming our classes with leading Caps and not using that for other language components so my regex just needs to find any string that starts with an UPPERCASE alpha, which is easily defined as [A-Z] followed by any alphanumeric character/s. Note that this precludes punctuation (like underscore _) so if you use those in your class names you'll need to expand the regex to include those characters.
So let's change the match condition to use that regex:
<dict>
<key>match</key>
<string>\b([A-Z][a-zA-Z0-9]+)\b</string>
<key>name</key>
<string>duncan.name.class</string>
</dict>
Now I have a new language component I need to tell my theme how to handle it. I am going to work on the Amy theme for this example.
So back to the ever helpful PackageResourceViewer like this:
In the Command Pallet select PackageResourceViewer: Open Resource
In the presented panel choose Color Scheme - Default
In the presented panel choose Amy.tmTheme
Now you have the theme file displayed for you.
Again I want to save my copy into the User folder and then duplicate an existing example. I am going to duplicate String but anything that grabs your fancy is fine.
<dict>
<key>name</key>
<string>String</string>
<key>scope</key>
<string>string</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#999999</string>
</dict>
</dict>
Now I'll change the duplicate of String to do colouring for my new language component.
<dict>
<key>name</key>
<string>Class Names :: Duncan</string>
<key>scope</key>
<string>duncan.name.class</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#999999</string>
<key>background</key>
<string>#FFFFFF</string>
<key>fontStyle</key>
<string>bold</string>
</dict>
</dict>
I hope you recognise the names I am using from the work we did above?
I have chosen a slightly mad set of syntax highlighting preferences just to show some of the options you have available. Knock yourself out playing with ideas here.
Does all this make sense?
With these ideas in place you can fix any syntax highlighting niggles you have in ST3 to work just exactly as you like. Perhaps my regex above is not too great? I didn't think about it a whole lot so you may find you need to do more work there to get just what you want.
Let me know if some of this doesn't make sense and I'll try and do better.

I would also suggest installing the package Scope Hunter, and then enable Toggle Instant Scoper via Ctrl+Shift+P. This'll show you the scope of the element under the cursor.
With this you can see how ST has parsed the file, and use the name of each scope in a theme definition to color elements of different scope differently.

Related

Is color scheme file cached?

I'm using IDLE color scheme for Sublime 3, but it did not have diff syntax highglight. SO I found one that I could use it. I added it using PackageResourceViewer. After adding additional content into scheme, I noticed diff syntax working. But I did not like that insertion was colored blue and diff header was green.
So I swapped colors between diff header and insertion. But it had no effect. Header was still green and insertion was still blue. It looks like file is cached somewhere, because I could even delete diff highlight declaration and it would still show me highlights.
My current scheme (with swapped colors) is this:
<dict>
<key>name</key>
<string>diff.header</string>
<key>scope</key>
<string>meta.diff, meta.diff.header</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#3333FF</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>diff.deleted</string>
<key>scope</key>
<string>markup.deleted</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#DD5555</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>diff.inserted</string>
<key>scope</key>
<string>markup.inserted</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#009933</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>diff.changed</string>
<key>scope</key>
<string>markup.changed</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#E6DB74</string>
</dict>
</dict>
Note. Even if I close sublime, open with PRV, I can see that in file my changes persist, but actual syntax is still showed from initial copy/paste.
Some packages quite often decide to make tweaks to the color scheme, and thus create a new color scheme so as to not mess with the original - then they set your active color scheme to their tweaked copy. Therefore, although ST doesn't cache the color scheme - it will look as though it does.
To check, you can see the output from view.settings().get('color_scheme') Enter in the ST console (View menu -> Show Console) - most often the packages that do this name their copy of the color scheme as the original with (package name) appended to the end.
Or check the main User preferences file. Just set it back to your color scheme, and the package will probably do the same thing again but at least it should include your changes this time.

Styling escape character in Sublime Text

I want to style escape character and only escape character differently using custom color scheme (I intend to make it faded to make regexp more readable).
Is it possible to do this? Preferably it would work in general, but something limited to Ruby also would be highly useful.
It sure is possible!
<dict>
<key>name</key>
<string>Escape character</string>
<key>scope</key>
<string>constant.character.escape</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#F38630</string>
</dict>
</dict>
Actually this is the scope for the escape character along with the escaped character.
Remember you may always find the corresponding scope by moving the caret to the characters and pressing:
{ "keys": ["ctrl+alt+shift+p"], "command": "show_scope_name" }
(You will see the scope key in the status bar).

Sublime Text: Change highlight background

I would like to change the color on all the found instances (when you use find) of the word that have been found. I noticed that the highlightBackground key just changes the first instance. Is there a way to change the highlight color of all the found instances?
The following examples are from the Neon Color Scheme (full disclosure - I'm its designer):
So I'm searching though my Package Control.sublime-settings file trying to find all of my packages with HTML (case-insensitive) in their name. I open up the Find dialog (CtrlF on Windows/Linux, ⌘F on OS X) and type HTML into the search box, after unchecking the regex, case-sensitive, and whole-word options. The following appears:
The yellow background behind the first found item on Line 22 is themed using the findHighlight setting, while the blue font (foreground) is themed using findHighlightForeground. I can hit F3 (Win/Lin, ⌘G on OS X) to scroll through each match.
However, say I want a multi-selection so I can change them all at once. I hit Find All (AltEnter, ⌥Enter on OS X) and the window now looks like so:
This is the same coloring used when you manually select text. The bright blue background is themed using the selection setting, while the bright green border around it is themed using selectionBorder. The text color (foreground) is the same as defined elsewhere in the color scheme for that particular scope. Defining selectionForeground doesn't seem to have any effect.
The full settings dict for Neon looks like this:
<dict>
<key>settings</key>
<dict>
<key>activeGuide</key>
<string>#FF0080</string>
<key>background</key>
<string>#000000</string>
<key>caret</key>
<string>#FFFFFF</string>
<key>findHighlight</key>
<string>#F2FF06</string>
<key>findHighlightForeground</key>
<string>#1515FF</string>
<key>foreground</key>
<string>#FFFFFF</string>
<key>guide</key>
<string>#6F6F6F</string>
<key>inactiveSelection</key>
<string>#353576</string>
<!-- invisibles doesn't seem to work for me -->
<key>invisibles</key>
<string>#06FF05</string>
<key>lineHighlight</key>
<string>#2D2D2D</string>
<key>selection</key>
<string>#0205FF</string>
<key>selectionBorder</key>
<string>#06FF05</string>
<key>stackGuide</key>
<string>#06FF05</string>
</dict>
</dict>
I hope this helps!

Sublime Text 2: how to change white space characters color?

The color seems to be linked to the foreground in themes. I assume it's using less alpha. Is there a way to control this?
There's now a Sublime plugin for this.
Install the HighlightWhitespaces plugin
Add the following color settings (tailored to your preference) to your color_scheme file. This file ends in .tmTheme and the path can be found by looking at the value for the key "color_scheme" in your settings file.
<dict>
<key>name</key>
<string>highlight.whitespace</string>
<key>scope</key>
<string>highlight.whitespace</string>
<key>settings</key>
<dict>
<key>background</key>
<string>#020202</string>
<key>foreground</key>
<string>#805050</string>
</dict>
</dict>
Specify that this color should be used by adding the following to the user settings of the HighlightWhitespaces plugin:
{
"highlight_whitespaces_space_highlight_scope_name": "highlight.whitespace",
"highlight_whitespaces_tab_highlight_scope_name": "highlight.whitespace",
"highlight_whitespaces_eol_highlight_scope_name": "highlight.whitespace",
"highlight_whitespaces_mixed_highlight_scope_name": "highlight.whitespace"
}
Relax and enjoy :-)
You may want to try installing PersistentRegexHighlight from Package Control and then use a blank-character regex like [\x20 ] to add a specific color or color scope.
You'd want to combine this with drawWhiteSpace: "all", in Sublime user prefs.
You can change the tab underline alpha by changing the foreground alpha.
To change the color of spaces requires changes to every syntax file.
A few years later, I was struggling with this in Sublime Text 3 build 3083. I hope this helps anyone. In addition to Chris Like's suggestion to install PersistentRegexHighlight and setting "draw_white_space": "all" in the user preferences and the pattern to one or more occurances of tabs and spaces, i.e. [ \t]+ in the PersistentRegexHighlight user settings, i had to also also set "color_scope": "highlight.whitespace" and add the following dict to the theme along with the other ones in the settings array:
<dict>
<key>name</key>
<string>highlight.whitespace</string>
<key>scope</key>
<string>highlight.whitespace</string>
<key>settings</key>
<dict>
<key>background</key>
<string>#020202</string>
<key>foreground</key>
<string>#805050</string>
</dict>
</dict>
which by the way oddly only outlines in red if the background is set to black, i.e #000000
Note that this method does not require editing any syntax files.

How do I create custom text macros in Xcode 4?

I updated to Xcode 4 today and my custom text macros are no longer working. I cannot find any information on how to use custom macros with Xcode 4. Is this still possible? Please let me know if anyone has come up with a solution.
This is the directory where I currently have my 'ObjectiveC.xctxtmacro' file.
~/Library/Application Support/Developer/Shared/Xcode/Specifications
The format of how Text Macros are defined, and where they live has changed - Text Macros have morphed into Code Snippets.
What you can do is make a new code snippet (with anything, just drag text into the Code Snippet window), then go looking for the current format of a Snippet (Macro), and migrate your current macros into there.
The directory where they go is:
~/Library/Developer/XCode/UserData/CodeSnippets
In there you'll see files with names like:
E4B300B5-E0EA-4E46-9963-6E9B2111E578.codesnippet
The great thing is, you don't have to use UUID names - I was able to copy one of those into a file called "MyTest.codesnippet" and XCode still reads it.
So you'd have one file per existing macro (as there are usually a number of macros in the older .xctxtmacro files), you can use the actual macro text as-is as the parameter syntax has not changed (although all of the meta-data around a macro has changed substantially). You will have to convert the "<" / ">" parts of any parameters defined to XML-safe syntax < / > as the files are XML plists now. As an example, the contents of a simple macro that produces NSLog(#"Hello Nurse: %#",Thing); when "nurse" is typed:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>nurse</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>All</string>
</array>
<key>IDECodeSnippetContents</key>
<string>NSLog(#"Hello Nurse %#", <#Thing#>);
</string>
<key>IDECodeSnippetIdentifier</key>
<string>E4B300B5-E0EA-4E46-9963-6E9B2111E579</string>
<key>IDECodeSnippetLanguage</key>
<string>Xcode.SourceCodeLanguage.Objective-C</string>
<key>IDECodeSnippetTitle</key>
<string>TestNurse</string>
<key>IDECodeSnippetUserSnippet</key>
<true/>
<key>IDECodeSnippetVersion</key>
<integer>2</integer>
</dict>
</plist>
Note that one aspect of Code Snippets that is missing from Text Macros, is that you used to be able to define parameters where selected text would go when you activated a Text Macro (by adding ! after the # as in <#!ReplaceParam!#> ) - in the Code Snippet system, there does not appear to be a way to apply a Code Snippet to selected text, you can only drag it out as new. The parameters still work as normal parameters though.

Resources