JSSyntaxHighlighter.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <LibJS/Lexer.h>
  30. #include <LibJS/Token.h>
  31. namespace GUI {
  32. static TextStyle style_for_token_type(JS::TokenType type)
  33. {
  34. switch (type) {
  35. case JS::TokenType::BoolLiteral:
  36. case JS::TokenType::NullLiteral:
  37. return { Color::Black, &Gfx::Font::default_bold_fixed_width_font() };
  38. case JS::TokenType::Await:
  39. case JS::TokenType::Catch:
  40. case JS::TokenType::Class:
  41. case JS::TokenType::Const:
  42. case JS::TokenType::Delete:
  43. case JS::TokenType::Do:
  44. case JS::TokenType::Else:
  45. case JS::TokenType::Finally:
  46. case JS::TokenType::For:
  47. case JS::TokenType::Function:
  48. case JS::TokenType::If:
  49. case JS::TokenType::In:
  50. case JS::TokenType::Instanceof:
  51. case JS::TokenType::Interface:
  52. case JS::TokenType::Let:
  53. case JS::TokenType::New:
  54. case JS::TokenType::QuestionMark:
  55. case JS::TokenType::Return:
  56. case JS::TokenType::Try:
  57. case JS::TokenType::Typeof:
  58. case JS::TokenType::Var:
  59. case JS::TokenType::Void:
  60. case JS::TokenType::While:
  61. case JS::TokenType::Yield:
  62. return { Color::Black, &Gfx::Font::default_bold_fixed_width_font() };
  63. case JS::TokenType::Identifier:
  64. return { Color::from_rgb(0x092e64) };
  65. case JS::TokenType::NumericLiteral:
  66. case JS::TokenType::StringLiteral:
  67. case JS::TokenType::RegexLiteral:
  68. return { Color::from_rgb(0x800000) };
  69. case JS::TokenType::Invalid:
  70. case JS::TokenType::Eof:
  71. return { Color::from_rgb(0x008000) };
  72. default:
  73. return { Color::Black };
  74. }
  75. }
  76. bool JSSyntaxHighlighter::is_identifier(void* token) const
  77. {
  78. auto js_token = static_cast<JS::TokenType>(reinterpret_cast<size_t>(token));
  79. return js_token == JS::TokenType::Identifier;
  80. }
  81. bool JSSyntaxHighlighter::is_navigatable(void* token) const
  82. {
  83. (void)token;
  84. return false;
  85. }
  86. void JSSyntaxHighlighter::rehighlight()
  87. {
  88. ASSERT(m_editor);
  89. auto text = m_editor->text();
  90. JS::Lexer lexer(text);
  91. Vector<GUI::TextDocumentSpan> spans;
  92. GUI::TextPosition position { 0, 0 };
  93. GUI::TextPosition start { 0, 0 };
  94. auto advance_position = [&](char ch) {
  95. if (ch == '\n') {
  96. position.set_line(position.line() + 1);
  97. position.set_column(0);
  98. } else
  99. position.set_column(position.column() + 1);
  100. };
  101. bool was_eof = false;
  102. for (auto token = lexer.next(); !was_eof; token = lexer.next()) {
  103. start = position;
  104. if (!token.trivia().is_empty()) {
  105. for (auto ch : token.trivia())
  106. advance_position(ch);
  107. GUI::TextDocumentSpan span;
  108. span.range.set_start(start);
  109. if (position.column() > 0)
  110. span.range.set_end({ position.line(), position.column() - 1 });
  111. else
  112. span.range.set_end({ position.line() - 1, 0 });
  113. auto style = style_for_token_type(JS::TokenType::Invalid);
  114. span.color = style.color;
  115. span.font = style.font;
  116. span.is_skippable = true;
  117. spans.append(span);
  118. #ifdef DEBUG_SYNTAX_HIGHLIGHTING
  119. dbg() << token.name() << " \"" << token.trivia() << "\" (trivia) @ " << span.range.start().line() << ":" << span.range.start().column() << " - " << span.range.end().line() << ":" << span.range.end().column();
  120. #endif
  121. }
  122. start = position;
  123. if (!token.value().is_empty()) {
  124. for (auto ch : token.value())
  125. advance_position(ch);
  126. GUI::TextDocumentSpan span;
  127. span.range.set_start(start);
  128. if (position.column() > 0)
  129. span.range.set_end({ position.line(), position.column() - 1 });
  130. else
  131. span.range.set_end({ position.line() - 1, 0 });
  132. auto style = style_for_token_type(token.type());
  133. span.color = style.color;
  134. span.font = style.font;
  135. span.is_skippable = false;
  136. span.data = reinterpret_cast<void*>(token.type());
  137. spans.append(span);
  138. #ifdef DEBUG_SYNTAX_HIGHLIGHTING
  139. dbg() << token.name() << " @ \"" << token.value() << "\" " << span.range.start().line() << ":" << span.range.start().column() << " - " << span.range.end().line() << ":" << span.range.end().column();
  140. #endif
  141. }
  142. if (token.type() == JS::TokenType::Eof)
  143. was_eof = true;
  144. }
  145. m_editor->document().set_spans(spans);
  146. m_has_brace_buddies = false;
  147. highlight_matching_token_pair();
  148. m_editor->update();
  149. }
  150. Vector<SyntaxHighlighter::MatchingTokenPair> JSSyntaxHighlighter::matching_token_pairs() const
  151. {
  152. static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
  153. if (pairs.is_empty()) {
  154. pairs.append({ reinterpret_cast<void*>(JS::TokenType::CurlyOpen), reinterpret_cast<void*>(JS::TokenType::CurlyClose) });
  155. pairs.append({ reinterpret_cast<void*>(JS::TokenType::ParenOpen), reinterpret_cast<void*>(JS::TokenType::ParenClose) });
  156. pairs.append({ reinterpret_cast<void*>(JS::TokenType::BracketOpen), reinterpret_cast<void*>(JS::TokenType::BracketClose) });
  157. }
  158. return pairs;
  159. }
  160. bool JSSyntaxHighlighter::token_types_equal(void* token1, void* token2) const
  161. {
  162. return static_cast<JS::TokenType>(reinterpret_cast<size_t>(token1)) == static_cast<JS::TokenType>(reinterpret_cast<size_t>(token2));
  163. }
  164. JSSyntaxHighlighter::~JSSyntaxHighlighter()
  165. {
  166. }
  167. }