SyntaxHighlighter.cpp 21 KB

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