|
| 1 | +module SyntaxErrorSearch |
| 2 | + # Multiple lines form a singular CodeBlock |
| 3 | + # |
| 4 | + # Source code is made of multiple CodeBlocks. A code block |
| 5 | + # has a reference to the source code that created itself, this allows |
| 6 | + # a code block to "expand" when needed |
| 7 | + # |
| 8 | + # The most important ability of a CodeBlock is this ability to expand: |
| 9 | + # |
| 10 | + # Example: |
| 11 | + # |
| 12 | + # code_block.to_s # => |
| 13 | + # # def foo |
| 14 | + # # puts "foo" |
| 15 | + # # end |
| 16 | + # |
| 17 | + # code_block.expand_until_next_boundry |
| 18 | + # |
| 19 | + # code_block.to_s # => |
| 20 | + # # class Foo |
| 21 | + # # def foo |
| 22 | + # # puts "foo" |
| 23 | + # # end |
| 24 | + # # end |
| 25 | + # |
| 26 | + class CodeBlock |
| 27 | + attr_reader :lines |
| 28 | + |
| 29 | + def initialize(code_lines:, lines: []) |
| 30 | + @lines = Array(lines) |
| 31 | + @code_lines = code_lines |
| 32 | + end |
| 33 | + |
| 34 | + def is_end? |
| 35 | + to_s.strip == "end" |
| 36 | + end |
| 37 | + |
| 38 | + def starts_at |
| 39 | + @lines.first&.line_number |
| 40 | + end |
| 41 | + |
| 42 | + def code_lines |
| 43 | + @code_lines |
| 44 | + end |
| 45 | + |
| 46 | + # This is used for frontier ordering, we are searching from |
| 47 | + # the largest indentation to the smallest. This allows us to |
| 48 | + # populate an array with multiple code blocks then call `sort!` |
| 49 | + # on it without having to specify the sorting criteria |
| 50 | + def <=>(other) |
| 51 | + self.current_indent <=> other.current_indent |
| 52 | + end |
| 53 | + |
| 54 | + # Only the lines that are not empty and visible |
| 55 | + def visible_lines |
| 56 | + @lines |
| 57 | + .select(&:not_empty?) |
| 58 | + .select(&:visible?) |
| 59 | + end |
| 60 | + |
| 61 | + # This method is used to expand a code block to capture it's calling context |
| 62 | + def expand_until_next_boundry |
| 63 | + expand_to_indent(next_indent) |
| 64 | + self |
| 65 | + end |
| 66 | + |
| 67 | + # This method expands the given code block until it captures |
| 68 | + # its nearest neighbors. This is used to expand a single line of code |
| 69 | + # to its smallest likely block. |
| 70 | + # |
| 71 | + # code_block.to_s # => |
| 72 | + # # puts "foo" |
| 73 | + # code_block.expand_until_neighbors |
| 74 | + # |
| 75 | + # code_block.to_s # => |
| 76 | + # # puts "foo" |
| 77 | + # # puts "bar" |
| 78 | + # # puts "baz" |
| 79 | + # |
| 80 | + def expand_until_neighbors |
| 81 | + expand_to_indent(current_indent) |
| 82 | + |
| 83 | + expand_hidden_parner_line if self.to_s.strip == "end" |
| 84 | + self |
| 85 | + end |
| 86 | + |
| 87 | + def expand_hidden_parner_line |
| 88 | + index = @lines.first.index |
| 89 | + indent = current_indent |
| 90 | + partner_line = code_lines.select {|line| line.index < index && line.indent == indent }.last |
| 91 | + |
| 92 | + if partner_line&.hidden? |
| 93 | + partner_line.mark_visible |
| 94 | + @lines.prepend(partner_line) |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + # This method expands the existing code block up (before) |
| 99 | + # and down (after). It will break on change in indentation |
| 100 | + # and empty lines. |
| 101 | + # |
| 102 | + # code_block.to_s # => |
| 103 | + # # def foo |
| 104 | + # # puts "foo" |
| 105 | + # # end |
| 106 | + # |
| 107 | + # code_block.expand_to_indent(0) |
| 108 | + # code_block.to_s # => |
| 109 | + # # class Foo |
| 110 | + # # def foo |
| 111 | + # # puts "foo" |
| 112 | + # # end |
| 113 | + # # end |
| 114 | + # |
| 115 | + private def expand_to_indent(indent) |
| 116 | + array = [] |
| 117 | + before_lines(skip_empty: false).each do |line| |
| 118 | + if line.empty? |
| 119 | + array.prepend(line) |
| 120 | + break |
| 121 | + end |
| 122 | + |
| 123 | + if line.indent == indent |
| 124 | + array.prepend(line) |
| 125 | + else |
| 126 | + break |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + array << @lines |
| 131 | + |
| 132 | + after_lines(skip_empty: false).each do |line| |
| 133 | + if line.empty? |
| 134 | + array << line |
| 135 | + break |
| 136 | + end |
| 137 | + |
| 138 | + if line.indent == indent |
| 139 | + array << line |
| 140 | + else |
| 141 | + break |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + @lines = array.flatten |
| 146 | + end |
| 147 | + |
| 148 | + def next_indent |
| 149 | + [ |
| 150 | + before_line&.indent || 0, |
| 151 | + after_line&.indent || 0 |
| 152 | + ].max |
| 153 | + end |
| 154 | + |
| 155 | + def current_indent |
| 156 | + lines.detect(&:not_empty?)&.indent || 0 |
| 157 | + end |
| 158 | + |
| 159 | + def before_line |
| 160 | + before_lines.first |
| 161 | + end |
| 162 | + |
| 163 | + def after_line |
| 164 | + after_lines.first |
| 165 | + end |
| 166 | + |
| 167 | + def before_lines(skip_empty: true) |
| 168 | + index = @lines.first.index |
| 169 | + lines = code_lines.select {|line| line.index < index } |
| 170 | + lines.select!(&:not_empty?) if skip_empty |
| 171 | + lines.select!(&:visible?) |
| 172 | + lines.reverse! |
| 173 | + |
| 174 | + lines |
| 175 | + end |
| 176 | + |
| 177 | + def after_lines(skip_empty: true) |
| 178 | + index = @lines.last.index |
| 179 | + lines = code_lines.select {|line| line.index > index } |
| 180 | + lines.select!(&:not_empty?) if skip_empty |
| 181 | + lines.select!(&:visible?) |
| 182 | + lines |
| 183 | + end |
| 184 | + |
| 185 | + # Returns a code block of the source that does not include |
| 186 | + # the current lines. This is useful for checking if a source |
| 187 | + # with the given lines removed parses successfully. If so |
| 188 | + # |
| 189 | + # Then it's proof that the current block is invalid |
| 190 | + def block_without |
| 191 | + @block_without ||= CodeBlock.new( |
| 192 | + source: @source, |
| 193 | + lines: @source.code_lines - @lines |
| 194 | + ) |
| 195 | + end |
| 196 | + |
| 197 | + def document_valid_without? |
| 198 | + block_without.valid? |
| 199 | + end |
| 200 | + |
| 201 | + def valid? |
| 202 | + SyntaxErrorSearch.valid?(self.to_s) |
| 203 | + end |
| 204 | + |
| 205 | + def to_s |
| 206 | + @lines.join |
| 207 | + end |
| 208 | + end |
| 209 | +end |
0 commit comments