QScintilla syntax highlighting with QsciLexerCustom - UTF-8 issue

372 views Asked by At

I'm using QScintilla as a text editor in my C++ Qt project and would like to have syntax highlighting for my custom(ish) language.

Following a great guide I found (qscintilla), I subclassed QsciLexerCustom and implemented all necessary methods, specifically QsciLexerCustom::styleText. All seems to be working great until I write a non-ASCII character in the editor, which makes the highlightight to be shifted. After some investigation I found out the key is most likey in different character lengths of UTF8. This is the beginning of my styleText method:

void TemplateHighlighter::styleText(int start, int end)
{
    startStyling(start);

    const auto editor = qobject_cast<QsciScintilla*>(parent());
    mText = editor->text(start, end);
    mTextPos = 0;

    // do the work
}

Now for example, if the editor contains a non-ASCII text like č-č-č, the styleText method is called with start == 0 and end == 8, because that's the size of the Scintilla UTF-8 buffer (3x 2B plus 2x 1B). But the QString I get from the editor for this range only has 5 characters. So when I style the whole string with setStyling(5, Styles::Example), it's not enough, because that means I haven't styled everything I was asked for and at least the last č won't be styled.

I was trying to figure out a way around this but really don't see any reasonable options, if I want to stick with QStrings and don't want to work with the raw buffer, which would render my whole lexer implementation useless. Is there something I'm missing? Or some more or less reasonable workaround?

0

There are 0 answers