JSSyntaxHighlighter.cpp 8.1 KB

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