GitCommitSyntaxHighlighter.cpp 1.7 KB

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