SyntaxHighlighter.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/SyntaxHighlighter.h>
  27. #include <LibGUI/TextEditor.h>
  28. namespace GUI {
  29. SyntaxHighlighter::~SyntaxHighlighter()
  30. {
  31. }
  32. void SyntaxHighlighter::highlight_matching_token_pair()
  33. {
  34. ASSERT(m_editor);
  35. auto& document = m_editor->document();
  36. enum class Direction {
  37. Forward,
  38. Backward,
  39. };
  40. auto find_span_of_type = [&](auto i, void* type, void* not_type, Direction direction) -> Optional<size_t> {
  41. size_t nesting_level = 0;
  42. bool forward = direction == Direction::Forward;
  43. if (forward) {
  44. ++i;
  45. if (i >= document.spans().size())
  46. return {};
  47. } else {
  48. if (i == 0)
  49. return {};
  50. --i;
  51. }
  52. for (;;) {
  53. auto& span = document.spans().at(i);
  54. auto span_token_type = span.data;
  55. if (token_types_equal(span_token_type, not_type)) {
  56. ++nesting_level;
  57. } else if (token_types_equal(span_token_type, type)) {
  58. if (nesting_level-- <= 0)
  59. return i;
  60. }
  61. if (forward) {
  62. ++i;
  63. if (i >= document.spans().size())
  64. return {};
  65. } else {
  66. if (i == 0)
  67. return {};
  68. --i;
  69. }
  70. }
  71. return {};
  72. };
  73. auto make_buddies = [&](int index0, int index1) {
  74. auto& buddy0 = document.spans()[index0];
  75. auto& buddy1 = document.spans()[index1];
  76. m_has_brace_buddies = true;
  77. m_brace_buddies[0].index = index0;
  78. m_brace_buddies[1].index = index1;
  79. m_brace_buddies[0].span_backup = buddy0;
  80. m_brace_buddies[1].span_backup = buddy1;
  81. buddy0.background_color = Color::DarkCyan;
  82. buddy1.background_color = Color::DarkCyan;
  83. buddy0.color = Color::White;
  84. buddy1.color = Color::White;
  85. m_editor->update();
  86. };
  87. auto pairs = matching_token_pairs();
  88. for (size_t i = 0; i < document.spans().size(); ++i) {
  89. auto& span = const_cast<GUI::TextDocumentSpan&>(document.spans().at(i));
  90. auto token_type = span.data;
  91. for (auto& pair : pairs) {
  92. if (token_types_equal(token_type, pair.open) && span.range.start() == m_editor->cursor()) {
  93. auto buddy = find_span_of_type(i, pair.close, pair.open, Direction::Forward);
  94. if (buddy.has_value())
  95. make_buddies(i, buddy.value());
  96. return;
  97. }
  98. }
  99. auto right_of_end = span.range.end();
  100. right_of_end.set_column(right_of_end.column() + 1);
  101. for (auto& pair : pairs) {
  102. if (token_types_equal(token_type, pair.close) && right_of_end == m_editor->cursor()) {
  103. auto buddy = find_span_of_type(i, pair.open, pair.close, Direction::Backward);
  104. if (buddy.has_value())
  105. make_buddies(i, buddy.value());
  106. return;
  107. }
  108. }
  109. }
  110. }
  111. void SyntaxHighlighter::attach(TextEditor& editor)
  112. {
  113. ASSERT(!m_editor);
  114. m_editor = editor.make_weak_ptr();
  115. }
  116. void SyntaxHighlighter::detach()
  117. {
  118. ASSERT(m_editor);
  119. m_editor = nullptr;
  120. }
  121. void SyntaxHighlighter::cursor_did_change()
  122. {
  123. ASSERT(m_editor);
  124. auto& document = m_editor->document();
  125. if (m_has_brace_buddies) {
  126. if (m_brace_buddies[0].index >= 0 && m_brace_buddies[0].index < static_cast<int>(document.spans().size()))
  127. document.set_span_at_index(m_brace_buddies[0].index, m_brace_buddies[0].span_backup);
  128. if (m_brace_buddies[1].index >= 0 && m_brace_buddies[1].index < static_cast<int>(document.spans().size()))
  129. document.set_span_at_index(m_brace_buddies[1].index, m_brace_buddies[1].span_backup);
  130. m_has_brace_buddies = false;
  131. m_editor->update();
  132. }
  133. highlight_matching_token_pair();
  134. }
  135. }