JSSyntaxHighlighter.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <LibGUI/JSSyntaxHighlighter.h>
  27. #include <LibGUI/TextEditor.h>
  28. #include <LibGfx/Font.h>
  29. #include <LibGfx/Palette.h>
  30. #include <LibJS/Lexer.h>
  31. #include <LibJS/Token.h>
  32. namespace GUI {
  33. static TextStyle style_for_token_type(Gfx::Palette palette, JS::TokenType type)
  34. {
  35. switch (JS::Token::category(type)) {
  36. case JS::TokenCategory::Invalid:
  37. return { palette.syntax_comment() };
  38. case JS::TokenCategory::Number:
  39. return { palette.syntax_number() };
  40. case JS::TokenCategory::String:
  41. return { palette.syntax_string() };
  42. case JS::TokenCategory::Punctuation:
  43. return { palette.syntax_punctuation() };
  44. case JS::TokenCategory::Operator:
  45. return { palette.syntax_operator() };
  46. case JS::TokenCategory::Keyword:
  47. return { palette.syntax_keyword(), &Gfx::Font::default_bold_fixed_width_font() };
  48. case JS::TokenCategory::ControlKeyword:
  49. return { palette.syntax_control_keyword(), &Gfx::Font::default_bold_fixed_width_font() };
  50. case JS::TokenCategory::Identifier:
  51. return { palette.syntax_identifier() };
  52. default:
  53. return { palette.base_text() };
  54. }
  55. }
  56. bool JSSyntaxHighlighter::is_identifier(void* token) const
  57. {
  58. auto js_token = static_cast<JS::TokenType>(reinterpret_cast<size_t>(token));
  59. return js_token == JS::TokenType::Identifier;
  60. }
  61. bool JSSyntaxHighlighter::is_navigatable([[maybe_unused]] void* token) const
  62. {
  63. return false;
  64. }
  65. void JSSyntaxHighlighter::rehighlight(Gfx::Palette palette)
  66. {
  67. ASSERT(m_editor);
  68. auto text = m_editor->text();
  69. JS::Lexer lexer(text);
  70. Vector<GUI::TextDocumentSpan> spans;
  71. GUI::TextPosition position { 0, 0 };
  72. GUI::TextPosition start { 0, 0 };
  73. auto advance_position = [&position](char ch) {
  74. if (ch == '\n') {
  75. position.set_line(position.line() + 1);
  76. position.set_column(0);
  77. } else
  78. position.set_column(position.column() + 1);
  79. };
  80. auto append_token = [&](StringView str, const JS::Token& token, bool is_trivia) {
  81. if (str.is_empty())
  82. return;
  83. start = position;
  84. for (size_t i = 0; i < str.length() - 1; ++i)
  85. advance_position(str[i]);
  86. GUI::TextDocumentSpan span;
  87. span.range.set_start(start);
  88. span.range.set_end({ position.line(), position.column() });
  89. auto type = is_trivia ? JS::TokenType::Invalid : token.type();
  90. auto style = style_for_token_type(palette, type);
  91. span.color = style.color;
  92. span.font = style.font;
  93. span.is_skippable = is_trivia;
  94. span.data = reinterpret_cast<void*>(static_cast<size_t>(type));
  95. spans.append(span);
  96. advance_position(str[str.length() - 1]);
  97. #ifdef DEBUG_SYNTAX_HIGHLIGHTING
  98. dbg() << token.name() << (is_trivia ? " (trivia) @ \"" : " @ \"") << token.value() << "\" "
  99. << span.range.start().line() << ":" << span.range.start().column() << " - "
  100. << span.range.end().line() << ":" << span.range.end().column();
  101. #endif
  102. };
  103. bool was_eof = false;
  104. for (auto token = lexer.next(); !was_eof; token = lexer.next()) {
  105. append_token(token.trivia(), token, true);
  106. append_token(token.value(), token, false);
  107. if (token.type() == JS::TokenType::Eof)
  108. was_eof = true;
  109. }
  110. m_editor->document().set_spans(spans);
  111. m_has_brace_buddies = false;
  112. highlight_matching_token_pair();
  113. m_editor->update();
  114. }
  115. Vector<SyntaxHighlighter::MatchingTokenPair> JSSyntaxHighlighter::matching_token_pairs() const
  116. {
  117. static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
  118. if (pairs.is_empty()) {
  119. pairs.append({ reinterpret_cast<void*>(JS::TokenType::CurlyOpen), reinterpret_cast<void*>(JS::TokenType::CurlyClose) });
  120. pairs.append({ reinterpret_cast<void*>(JS::TokenType::ParenOpen), reinterpret_cast<void*>(JS::TokenType::ParenClose) });
  121. pairs.append({ reinterpret_cast<void*>(JS::TokenType::BracketOpen), reinterpret_cast<void*>(JS::TokenType::BracketClose) });
  122. }
  123. return pairs;
  124. }
  125. bool JSSyntaxHighlighter::token_types_equal(void* token1, void* token2) const
  126. {
  127. return static_cast<JS::TokenType>(reinterpret_cast<size_t>(token1)) == static_cast<JS::TokenType>(reinterpret_cast<size_t>(token2));
  128. }
  129. JSSyntaxHighlighter::~JSSyntaxHighlighter()
  130. {
  131. }
  132. }