class SyntaxSuggest::CaptureCodeContext

Turns a “invalid block(s)” into useful context

There are three main phases in the algorithm:

  1. Sanitize/format input source

  2. Search for invalid blocks

  3. Format invalid blocks into something meaningful

This class handles the third part.

The algorithm is very good at capturing all of a syntax error in a single block in number 2, however the results can contain ambiguities. Humans are good at pattern matching and filtering and can mentally remove extraneous data, but they can’t add extra data that’s not present.

In the case of known ambiguious cases, this class adds context back to the ambiguity so the programmer has full information.

Beyond handling these ambiguities, it also captures surrounding code context information:

puts block.to_s # => "def bark"

context = CaptureCodeContext.new(
  blocks: block,
  code_lines: code_lines
)

lines = context.call.map(&:original)
puts lines.join
# =>
  class Dog
    def bark
  end