Sublime Forum

C++ syntax highlighting w/ namespace definition

#1

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:

namespace lol { //... }

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

Thanks a lot :smile:

0 Likes

#2

These already have different scopes:

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.

0 Likes

#3

[quote=“agibsonsw”]These already have different scopes:

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.[/quote]

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. :frowning:

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!

0 Likes

#4

Try dropping the pluses, they are probably problematic.

0 Likes

#5

[quote=“amphetamine”]
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![/quote]

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

--- 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>
0 Likes

#6

[quote=“ljbo3”]

[quote=“amphetamine”]
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![/quote]

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]
— 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 @@

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

  •   			<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>
    

[/code][/quote]

Brilliant, thanks! That’s the one. :smile:

0 Likes

#7

Fixed regexp for highlighting following code

namespace { using namespace ::testing; }

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

added (?!::slight_smile:

0 Likes