Highlighter.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/TextEditor.h>
  7. #include <LibGfx/Color.h>
  8. #include <LibSyntax/Highlighter.h>
  9. namespace Syntax {
  10. Highlighter::~Highlighter()
  11. {
  12. }
  13. void Highlighter::highlight_matching_token_pair()
  14. {
  15. auto& document = m_client->get_document();
  16. enum class Direction {
  17. Forward,
  18. Backward,
  19. };
  20. auto find_span_of_type = [&](auto i, u64 type, u64 not_type, Direction direction) -> Optional<size_t> {
  21. size_t nesting_level = 0;
  22. bool forward = direction == Direction::Forward;
  23. if (forward) {
  24. ++i;
  25. if (i >= document.spans().size())
  26. return {};
  27. } else {
  28. if (i == 0)
  29. return {};
  30. --i;
  31. }
  32. for (;;) {
  33. auto& span = document.spans().at(i);
  34. auto span_token_type = span.data;
  35. if (token_types_equal(span_token_type, not_type)) {
  36. ++nesting_level;
  37. } else if (token_types_equal(span_token_type, type)) {
  38. if (nesting_level-- <= 0)
  39. return i;
  40. }
  41. if (forward) {
  42. ++i;
  43. if (i >= document.spans().size())
  44. return {};
  45. } else {
  46. if (i == 0)
  47. return {};
  48. --i;
  49. }
  50. }
  51. return {};
  52. };
  53. auto make_buddies = [&](int index0, int index1) {
  54. auto& buddy0 = document.spans()[index0];
  55. auto& buddy1 = document.spans()[index1];
  56. m_has_brace_buddies = true;
  57. m_brace_buddies[0].index = index0;
  58. m_brace_buddies[1].index = index1;
  59. m_brace_buddies[0].span_backup = buddy0;
  60. m_brace_buddies[1].span_backup = buddy1;
  61. buddy0.attributes.background_color = Color::DarkCyan;
  62. buddy1.attributes.background_color = Color::DarkCyan;
  63. buddy0.attributes.color = Color::White;
  64. buddy1.attributes.color = Color::White;
  65. m_client->do_update();
  66. };
  67. auto pairs = matching_token_pairs();
  68. for (size_t i = 0; i < document.spans().size(); ++i) {
  69. auto& span = const_cast<GUI::TextDocumentSpan&>(document.spans().at(i));
  70. auto token_type = span.data;
  71. for (auto& pair : pairs) {
  72. if (token_types_equal(token_type, pair.open) && span.range.start() == m_client->get_cursor()) {
  73. auto buddy = find_span_of_type(i, pair.close, pair.open, Direction::Forward);
  74. if (buddy.has_value())
  75. make_buddies(i, buddy.value());
  76. return;
  77. }
  78. }
  79. auto right_of_end = span.range.end();
  80. right_of_end.set_column(right_of_end.column() + 1);
  81. for (auto& pair : pairs) {
  82. if (token_types_equal(token_type, pair.close) && right_of_end == m_client->get_cursor()) {
  83. auto buddy = find_span_of_type(i, pair.open, pair.close, Direction::Backward);
  84. if (buddy.has_value())
  85. make_buddies(i, buddy.value());
  86. return;
  87. }
  88. }
  89. }
  90. }
  91. void Highlighter::attach(HighlighterClient& client)
  92. {
  93. VERIFY(!m_client);
  94. m_client = &client;
  95. }
  96. void Highlighter::detach()
  97. {
  98. m_client = nullptr;
  99. }
  100. void Highlighter::cursor_did_change()
  101. {
  102. auto& document = m_client->get_document();
  103. if (m_has_brace_buddies) {
  104. if (m_brace_buddies[0].index >= 0 && m_brace_buddies[0].index < static_cast<int>(document.spans().size()))
  105. document.set_span_at_index(m_brace_buddies[0].index, m_brace_buddies[0].span_backup);
  106. if (m_brace_buddies[1].index >= 0 && m_brace_buddies[1].index < static_cast<int>(document.spans().size()))
  107. document.set_span_at_index(m_brace_buddies[1].index, m_brace_buddies[1].span_backup);
  108. m_has_brace_buddies = false;
  109. m_client->do_update();
  110. }
  111. highlight_matching_token_pair();
  112. }
  113. Vector<Highlighter::MatchingTokenPair> Highlighter::matching_token_pairs() const
  114. {
  115. auto own_pairs = matching_token_pairs_impl();
  116. own_pairs.ensure_capacity(own_pairs.size() + m_nested_token_pairs.size());
  117. for (auto& nested_pair : m_nested_token_pairs)
  118. own_pairs.append(nested_pair);
  119. return own_pairs;
  120. }
  121. void Highlighter::register_nested_token_pairs(Vector<MatchingTokenPair> pairs)
  122. {
  123. for (auto& pair : pairs)
  124. m_nested_token_pairs.set(pair);
  125. }
  126. }