Hi guys,
TL;DR: code refactoring, boring stuff :D
I tried a rewrite of highlighting.c:styleset_*() functions for all this code to be better readable and hopefully more easily maintainable. I ended up with no logic change (yet at least ^^), but a big refactoring, in the form of arrays mapping the different values.
The code itself can be found here: https://github.com/b4n/geany/commits/highlighting-rewrite, the most important commit being https://github.com/b4n/geany/commit/ab2663e9aef8aa17af330c54eb7e6223647aa0f5
Basically, that's how I made this:
A filetype/styling definition [1] is split in 4 variables/arrays:
* highlighting_lexer_<LANG>: (guint) the SCI lexer, e.g. SCLEX_CPP * highlighting_styles_<LANG>: (HLStyle[]) SCI style/named style mappings, e.g. SCE_ASM_DEFAULT to "default" * highlighting_keywords_<LANG>: (HLKeyword[]) keywords ID/name mappings, e.g. 0 mapped to "primary" * highlighting_properties_<LANG>: (HLProperty[]) default SCI properties and their values, only used to disable fold.cpp.comment.explicit ATM, may be dropped maybe.
These definitions are enough to do all the initialization and SCI setup, and because of the naming scheme, can be hidden behind a macro:
STYLESET_INIT_FROM_MAPPING(ft_id, config, configh, <LANG>) STYLESET_FROM_MAPPING(sci, ft_id, <LANG>)
These does preprocessing "##" magic to hook the different idendifiers and call (roughly, there are actually array length too):
styleset_init_from_mapping(ft_id, config, configh, styles, keywords) styleset_from_mapping(sci, ft_id, lexer, styles, keywords, properties)
Finally, the old init_styleset_case(geany_ft_id, init_func) can be replaced by init_styleset_case_auto(<LANG>), since <LANG> is the same as in GEANY_FILETYPE_<LANG>.
OK, this may look a little complicated at first glance (though the old wasn't that straightforward either). But OTOH, the definitions are (IMO) far easier to read and update:
* SCI style/named styles are next to each other * No need to allocate enough space manually (new_styleset()), it's done automatically * All the repetitive calls are in proper loops, making the actually useful information more readable. * ...
What's left to do is porting DocBook, HTML, PHP and XML.
For DocBook it is because it uses SCI_STYLESETEOLFILLED that my port don't support yet (could easily with one more column);
For HTML, PHP and XML it's a bit harder, because they use a weird mixed setup (that I didn't try to understand too deeply yet). IMO, first thing to do would be to make each have its own styles, and inherit XML's one in filetypes definitions ([styling=XML]) rather than in-code. There wouldn't be manual duplication with the proposed setup, just alias XML definition to HTML and PHP. Not sure what to do with the Python filetype dependency though.
Anyway, I'd like your opinion on this, what to improve, what you feel good and bad about.
Regards, Colomban
[1] currently in new highlightingmappings.h header, but may go back to highlighting.c or to another .c since it's actually array definitions, not declarations