instance method find_offset

Ruby on Rails 8.0.4

Since v7.1.6 Private

Available in: v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

find_offset(compiled, source_tokens, error_column)

Find which token in the source template spans the byte range that contains the error_column, then return the offset compared to the original source template.

Iterate consecutive pairs of CODE or TEXT tokens, requiring a match of the first token before matching either token.

For example, if we want to find tokens A, B, C, we do the following:

  1. Find a match for A: test error_column or advance scanner.

  2. Find a match for B or A:

a. If B: start over with next token set (B, C).
b. If A: test error_column or advance scanner.
c. Otherwise: Advance 1 byte

Prioritize matching the next token over the current token once a match for the current token has been found. This is to prevent the current token from looping past the next token if they both match (i.e. if the current token is a single space character).

Parameters

compiled req
source_tokens req
error_column req
Source
# File actionview/lib/action_view/template/handlers/erb.rb, line 128
        def find_offset(compiled, source_tokens, error_column)
          compiled = StringScanner.new(compiled)
          offset_source_tokens(source_tokens).each_cons(2) do |(name, str, offset), (_, next_str, _)|
            matched_str = false

            until compiled.eos?
              if matched_str && next_str && compiled.match?(next_str)
                break
              elsif compiled.match?(str)
                matched_str = true

                if name == :CODE && compiled.pos <= error_column && compiled.pos + str.bytesize >= error_column
                  return error_column - compiled.pos + offset
                end

                compiled.pos += str.bytesize
              else
                compiled.pos += 1
              end
            end
          end

          raise LocationParsingError, "Couldn't find code snippet"
        end

Defined in actionview/lib/action_view/template/handlers/erb.rb line 128 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Template::Handlers::ERB

Type at least 2 characters to search.

↑↓ navigate · open · esc close