SyntaxHighlighter.cpp 4.1 KB

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