Skip to content

Commit 315728c

Browse files
committed
Rollback comment indentation behavior
Originally I fixed #177 by making the process of comment removal indentation aware. The next commit is the more general fix and means we don't need to carry that additional logic/overhead. Also: Update syntax via linter
1 parent f94fd1f commit 315728c

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

lib/syntax_suggest/clean_document.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ def to_s
155155
# ).to eq(2)
156156
#
157157
def clean_sweep(source:)
158+
# Match comments, but not HEREDOC strings with #{variable} interpolation
159+
# https://rubular.com/r/PzrI9jaIalKOz4
158160
source.lines.map do |line|
159-
if line.match?(/^\s*#([^{].*)?$/) # https://rubular.com/r/LLE10D8HKMkJvs
160-
whitespace = /^(?<whitespace>\s*)#([^{].*)?$/.match(line).named_captures["whitespace"] || ""
161-
whitespace + $/
161+
if line.match?(/^(?<whitespace>\s*)#[^{].*?$/)
162+
$/
162163
else
163164
line
164165
end

lib/syntax_suggest/code_line.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ def initialize(line:, index:, lex:)
4848
strip_line = line.dup
4949
strip_line.lstrip!
5050

51-
if (@empty = strip_line.empty?)
52-
@indent = line.length - 1 # Newline removed from strip_line is not "whitespace"
51+
@indent = if (@empty = strip_line.empty?)
52+
line.length - 1 # Newline removed from strip_line is not "whitespace"
5353
else
54-
@indent = line.length - strip_line.length
54+
line.length - strip_line.length
5555
end
5656

5757
set_kw_end

spec/unit/block_expand_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def sit # index 5
2828
block = expansion.expand_neighbors(block)
2929

3030
expect(block.to_s).to eq(<<~EOM.indent(2))
31-
def bark # index 1
31+
def bark # index 1
3232
33-
end # index 3
33+
end # index 3
3434
EOM
3535
end
3636

spec/unit/clean_document_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ module SyntaxSuggest
7272
EOM
7373
end
7474

75-
7675
it "joins multi-line chained methods when separated by comments" do
7776
source = <<~EOM
7877
User.
@@ -114,7 +113,7 @@ module SyntaxSuggest
114113
lines = CleanDocument.new(source: source).lines
115114
expect(lines[0].to_s).to eq($/)
116115
expect(lines[1].to_s).to eq('puts "what"' + $/)
117-
expect(lines[2].to_s).to eq(' ' + $/)
116+
expect(lines[2].to_s).to eq($/)
118117
end
119118

120119
it "trailing slash: does not join trailing do" do

0 commit comments

Comments
 (0)