SyntaxHighlighter.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibWeb/CSS/Parser/Tokenizer.h>
  8. #include <LibWeb/CSS/SyntaxHighlighter/SyntaxHighlighter.h>
  9. namespace Web::CSS {
  10. bool SyntaxHighlighter::is_identifier(u64 token) const
  11. {
  12. return static_cast<CSS::Token::Type>(token) == CSS::Token::Type::Ident;
  13. }
  14. bool SyntaxHighlighter::is_navigatable(u64) const
  15. {
  16. return false;
  17. }
  18. void SyntaxHighlighter::rehighlight(Palette const& palette)
  19. {
  20. dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "(CSS::SyntaxHighlighter) starting rehighlight");
  21. auto text = m_client->get_text();
  22. Vector<GUI::TextDocumentSpan> spans;
  23. auto highlight = [&](auto start_line, auto start_column, auto end_line, auto end_column, Gfx::TextAttributes attributes, CSS::Token::Type type) {
  24. if (start_line > end_line || (start_line == end_line && start_column >= end_column)) {
  25. dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "(CSS::SyntaxHighlighter) discarding ({}-{}) to ({}-{}) because it has zero or negative length", start_line, start_column, end_line, end_column);
  26. return;
  27. }
  28. dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "(CSS::SyntaxHighlighter) highlighting ({}-{}) to ({}-{}) with color {}", start_line, start_column, end_line, end_column, attributes.color);
  29. spans.empend(
  30. GUI::TextRange {
  31. { start_line, start_column },
  32. { end_line, end_column },
  33. },
  34. move(attributes),
  35. static_cast<u64>(type),
  36. false);
  37. };
  38. CSS::Tokenizer tokenizer { text, "utf-8" };
  39. auto tokens = tokenizer.parse();
  40. for (auto const& token : tokens) {
  41. if (token.is(Token::Type::EndOfFile))
  42. break;
  43. switch (token.type()) {
  44. case Token::Type::Ident:
  45. highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column, { palette.syntax_identifier(), {} }, token.type());
  46. break;
  47. case Token::Type::String:
  48. highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column, { palette.syntax_string(), {} }, token.type());
  49. break;
  50. case Token::Type::Whitespace:
  51. // CSS doesn't produce comment tokens, they're just included as part of whitespace.
  52. highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column, { palette.syntax_comment(), {} }, token.type());
  53. break;
  54. case Token::Type::AtKeyword:
  55. highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column, { palette.syntax_keyword(), {} }, token.type());
  56. break;
  57. case Token::Type::Function:
  58. // Function tokens include the opening '(', so we split that into two tokens for highlighting purposes.
  59. highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column - 1, { palette.syntax_keyword(), {} }, token.type());
  60. highlight(token.end_position().line, token.end_position().column - 1, token.end_position().line, token.end_position().column, { palette.syntax_punctuation(), {} }, Token::Type::OpenParen);
  61. break;
  62. case Token::Type::Url:
  63. // An Url token is a `url()` function with its parameter string unquoted.
  64. // url
  65. highlight(token.start_position().line, token.start_position().column, token.start_position().line, token.start_position().column + 3, { palette.syntax_keyword(), {} }, token.type());
  66. // (
  67. highlight(token.start_position().line, token.start_position().column + 3, token.start_position().line, token.start_position().column + 4, { palette.syntax_punctuation(), {} }, Token::Type::OpenParen);
  68. // <string>
  69. highlight(token.start_position().line, token.start_position().column + 4, token.end_position().line, token.end_position().column - 1, { palette.syntax_string(), {} }, Token::Type::String);
  70. // )
  71. highlight(token.end_position().line, token.end_position().column - 1, token.end_position().line, token.end_position().column, { palette.syntax_punctuation(), {} }, Token::Type::CloseParen);
  72. break;
  73. case Token::Type::Number:
  74. case Token::Type::Dimension:
  75. case Token::Type::Percentage:
  76. highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column, { palette.syntax_number(), {} }, token.type());
  77. break;
  78. case Token::Type::Delim:
  79. case Token::Type::Colon:
  80. case Token::Type::Comma:
  81. case Token::Type::Semicolon:
  82. case Token::Type::OpenCurly:
  83. case Token::Type::OpenParen:
  84. case Token::Type::OpenSquare:
  85. case Token::Type::CloseCurly:
  86. case Token::Type::CloseParen:
  87. case Token::Type::CloseSquare:
  88. highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column, { palette.syntax_punctuation(), {} }, token.type());
  89. break;
  90. case Token::Type::CDO:
  91. case Token::Type::CDC:
  92. highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column, { palette.syntax_comment(), {} }, token.type());
  93. break;
  94. case Token::Type::Hash:
  95. // FIXME: Hash tokens can be ID selectors or colors, we don't know which without parsing properly.
  96. highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column, { palette.syntax_number(), {} }, token.type());
  97. break;
  98. case Token::Type::Invalid:
  99. case Token::Type::BadUrl:
  100. case Token::Type::BadString:
  101. // FIXME: Error highlighting color in palette?
  102. highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column, { Color(Color::NamedColor::Red), {}, false, true }, token.type());
  103. break;
  104. case Token::Type::EndOfFile:
  105. default:
  106. break;
  107. }
  108. }
  109. if constexpr (SYNTAX_HIGHLIGHTING_DEBUG) {
  110. dbgln("(CSS::SyntaxHighlighter) list of all spans:");
  111. for (auto& span : spans)
  112. dbgln("{}, {} - {}", span.range, span.attributes.color, span.data);
  113. dbgln("(CSS::SyntaxHighlighter) end of list");
  114. }
  115. m_client->do_set_spans(move(spans));
  116. m_has_brace_buddies = false;
  117. highlight_matching_token_pair();
  118. m_client->do_update();
  119. }
  120. Vector<Syntax::Highlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs_impl() const
  121. {
  122. static Vector<Syntax::Highlighter::MatchingTokenPair> pairs;
  123. if (pairs.is_empty()) {
  124. pairs.append({ static_cast<u64>(CSS::Token::Type::OpenCurly), static_cast<u64>(CSS::Token::Type::CloseCurly) });
  125. pairs.append({ static_cast<u64>(CSS::Token::Type::OpenParen), static_cast<u64>(CSS::Token::Type::CloseParen) });
  126. pairs.append({ static_cast<u64>(CSS::Token::Type::OpenSquare), static_cast<u64>(CSS::Token::Type::CloseSquare) });
  127. pairs.append({ static_cast<u64>(CSS::Token::Type::CDO), static_cast<u64>(CSS::Token::Type::CDC) });
  128. }
  129. return pairs;
  130. }
  131. bool SyntaxHighlighter::token_types_equal(u64 token0, u64 token1) const
  132. {
  133. return token0 == token1;
  134. }
  135. }