On this page
Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
SyntaxSuggest::CaptureCodeContext
Turns a “invalid block(s)” into useful context
There are three main phases in the algorithm:
-
Sanitize/format input source
-
Search for invalid blocks
-
Format invalid blocks into something meaninful
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 ambiguitiy 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