Skip to content

Commit dc18f9c

Browse files
committed
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 #177
1 parent 5c2bf6b commit dc18f9c

File tree

6 files changed

+58
-28
lines changed

6 files changed

+58
-28
lines changed

lib/syntax_suggest/clean_document.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def to_s
110110
@document.join
111111
end
112112

113-
# Remove comments and whitespace only lines
113+
# Remove comments
114114
#
115115
# replace with empty newlines
116116
#
@@ -156,8 +156,9 @@ def to_s
156156
#
157157
def clean_sweep(source:)
158158
source.lines.map do |line|
159-
if line.match?(/^\s*(#[^{].*)?$/) # https://rubular.com/r/LLE10D8HKMkJvs
160-
$/
159+
if line.match?(/^\s*#([^{].*)?$/) # https://rubular.com/r/LLE10D8HKMkJvs
160+
whitespace = /^(?<whitespace>\s*)#([^{].*)?$/.match(line).named_captures["whitespace"] || ""
161+
whitespace + $/
161162
else
162163
line
163164
end

lib/syntax_suggest/code_line.rb

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

51-
if strip_line.empty?
52-
@empty = true
53-
@indent = 0
51+
if (@empty = strip_line.empty?)
52+
@indent = line.length - 1 # Newline removed from strip_line is not "whitespace"
5453
else
55-
@empty = false
5654
@indent = line.length - strip_line.length
5755
end
5856

spec/integration/syntax_suggest_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,32 @@ def bark
207207
> 4 end
208208
EOM
209209
end
210+
211+
it "comment inside of a method" do
212+
source = <<~'EOM'
213+
class Dog
214+
def bark
215+
# todo
216+
end
217+
218+
def sit
219+
print "sit"
220+
end
221+
end
222+
end # extra end
223+
EOM
224+
225+
io = StringIO.new
226+
SyntaxSuggest.call(
227+
io: io,
228+
source: source
229+
)
230+
out = io.string
231+
expect(out).to include(<<~EOM)
232+
> 1 class Dog
233+
> 9 end
234+
> 10 end # extra end
235+
EOM
236+
end
210237
end
211238
end

spec/unit/clean_document_spec.rb

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ module SyntaxSuggest
7272
EOM
7373
end
7474

75+
76+
it "joins multi-line chained methods when separated by comments" do
77+
source = <<~EOM
78+
User.
79+
# comment
80+
where(name: 'schneems').
81+
# another comment
82+
first
83+
EOM
84+
85+
doc = CleanDocument.new(source: source).join_consecutive!
86+
code_lines = doc.lines
87+
88+
expect(code_lines[0].to_s.count($/)).to eq(5)
89+
code_lines[1..-1].each do |line|
90+
expect(line.to_s.strip.length).to eq(0)
91+
end
92+
end
93+
7594
it "helper method: take_while_including" do
7695
source = <<~EOM
7796
User
@@ -92,27 +111,10 @@ module SyntaxSuggest
92111
# yolo
93112
EOM
94113

95-
out = CleanDocument.new(source: source).lines.join
96-
expect(out.to_s).to eq(<<~EOM)
97-
98-
puts "what"
99-
100-
EOM
101-
end
102-
103-
it "whitespace: removes whitespace" do
104-
source = " \n" + <<~EOM
105-
puts "what"
106-
EOM
107-
108-
out = CleanDocument.new(source: source).lines.join
109-
expect(out.to_s).to eq(<<~EOM)
110-
111-
puts "what"
112-
EOM
113-
114-
expect(source.lines.first.to_s).to_not eq("\n")
115-
expect(out.lines.first.to_s).to eq("\n")
114+
lines = CleanDocument.new(source: source).lines
115+
expect(lines[0].to_s).to eq($/)
116+
expect(lines[1].to_s).to eq('puts "what"' + $/)
117+
expect(lines[2].to_s).to eq(' ' + $/)
116118
end
117119

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

spec/unit/code_line_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def square(x) = x * x
4848
# Indicates line 1 can join 2, 2 can join 3, but 3 won't join it's next line
4949
expect(code_lines.map(&:ignore_newline_not_beg?)).to eq([true, true, false, false])
5050
end
51+
5152
it "trailing if" do
5253
code_lines = CodeLine.from_source(<<~'EOM')
5354
puts "lol" if foo

spec/unit/code_search_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ def dog
338338
end
339339
EOM
340340
search.call
341+
puts "done"
341342

342343
expect(search.invalid_blocks.join).to eq(<<~'EOM')
343344
Foo.call do

0 commit comments

Comments
 (0)