Lexical scope color for array/hash/scalar/regex


#1

Hi to everyone,

Finally after years of editing with vim I decided to give atom a try and I wanted to have different color for different things (I am working on a Perl based project). I read the documentation and I thought that it was easy to change it just see the editor log scope for seeing the CSS class then change the color for that.
Unluckily it was not so easy.
Specifically I am looking for a way to change array/hash/scalar/regexp (including grep), having the double quoted strings of the same color (regardless if it is a path to file or a literal double quoted string).
Can you give me some hint on how to do it?
Should I define my own grammar?

Thanks a lot.


#2

It depends on how the language author chooses to describe each thing, but there are certain standard classes and they don’t account for every single feature of most languages.

Should I define my own grammar?

Yes. This is pretty easy to do, especially for what you want. I’ve made a grammar boilerplate with annotations and I’ve whipped up a couple of custom language packages for people, so one of those should be easier to parse out than a grammar for an actual language.

You can set up a grammar as a wrapper for Perl that matches your things and then includes the standard Perl grammar.

What you want to do:

  1. Create the language package. You can use the package generator and delete lib/ if you want (then you don’t have to touch package.json). Let’s call it Custom Perl Wrapper.
  2. Create a grammars/custom-perl-wrapper.cson file based on the boilerplate. Tell it to match for the Perl extensions and the first line #!(\/\w+)*\/perl.
  3. Set the scope name to something like source.perl.custom (the first two parts of that are important, the third can be whatever you like). This page explains scopes in more detail. Set the name to something not Perl.
  4. Create patterns to match for the specific things you want to change. I would use a class that is not part of the standard class spread, like .wrapper-regex or something. This will let you easily style individual items from your local stylesheet.
  5. As the last pattern, simply follow this example with the line include: 'source.perl'.
  6. Reload and check to make sure the grammar is being matched. If it isn’t, you can easily tell Atom to load your custom package instead.

#3

Thanks a lot,
Then I have to tell the syntax to take the selector from the grammar to have specific colors right?


#4

You can just put that in styles.less. When you’re modifying syntax styling from there, you need to tell it to access the shadow DOM, so something like this:

atom-text-editor::shadow .regex-wrapper {
  color: blue;
}

#5

Perfect, I wrote it, just one silly question, for CSON file how to save in that format? simply changing the extension?
And also so from the package after deleting lib I need to create a grammars folder (searching it does not give any results) ?
for using the default language and my patterns it’s simply ‘include source perl’ line or the repository also?


#6

Yep. It’s a plain-text format.

And also so from the package after deleting lib I need to create a grammars folder (searching it does not give any results) ?

Yes.

for using the default language and my patterns it’s simply ‘include source perl’ line or the repository also?

You don’t need a repository for this.