Serenity Operating System
at hosted 155 lines 5.3 kB view raw
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 27#include <LibGUI/SyntaxHighlighter.h> 28#include <LibGUI/TextEditor.h> 29 30namespace GUI { 31 32SyntaxHighlighter::~SyntaxHighlighter() 33{ 34} 35 36void SyntaxHighlighter::highlight_matching_token_pair() 37{ 38 ASSERT(m_editor); 39 auto& document = m_editor->document(); 40 41 enum class Direction { 42 Forward, 43 Backward, 44 }; 45 46 auto find_span_of_type = [&](auto i, void* type, void* not_type, Direction direction) -> Optional<size_t> { 47 size_t nesting_level = 0; 48 bool forward = direction == Direction::Forward; 49 50 if (forward) { 51 ++i; 52 if (i >= document.spans().size()) 53 return {}; 54 } else { 55 if (i == 0) 56 return {}; 57 --i; 58 } 59 60 for (;;) { 61 auto& span = document.spans().at(i); 62 auto span_token_type = span.data; 63 if (token_types_equal(span_token_type, not_type)) { 64 ++nesting_level; 65 } else if (token_types_equal(span_token_type, type)) { 66 if (nesting_level-- <= 0) 67 return i; 68 } 69 70 if (forward) { 71 ++i; 72 if (i >= document.spans().size()) 73 return {}; 74 } else { 75 if (i == 0) 76 return {}; 77 --i; 78 } 79 } 80 81 return {}; 82 }; 83 84 auto make_buddies = [&](int index0, int index1) { 85 auto& buddy0 = document.spans()[index0]; 86 auto& buddy1 = document.spans()[index1]; 87 m_has_brace_buddies = true; 88 m_brace_buddies[0].index = index0; 89 m_brace_buddies[1].index = index1; 90 m_brace_buddies[0].span_backup = buddy0; 91 m_brace_buddies[1].span_backup = buddy1; 92 buddy0.background_color = Color::DarkCyan; 93 buddy1.background_color = Color::DarkCyan; 94 buddy0.color = Color::White; 95 buddy1.color = Color::White; 96 m_editor->update(); 97 }; 98 99 auto pairs = matching_token_pairs(); 100 101 for (size_t i = 0; i < document.spans().size(); ++i) { 102 auto& span = const_cast<GUI::TextDocumentSpan&>(document.spans().at(i)); 103 auto token_type = span.data; 104 105 for (auto& pair : pairs) { 106 if (token_types_equal(token_type, pair.open) && span.range.start() == m_editor->cursor()) { 107 auto buddy = find_span_of_type(i, pair.close, pair.open, Direction::Forward); 108 if (buddy.has_value()) 109 make_buddies(i, buddy.value()); 110 return; 111 } 112 } 113 114 auto right_of_end = span.range.end(); 115 right_of_end.set_column(right_of_end.column() + 1); 116 117 for (auto& pair : pairs) { 118 if (token_types_equal(token_type, pair.close) && right_of_end == m_editor->cursor()) { 119 auto buddy = find_span_of_type(i, pair.open, pair.close, Direction::Backward); 120 if (buddy.has_value()) 121 make_buddies(i, buddy.value()); 122 return; 123 } 124 } 125 } 126} 127 128void SyntaxHighlighter::attach(TextEditor& editor) 129{ 130 ASSERT(!m_editor); 131 m_editor = editor.make_weak_ptr(); 132} 133 134void SyntaxHighlighter::detach() 135{ 136 ASSERT(m_editor); 137 m_editor = nullptr; 138} 139 140void SyntaxHighlighter::cursor_did_change() 141{ 142 ASSERT(m_editor); 143 auto& document = m_editor->document(); 144 if (m_has_brace_buddies) { 145 if (m_brace_buddies[0].index >= 0 && m_brace_buddies[0].index < static_cast<int>(document.spans().size())) 146 document.set_span_at_index(m_brace_buddies[0].index, m_brace_buddies[0].span_backup); 147 if (m_brace_buddies[1].index >= 0 && m_brace_buddies[1].index < static_cast<int>(document.spans().size())) 148 document.set_span_at_index(m_brace_buddies[1].index, m_brace_buddies[1].span_backup); 149 m_has_brace_buddies = false; 150 m_editor->update(); 151 } 152 highlight_matching_token_pair(); 153} 154 155}