GitCommitSyntaxHighlighter.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 Syntax::TextStyle 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. auto style = style_for_token_type(palette, token.m_type);
  31. span.attributes.color = style.color;
  32. span.attributes.bold = style.bold;
  33. span.is_skippable = false;
  34. span.data = static_cast<u64>(token.m_type);
  35. spans.append(span);
  36. }
  37. m_client->do_set_spans(move(spans));
  38. m_client->do_update();
  39. }
  40. Vector<GitCommitSyntaxHighlighter::MatchingTokenPair> GitCommitSyntaxHighlighter::matching_token_pairs_impl() const
  41. {
  42. static Vector<MatchingTokenPair> empty;
  43. return empty;
  44. }
  45. bool GitCommitSyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
  46. {
  47. return static_cast<GUI::GitCommitToken::Type>(token1) == static_cast<GUI::GitCommitToken::Type>(token2);
  48. }
  49. }