1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:18:12 +00:00

LibGUI: Fix wrong Sequence highlight range in ShellSyntaxHighlighter

This fixes the wrong highlight behaviour when a newline is used as
sequence separator:
```sh
echo foo
if foo {}
^ This character would previously be bold
```
This commit is contained in:
AnotherTest 2020-10-14 19:52:13 +03:30 committed by Andreas Kling
parent b1b202124c
commit 9545ed1c21

View file

@ -43,9 +43,10 @@ enum class AugmentedTokenKind : u32 {
class HighlightVisitor : public AST::NodeVisitor { class HighlightVisitor : public AST::NodeVisitor {
public: public:
HighlightVisitor(Vector<GUI::TextDocumentSpan>& spans, const Gfx::Palette& palette) HighlightVisitor(Vector<GUI::TextDocumentSpan>& spans, const Gfx::Palette& palette, const TextDocument& document)
: m_spans(spans) : m_spans(spans)
, m_palette(palette) , m_palette(palette)
, m_document(document)
{ {
} }
@ -56,13 +57,34 @@ private:
size_t offset { 0 }; size_t offset { 0 };
}; };
static void set_offset_range_end(TextRange& range, const AST::Position::Line& line, size_t offset = 1) AST::Position::Line offset_line(const AST::Position::Line& line, size_t offset)
{ {
range.set_end({ line.line_number, line.line_column - min(line.line_column, offset) }); // We need to look at the line(s) above.
AST::Position::Line new_line { line };
while (new_line.line_column < offset) {
offset -= new_line.line_column;
--offset;
ASSERT(new_line.line_number > 0);
--new_line.line_number;
auto line = m_document.line(new_line.line_number);
new_line.line_column = line.length();
}
if (offset > 0)
new_line.line_column -= offset;
return new_line;
} }
static void set_offset_range_start(TextRange& range, const AST::Position::Line& line, size_t offset = 1) void set_offset_range_end(TextRange& range, const AST::Position::Line& line, size_t offset = 1)
{ {
range.set_start({ line.line_number, line.line_column - min(line.line_column, offset) }); auto new_line = offset_line(line, offset);
range.set_end({ new_line.line_number, new_line.line_column });
}
void set_offset_range_start(TextRange& range, const AST::Position::Line& line, size_t offset = 1)
{
auto new_line = offset_line(line, offset);
range.set_start({ new_line.line_number, new_line.line_column });
} }
GUI::TextDocumentSpan& span_for_node(const AST::Node* node) GUI::TextDocumentSpan& span_for_node(const AST::Node* node)
@ -435,6 +457,7 @@ private:
Vector<GUI::TextDocumentSpan>& m_spans; Vector<GUI::TextDocumentSpan>& m_spans;
const Gfx::Palette& m_palette; const Gfx::Palette& m_palette;
const TextDocument& m_document;
bool m_is_first_in_command { false }; bool m_is_first_in_command { false };
}; };
@ -468,7 +491,7 @@ void ShellSyntaxHighlighter::rehighlight(Gfx::Palette palette)
Vector<GUI::TextDocumentSpan> spans; Vector<GUI::TextDocumentSpan> spans;
GUI::TextPosition position { 0, 0 }; GUI::TextPosition position { 0, 0 };
HighlightVisitor visitor { spans, palette }; HighlightVisitor visitor { spans, palette, m_editor->document() };
if (ast) if (ast)
ast->visit(visitor); ast->visit(visitor);