Serenity Operating System
1/*
2 * Copyright (c) 2022, Brian Gianforcaro <bgianf@serenityos.org>
3 * Copyright (c) 2022, the SerenityOS developers.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <LibGUI/GitCommitLexer.h>
9#include <LibGUI/GitCommitSyntaxHighlighter.h>
10#include <LibGfx/Palette.h>
11
12namespace GUI {
13static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, GitCommitToken::Type type)
14{
15 switch (type) {
16 case GitCommitToken::Type::Comment:
17 return { palette.syntax_comment() };
18 default:
19 return { palette.base_text() };
20 }
21}
22
23void GitCommitSyntaxHighlighter::rehighlight(Palette const& palette)
24{
25 auto text = m_client->get_text();
26 GitCommitLexer lexer(text);
27 auto tokens = lexer.lex();
28
29 Vector<GUI::TextDocumentSpan> spans;
30 for (auto& token : tokens) {
31 GUI::TextDocumentSpan span;
32 span.range.set_start({ token.m_start.line, token.m_start.column });
33 span.range.set_end({ token.m_end.line, token.m_end.column });
34 auto style = style_for_token_type(palette, token.m_type);
35 span.attributes.color = style.color;
36 span.attributes.bold = style.bold;
37 span.is_skippable = false;
38 span.data = static_cast<u64>(token.m_type);
39 spans.append(span);
40 }
41 m_client->do_set_spans(move(spans));
42 m_client->do_update();
43}
44
45Vector<GitCommitSyntaxHighlighter::MatchingTokenPair> GitCommitSyntaxHighlighter::matching_token_pairs_impl() const
46{
47 static Vector<MatchingTokenPair> empty;
48 return empty;
49}
50
51bool GitCommitSyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
52{
53 return static_cast<GUI::GitCommitToken::Type>(token1) == static_cast<GUI::GitCommitToken::Type>(token2);
54}
55
56}