diff options
author | schneems <richard.schneeman+foo@gmail.com> | 2023-03-08 08:51:00 -0600 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2023-04-06 15:45:28 +0900 |
commit | e5236471c3ce194199a6ceb70012eb2ca243407e (patch) | |
tree | 83455ffeecb919833e32998ed83fd018092cbd1c /spec/syntax_suggest/unit | |
parent | 33cfd262fcfe65737b6d4cde416a24cd81406885 (diff) | |
download | ruby-e5236471c3ce194199a6ceb70012eb2ca243407e.tar.gz |
[ruby/syntax_suggest] Preserve whitespace in front of comments
When removing comments I previously replaced them with a newline. This loses some context and may affect the order of the indent search which in turn affects the final result. By preserving whitespace in front of the comment, we preserve the "natural" indentation order of the line while also allowing the parser/lexer to see and join naturally consecutive (method chain) lines.
close https://github.com/ruby/syntax_suggest/pull/177
Diffstat (limited to 'spec/syntax_suggest/unit')
-rw-r--r-- | spec/syntax_suggest/unit/clean_document_spec.rb | 44 | ||||
-rw-r--r-- | spec/syntax_suggest/unit/code_line_spec.rb | 1 | ||||
-rw-r--r-- | spec/syntax_suggest/unit/code_search_spec.rb | 1 |
3 files changed, 25 insertions, 21 deletions
diff --git a/spec/syntax_suggest/unit/clean_document_spec.rb b/spec/syntax_suggest/unit/clean_document_spec.rb index 4fb79efd62..bb26e2f392 100644 --- a/spec/syntax_suggest/unit/clean_document_spec.rb +++ b/spec/syntax_suggest/unit/clean_document_spec.rb @@ -72,6 +72,25 @@ module SyntaxSuggest EOM end + + it "joins multi-line chained methods when separated by comments" do + source = <<~EOM + User. + # comment + where(name: 'schneems'). + # another comment + first + EOM + + doc = CleanDocument.new(source: source).join_consecutive! + code_lines = doc.lines + + expect(code_lines[0].to_s.count($/)).to eq(5) + code_lines[1..-1].each do |line| + expect(line.to_s.strip.length).to eq(0) + end + end + it "helper method: take_while_including" do source = <<~EOM User @@ -92,27 +111,10 @@ module SyntaxSuggest # yolo EOM - out = CleanDocument.new(source: source).lines.join - expect(out.to_s).to eq(<<~EOM) - - puts "what" - - EOM - end - - it "whitespace: removes whitespace" do - source = " \n" + <<~EOM - puts "what" - EOM - - out = CleanDocument.new(source: source).lines.join - expect(out.to_s).to eq(<<~EOM) - - puts "what" - EOM - - expect(source.lines.first.to_s).to_not eq("\n") - expect(out.lines.first.to_s).to eq("\n") + 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(' ' + $/) end it "trailing slash: does not join trailing do" do diff --git a/spec/syntax_suggest/unit/code_line_spec.rb b/spec/syntax_suggest/unit/code_line_spec.rb index cc4fa48bc9..d5b568fd19 100644 --- a/spec/syntax_suggest/unit/code_line_spec.rb +++ b/spec/syntax_suggest/unit/code_line_spec.rb @@ -48,6 +48,7 @@ module SyntaxSuggest # Indicates line 1 can join 2, 2 can join 3, but 3 won't join it's next line expect(code_lines.map(&:ignore_newline_not_beg?)).to eq([true, true, false, false]) end + it "trailing if" do code_lines = CodeLine.from_source(<<~'EOM') puts "lol" if foo diff --git a/spec/syntax_suggest/unit/code_search_spec.rb b/spec/syntax_suggest/unit/code_search_spec.rb index f836ba36f3..9a8115c0c8 100644 --- a/spec/syntax_suggest/unit/code_search_spec.rb +++ b/spec/syntax_suggest/unit/code_search_spec.rb @@ -338,6 +338,7 @@ module SyntaxSuggest end EOM search.call + puts "done" expect(search.invalid_blocks.join).to eq(<<~'EOM') Foo.call do |