JSSyntaxHighlighter.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 (type) {
  36. case JS::TokenType::Invalid:
  37. case JS::TokenType::Eof:
  38. return { palette.syntax_comment() };
  39. case JS::TokenType::NumericLiteral:
  40. return { palette.syntax_number() };
  41. case JS::TokenType::StringLiteral:
  42. case JS::TokenType::TemplateLiteral:
  43. case JS::TokenType::RegexLiteral:
  44. case JS::TokenType::UnterminatedStringLiteral:
  45. return { palette.syntax_string() };
  46. case JS::TokenType::BracketClose:
  47. case JS::TokenType::BracketOpen:
  48. case JS::TokenType::Caret:
  49. case JS::TokenType::Comma:
  50. case JS::TokenType::CurlyClose:
  51. case JS::TokenType::CurlyOpen:
  52. case JS::TokenType::ParenClose:
  53. case JS::TokenType::ParenOpen:
  54. case JS::TokenType::Semicolon:
  55. return { palette.syntax_punctuation() };
  56. case JS::TokenType::Ampersand:
  57. case JS::TokenType::AmpersandEquals:
  58. case JS::TokenType::Asterisk:
  59. case JS::TokenType::AsteriskAsteriskEquals:
  60. case JS::TokenType::AsteriskEquals:
  61. case JS::TokenType::DoubleAmpersand:
  62. case JS::TokenType::DoubleAsterisk:
  63. case JS::TokenType::DoublePipe:
  64. case JS::TokenType::DoubleQuestionMark:
  65. case JS::TokenType::Equals:
  66. case JS::TokenType::EqualsEquals:
  67. case JS::TokenType::EqualsEqualsEquals:
  68. case JS::TokenType::ExclamationMark:
  69. case JS::TokenType::ExclamationMarkEquals:
  70. case JS::TokenType::ExclamationMarkEqualsEquals:
  71. case JS::TokenType::GreaterThan:
  72. case JS::TokenType::GreaterThanEquals:
  73. case JS::TokenType::LessThan:
  74. case JS::TokenType::LessThanEquals:
  75. case JS::TokenType::Minus:
  76. case JS::TokenType::MinusEquals:
  77. case JS::TokenType::MinusMinus:
  78. case JS::TokenType::Percent:
  79. case JS::TokenType::PercentEquals:
  80. case JS::TokenType::Period:
  81. case JS::TokenType::Pipe:
  82. case JS::TokenType::PipeEquals:
  83. case JS::TokenType::Plus:
  84. case JS::TokenType::PlusEquals:
  85. case JS::TokenType::PlusPlus:
  86. case JS::TokenType::QuestionMark:
  87. case JS::TokenType::QuestionMarkPeriod:
  88. case JS::TokenType::ShiftLeft:
  89. case JS::TokenType::ShiftLeftEquals:
  90. case JS::TokenType::ShiftRight:
  91. case JS::TokenType::ShiftRightEquals:
  92. case JS::TokenType::Slash:
  93. case JS::TokenType::SlashEquals:
  94. case JS::TokenType::Tilde:
  95. case JS::TokenType::UnsignedShiftRight:
  96. case JS::TokenType::UnsignedShiftRightEquals:
  97. return { palette.syntax_operator() };
  98. case JS::TokenType::BoolLiteral:
  99. case JS::TokenType::Class:
  100. case JS::TokenType::Const:
  101. case JS::TokenType::Delete:
  102. case JS::TokenType::Function:
  103. case JS::TokenType::In:
  104. case JS::TokenType::Instanceof:
  105. case JS::TokenType::Interface:
  106. case JS::TokenType::Let:
  107. case JS::TokenType::New:
  108. case JS::TokenType::NullLiteral:
  109. case JS::TokenType::Typeof:
  110. case JS::TokenType::Var:
  111. case JS::TokenType::Void:
  112. return { palette.syntax_keyword(), &Gfx::Font::default_bold_fixed_width_font() };
  113. case JS::TokenType::Await:
  114. case JS::TokenType::Catch:
  115. case JS::TokenType::Do:
  116. case JS::TokenType::Else:
  117. case JS::TokenType::Finally:
  118. case JS::TokenType::For:
  119. case JS::TokenType::If:
  120. case JS::TokenType::Return:
  121. case JS::TokenType::Try:
  122. case JS::TokenType::While:
  123. case JS::TokenType::Yield:
  124. return { palette.syntax_control_keyword(), &Gfx::Font::default_bold_fixed_width_font() };
  125. case JS::TokenType::Identifier:
  126. return { palette.syntax_identifier() };
  127. default:
  128. return { palette.base_text() };
  129. }
  130. }
  131. bool JSSyntaxHighlighter::is_identifier(void* token) const
  132. {
  133. auto js_token = static_cast<JS::TokenType>(reinterpret_cast<size_t>(token));
  134. return js_token == JS::TokenType::Identifier;
  135. }
  136. bool JSSyntaxHighlighter::is_navigatable(void* token) const
  137. {
  138. (void)token;
  139. return false;
  140. }
  141. void JSSyntaxHighlighter::rehighlight(Gfx::Palette palette)
  142. {
  143. ASSERT(m_editor);
  144. auto text = m_editor->text();
  145. JS::Lexer lexer(text);
  146. Vector<GUI::TextDocumentSpan> spans;
  147. GUI::TextPosition position { 0, 0 };
  148. GUI::TextPosition start { 0, 0 };
  149. auto advance_position = [&position](char ch) {
  150. if (ch == '\n') {
  151. position.set_line(position.line() + 1);
  152. position.set_column(0);
  153. } else
  154. position.set_column(position.column() + 1);
  155. };
  156. auto append_token = [&](StringView str, const JS::Token& token, bool is_trivia) {
  157. if (str.is_empty())
  158. return;
  159. start = position;
  160. for (size_t i = 0; i < str.length() - 1; ++i)
  161. advance_position(str[i]);
  162. GUI::TextDocumentSpan span;
  163. span.range.set_start(start);
  164. span.range.set_end({ position.line(), position.column() });
  165. auto type = is_trivia ? JS::TokenType::Invalid : token.type();
  166. auto style = style_for_token_type(palette, type);
  167. span.color = style.color;
  168. span.font = style.font;
  169. span.is_skippable = is_trivia;
  170. span.data = reinterpret_cast<void*>(static_cast<size_t>(type));
  171. spans.append(span);
  172. advance_position(str[str.length() - 1]);
  173. #ifdef DEBUG_SYNTAX_HIGHLIGHTING
  174. dbg() << token.name() << (is_trivia ? " (trivia) @ \"" : " @ \"") << token.value() << "\" "
  175. << span.range.start().line() << ":" << span.range.start().column() << " - "
  176. << span.range.end().line() << ":" << span.range.end().column();
  177. #endif
  178. };
  179. bool was_eof = false;
  180. for (auto token = lexer.next(); !was_eof; token = lexer.next()) {
  181. append_token(token.trivia(), token, true);
  182. append_token(token.value(), token, false);
  183. if (token.type() == JS::TokenType::Eof)
  184. was_eof = true;
  185. }
  186. m_editor->document().set_spans(spans);
  187. m_has_brace_buddies = false;
  188. highlight_matching_token_pair();
  189. m_editor->update();
  190. }
  191. Vector<SyntaxHighlighter::MatchingTokenPair> JSSyntaxHighlighter::matching_token_pairs() const
  192. {
  193. static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
  194. if (pairs.is_empty()) {
  195. pairs.append({ reinterpret_cast<void*>(JS::TokenType::CurlyOpen), reinterpret_cast<void*>(JS::TokenType::CurlyClose) });
  196. pairs.append({ reinterpret_cast<void*>(JS::TokenType::ParenOpen), reinterpret_cast<void*>(JS::TokenType::ParenClose) });
  197. pairs.append({ reinterpret_cast<void*>(JS::TokenType::BracketOpen), reinterpret_cast<void*>(JS::TokenType::BracketClose) });
  198. }
  199. return pairs;
  200. }
  201. bool JSSyntaxHighlighter::token_types_equal(void* token1, void* token2) const
  202. {
  203. return static_cast<JS::TokenType>(reinterpret_cast<size_t>(token1)) == static_cast<JS::TokenType>(reinterpret_cast<size_t>(token2));
  204. }
  205. JSSyntaxHighlighter::~JSSyntaxHighlighter()
  206. {
  207. }
  208. }