SyntaxHighlighter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibGfx/Palette.h>
  8. #include <LibJS/Lexer.h>
  9. #include <LibJS/SyntaxHighlighter.h>
  10. #include <LibJS/Token.h>
  11. namespace JS {
  12. static Syntax::TextStyle style_for_token_type(const Gfx::Palette& palette, JS::TokenType type)
  13. {
  14. switch (JS::Token::category(type)) {
  15. case JS::TokenCategory::Invalid:
  16. return { palette.syntax_comment() };
  17. case JS::TokenCategory::Number:
  18. return { palette.syntax_number() };
  19. case JS::TokenCategory::String:
  20. return { palette.syntax_string() };
  21. case JS::TokenCategory::Punctuation:
  22. return { palette.syntax_punctuation() };
  23. case JS::TokenCategory::Operator:
  24. return { palette.syntax_operator() };
  25. case JS::TokenCategory::Keyword:
  26. return { palette.syntax_keyword(), true };
  27. case JS::TokenCategory::ControlKeyword:
  28. return { palette.syntax_control_keyword(), true };
  29. case JS::TokenCategory::Identifier:
  30. return { palette.syntax_identifier() };
  31. default:
  32. return { palette.base_text() };
  33. }
  34. }
  35. bool SyntaxHighlighter::is_identifier(u64 token) const
  36. {
  37. auto js_token = static_cast<JS::TokenType>(static_cast<size_t>(token));
  38. return js_token == JS::TokenType::Identifier;
  39. }
  40. bool SyntaxHighlighter::is_navigatable([[maybe_unused]] u64 token) const
  41. {
  42. return false;
  43. }
  44. void SyntaxHighlighter::rehighlight(const Palette& palette)
  45. {
  46. auto text = m_client->get_text();
  47. JS::Lexer lexer(text);
  48. Vector<GUI::TextDocumentSpan> spans;
  49. GUI::TextPosition position { 0, 0 };
  50. GUI::TextPosition start { 0, 0 };
  51. auto advance_position = [&position](char ch) {
  52. if (ch == '\n') {
  53. position.set_line(position.line() + 1);
  54. position.set_column(0);
  55. } else
  56. position.set_column(position.column() + 1);
  57. };
  58. auto append_token = [&](StringView str, const JS::Token& token, bool is_trivia) {
  59. if (str.is_empty())
  60. return;
  61. start = position;
  62. for (size_t i = 0; i < str.length(); ++i)
  63. advance_position(str[i]);
  64. GUI::TextDocumentSpan span;
  65. span.range.set_start(start);
  66. span.range.set_end({ position.line(), position.column() });
  67. auto type = is_trivia ? JS::TokenType::Invalid : token.type();
  68. auto style = style_for_token_type(palette, type);
  69. span.attributes.color = style.color;
  70. span.attributes.bold = style.bold;
  71. span.is_skippable = is_trivia;
  72. span.data = static_cast<u64>(type);
  73. spans.append(span);
  74. dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "{}{} @ '{}' {}:{} - {}:{}",
  75. token.name(),
  76. is_trivia ? " (trivia)" : "",
  77. token.value(),
  78. span.range.start().line(), span.range.start().column(),
  79. span.range.end().line(), span.range.end().column());
  80. };
  81. bool was_eof = false;
  82. for (auto token = lexer.next(); !was_eof; token = lexer.next()) {
  83. append_token(token.trivia(), token, true);
  84. append_token(token.value(), token, false);
  85. if (token.type() == JS::TokenType::Eof)
  86. was_eof = true;
  87. }
  88. m_client->do_set_spans(move(spans));
  89. m_has_brace_buddies = false;
  90. highlight_matching_token_pair();
  91. m_client->do_update();
  92. }
  93. Vector<Syntax::Highlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs_impl() const
  94. {
  95. static Vector<Syntax::Highlighter::MatchingTokenPair> pairs;
  96. if (pairs.is_empty()) {
  97. pairs.append({ static_cast<u64>(JS::TokenType::CurlyOpen), static_cast<u64>(JS::TokenType::CurlyClose) });
  98. pairs.append({ static_cast<u64>(JS::TokenType::ParenOpen), static_cast<u64>(JS::TokenType::ParenClose) });
  99. pairs.append({ static_cast<u64>(JS::TokenType::BracketOpen), static_cast<u64>(JS::TokenType::BracketClose) });
  100. }
  101. return pairs;
  102. }
  103. bool SyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
  104. {
  105. return static_cast<JS::TokenType>(token1) == static_cast<JS::TokenType>(token2);
  106. }
  107. SyntaxHighlighter::~SyntaxHighlighter()
  108. {
  109. }
  110. }