|
| 1 | +#include "./scope_sequence.h" |
| 2 | + |
| 3 | +#include "bandit/bandit.h" |
| 4 | +#include <sstream> |
| 5 | +#include "helpers/stream_methods.h" |
| 6 | +#include "helpers/point_helpers.h" |
| 7 | + |
| 8 | +using std::string; |
| 9 | +using std::cout; |
| 10 | + |
| 11 | +static void append_text_to_scope_sequence(ScopeSequence *sequence, |
| 12 | + ScopeStack *current_scopes, |
| 13 | + const std::string &text, |
| 14 | + size_t length) { |
| 15 | + for (size_t i = 0; i < length; i++) { |
| 16 | + string character(1, text[sequence->size()]); |
| 17 | + sequence->push_back(*current_scopes); |
| 18 | + sequence->back().push_back("'" + character + "'"); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +static void append_to_scope_sequence(ScopeSequence *sequence, |
| 23 | + ScopeStack *current_scopes, |
| 24 | + TSNode node, TSDocument *document, |
| 25 | + const std::string &text) { |
| 26 | + append_text_to_scope_sequence(sequence, current_scopes, text, ts_node_start_byte(node) - sequence->size()); |
| 27 | + |
| 28 | + string scope = ts_node_type(node, document); |
| 29 | + current_scopes->push_back(scope); |
| 30 | + size_t child_count = ts_node_child_count(node); |
| 31 | + if (child_count > 0) { |
| 32 | + for (size_t i = 0; i < child_count; i++) { |
| 33 | + TSNode child = ts_node_child(node, i); |
| 34 | + append_to_scope_sequence(sequence, current_scopes, child, document, text); |
| 35 | + } |
| 36 | + } else { |
| 37 | + size_t length = ts_node_end_byte(node) - ts_node_start_byte(node); |
| 38 | + append_text_to_scope_sequence(sequence, current_scopes, text, length); |
| 39 | + } |
| 40 | + current_scopes->pop_back(); |
| 41 | +} |
| 42 | + |
| 43 | +ScopeSequence build_scope_sequence(TSDocument *document, const std::string &text) { |
| 44 | + ScopeSequence sequence; |
| 45 | + ScopeStack current_scopes; |
| 46 | + TSNode node = ts_document_root_node(document); |
| 47 | + append_to_scope_sequence(&sequence, ¤t_scopes, node, document, text); |
| 48 | + return sequence; |
| 49 | +} |
| 50 | + |
| 51 | +bool operator<=(const TSPoint &left, const TSPoint &right) { |
| 52 | + if (left.row < right.row) |
| 53 | + return true; |
| 54 | + else if (left.row == right.row) |
| 55 | + return left.column <= right.column; |
| 56 | + else |
| 57 | + return false; |
| 58 | +} |
| 59 | + |
| 60 | +void verify_changed_ranges(const ScopeSequence &old_sequence, const ScopeSequence &new_sequence, |
| 61 | + const string &text, TSRange *ranges, size_t range_count) { |
| 62 | + TSPoint current_position = {0, 0}; |
| 63 | + for (size_t i = 0; i < old_sequence.size(); i++) { |
| 64 | + if (text[i] == '\n') { |
| 65 | + current_position.row++; |
| 66 | + current_position.column = 0; |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + const ScopeStack &old_scopes = old_sequence[i]; |
| 71 | + const ScopeStack &new_scopes = new_sequence[i]; |
| 72 | + if (old_scopes != new_scopes) { |
| 73 | + bool found_containing_range = false; |
| 74 | + for (size_t j = 0; j < range_count; j++) { |
| 75 | + TSRange range = ranges[j]; |
| 76 | + if (range.start <= current_position && current_position <= range.end) { |
| 77 | + found_containing_range = true; |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (!found_containing_range) { |
| 83 | + std::stringstream message_stream; |
| 84 | + message_stream << "Found changed scope outside of any invalidated range;\n"; |
| 85 | + message_stream << "Position: " << current_position << "\n"; |
| 86 | + message_stream << "Byte index: " << i << "\n"; |
| 87 | + size_t line_start_index = i - current_position.column; |
| 88 | + size_t line_end_index = text.find_first_of('\n', i); |
| 89 | + message_stream << "Line: " << text.substr(line_start_index, line_end_index - line_start_index) << "\n"; |
| 90 | + for (size_t j = 0; j < current_position.column + string("Line: ").size(); j++) |
| 91 | + message_stream << " "; |
| 92 | + message_stream << "^\n"; |
| 93 | + message_stream << "Old scopes: " << old_scopes << "\n"; |
| 94 | + message_stream << "New scopes: " << new_scopes << "\n"; |
| 95 | + message_stream << "Invalidated ranges:\n"; |
| 96 | + for (size_t j = 0; j < range_count; j++) { |
| 97 | + message_stream << " " << ranges[j] << "\n"; |
| 98 | + } |
| 99 | + Assert::Failure(message_stream.str()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + current_position.column++; |
| 104 | + } |
| 105 | +} |
0 commit comments