emacs icon indicating copy to clipboard operation
emacs copied to clipboard

Decorators highlighting in python

Open sivakov512 opened this issue 6 years ago • 8 comments

What about the highlighting of Python decorators with color different from classes and methods? For example screenshots from Emacs (first) and Visual Studio Code (second)

image image

sivakov512 avatar Oct 12 '17 04:10 sivakov512

I've tried to add the py-decorator-face face, but it seems to have no effect because the decorator inherits from the font-lock-type-face.

arcticicestudio avatar Oct 12 '17 07:10 arcticicestudio

@sivakov512: You can "fontify" decorators by adding a regular expression for them to the keyword list for font locking. (This has nothing to do with Nord theme, specifically, cf. docs on font locking.)

For example, try adding the following to your Emacs config:

(add-hook 'python-mode-hook
	  (lambda ()
	    (font-lock-add-keywords
	     nil
	     '(("\\(^@[^(]*\\)" 1 'font-lock-preprocessor-face)))))

With an optional customization of the preprocessor face

(set-face-attribute 'font-lock-preprocessor-face nil
  :weight 'normal
  :foreground "#B48EAD")

you should get something like this (aside from further custom coloring of constant and keyword faces):

screen shot 2018-10-01 at 12 26 18

egnha avatar Oct 01 '18 10:10 egnha

@egnha Thanks for your explanation and examples, this looks like a really promising solution :rocket: Would you like to submit a PR for this? Nord uses nord12 for annotations/decorators if you'd like to go for it.

Also Hacktoberfest started to today, I'd really appreciate some contributions to get some things done during this month :smile: (and you can get a swaggy t-shirt :shirt: as recognition for free :laughing: )

arcticicestudio avatar Oct 01 '18 18:10 arcticicestudio

I’d be glad to submit a PR (though I won’t be able to get around to that until the weekend). Thanks for the 👕 offer :)

egnha avatar Oct 01 '18 18:10 egnha

@egnha Take your time, there's no time pressure. You can register on their site by simply logging in with your GitHub account (via OAuth) and as soon as you submited 5 PRs in october to any open source project on GitHub they'll send you a T-Shirt and some other swag like a certificate of participation :smile:

arcticicestudio avatar Oct 01 '18 19:10 arcticicestudio

In the meantime, a more robust regex to identify decorators:

(add-hook 'python-mode-hook
	  (lambda ()
	    (font-lock-add-keywords
	     nil
	     '(("^[[:space:]]*\\(@[^(#[:space:]\n]*\\)" 1 'font-lock-preprocessor-face)))))

egnha avatar Oct 17 '18 13:10 egnha

@arcticicestudio, unfortunately, I won't have time to submit a PR. Sorry.

egnha avatar Nov 16 '18 10:11 egnha

@egnha No problem, I'm also busy with other urgent tasks and projects, but I'll try to test and put your code into a PR as soon as there is some time.

arcticicestudio avatar Nov 16 '18 12:11 arcticicestudio