diff --git a/lib/syntax_suggest/clean_document.rb b/lib/syntax_suggest/clean_document.rb index 08a465dfb0..2c26061bfc 100644 --- a/lib/syntax_suggest/clean_document.rb +++ b/lib/syntax_suggest/clean_document.rb @@ -155,10 +155,11 @@ module SyntaxSuggest # ).to eq(2) # def clean_sweep(source:) + # Match comments, but not HEREDOC strings with #{variable} interpolation + # https://rubular.com/r/HPwtW9OYxKUHXQ source.lines.map do |line| - if line.match?(/^\s*#([^{].*)?$/) # https://rubular.com/r/LLE10D8HKMkJvs - whitespace = /^(?\s*)#([^{].*)?$/.match(line).named_captures["whitespace"] || "" - whitespace + $/ + if line.match?(/^\s*#([^{].*|)$/) + $/ else line end diff --git a/lib/syntax_suggest/code_line.rb b/lib/syntax_suggest/code_line.rb index d771a2c0dd..a20f34afa4 100644 --- a/lib/syntax_suggest/code_line.rb +++ b/lib/syntax_suggest/code_line.rb @@ -48,10 +48,10 @@ module SyntaxSuggest strip_line = line.dup strip_line.lstrip! - if (@empty = strip_line.empty?) - @indent = line.length - 1 # Newline removed from strip_line is not "whitespace" + @indent = if (@empty = strip_line.empty?) + line.length - 1 # Newline removed from strip_line is not "whitespace" else - @indent = line.length - strip_line.length + line.length - strip_line.length end set_kw_end diff --git a/spec/syntax_suggest/unit/block_expand_spec.rb b/spec/syntax_suggest/unit/block_expand_spec.rb index 4f93210368..5cff73621d 100644 --- a/spec/syntax_suggest/unit/block_expand_spec.rb +++ b/spec/syntax_suggest/unit/block_expand_spec.rb @@ -28,9 +28,9 @@ module SyntaxSuggest block = expansion.expand_neighbors(block) expect(block.to_s).to eq(<<~EOM.indent(2)) - def bark # index 1 + def bark # index 1 - end # index 3 + end # index 3 EOM end diff --git a/spec/syntax_suggest/unit/clean_document_spec.rb b/spec/syntax_suggest/unit/clean_document_spec.rb index bb26e2f392..25a62e4454 100644 --- a/spec/syntax_suggest/unit/clean_document_spec.rb +++ b/spec/syntax_suggest/unit/clean_document_spec.rb @@ -72,7 +72,6 @@ module SyntaxSuggest EOM end - it "joins multi-line chained methods when separated by comments" do source = <<~EOM User. @@ -114,7 +113,7 @@ module SyntaxSuggest lines = CleanDocument.new(source: source).lines expect(lines[0].to_s).to eq($/) expect(lines[1].to_s).to eq('puts "what"' + $/) - expect(lines[2].to_s).to eq(' ' + $/) + expect(lines[2].to_s).to eq($/) end it "trailing slash: does not join trailing do" do