1

Having some trouble getting wxStyledTextCtrl to colourise my word listings.

x->m_ctlEdit->SetKeyWords(0,"true false");
x->SetWordChars(wxT("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMONPQRSTUVWXYZ._") );
x->StyleSetForeground(wxSTC_HPHP_WORD, wxColour(0x67,0xa6,0xff));

true and false receive no colouring this way, I've used the StyleSetForeground on many of the other definitions and it all works fine, just having trouble with the word lists.

As a second question, How do I seperate colours for different word listings? I'm aware I can set different keywords list with the number identifier, but how do I apply the styles per keyword list since the function doesn't take in an identifier?

Note: Using the HTML/PHP lexer that comes as a default option with wxStyledTextCtrl

Prodigle
  • 1,757
  • 12
  • 23

1 Answers1

3

For the wxSTC_LEX_HTML html lexer or the wxSTC_LEX_PHPSCRIPT php lexer, you need to specify key word set 4. So for example:

x->m_ctlEdit->SetKeyWords(4,"true false");

If you're using the html lexer, you can learn this by calling x->m_ctlEdit->DescribeKeyWordSets(); which will return the following list:

  • HTML elements and attributes
  • JavaScript keywords
  • VBScript keywords
  • Python keywords
  • PHP keywords
  • SGML and DTD keywords

In this case, the 0-based index of the PHP keywords is 4, so this would be the number to pass in to the SetKeyWords method.

However this way of checking this fails when using the PHP lexer since calling DescribeKeyWordSets will only return "PHP keywords". So you would think you should call SetKeyWords with 0, but in fact you still need to use 4 because the php script lexer is the same as the html lexer. That just seems to be an oddity of Scintilla.

On an unrelated note, I think the call to SetWordChars is unnecessary. According to the documentation, that is for searching by words and not for keywords.

As a second question, How do I seperate colours for different word listings?

That depends on the lexer. For example, the C lexer offers the following keyword sets

  • Primary keywords and identifiers
  • Secondary keywords and identifiers
  • Documentation comment keywords
  • Global classes and typedefs
  • Preprocessor definitions

which correspond to the lexer states wxSTC_C_WORD, wxSTC_C_WORD2,wxSTC_C_COMMENTDOCKEYWORD, etc.

Unfortunately, as described above the html lexer only offers 1 keyword set for PHP.

New Pagodi
  • 3,484
  • 1
  • 14
  • 24
  • Thanks for the well crafted answer, Would it be possible to extend the HTML lexer's key sets or overwrite existing definitions? For example if I fill in a keyword set of 7 with (true false null), would this or keywordset 4 take precedence when styling? – Prodigle May 10 '19 at 13:55
  • 1
    Before posting this answer I did try to see if the JavaScript and Python could be used, but it looks like the lexer won't use them with PHP code. And if you set keyword sets 6 or 7, the lexer won't use them at all. So I don't either overwriting or extending will work. – New Pagodi May 10 '19 at 14:26
  • 1
    If you really need to use multiple keyword sets, the only thing I can think of is to take the existing scintilla [html lexer](https://github.com/wxWidgets/wxWidgets/blob/master/src/stc/scintilla/lexers/LexHTML.cxx), convert it to an external lexer, and modify it to use keyword sets 6 and 7 for additional PHP keyword sets. [Here's](https://www.scintilla.org/LexPython.cxx.html) an example of how to convert the existing python lexer to an external lexer. Looking over the code of the html lexer, it looks like that shouldn't be too hard. – New Pagodi May 10 '19 at 14:34
  • 1
    You might also try asking on the scintilla-interest mailing list if there is a way to use multiple keyword sets with PHP. – New Pagodi May 10 '19 at 14:35
  • Appreciate the help. Should be more than enough to go off :) – Prodigle May 10 '19 at 14:38
  • Scoured through the source for a JSON lexer and couldn't find one, couldn't quite work out if the HTML includes one either, it includes JS lexing but any idea if this extends to JSON or if I need to grab a custom one? – Prodigle May 14 '19 at 16:53
  • If you're using the 3.1.1 or higher, there is a JSON lexer. But if you're using the 3.0 branch, I'll guess you'll have to use the java script lexer that's part of the HTML one. (Actually all the stuff I said above about external lexers only applies for 3.1.1 or higher too.) – New Pagodi May 14 '19 at 17:05
  • Took a look at just throwing the newest Scintilla JSON lexer into my older version but based on the Wiki every lexer needs a matching Colourise*Line and Colourise*Doc function which the JSON lexer just seems to be missing. A scour through the rest of Scintilla couldn't find that either :/ – Prodigle May 15 '19 at 09:09
  • 1
    That's one area I can't help you with. If you can't update to a higher wxWidgets version and are going to try modifying the wxSTC sources, I think you'll have better luck adding the [changes](https://github.com/wxWidgets/wxWidgets/commit/a6e249ea1a0499e1b2da52ea74da673068108c01) that let wxSTC use external lexers and then trying to use the JSON lexer as an external one. – New Pagodi May 15 '19 at 11:06
  • To wrap it up for anyone stumbling across this, the YAML lexer will work with JSON, but does not have a special identifier for value, but does for key E.G in the case title: "hello", you can colourise 'title' but not 'hello' which was good enough for my needs. Thanks again Pagodi – Prodigle May 16 '19 at 08:55