SyntaxHighlighter.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 <AK/TemporaryChange.h>
  27. #include <LibGUI/TextEditor.h>
  28. #include <LibGfx/Font.h>
  29. #include <LibGfx/Palette.h>
  30. #include <Shell/NodeVisitor.h>
  31. #include <Shell/Parser.h>
  32. #include <Shell/SyntaxHighlighter.h>
  33. namespace Shell {
  34. enum class AugmentedTokenKind : u32 {
  35. __TokenTypeCount = (u32)AST::Node::Kind::__Count,
  36. OpenParen,
  37. CloseParen,
  38. };
  39. class HighlightVisitor : public AST::NodeVisitor {
  40. public:
  41. HighlightVisitor(Vector<GUI::TextDocumentSpan>& spans, const Gfx::Palette& palette, const GUI::TextDocument& document)
  42. : m_spans(spans)
  43. , m_palette(palette)
  44. , m_document(document)
  45. {
  46. }
  47. private:
  48. AST::Position::Line offset_line(const AST::Position::Line& line, size_t offset)
  49. {
  50. // We need to look at the line(s) above.
  51. AST::Position::Line new_line { line };
  52. while (new_line.line_column < offset) {
  53. offset -= new_line.line_column;
  54. --offset;
  55. if (new_line.line_number == 0)
  56. break;
  57. --new_line.line_number;
  58. auto line = m_document.line(new_line.line_number);
  59. new_line.line_column = line.length();
  60. }
  61. if (offset > 0)
  62. new_line.line_column -= offset;
  63. return new_line;
  64. }
  65. void set_offset_range_end(GUI::TextRange& range, const AST::Position::Line& line, size_t offset = 1)
  66. {
  67. auto new_line = offset_line(line, offset);
  68. range.set_end({ new_line.line_number, new_line.line_column });
  69. }
  70. void set_offset_range_start(GUI::TextRange& range, const AST::Position::Line& line, size_t offset = 0)
  71. {
  72. auto new_line = offset_line(line, offset);
  73. range.set_start({ new_line.line_number, new_line.line_column });
  74. }
  75. GUI::TextDocumentSpan& span_for_node(const AST::Node* node)
  76. {
  77. GUI::TextDocumentSpan span;
  78. set_offset_range_start(span.range, node->position().start_line);
  79. set_offset_range_end(span.range, node->position().end_line);
  80. span.data = (void*)static_cast<size_t>(node->kind());
  81. span.is_skippable = false;
  82. m_spans.append(move(span));
  83. return m_spans.last();
  84. }
  85. virtual void visit(const AST::PathRedirectionNode* node) override
  86. {
  87. if (node->path()->is_bareword()) {
  88. auto& span = span_for_node(node->path());
  89. span.attributes.color = m_palette.link();
  90. span.attributes.underline = true;
  91. } else {
  92. NodeVisitor::visit(node);
  93. }
  94. }
  95. virtual void visit(const AST::And* node) override
  96. {
  97. {
  98. ScopedValueRollback first_in_command { m_is_first_in_command };
  99. node->left()->visit(*this);
  100. }
  101. {
  102. ScopedValueRollback first_in_command { m_is_first_in_command };
  103. node->right()->visit(*this);
  104. }
  105. auto& span = span_for_node(node);
  106. set_offset_range_start(span.range, node->and_position().start_line);
  107. set_offset_range_end(span.range, node->and_position().end_line);
  108. span.attributes.color = m_palette.syntax_punctuation();
  109. span.attributes.bold = true;
  110. }
  111. virtual void visit(const AST::ListConcatenate* node) override
  112. {
  113. NodeVisitor::visit(node);
  114. }
  115. virtual void visit(const AST::Background* node) override
  116. {
  117. NodeVisitor::visit(node);
  118. auto& span = span_for_node(node);
  119. set_offset_range_start(span.range, node->position().end_line, 1);
  120. span.attributes.color = m_palette.syntax_punctuation();
  121. span.attributes.bold = true;
  122. }
  123. virtual void visit(const AST::BraceExpansion* node) override
  124. {
  125. NodeVisitor::visit(node);
  126. }
  127. virtual void visit(const AST::BarewordLiteral* node) override
  128. {
  129. NodeVisitor::visit(node);
  130. auto& span = span_for_node(node);
  131. if (m_is_first_in_command) {
  132. span.attributes.color = m_palette.syntax_keyword();
  133. span.attributes.bold = true;
  134. m_is_first_in_command = false;
  135. } else if (node->text().starts_with("-")) {
  136. span.attributes.color = m_palette.syntax_preprocessor_statement();
  137. }
  138. }
  139. virtual void visit(const AST::CastToCommand* node) override
  140. {
  141. NodeVisitor::visit(node);
  142. }
  143. virtual void visit(const AST::CastToList* node) override
  144. {
  145. NodeVisitor::visit(node);
  146. auto& start_span = span_for_node(node);
  147. start_span.attributes.color = m_palette.syntax_punctuation();
  148. start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 1 });
  149. start_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen);
  150. auto& end_span = span_for_node(node);
  151. end_span.attributes.color = m_palette.syntax_punctuation();
  152. set_offset_range_start(end_span.range, node->position().end_line, 1);
  153. end_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::CloseParen);
  154. }
  155. virtual void visit(const AST::CloseFdRedirection* node) override
  156. {
  157. NodeVisitor::visit(node);
  158. }
  159. virtual void visit(const AST::CommandLiteral* node) override
  160. {
  161. NodeVisitor::visit(node);
  162. }
  163. virtual void visit(const AST::Comment* node) override
  164. {
  165. NodeVisitor::visit(node);
  166. auto& span = span_for_node(node);
  167. span.attributes.color = m_palette.syntax_comment();
  168. }
  169. virtual void visit(const AST::ContinuationControl* node) override
  170. {
  171. NodeVisitor::visit(node);
  172. auto& span = span_for_node(node);
  173. span.attributes.color = m_palette.syntax_control_keyword();
  174. }
  175. virtual void visit(const AST::DynamicEvaluate* node) override
  176. {
  177. NodeVisitor::visit(node);
  178. auto& start_span = span_for_node(node);
  179. start_span.attributes.color = m_palette.syntax_punctuation();
  180. start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column });
  181. }
  182. virtual void visit(const AST::DoubleQuotedString* node) override
  183. {
  184. NodeVisitor::visit(node);
  185. auto& start_span = span_for_node(node);
  186. start_span.attributes.color = m_palette.syntax_string();
  187. start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column });
  188. start_span.is_skippable = true;
  189. auto& end_span = span_for_node(node);
  190. set_offset_range_start(end_span.range, node->position().end_line, 1);
  191. end_span.attributes.color = m_palette.syntax_string();
  192. end_span.is_skippable = true;
  193. if (m_is_first_in_command) {
  194. start_span.attributes.bold = true;
  195. end_span.attributes.bold = true;
  196. }
  197. m_is_first_in_command = false;
  198. }
  199. virtual void visit(const AST::Fd2FdRedirection* node) override
  200. {
  201. NodeVisitor::visit(node);
  202. }
  203. virtual void visit(const AST::FunctionDeclaration* node) override
  204. {
  205. NodeVisitor::visit(node);
  206. // fn name
  207. auto& name_span = span_for_node(node);
  208. set_offset_range_start(name_span.range, node->name().position.start_line);
  209. set_offset_range_end(name_span.range, node->name().position.end_line);
  210. name_span.attributes.color = m_palette.syntax_identifier();
  211. // arguments
  212. for (auto& arg : node->arguments()) {
  213. auto& name_span = span_for_node(node);
  214. set_offset_range_start(name_span.range, arg.position.start_line);
  215. set_offset_range_end(name_span.range, arg.position.end_line);
  216. name_span.attributes.color = m_palette.syntax_identifier();
  217. }
  218. }
  219. virtual void visit(const AST::ForLoop* node) override
  220. {
  221. // The iterated expression is an expression, not a command.
  222. m_is_first_in_command = false;
  223. NodeVisitor::visit(node);
  224. // "for"
  225. auto& for_span = span_for_node(node);
  226. // FIXME: "fo\\\nr" is valid too
  227. for_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 2 });
  228. for_span.attributes.color = m_palette.syntax_keyword();
  229. // "in"
  230. if (auto maybe_position = node->in_keyword_position(); maybe_position.has_value()) {
  231. auto& position = maybe_position.value();
  232. auto& in_span = span_for_node(node);
  233. set_offset_range_start(in_span.range, position.start_line);
  234. set_offset_range_end(in_span.range, position.end_line);
  235. in_span.attributes.color = m_palette.syntax_keyword();
  236. }
  237. }
  238. virtual void visit(const AST::Glob* node) override
  239. {
  240. NodeVisitor::visit(node);
  241. auto& span = span_for_node(node);
  242. span.attributes.color = m_palette.syntax_preprocessor_value();
  243. }
  244. virtual void visit(const AST::Execute* node) override
  245. {
  246. TemporaryChange first { m_is_first_in_command, true };
  247. NodeVisitor::visit(node);
  248. if (node->does_capture_stdout()) {
  249. auto& start_span = span_for_node(node);
  250. start_span.attributes.color = m_palette.syntax_punctuation();
  251. start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 1 });
  252. start_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen);
  253. auto& end_span = span_for_node(node);
  254. end_span.attributes.color = m_palette.syntax_punctuation();
  255. set_offset_range_start(end_span.range, node->position().end_line, 1);
  256. end_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::CloseParen);
  257. }
  258. }
  259. virtual void visit(const AST::IfCond* node) override
  260. {
  261. m_is_first_in_command = false;
  262. NodeVisitor::visit(node);
  263. // "if"
  264. auto& if_span = span_for_node(node);
  265. // FIXME: "i\\\nf" is valid too
  266. if_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 1 });
  267. if_span.attributes.color = m_palette.syntax_keyword();
  268. // "else"
  269. if (auto maybe_position = node->else_position(); maybe_position.has_value()) {
  270. auto& position = maybe_position.value();
  271. auto& else_span = span_for_node(node);
  272. set_offset_range_start(else_span.range, position.start_line);
  273. set_offset_range_end(else_span.range, position.end_line);
  274. else_span.attributes.color = m_palette.syntax_keyword();
  275. }
  276. }
  277. virtual void visit(const AST::ImmediateExpression* node) override
  278. {
  279. TemporaryChange first { m_is_first_in_command, false };
  280. NodeVisitor::visit(node);
  281. // ${
  282. auto& start_span = span_for_node(node);
  283. start_span.attributes.color = m_palette.syntax_punctuation();
  284. start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 1 });
  285. start_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen);
  286. // Function name
  287. auto& name_span = span_for_node(node);
  288. name_span.attributes.color = m_palette.syntax_preprocessor_statement(); // Closest thing we have to this
  289. set_offset_range_start(name_span.range, node->function_position().start_line);
  290. set_offset_range_end(name_span.range, node->function_position().end_line);
  291. // }
  292. auto& end_span = span_for_node(node);
  293. end_span.attributes.color = m_palette.syntax_punctuation();
  294. set_offset_range_start(end_span.range, node->position().end_line, 1);
  295. end_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::CloseParen);
  296. }
  297. virtual void visit(const AST::Join* node) override
  298. {
  299. NodeVisitor::visit(node);
  300. }
  301. virtual void visit(const AST::MatchExpr* node) override
  302. {
  303. // The matched expression is an expression, not a command.
  304. m_is_first_in_command = false;
  305. NodeVisitor::visit(node);
  306. // "match"
  307. auto& match_expr = span_for_node(node);
  308. // FIXME: "mat\\\nch" is valid too
  309. match_expr.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 4 });
  310. match_expr.attributes.color = m_palette.syntax_keyword();
  311. // "as"
  312. if (auto maybe_position = node->as_position(); maybe_position.has_value()) {
  313. auto& position = maybe_position.value();
  314. auto& as_span = span_for_node(node);
  315. as_span.range.set_start({ position.start_line.line_number, position.start_line.line_column });
  316. as_span.range.set_end({ position.end_line.line_number, position.end_line.line_column });
  317. as_span.attributes.color = m_palette.syntax_keyword();
  318. }
  319. }
  320. virtual void visit(const AST::Or* node) override
  321. {
  322. {
  323. ScopedValueRollback first_in_command { m_is_first_in_command };
  324. node->left()->visit(*this);
  325. }
  326. {
  327. ScopedValueRollback first_in_command { m_is_first_in_command };
  328. node->right()->visit(*this);
  329. }
  330. auto& span = span_for_node(node);
  331. set_offset_range_start(span.range, node->or_position().start_line);
  332. set_offset_range_end(span.range, node->or_position().end_line);
  333. span.attributes.color = m_palette.syntax_punctuation();
  334. span.attributes.bold = true;
  335. }
  336. virtual void visit(const AST::Pipe* node) override
  337. {
  338. NodeVisitor::visit(node);
  339. }
  340. virtual void visit(const AST::Range* node) override
  341. {
  342. NodeVisitor::visit(node);
  343. auto& start_span = span_for_node(node->start());
  344. set_offset_range_start(start_span.range, node->start()->position().start_line, 1);
  345. set_offset_range_end(start_span.range, node->start()->position().start_line, 0);
  346. start_span.attributes.color = m_palette.syntax_punctuation();
  347. auto& end_span = span_for_node(node->start());
  348. set_offset_range_start(end_span.range, node->end()->position().end_line, 1);
  349. set_offset_range_end(end_span.range, node->end()->position().end_line, 0);
  350. end_span.attributes.color = m_palette.syntax_punctuation();
  351. }
  352. virtual void visit(const AST::ReadRedirection* node) override
  353. {
  354. NodeVisitor::visit(node);
  355. }
  356. virtual void visit(const AST::ReadWriteRedirection* node) override
  357. {
  358. NodeVisitor::visit(node);
  359. }
  360. virtual void visit(const AST::Sequence* node) override
  361. {
  362. for (auto& entry : node->entries()) {
  363. ScopedValueRollback first_in_command { m_is_first_in_command };
  364. entry.visit(*this);
  365. }
  366. for (auto& position : node->separator_positions()) {
  367. if (position.start_offset == position.end_offset)
  368. continue;
  369. auto& span = span_for_node(node);
  370. set_offset_range_start(span.range, position.start_line);
  371. set_offset_range_end(span.range, position.end_line, 1);
  372. span.attributes.color = m_palette.syntax_punctuation();
  373. span.attributes.bold = true;
  374. span.is_skippable = true;
  375. }
  376. }
  377. virtual void visit(const AST::Subshell* node) override
  378. {
  379. NodeVisitor::visit(node);
  380. }
  381. virtual void visit(const AST::SimpleVariable* node) override
  382. {
  383. NodeVisitor::visit(node);
  384. auto& span = span_for_node(node);
  385. span.attributes.color = m_palette.syntax_identifier();
  386. }
  387. virtual void visit(const AST::SpecialVariable* node) override
  388. {
  389. NodeVisitor::visit(node);
  390. auto& span = span_for_node(node);
  391. span.attributes.color = m_palette.syntax_identifier();
  392. }
  393. virtual void visit(const AST::Juxtaposition* node) override
  394. {
  395. NodeVisitor::visit(node);
  396. }
  397. virtual void visit(const AST::StringLiteral* node) override
  398. {
  399. NodeVisitor::visit(node);
  400. if (node->text().is_empty())
  401. return;
  402. auto& span = span_for_node(node);
  403. span.attributes.color = m_palette.syntax_string();
  404. if (m_is_first_in_command)
  405. span.attributes.bold = true;
  406. m_is_first_in_command = false;
  407. }
  408. virtual void visit(const AST::StringPartCompose* node) override
  409. {
  410. NodeVisitor::visit(node);
  411. }
  412. virtual void visit(const AST::SyntaxError* node) override
  413. {
  414. NodeVisitor::visit(node);
  415. auto& span = span_for_node(node);
  416. span.attributes.underline = true;
  417. span.attributes.background_color = Color(Color::NamedColor::MidRed).lightened(1.3f).with_alpha(128);
  418. }
  419. virtual void visit(const AST::Tilde* node) override
  420. {
  421. NodeVisitor::visit(node);
  422. auto& span = span_for_node(node);
  423. span.attributes.color = m_palette.link();
  424. }
  425. virtual void visit(const AST::VariableDeclarations* node) override
  426. {
  427. TemporaryChange first_in_command { m_is_first_in_command, false };
  428. for (auto& decl : node->variables()) {
  429. auto& name_span = span_for_node(decl.name);
  430. name_span.attributes.color = m_palette.syntax_identifier();
  431. decl.value->visit(*this);
  432. auto& start_span = span_for_node(decl.name);
  433. start_span.range.set_start({ decl.name->position().end_line.line_number, decl.name->position().end_line.line_column });
  434. start_span.range.set_end({ decl.value->position().start_line.line_number, decl.value->position().start_line.line_column });
  435. start_span.attributes.color = m_palette.syntax_punctuation();
  436. start_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen);
  437. }
  438. }
  439. virtual void visit(const AST::WriteAppendRedirection* node) override
  440. {
  441. NodeVisitor::visit(node);
  442. }
  443. virtual void visit(const AST::WriteRedirection* node) override
  444. {
  445. NodeVisitor::visit(node);
  446. }
  447. Vector<GUI::TextDocumentSpan>& m_spans;
  448. const Gfx::Palette& m_palette;
  449. const GUI::TextDocument& m_document;
  450. bool m_is_first_in_command { false };
  451. };
  452. bool SyntaxHighlighter::is_identifier(void* token) const
  453. {
  454. if (!token)
  455. return false;
  456. auto kind = (size_t)token;
  457. return kind == (size_t)AST::Node::Kind::BarewordLiteral
  458. || kind == (size_t)AST::Node::Kind::StringLiteral
  459. || kind == (size_t)AST::Node::Kind::Tilde;
  460. }
  461. bool SyntaxHighlighter::is_navigatable(void*) const
  462. {
  463. return false;
  464. }
  465. void SyntaxHighlighter::rehighlight(const Palette& palette)
  466. {
  467. auto text = m_client->get_text();
  468. Parser parser(text);
  469. auto ast = parser.parse();
  470. Vector<GUI::TextDocumentSpan> spans;
  471. HighlightVisitor visitor { spans, palette, m_client->get_document() };
  472. if (ast)
  473. ast->visit(visitor);
  474. quick_sort(spans, [](auto& a, auto& b) { return a.range.start() < b.range.start() && a.range.end() < b.range.end(); });
  475. m_client->do_set_spans(move(spans));
  476. m_has_brace_buddies = false;
  477. highlight_matching_token_pair();
  478. m_client->do_update();
  479. }
  480. Vector<Syntax::Highlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs() const
  481. {
  482. static Vector<MatchingTokenPair> pairs;
  483. if (pairs.is_empty()) {
  484. pairs.append({
  485. (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen),
  486. (void*)static_cast<size_t>(AugmentedTokenKind::CloseParen),
  487. });
  488. }
  489. return pairs;
  490. }
  491. bool SyntaxHighlighter::token_types_equal(void* token0, void* token1) const
  492. {
  493. return token0 == token1;
  494. }
  495. SyntaxHighlighter::~SyntaxHighlighter()
  496. {
  497. }
  498. }