SyntaxHighlighter.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(void* token) const
  38. {
  39. auto js_token = static_cast<JS::TokenType>(reinterpret_cast<size_t>(token));
  40. return js_token == JS::TokenType::Identifier;
  41. }
  42. bool SyntaxHighlighter::is_navigatable([[maybe_unused]] void* 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() - 1; ++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 = reinterpret_cast<void*>(static_cast<size_t>(type));
  75. spans.append(span);
  76. advance_position(str[str.length() - 1]);
  77. dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "{}{} @ '{}' {}:{} - {}:{}",
  78. token.name(),
  79. is_trivia ? " (trivia)" : "",
  80. token.value(),
  81. span.range.start().line(), span.range.start().column(),
  82. span.range.end().line(), span.range.end().column());
  83. };
  84. bool was_eof = false;
  85. for (auto token = lexer.next(); !was_eof; token = lexer.next()) {
  86. append_token(token.trivia(), token, true);
  87. append_token(token.value(), token, false);
  88. if (token.type() == JS::TokenType::Eof)
  89. was_eof = true;
  90. }
  91. m_client->do_set_spans(move(spans));
  92. m_has_brace_buddies = false;
  93. highlight_matching_token_pair();
  94. m_client->do_update();
  95. }
  96. Vector<Syntax::Highlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs() const
  97. {
  98. static Vector<Syntax::Highlighter::MatchingTokenPair> pairs;
  99. if (pairs.is_empty()) {
  100. pairs.append({ reinterpret_cast<void*>(JS::TokenType::CurlyOpen), reinterpret_cast<void*>(JS::TokenType::CurlyClose) });
  101. pairs.append({ reinterpret_cast<void*>(JS::TokenType::ParenOpen), reinterpret_cast<void*>(JS::TokenType::ParenClose) });
  102. pairs.append({ reinterpret_cast<void*>(JS::TokenType::BracketOpen), reinterpret_cast<void*>(JS::TokenType::BracketClose) });
  103. }
  104. return pairs;
  105. }
  106. bool SyntaxHighlighter::token_types_equal(void* token1, void* token2) const
  107. {
  108. return static_cast<JS::TokenType>(reinterpret_cast<size_t>(token1)) == static_cast<JS::TokenType>(reinterpret_cast<size_t>(token2));
  109. }
  110. SyntaxHighlighter::~SyntaxHighlighter()
  111. {
  112. }
  113. }