GitCommitSyntaxHighlighter.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #include <LibGUI/GitCommitLexer.h>
  8. #include <LibGUI/GitCommitSyntaxHighlighter.h>
  9. #include <LibGfx/Palette.h>
  10. namespace GUI {
  11. static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, GitCommitToken::Type type)
  12. {
  13. switch (type) {
  14. case GitCommitToken::Type::Comment:
  15. return { palette.syntax_comment() };
  16. default:
  17. return { palette.base_text() };
  18. }
  19. }
  20. void GitCommitSyntaxHighlighter::rehighlight(Palette const& palette)
  21. {
  22. auto text = m_client->get_text();
  23. GitCommitLexer lexer(text);
  24. auto tokens = lexer.lex();
  25. Vector<GUI::TextDocumentSpan> spans;
  26. for (auto& token : tokens) {
  27. GUI::TextDocumentSpan span;
  28. span.range.set_start({ token.m_start.line, token.m_start.column });
  29. span.range.set_end({ token.m_end.line, token.m_end.column });
  30. span.attributes = style_for_token_type(palette, token.m_type);
  31. span.is_skippable = false;
  32. span.data = static_cast<u64>(token.m_type);
  33. spans.append(span);
  34. }
  35. m_client->do_set_spans(move(spans));
  36. m_client->do_update();
  37. }
  38. Vector<GitCommitSyntaxHighlighter::MatchingTokenPair> GitCommitSyntaxHighlighter::matching_token_pairs_impl() const
  39. {
  40. static Vector<MatchingTokenPair> empty;
  41. return empty;
  42. }
  43. bool GitCommitSyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
  44. {
  45. return static_cast<GUI::GitCommitToken::Type>(token1) == static_cast<GUI::GitCommitToken::Type>(token2);
  46. }
  47. }