SyntaxHighlighter.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Gfx::TextAttributes 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(), {}, false, Gfx::TextAttributes::UnderlineStyle::Wavy, 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<Syntax::TextDocumentFoldingRegion> folding_regions;
  55. Vector<Syntax::TextDocumentSpan> spans;
  56. auto highlight_span = [&](Token::Type type, Position const& start, Position const& end) {
  57. Syntax::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. span.attributes = style_for_token_type(palette, type);
  63. span.is_skippable = false;
  64. span.data = static_cast<u64>(type);
  65. spans.append(move(span));
  66. };
  67. auto create_region_from_block_type = [&](auto control_keywords, Token const& end_token) {
  68. if (open_blocks.is_empty())
  69. return;
  70. // Find the most recent open block with a matching keyword.
  71. Optional<size_t> found_index;
  72. OpenBlock open_block;
  73. for (int i = open_blocks.size() - 1; i >= 0; i--) {
  74. for (auto value : control_keywords) {
  75. if (open_blocks[i].token.control_keyword == value) {
  76. found_index = i;
  77. open_block = open_blocks[i];
  78. break;
  79. }
  80. }
  81. if (found_index.has_value())
  82. break;
  83. }
  84. if (found_index.has_value()) {
  85. // Remove the found token and all after it.
  86. open_blocks.shrink(found_index.value());
  87. // Create a region.
  88. Syntax::TextDocumentFoldingRegion region;
  89. if (open_block.ending_paren.has_value()) {
  90. region.range.set_start({ open_block.ending_paren->end.line, open_block.ending_paren->end.column });
  91. } else {
  92. // The opening command is invalid, it does not have a closing paren.
  93. // So, we just start the region at the end of the line where the command identifier was. (eg, `if`)
  94. region.range.set_start({ open_block.token.end.line, document.line(open_block.token.end.line).last_non_whitespace_column().value() });
  95. }
  96. region.range.set_end({ end_token.start.line, end_token.start.column });
  97. folding_regions.append(move(region));
  98. }
  99. };
  100. for (auto const& token : tokens) {
  101. if (token.type == Token::Type::QuotedArgument || token.type == Token::Type::UnquotedArgument) {
  102. // Alternately highlight the regular/variable-reference parts.
  103. // 0-length ranges are caught in highlight_span() so we don't have to worry about them.
  104. Position previous_position = token.start;
  105. for (auto const& reference : token.variable_references) {
  106. highlight_span(token.type, previous_position, reference.start);
  107. highlight_span(Token::Type::VariableReference, reference.start, reference.end);
  108. previous_position = reference.end;
  109. }
  110. highlight_span(token.type, previous_position, token.end);
  111. continue;
  112. }
  113. highlight_span(token.type, token.start, token.end);
  114. if (!open_blocks.is_empty() && !open_blocks.last().ending_paren.has_value()) {
  115. auto& open_block = open_blocks.last();
  116. if (token.type == Token::Type::OpenParen) {
  117. open_block.open_paren_count++;
  118. } else if (token.type == Token::Type::CloseParen) {
  119. open_block.open_paren_count--;
  120. if (open_block.open_paren_count == 0)
  121. open_block.ending_paren = token;
  122. }
  123. }
  124. // Create folding regions from control-keyword blocks.
  125. if (token.type == Token::Type::ControlKeyword) {
  126. switch (token.control_keyword.value()) {
  127. case ControlKeywordType::If:
  128. open_blocks.empend(token);
  129. break;
  130. case ControlKeywordType::ElseIf:
  131. case ControlKeywordType::Else:
  132. create_region_from_block_type(Array { ControlKeywordType::If, ControlKeywordType::ElseIf }, token);
  133. open_blocks.empend(token);
  134. break;
  135. case ControlKeywordType::EndIf:
  136. create_region_from_block_type(Array { ControlKeywordType::If, ControlKeywordType::ElseIf, ControlKeywordType::Else }, token);
  137. break;
  138. case ControlKeywordType::ForEach:
  139. open_blocks.empend(token);
  140. break;
  141. case ControlKeywordType::EndForEach:
  142. create_region_from_block_type(Array { ControlKeywordType::ForEach }, token);
  143. break;
  144. case ControlKeywordType::While:
  145. open_blocks.empend(token);
  146. break;
  147. case ControlKeywordType::EndWhile:
  148. create_region_from_block_type(Array { ControlKeywordType::While }, token);
  149. break;
  150. case ControlKeywordType::Macro:
  151. open_blocks.empend(token);
  152. break;
  153. case ControlKeywordType::EndMacro:
  154. create_region_from_block_type(Array { ControlKeywordType::Macro }, token);
  155. break;
  156. case ControlKeywordType::Function:
  157. open_blocks.empend(token);
  158. break;
  159. case ControlKeywordType::EndFunction:
  160. create_region_from_block_type(Array { ControlKeywordType::Function }, token);
  161. break;
  162. case ControlKeywordType::Block:
  163. open_blocks.empend(token);
  164. break;
  165. case ControlKeywordType::EndBlock:
  166. create_region_from_block_type(Array { ControlKeywordType::Block }, token);
  167. break;
  168. default:
  169. break;
  170. }
  171. }
  172. }
  173. m_client->do_set_spans(move(spans));
  174. m_client->do_set_folding_regions(move(folding_regions));
  175. m_has_brace_buddies = false;
  176. highlight_matching_token_pair();
  177. m_client->do_update();
  178. }
  179. Vector<SyntaxHighlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs_impl() const
  180. {
  181. static Vector<MatchingTokenPair> pairs;
  182. if (pairs.is_empty()) {
  183. pairs.append({ static_cast<u64>(Token::Type::OpenParen), static_cast<u64>(Token::Type::CloseParen) });
  184. }
  185. return pairs;
  186. }
  187. bool SyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
  188. {
  189. return static_cast<Token::Type>(token1) == static_cast<Token::Type>(token2);
  190. }
  191. }