JSSyntaxHighlighter.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <LibGUI/JSSyntaxHighlighter.h>
  28. #include <LibGUI/TextEditor.h>
  29. #include <LibGfx/Font.h>
  30. #include <LibGfx/Palette.h>
  31. #include <LibJS/Lexer.h>
  32. #include <LibJS/Token.h>
  33. namespace GUI {
  34. static Syntax::TextStyle style_for_token_type(Gfx::Palette palette, JS::TokenType type)
  35. {
  36. switch (JS::Token::category(type)) {
  37. case JS::TokenCategory::Invalid:
  38. return { palette.syntax_comment() };
  39. case JS::TokenCategory::Number:
  40. return { palette.syntax_number() };
  41. case JS::TokenCategory::String:
  42. return { palette.syntax_string() };
  43. case JS::TokenCategory::Punctuation:
  44. return { palette.syntax_punctuation() };
  45. case JS::TokenCategory::Operator:
  46. return { palette.syntax_operator() };
  47. case JS::TokenCategory::Keyword:
  48. return { palette.syntax_keyword(), true };
  49. case JS::TokenCategory::ControlKeyword:
  50. return { palette.syntax_control_keyword(), true };
  51. case JS::TokenCategory::Identifier:
  52. return { palette.syntax_identifier() };
  53. default:
  54. return { palette.base_text() };
  55. }
  56. }
  57. bool JSSyntaxHighlighter::is_identifier(void* token) const
  58. {
  59. auto js_token = static_cast<JS::TokenType>(reinterpret_cast<size_t>(token));
  60. return js_token == JS::TokenType::Identifier;
  61. }
  62. bool JSSyntaxHighlighter::is_navigatable([[maybe_unused]] void* token) const
  63. {
  64. return false;
  65. }
  66. void JSSyntaxHighlighter::rehighlight(Gfx::Palette palette)
  67. {
  68. ASSERT(m_editor);
  69. auto text = m_editor->text();
  70. JS::Lexer lexer(text);
  71. Vector<GUI::TextDocumentSpan> spans;
  72. GUI::TextPosition position { 0, 0 };
  73. GUI::TextPosition start { 0, 0 };
  74. auto advance_position = [&position](char ch) {
  75. if (ch == '\n') {
  76. position.set_line(position.line() + 1);
  77. position.set_column(0);
  78. } else
  79. position.set_column(position.column() + 1);
  80. };
  81. auto append_token = [&](StringView str, const JS::Token& token, bool is_trivia) {
  82. if (str.is_empty())
  83. return;
  84. start = position;
  85. for (size_t i = 0; i < str.length() - 1; ++i)
  86. advance_position(str[i]);
  87. GUI::TextDocumentSpan span;
  88. span.range.set_start(start);
  89. span.range.set_end({ position.line(), position.column() });
  90. auto type = is_trivia ? JS::TokenType::Invalid : token.type();
  91. auto style = style_for_token_type(palette, type);
  92. span.attributes.color = style.color;
  93. span.attributes.bold = style.bold;
  94. span.is_skippable = is_trivia;
  95. span.data = reinterpret_cast<void*>(static_cast<size_t>(type));
  96. spans.append(span);
  97. advance_position(str[str.length() - 1]);
  98. dbgln<SYNTAX_HIGHLIGHTING_DEBUG>("{}{} @ '{}' {}:{} - {}:{}",
  99. token.name(),
  100. is_trivia ? " (trivia)" : "",
  101. token.value(),
  102. span.range.start().line(), span.range.start().column(),
  103. span.range.end().line(), span.range.end().column());
  104. };
  105. bool was_eof = false;
  106. for (auto token = lexer.next(); !was_eof; token = lexer.next()) {
  107. append_token(token.trivia(), token, true);
  108. append_token(token.value(), token, false);
  109. if (token.type() == JS::TokenType::Eof)
  110. was_eof = true;
  111. }
  112. m_editor->document().set_spans(spans);
  113. m_has_brace_buddies = false;
  114. highlight_matching_token_pair();
  115. m_editor->update();
  116. }
  117. Vector<Syntax::Highlighter::MatchingTokenPair> JSSyntaxHighlighter::matching_token_pairs() const
  118. {
  119. static Vector<Syntax::Highlighter::MatchingTokenPair> pairs;
  120. if (pairs.is_empty()) {
  121. pairs.append({ reinterpret_cast<void*>(JS::TokenType::CurlyOpen), reinterpret_cast<void*>(JS::TokenType::CurlyClose) });
  122. pairs.append({ reinterpret_cast<void*>(JS::TokenType::ParenOpen), reinterpret_cast<void*>(JS::TokenType::ParenClose) });
  123. pairs.append({ reinterpret_cast<void*>(JS::TokenType::BracketOpen), reinterpret_cast<void*>(JS::TokenType::BracketClose) });
  124. }
  125. return pairs;
  126. }
  127. bool JSSyntaxHighlighter::token_types_equal(void* token1, void* token2) const
  128. {
  129. return static_cast<JS::TokenType>(reinterpret_cast<size_t>(token1)) == static_cast<JS::TokenType>(reinterpret_cast<size_t>(token2));
  130. }
  131. JSSyntaxHighlighter::~JSSyntaxHighlighter()
  132. {
  133. }
  134. }