Modifying language-c for C++ Syntax Highlighting


#1

Hi,
I’ve been using Atom to work on C++ projects, but there seems to be a problem with the language-c package. My header files have the extension “.h”, so certain C++ keywords are not being highlighted because the file is treated as a C file. I’ve found other threads (https://github.com/atom/atom/issues/4156) that have discussed this problem, but I don’t know how to modify core packages within Atom. I’ve also seen another thread that says I should be changing core packages through config.cson, but I don’t think I can exclude file types from core packages through that. How should I go about removing “.h” files from the C grammar within the langauge-c package?


#2

You can solve this by using the customFileTypes feature. Simply ensure you have something similar to this in your config.cson file

"*":
  core:
    customFileTypes:
      "source.cpp": [
        "h"
      ]
    disabledPackages: [ # ...

#3

There’s also a FAQ for this now:


#4

Thank you both of you. I’m new to Atom, so I wasn’t entirely sure what I was looking for.


#5

Hello,

My current config.cson file is :

"*":
  editor:
    tabLength: 4
  "exception-reporting":
    userId: "41993163-29ea-1023-3819-adb6d2303e2e"
  welcome:
    showOnStartup: false
".c.source":
  editor:
    autoIndent: true
    tabLength: 4
".cpp.source":
  autoIndent: true
  tabLength: 4

I added your bit as follows :

"*":
  core:
    customFileTypes:
      "source.cpp": [
        "h"
      ]
  editor:
    tabLength: 4
  "exception-reporting":
    userId: "41993163-29ea-1023-3819-adb6d2303e2e"
  welcome:
    showOnStartup: false
".c.source":
  editor:
    autoIndent: true
    tabLength: 4
".cpp.source":
  autoIndent: true
  tabLength: 4

closed atom and reopened it. Not a single change in the highlighting of my .h file.

Any idea ?


#6

You may need to close and reopen the files for the changes to take effect. When Atom closes, it serializes the workspace state. This includes the selected grammar for open editors. So if you make the change, leave the files open and restart Atom … they’re just going to keep the grammar that was selected when you closed Atom.


#7

You might also want to try the language-cpp14 package.


#8

This is what I did. Changed nothing. Strange thing tough : lauching atom today was followed systematically by reopening of three windows I had (and closed) yesterday, and then a crash. Now, it works normally. (ubuntu 16.04 64 bits, last atom version.)


#9

Tried it a couple a minutes before : did not change anything. Should I conclude that there’s no correct way for me in atom to have correct highlighting of .h files viewed as c++ header files ?


#10

Can you give an example of what is not working correctly?


#11

I have the same problem as the OP had : so certain C++ keywords are not being highlighted because the file is treated as a C file, class keywork, class name etc.


#12

They won’t be treated as C files if the grammar is set to C++ or C++14. If Atom is automatically detecting .h files as C, you can force the grammar with this config.cson setting:

"*":
  core:
    customFileTypes:
      "source.cpp": [
        "h"
      ]

The personal config file takes precedence over file types mentioned in any grammars. If you want to get fancier, you could write an init.cson script that checks the file extensions of other files in the directory to identify if the directory is a C++ project or a C project and then override the grammar choice based on that analysis. That might make a good package, actually.


#13

I don’t think you paid attention to my first post in this thread as what you suggested was exactly what I did and what @thomasjo suggested, and what does not work for me. That’s the very reason of me coming here to post in the first place.


#14

I have no idea why your instance of Atom would be behaving differently from mine, because when I force grammars like that, it works. Have you tried disabling language-c and using C++14?


#15

Yes, I tried, as I already wrote a couple of days earlier. And it doesn’t work either.


#16

You did not say that you had disabled language-c. If you had, then there would have been no C grammar available to Atom so it could not possibly have assigned it to the file.


#17

I don’t know what’s happening, I just see that there’s no right c++ highlighting for my c++ .h files. Anyway, as I have an atom crash right after choosing to launch it from windows (10, 64bits) installer after having installed, as well as when I run it from start menu, I will try to solve this new problem.


#18

What grammar does Atom say is active for those files? The grammar is on the right-hand side of the status bar.