Highlighter.cpp 4.3 KB

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