End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
SyntaxSuggest::CleanDocument#clean_sweep
clean_sweep(source:)
lib/syntax_suggest/clean_document.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.2.0/lib/syntax_suggest/clean_document.rb, line 157
def clean_sweep(source:)
source.lines.map do |line|
if line.match?(/^\s*(#[^{].*)?$/) # https://rubular.com/r/LLE10D8HKMkJvs
$/
else
line
end
end
end
Remove comments and whitespace only lines
replace with empty newlines
source = <<~'EOM'
# Comment 1
puts "hello"
# Comment 2
puts "world"
EOM
lines = CleanDocument.new(source: source).lines
expect(lines[0].to_s).to eq("\n")
expect(lines[1].to_s).to eq("puts "hello")
expect(lines[2].to_s).to eq("\n")
expect(lines[3].to_s).to eq("puts "world")
Important: This must be done before lexing.
After this change is made, we lex the document because removing comments can change how the doc is parsed.
For example:
values = LexAll.new(source: <<~EOM))
User.
# comment
where(name: 'schneems')
EOM
expect(
values.count {|v| v.type == :on_ignored_nl}
).to eq(1)
After the comment is removed:
values = LexAll.new(source: <<~EOM))
User.
where(name: 'schneems')
EOM
expect(
values.count {|v| v.type == :on_ignored_nl}
).to eq(2)