Question

I'm trying to replicate the syntax highlighting style for Lua shown here:

I'm using DScintilla, the VLC component to support Scintilla in Delphi. I read every single module of Dscintilla to see the functions and get what I was looking for, and I have already done the following:

  • margins (colour, number, width, etc.)
  • colorize =, >, <,etc
  • colorize numbers
  • colorize comments
  • colorize strings with '...'

The problem now is that I can't change the comment style to italic, or colorize conditionals (if, else, then, while, etc.) from the lexer of Lua that Scintilla has, or colorize variables.

My current code, the one which works, is this:

with EditScript do
begin
  SetScrollWidth(430); //so we don't see it
  Margins.Right := 1;
  Margins.Left := 0;
  Margins.Top := 0;
  Margins.Bottom := 0;
  SetMarginLeft(5);
  SetMarginWidthN(0, 40);
  SetMarginWidthN(1, 0);
  SetExtraAscent(4); //space between lines

  //-----------------------margin line numbers
  StyleSetFont(STYLE_LINENUMBER, 'Default');
  StyleSetBold(STYLE_LINENUMBER, true);
  StyleSetBack(STYLE_LINENUMBER, gray);
  StyleSetFore(STYLE_LINENUMBER, clWebBlack);

  //-----------------------Text
  StyleSetFont(STYLE_DEFAULT, 'Courier New'); 
  //TO DO: variables and comments in cursive
  SetLexerLanguage('Lua');
  SetLexer(SCLEX_LUA);
  SetCodePage(CP_UTF8);

  //-----------------------Colours
  StyleSetFore(SCE_LUA_COMMENTLINE, green_comments);
  StyleSetFore(SCE_LUA_NUMBER, blue_numbers);
  StyleSetFore(SCE_LUA_CHARACTER, red_strings);
  StyleSetFore(SCE_LUA_OPERATOR, green_conditionals);
end;

what doesn't work? this:

//-----------------------Italics
StyleSetItalic(SCE_LUA_DEFAULT, true);
StyleSetItalic(SCE_LUA_COMMENTLINE, true);
StyleSetItalic(SCE_LUA_COMMENTDOC, true);

or

StyleSetFore(SCE_LUA_COMMENT, clGreen);
StyleSetFore(SCE_LUA_COMMENTDOC, clGreen);
StyleSetFore(SCE_LUA_STRING, clGreen);
StyleSetFore(SCE_LUA_LITERALSTRING, clGreen);
StyleSetFore(SCE_LUA_PREPROCESSOR, clGreen);
// StyleSetFore(SCE_LUA_IDENTIFIER, clGreen); //it highlights everything, wtf?
StyleSetFore(SCE_LUA_STRINGEOL, clGreen);
StyleSetFore(SCE_LUA_WORD, clGreen);
StyleSetFore(SCE_LUA_WORD2, clGreen);
StyleSetFore(SCE_LUA_WORD3, clGreen);
StyleSetFore(SCE_LUA_WORD4, clGreen);
StyleSetFore(SCE_LUA_WORD5, clGreen);
StyleSetFore(SCE_LUA_WORD6, clGreen);
StyleSetFore(SCE_LUA_WORD7, clGreen);
StyleSetFore(SCE_LUA_WORD8, clGreen);
StyleSetFore(SCE_LUA_LABEL, clGreen);

If I call ShowMessage(EditScript.DescribeKeyWordSets), I get the following:

Keywords
Basic functions
String, (table) & math functions
(coroutines), I/O & system facilities
user1
user2
user3
user4
Was it helpful?

Solution 2

Ok, I have tried a lot of combinations and ended up with this one "semi"-working. Before writing the setItalic if I write the SetFont for that style it will work in most cases, something like this:

  StyleSetFont(SCE_LUA_COMMENTLINE, 'Courier New');
  StyleSetSize(SCE_LUA_COMMENTLINE,10);

  StyleSetItalic(SCE_LUA_COMMENTLINE,true);  // "--..."

So now the ONLY problem would be keywords and variable names (in purple in the picture)

@Edited: here is how it looks like now: enter image description here

and here is the code I used:

  with EditScript do
  begin
    SetScrollWidth(430); //so we don't see it
    Margins.Right:=1;
    Margins.Left:=0;
    Margins.Top:=0;
    Margins.Bottom:=0;
    SetMarginLeft(5);
    SetMarginWidthN(0,40);
    SetMarginWidthN(1,0);
    SetExtraAscent(4); //space between lines

    //-----------------------margin line numbers
    StyleSetFont(STYLE_LINENUMBER, 'Default');
    StyleSetBold(STYLE_LINENUMBER, true);
    StyleSetBack(STYLE_LINENUMBER, gray);
    StyleSetFore(STYLE_LINENUMBER,clWebBlack);

    //-----------------------Text
    SetLexerLanguage('Lua');  //don't work
      SetLexer(SCLEX_LUA);
      SetCodePage(CP_UTF8);
      StyleSetFont(SCE_LUA_DEFAULT, 'Courier New');
      StyleSetSize(SCE_LUA_DEFAULT,10);
      StyleSetFont(SCE_LUA_IDENTIFIER, 'Courier New');
      StyleSetSize(SCE_LUA_IDENTIFIER,10);
      StyleSetFont(SCE_LUA_COMMENTLINE, 'Courier New');
      StyleSetSize(SCE_LUA_COMMENTLINE,10);
      StyleSetFont(SCE_LUA_WORD, 'Courier New');
      StyleSetSize(SCE_LUA_WORD,10);
      StyleSetFont(SCE_LUA_LITERALSTRING, 'Courier New');
      StyleSetSize(SCE_LUA_LITERALSTRING,10);
      StyleSetFont(SCE_LUA_COMMENTDOC, 'Courier New');

     //-----------------------Italics
      StyleSetItalic(SCE_LUA_COMMENTLINE,true);  // "--..."
      StyleSetItalic(SCE_LUA_LITERALSTRING,true);   // "[[...]]"
      StyleSetItalic(SCE_LUA_IDENTIFIER,true);   // "variables"
      StyleSetBold(SCE_LUA_IDENTIFIER,true);
      StyleSetBold(SCE_LUA_WORD,true);   //init start/end true false
      StyleSetItalic(SCE_LUA_COMMENTDOC,true); //don't work

     //-----------------------KeyWords
      SetKeyWords(0, 'if then else init start end true false');

     //-----------------------Colours
      StyleSetFore(SCE_LUA_COMMENTLINE, green_comments);
      StyleSetFore(SCE_LUA_COMMENTDOC, green_comments);
      StyleSetFore(SCE_LUA_NUMBER, blue_numbers);
      StyleSetFore(SCE_LUA_CHARACTER, red_strings);
      StyleSetFore(SCE_LUA_OPERATOR, green_conditionals);
      StyleSetFore(SCE_LUA_LITERALSTRING, green_comments);
      StyleSetFore(SCE_LUA_IDENTIFIER, purple);
      StyleSetFore(SCE_LUA_WORD, blue_strings);
    end;

OTHER TIPS

If the problem is only with keywords and variable names, it seems like it comes down to two things:

  1. You need to use SetKeyWords call to assign whatever keywords you have to those groups that correspond to SCE_LUA_WORD and SCE_LUA_WORD# groups. For example, editor:SetKeyWords(0,"if then else").
  2. For variable names it seems like you may need to use SCE_LUA_IDENTIFIER and color everything else in a different color. In my case, setting editor:StyleSetForeground(wxstc.wxSTC_LUA_IDENTIFIER, wx.wxColour(127, 0, 0)) correctly colors only variables used.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top