LibWeb: Fix off-by-one error in SyntaxHighlighter

This changes the HTML SyntaxHighlighter to conform to the now-fixed
rendering of syntax highlighting spans in GUI::TextEditor. It also
avoids emitting tokens if they have a zero or negative length.

This fixes a bug where single-character tokens were not highlighted
properly.
This commit is contained in:
Max Wipfli 2021-06-04 11:38:57 +02:00 committed by Ali Mohammad Pur
parent 932161e581
commit 44c438d0ca
Notes: sideshowbarker 2024-07-18 16:52:56 +09:00

View File

@ -40,6 +40,10 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
Vector<GUI::TextDocumentSpan> spans;
auto highlight = [&](auto start_line, auto start_column, auto end_line, auto end_column, Gfx::TextAttributes attributes, AugmentedTokenKind kind) {
if (start_line > end_line || (start_line == end_line && start_column >= end_column)) {
dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "(HTML::SyntaxHighlighter) discarding ({}-{}) to ({}-{}) because it has zero or negative length", start_line, start_column, end_line, end_column);
return;
}
dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "(HTML::SyntaxHighlighter) highlighting ({}-{}) to ({}-{}) with color {}", start_line, start_column, end_line, end_column, attributes.color);
spans.empend(
GUI::TextRange {
@ -59,7 +63,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
} state { State::HTML };
for (;;) {
auto token = tokenizer.next_token();
if (!token.has_value())
if (!token.has_value() || token.value().is_end_of_file())
break;
dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "(HTML::SyntaxHighlighter) got token of type {}", token->to_string());
@ -88,17 +92,16 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
highlight(
token->start_position().line,
token->start_position().column,
token->start_position().line,
token->start_position().column,
token->end_position().line,
token->end_position().column,
{ palette.syntax_comment(), {} },
AugmentedTokenKind::Comment);
} else if (token->is_start_tag() || token->is_end_tag()) {
// FIXME: This breaks with single-character tag names.
highlight(
token->start_position().line,
token->start_position().column + token_start_offset,
token->start_position().line,
token->start_position().column + token->tag_name().length() + token_start_offset - 1,
token->start_position().column + token_start_offset + token->tag_name().length(),
{ palette.syntax_keyword(), {}, false, true },
token->is_start_tag() ? AugmentedTokenKind::OpenTag : AugmentedTokenKind::CloseTag);