JSSyntaxHighlighter.cpp 8.6 KB

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