SyntaxHighlighter.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SyntaxHighlighter.h"
  7. #include <LibCMake/Lexer.h>
  8. #include <LibCMake/Token.h>
  9. namespace CMake {
  10. static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token::Type type)
  11. {
  12. switch (type) {
  13. case Token::Type::BracketComment:
  14. case Token::Type::LineComment:
  15. return { palette.syntax_comment() };
  16. case Token::Type::Identifier:
  17. return { palette.syntax_function() };
  18. case Token::Type::ControlKeyword:
  19. return { palette.syntax_control_keyword() };
  20. case Token::Type::OpenParen:
  21. case Token::Type::CloseParen:
  22. return { palette.syntax_punctuation() };
  23. case Token::Type::BracketArgument:
  24. return { palette.syntax_parameter() };
  25. case Token::Type::QuotedArgument:
  26. return { palette.syntax_string() };
  27. case Token::Type::UnquotedArgument:
  28. return { palette.syntax_parameter() };
  29. case Token::Type::Garbage:
  30. return { palette.red() };
  31. case Token::Type::VariableReference:
  32. // This is a bit arbitrary, since we don't have a color specifically for this.
  33. return { palette.syntax_preprocessor_value() };
  34. default:
  35. return { palette.base_text() };
  36. }
  37. }
  38. bool SyntaxHighlighter::is_identifier(u64 token_type) const
  39. {
  40. auto cmake_token = static_cast<Token::Type>(token_type);
  41. return cmake_token == Token::Type::Identifier;
  42. }
  43. void SyntaxHighlighter::rehighlight(Gfx::Palette const& palette)
  44. {
  45. auto text = m_client->get_text();
  46. auto tokens = Lexer::lex(text).release_value_but_fixme_should_propagate_errors();
  47. auto& document = m_client->get_document();
  48. struct OpenBlock {
  49. Token token;
  50. int open_paren_count { 0 };
  51. Optional<Token> ending_paren {};
  52. };
  53. Vector<OpenBlock> open_blocks;
  54. Vector<GUI::TextDocumentFoldingRegion> folding_regions;
  55. Vector<GUI::TextDocumentSpan> spans;
  56. auto highlight_span = [&](Token::Type type, Position const& start, Position const& end) {
  57. GUI::TextDocumentSpan span;
  58. span.range.set_start({ start.line, start.column });
  59. span.range.set_end({ end.line, end.column });
  60. if (!span.range.is_valid())
  61. return;
  62. auto style = style_for_token_type(palette, type);
  63. span.attributes.color = style.color;
  64. span.attributes.bold = style.bold;
  65. if (type == Token::Type::Garbage) {
  66. span.attributes.underline = true;
  67. span.attributes.underline_color = palette.red();
  68. span.attributes.underline_style = Gfx::TextAttributes::UnderlineStyle::Wavy;
  69. }
  70. span.is_skippable = false;
  71. span.data = static_cast<u64>(type);
  72. spans.append(move(span));
  73. };
  74. auto create_region_from_block_type = [&](auto control_keywords, Token const& end_token) {
  75. if (open_blocks.is_empty())
  76. return;
  77. // Find the most recent open block with a matching keyword.
  78. Optional<size_t> found_index;
  79. OpenBlock open_block;
  80. for (int i = open_blocks.size() - 1; i >= 0; i--) {
  81. for (auto value : control_keywords) {
  82. if (open_blocks[i].token.control_keyword == value) {
  83. found_index = i;
  84. open_block = open_blocks[i];
  85. break;
  86. }
  87. }
  88. if (found_index.has_value())
  89. break;
  90. }
  91. if (found_index.has_value()) {
  92. // Remove the found token and all after it.
  93. open_blocks.shrink(found_index.value());
  94. // Create a region.
  95. GUI::TextDocumentFoldingRegion region;
  96. if (open_block.ending_paren.has_value()) {
  97. region.range.set_start({ open_block.ending_paren->end.line, open_block.ending_paren->end.column });
  98. } else {
  99. // The opening command is invalid, it does not have a closing paren.
  100. // So, we just start the region at the end of the line where the command identifier was. (eg, `if`)
  101. region.range.set_start({ open_block.token.end.line, document.line(open_block.token.end.line).last_non_whitespace_column().value() });
  102. }
  103. region.range.set_end({ end_token.start.line, end_token.start.column });
  104. folding_regions.append(move(region));
  105. }
  106. };
  107. for (auto const& token : tokens) {
  108. if (token.type == Token::Type::QuotedArgument || token.type == Token::Type::UnquotedArgument) {
  109. // Alternately highlight the regular/variable-reference parts.
  110. // 0-length ranges are caught in highlight_span() so we don't have to worry about them.
  111. Position previous_position = token.start;
  112. for (auto const& reference : token.variable_references) {
  113. highlight_span(token.type, previous_position, reference.start);
  114. highlight_span(Token::Type::VariableReference, reference.start, reference.end);
  115. previous_position = reference.end;
  116. }
  117. highlight_span(token.type, previous_position, token.end);
  118. continue;
  119. }
  120. highlight_span(token.type, token.start, token.end);
  121. if (!open_blocks.is_empty() && !open_blocks.last().ending_paren.has_value()) {
  122. auto& open_block = open_blocks.last();
  123. if (token.type == Token::Type::OpenParen) {
  124. open_block.open_paren_count++;
  125. } else if (token.type == Token::Type::CloseParen) {
  126. open_block.open_paren_count--;
  127. if (open_block.open_paren_count == 0)
  128. open_block.ending_paren = token;
  129. }
  130. }
  131. // Create folding regions from control-keyword blocks.
  132. if (token.type == Token::Type::ControlKeyword) {
  133. switch (token.control_keyword.value()) {
  134. case ControlKeywordType::If:
  135. open_blocks.empend(token);
  136. break;
  137. case ControlKeywordType::ElseIf:
  138. case ControlKeywordType::Else:
  139. create_region_from_block_type(Array { ControlKeywordType::If, ControlKeywordType::ElseIf }, token);
  140. open_blocks.empend(token);
  141. break;
  142. case ControlKeywordType::EndIf:
  143. create_region_from_block_type(Array { ControlKeywordType::If, ControlKeywordType::ElseIf, ControlKeywordType::Else }, token);
  144. break;
  145. case ControlKeywordType::ForEach:
  146. open_blocks.empend(token);
  147. break;
  148. case ControlKeywordType::EndForEach:
  149. create_region_from_block_type(Array { ControlKeywordType::ForEach }, token);
  150. break;
  151. case ControlKeywordType::While:
  152. open_blocks.empend(token);
  153. break;
  154. case ControlKeywordType::EndWhile:
  155. create_region_from_block_type(Array { ControlKeywordType::While }, token);
  156. break;
  157. case ControlKeywordType::Macro:
  158. open_blocks.empend(token);
  159. break;
  160. case ControlKeywordType::EndMacro:
  161. create_region_from_block_type(Array { ControlKeywordType::Macro }, token);
  162. break;
  163. case ControlKeywordType::Function:
  164. open_blocks.empend(token);
  165. break;
  166. case ControlKeywordType::EndFunction:
  167. create_region_from_block_type(Array { ControlKeywordType::Function }, token);
  168. break;
  169. case ControlKeywordType::Block:
  170. open_blocks.empend(token);
  171. break;
  172. case ControlKeywordType::EndBlock:
  173. create_region_from_block_type(Array { ControlKeywordType::Block }, token);
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. }
  180. m_client->do_set_spans(move(spans));
  181. m_client->do_set_folding_regions(move(folding_regions));
  182. m_has_brace_buddies = false;
  183. highlight_matching_token_pair();
  184. m_client->do_update();
  185. }
  186. Vector<SyntaxHighlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs_impl() const
  187. {
  188. static Vector<MatchingTokenPair> pairs;
  189. if (pairs.is_empty()) {
  190. pairs.append({ static_cast<u64>(Token::Type::OpenParen), static_cast<u64>(Token::Type::CloseParen) });
  191. }
  192. return pairs;
  193. }
  194. bool SyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
  195. {
  196. return static_cast<Token::Type>(token1) == static_cast<Token::Type>(token2);
  197. }
  198. }