3
$\begingroup$

Blender has a colour assigned for Builtins, Symbol, Special, Preprocessor, Reserved, Comment, String and Numbers.

enter image description here The text editor theme for syntax highlighting

While testing importing Vim colorschemes to blender I noticed there is quite some variance in keyword assignment.

Vim uses a syntax file to highlight colors from the colorscheme

Snip from vim syntax file for python python.vim

"
" Keywords
"

syn keyword pythonStatement     break continue del
syn keyword pythonStatement     exec return
syn keyword pythonStatement     pass raise
syn keyword pythonStatement     global assert
syn keyword pythonStatement     lambda
syn keyword pythonStatement     with
syn keyword pythonStatement     def class nextgroup=pythonFunction skipwhite
syn keyword pythonRepeat        for while
syn keyword pythonConditional   if elif else
syn keyword pythonImport        import
syn keyword pythonException     try except finally
syn keyword pythonOperator      and in is not or

enter image description here My current result for importing the github colorscheme

How and where are the python keywords assigned to syntax type in blender. Can they be modified using python?

$\endgroup$

1 Answer 1

2
$\begingroup$

Providing syntax highlighting in the editor requires parsers for the programming languages in order to identify keywords. These parsers can be found in source/blender/editors/space_text. Currently implement are text_format_lua.c, text_format_osl.c, text_format_pov.c, text_format_pov_ini.c and text_format_py.c. Each of these implementation provides a functions to map strings to internal identifier types which can later be used to give these a distinct color, e.g. in case of the Python:

static void txtfmt_py_format_line(SpaceText *st, TextLine *line, const bool do_next)

Every format implementation provide a function to register the parser as well as the formatter. They are called ED_text_format_register_ followed by the language suffix. Ultimately they all call ED_text_format_register to register the callback functions. The registration using ED_text_format_register_ functions is performed once in source/blender/editors/space_text/space_text.c by calling:

void ED_spacetype_text(void)

Once this is completed the text can be formatted and syntax highlighted by

void draw_text_main(SpaceText *st, ARegion *ar)

which can be found in source/blender/editors/space_text/text_draw.c. This function retrieves the pointers to formatting callbacks for a given text using tft = ED_text_format_get(text);. The return value can then be used to call the callback with tft->format_line(st, tmp, false);. In case the current language is Python that would result in a call to txtfmt_py_format_line. It stores identifiers / format character in tmp which can then be used to highlight keywords by one of the drawing functions text_draw, text_draw_wrapped and draw_suggestion_list. Each of them will call the function:

static void format_draw_color(const TextDrawContext *tdc, char formatchar)

This functions contains a switch statement that identifies which format character is given and applies the matching color from the theme. For example if the format character would be FMT_TYPE_KEYWORD it would set the color with UI_FontThemeColor(tdc->font_id, TH_SYNTAX_B). The parameter TH_SYNTAX_B determines the color.

In source/blender/editors/interface/resource.c is the function that is parsing this color id TH_SYNTAX_B to retrieve the actual color.

const uchar *UI_ThemeGetColorPtr(bTheme *btheme, int spacetype, int colorid)

This function use ts->syntaxb when the color id is TH_SYNTAX_B where ts is a pointer to a ThemeSpace.

The place where syntaxb is assigned its value is in release/datafiles/userdef/userdef_default_theme.c. The value is given in .space_text as

.syntaxb = RGBA(0xff1961ff),

For this syntaxb we have a matching RNA property definition in source/blender/makesrna/intern/rna_userdef.c

  prop = RNA_def_property(srna, "syntax_builtin", PROP_FLOAT, PROP_COLOR_GAMMA);
  RNA_def_property_float_sdna(prop, NULL, "syntaxb");
  RNA_def_property_array(prop, 3);
  RNA_def_property_ui_text(prop, "Syntax Built-in", "");
  RNA_def_property_update(prop, 0, "rna_userdef_theme_update"); 

The property syntax_builtin can be set in the Blender theme. For instance release/scripts/presets/interface_theme/blender_light.xml sets syntax_builtin to:

syntax_builtin="#ff1961"
$\endgroup$
6
  • $\begingroup$ It's not so much the color which I can get from the theme, rather the which keywords are assigned to which type. $\endgroup$
    – batFINGER
    Commented Jul 27, 2019 at 23:39
  • $\begingroup$ @batFINGER Oh I see. Have to check. BTW def, class etc. aren't considered "keywords". They are affected by the Syntax Special setting. $\endgroup$ Commented Jul 27, 2019 at 23:42
  • $\begingroup$ @batFINGER the formats and highlighting rules are defined in source/blender/editors/space_text with different files for the languages. text_format_py.c, text_format_pov_ini.c, text_format_pov.c, text_format_osl.c, text_format_lua.c. So you have to modify the source, nothing that can be done in python. $\endgroup$ Commented Jul 27, 2019 at 23:46
  • $\begingroup$ Cool, edit into answer and will accept. Not after modifying just after the set of keywords associated with type to compare with the vim set of same type as shown in question. Thanks again. Oh and god knows which python.vim that one is... plucked it from a folder search for example paste. Thanks for heads up tho. $\endgroup$
    – batFINGER
    Commented Jul 27, 2019 at 23:54
  • $\begingroup$ @batFINGER edited the answer. Let me know if there is something else you need. $\endgroup$ Commented Jul 28, 2019 at 0:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .