Syntax Theme for python


#1

Hello,

Anybody knows of a good custom theme for highlighting python ? The packages I’ve tested so far have a very bad set of syntax variables by default.

Thanks


#2

Sure, the flight manual has a guide on writing such grammars. If it’s just an addition of keywords, you can write one to inject them into a python grammar.

If you explained which packages you tried, and what “bad set of syntax variables” means, I’d be able to assist further.


#3

“Bad set of syntax variables” is the fact that 1 variable covers too many things, in sublime Text I’ve found themes where I could break down do characters like ’ or "

for example, in the default theme One-Dark we have variables such as:
@syntax-color-variable:
@syntax-color-constant:
@syntax-color-property:
@syntax-color-value:

but there is no
@syntax-color-string (it regrouped under syntax-color-snipped as well as other things)
then you could go further
@syntax-color-string
@syntax-color-docstring
etc etc

But I guess this would be created in a python.less ?


#4

Oh, my bad. I (somehow) saw grammar, not theme.

You can add anything you like to your styles.less file. Use Editor: Log cursor scope in the command palette to see the scopes to target. Prepend all scopes with syntax--. Here’s an example to start with, though you might want to get familiar with LESS / CSS

atom-text-editor[data-grammar="source python"] {
  .syntax--string {
    color: #00ccff;

    &.syntax--quoted {
      color: red;
    }
  }

  .syntax--group {
    color: #aed984
  }

  .syntax--block {
    color: #ff8485;
  }
}

As for the existing variables, I guess that’s all the developers needed. You can define as many as you like though.


#5

I will give that a try.

Thank you


#6

This doesn’t seem to be working.

I’ve added the following:

atom-text-editor[data-grammar="source python"] {
    .syntax--meta.function.decorator.python {
        color: rgb(255,0,0);
    }
}

meta.function.decorator.python being the scope at the cursor.

But this doesn’t change anything.


#7

syntax-- needs to be prepended to all the things. Also, there is precedence involved. The entity.name.function.decorator scope has a greater priority than the one above it

Try

atom-text-editor[data-grammar="source python"] {
    .syntax--entity.syntax--name.syntax--function.syntax--decorator {
        color: rgb(255,0,0);
    }
}

#8

It there a way to extend that to a specific word or character?

.syntax--support.syntax--type

this would highlight super and decorator. Can I specify that I only want decorator ?


#9

I don’t think you can select based on content. Basically, the grammar (language-python) has a bunch of rules about how to interpret text. These rules are used to apply the classes (.syntax--support, etc). These classes are pure HTML / CSS, so you can do anything to them that you can with normal CSS (which is what themes are; just CSS).

If the grammar does not distinguish between different words, and gives them the same scope, I don’t think it’s possible to do it.

There is a new grammar engine being rolled out (Tree-sitter), where you can apply scopes by content match, but I don’t think the customisation is directly available to the user yet (only the grammar package can apply the scopes for now).