Home Download Buy Blog Forum Support

C++ syntax highlighting w/ namespace definition

C++ syntax highlighting w/ namespace definition

Postby amphetamine on Fri Dec 14, 2012 10:05 pm

Hi,

Does anyone have a modified C++ language file that treats the namespace keyword correctly (as a keyword.control.c++, I think) in the context of definition scope?

For example:
Code: Select all
namespace lol {
//...
}


Failing that and since I'm sure the changes are minor, could someone please help me out with it?

Thanks a lot :)
amphetamine
 
Posts: 23
Joined: Fri Dec 14, 2012 8:59 pm

Re: C++ syntax highlighting w/ namespace definition

Postby agibsonsw on Fri Dec 14, 2012 10:44 pm

These already have different scopes:

Code: Select all
meta.namespace-block.c++ // and
meta.namespace-block.c++ comment.line.double-slash.c++ // etc.

which you can check by clicking within the text and pressing Ctrl-Alt-Shift-P and reading the status-bar. So you just need to slip in some colours for these into your chosen theme. The package ScopeHunter is very useful for reading and copying scopes.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: C++ syntax highlighting w/ namespace definition

Postby amphetamine on Tue Dec 18, 2012 12:48 am

agibsonsw wrote:These already have different scopes:

Code: Select all
meta.namespace-block.c++ // and
meta.namespace-block.c++ comment.line.double-slash.c++ // etc.

which you can check by clicking within the text and pressing Ctrl-Alt-Shift-P and reading the status-bar. So you just need to slip in some colours for these into your chosen theme. The package ScopeHunter is very useful for reading and copying scopes.

Thanks for the tip about Scopehunter. That sounds good!

I tried to use "meta.namespace-block.c++ keyword.control.c++" to style the actual namespace keyword in that scenario but it didn't work and "meta.namespace-block.c++" colours the whole thing obviously. :(

Actually, thinking about it, if that had worked then I wouldn't have needed it because my "keyword.control.c++" style would've done it anyway! Duh!
amphetamine
 
Posts: 23
Joined: Fri Dec 14, 2012 8:59 pm

Re: C++ syntax highlighting w/ namespace definition

Postby agibsonsw on Tue Dec 18, 2012 6:14 pm

Try dropping the pluses, they are probably problematic.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: C++ syntax highlighting w/ namespace definition

Postby ljbo3 on Thu Jan 03, 2013 12:24 pm

amphetamine wrote:Actually, thinking about it, if that had worked then I wouldn't have needed it because my "keyword.control.c++" style would've done it anyway! Duh!

Indeed. The problem I think is that the pattern does not capture the keyword "namespace". Here is a patch that works for me against the C++.tmLanguage installed by version 2.0.1
Code: Select all
--- C++.tmLanguage-original   2013-01-03 13:03:50.000000000 +0100
+++ C++.tmLanguage   2013-01-03 13:03:58.000000000 +0100
@@ -337,6 +337,14 @@
            <dict>
               <key>begin</key>
               <string>\b(namespace)\s+([A-Za-z_][_A-Za-z0-9:]*\b)?+(?!\s*?(;|=|,))</string>
+               <key>beginCaptures</key>
+               <dict>
+                  <key>1</key>
+                  <dict>
+                     <key>name</key>
+                     <string>keyword.control.c++</string>
+                  </dict>
+               </dict>
               <key>end</key>
               <string>(?&lt;=\})</string>
               <key>name</key>
ljbo3
 
Posts: 7
Joined: Wed Aug 15, 2012 2:23 pm

Re: C++ syntax highlighting w/ namespace definition

Postby amphetamine on Thu Jan 03, 2013 4:53 pm

ljbo3 wrote:
amphetamine wrote:Actually, thinking about it, if that had worked then I wouldn't have needed it because my "keyword.control.c++" style would've done it anyway! Duh!

Indeed. The problem I think is that the pattern does not capture the keyword "namespace". Here is a patch that works for me against the C++.tmLanguage installed by version 2.0.1
Code: Select all
--- C++.tmLanguage-original   2013-01-03 13:03:50.000000000 +0100
+++ C++.tmLanguage   2013-01-03 13:03:58.000000000 +0100
@@ -337,6 +337,14 @@
            <dict>
               <key>begin</key>
               <string>\b(namespace)\s+([A-Za-z_][_A-Za-z0-9:]*\b)?+(?!\s*?(;|=|,))</string>
+               <key>beginCaptures</key>
+               <dict>
+                  <key>1</key>
+                  <dict>
+                     <key>name</key>
+                     <string>keyword.control.c++</string>
+                  </dict>
+               </dict>
               <key>end</key>
               <string>(?&lt;=\})</string>
               <key>name</key>


Brilliant, thanks! That's the one. :)
amphetamine
 
Posts: 23
Joined: Fri Dec 14, 2012 8:59 pm

Re: C++ syntax highlighting w/ namespace definition

Postby esmirnov on Wed Dec 18, 2013 8:24 pm

Fixed regexp for highlighting following code

Code: Select all
namespace
{
using namespace ::testing;
}


Code: Select all
\b(namespace)\s+(?!::)([A-Za-z_][_A-Za-z0-9:]*\b)?+(?!\s*?(;|=|,))


added (?!::)
esmirnov
 
Posts: 2
Joined: Wed Aug 07, 2013 8:11 am


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 21 guests