SyntaxHighlighter.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibCpp/Lexer.h>
  8. #include <LibCpp/SyntaxHighlighter.h>
  9. #include <LibGUI/TextEditor.h>
  10. #include <LibGfx/Font/Font.h>
  11. #include <LibGfx/Palette.h>
  12. namespace Cpp {
  13. static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, Cpp::Token::Type type)
  14. {
  15. switch (type) {
  16. case Cpp::Token::Type::Keyword:
  17. return { palette.syntax_keyword(), {}, true };
  18. case Cpp::Token::Type::KnownType:
  19. return { palette.syntax_type(), {}, true };
  20. case Cpp::Token::Type::Identifier:
  21. return { palette.syntax_identifier() };
  22. case Cpp::Token::Type::DoubleQuotedString:
  23. case Cpp::Token::Type::SingleQuotedString:
  24. case Cpp::Token::Type::RawString:
  25. return { palette.syntax_string() };
  26. case Cpp::Token::Type::Integer:
  27. case Cpp::Token::Type::Float:
  28. return { palette.syntax_number() };
  29. case Cpp::Token::Type::IncludePath:
  30. return { palette.syntax_preprocessor_value() };
  31. case Cpp::Token::Type::EscapeSequence:
  32. return { palette.syntax_keyword(), {}, true };
  33. case Cpp::Token::Type::PreprocessorStatement:
  34. case Cpp::Token::Type::IncludeStatement:
  35. return { palette.syntax_preprocessor_statement() };
  36. case Cpp::Token::Type::Comment:
  37. return { palette.syntax_comment() };
  38. default:
  39. return { palette.base_text() };
  40. }
  41. }
  42. bool SyntaxHighlighter::is_identifier(u64 token) const
  43. {
  44. auto cpp_token = static_cast<Cpp::Token::Type>(token);
  45. return cpp_token == Cpp::Token::Type::Identifier;
  46. }
  47. bool SyntaxHighlighter::is_navigatable(u64 token) const
  48. {
  49. auto cpp_token = static_cast<Cpp::Token::Type>(token);
  50. return cpp_token == Cpp::Token::Type::IncludePath;
  51. }
  52. void SyntaxHighlighter::rehighlight(Palette const& palette)
  53. {
  54. auto text = m_client->get_text();
  55. Cpp::Lexer lexer(text);
  56. Vector<Token> folding_region_start_tokens;
  57. Vector<GUI::TextDocumentFoldingRegion> folding_regions;
  58. Vector<GUI::TextDocumentSpan> spans;
  59. lexer.lex_iterable([&](auto token) {
  60. // FIXME: The +1 for the token end column is a quick hack due to not wanting to modify the lexer (which is also used by the parser). Maybe there's a better way to do this.
  61. dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "{} @ {}:{} - {}:{}", token.type_as_byte_string(), token.start().line, token.start().column, token.end().line, token.end().column + 1);
  62. GUI::TextDocumentSpan span;
  63. span.range.set_start({ token.start().line, token.start().column });
  64. span.range.set_end({ token.end().line, token.end().column + 1 });
  65. span.attributes = style_for_token_type(palette, token.type());
  66. span.is_skippable = token.type() == Cpp::Token::Type::Whitespace;
  67. span.data = static_cast<u64>(token.type());
  68. spans.append(span);
  69. if (token.type() == Token::Type::LeftCurly) {
  70. folding_region_start_tokens.append(token);
  71. } else if (token.type() == Token::Type::RightCurly) {
  72. if (!folding_region_start_tokens.is_empty()) {
  73. auto start_token = folding_region_start_tokens.take_last();
  74. GUI::TextDocumentFoldingRegion folding_region;
  75. folding_region.range.set_start({ start_token.end().line, start_token.end().column });
  76. folding_region.range.set_end({ token.start().line, token.start().column });
  77. folding_regions.append(move(folding_region));
  78. }
  79. }
  80. });
  81. m_client->do_set_spans(move(spans));
  82. m_client->do_set_folding_regions(move(folding_regions));
  83. m_has_brace_buddies = false;
  84. highlight_matching_token_pair();
  85. m_client->do_update();
  86. }
  87. Vector<SyntaxHighlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs_impl() const
  88. {
  89. static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
  90. if (pairs.is_empty()) {
  91. pairs.append({ static_cast<u64>(Cpp::Token::Type::LeftCurly), static_cast<u64>(Cpp::Token::Type::RightCurly) });
  92. pairs.append({ static_cast<u64>(Cpp::Token::Type::LeftParen), static_cast<u64>(Cpp::Token::Type::RightParen) });
  93. pairs.append({ static_cast<u64>(Cpp::Token::Type::LeftBracket), static_cast<u64>(Cpp::Token::Type::RightBracket) });
  94. }
  95. return pairs;
  96. }
  97. bool SyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
  98. {
  99. return static_cast<Cpp::Token::Type>(token1) == static_cast<Cpp::Token::Type>(token2);
  100. }
  101. }