ScintillaOrg / lexilla

A library of language lexers for use with Scintilla
https://www.scintilla.org/Lexilla.html
Other
179 stars 64 forks source link

Fixed python decorators #219

Closed PavelBlend closed 9 months ago

PavelBlend commented 9 months ago

See: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/5894 See: https://sourceforge.net/p/scintilla/bugs/2139/

Notepad++ now colors decorators correctly: Before: 01

Fixed: 02

I took the fix code from here (@Ekopalypse wrote it): https://github.com/notepad-plus-plus/notepad-plus-plus/issues/5894#issuecomment-508936642

nyamatongwe commented 9 months ago

See #49 and lexer.python.decorator.attributes=1.

PavelBlend commented 9 months ago

@nyamatongwe I see that this bug is fixed in 2022, but notepad++ 8.6 still does not display decorators correctly. With what it can be connected? Notepad++ is using an old version of lexilla?

rdipardo commented 9 months ago

@PavelBlend

I see that this bug is fixed in 2022, but notepad++ 8.6 still does not display decorators correctly. With what it can be connected? Notepad++ is using an old version of lexilla?

Integrating a new lexer property with Notepad++ requires code changes and an updated style configuration.

Unfortunately the application sets every property at compile time, so an explicit call to SCI_SETPROPERTY needs to be added to the ScintillaEditView::setPythonLexer method. Then a style has to be mapped to the new lexical class in the stylers.model.xml template file. To make SCE_P_ATTRIBUTE blend in with decorators, for example, it would be enough to duplicate the existing SCE_P_DECORATOR style to create a new mapping, e.g.,

<!-- notepad-plus-plus/PowerEditor/src/stylers.model.xml -->
<LexerType name="python" desc="Python" ext="">
    <!-- . . . -->
    <WordsStyle name="ATTRIBUTE" styleID="20" fgColor="FF8000" bgColor="FFFFFF" fontName="" fontStyle="2" fontSize="" />
</LexerType>

Users who prefer a theme other than the default will have to update them by themselves. There has never been a co-ordinated effort to propagate new lexical classes into every theme's XML descriptor. Some themes are still missing a style for f-strings, introduced in 2017 (1438aa0).

An alternative is to use a plugin that can send messages directly to Scintilla. The current favourite is Python Script.

You still have to amend your preferred theme's XML file as above (for illustration, I chose the default theme defined in %AppData%\Notepad++\stylers.xml). After that, you can set the lexer property in a script and have the plugin's Python host run it, e.g.,

# $(NOTEPAD++_INSTALL_PATH)/plugins/PythonScript/scripts/LexPythonWithAttributes.py
from Npp import *

def _style_python_attributes(args):
    if notepad.getCurrentFilename().endswith('py'):
        editor.setProperty('lexer.python.decorator.attributes', 1)

if __name__ == '__main__':
    notepad.callback(_style_python_attributes,
                     [NOTIFICATION.BUFFERACTIVATED,
                      NOTIFICATION.LANGCHANGED,
                      NOTIFICATION.WORDSTYLESUPDATED])
lexpython-with-attributes

This will work in any version of Notepad++ from 8.4 onward.