summaryrefslogtreecommitdiff
path: root/lib/syntax_suggest/clean_document.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/syntax_suggest/clean_document.rb')
-rw-r--r--lib/syntax_suggest/clean_document.rb20
1 files changed, 11 insertions, 9 deletions
diff --git a/lib/syntax_suggest/clean_document.rb b/lib/syntax_suggest/clean_document.rb
index b572189259..2790ccae86 100644
--- a/lib/syntax_suggest/clean_document.rb
+++ b/lib/syntax_suggest/clean_document.rb
@@ -47,9 +47,9 @@ module SyntaxSuggest
# ## Heredocs
#
# A heredoc is an way of defining a multi-line string. They can cause many
- # problems. If left as a single line, Ripper would try to parse the contents
+ # problems. If left as a single line, the parser would try to parse the contents
# as ruby code rather than as a string. Even without this problem, we still
- # hit an issue with indentation
+ # hit an issue with indentation:
#
# 1 foo = <<~HEREDOC
# 2 "Be yourself; everyone else is already taken.""
@@ -110,7 +110,7 @@ module SyntaxSuggest
@document.join
end
- # Remove comments and whitespace only lines
+ # Remove comments
#
# replace with empty newlines
#
@@ -155,8 +155,10 @@ 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
+ if line.match?(/^\s*#([^{].*|)$/)
$/
else
line
@@ -222,7 +224,7 @@ module SyntaxSuggest
#
def join_consecutive!
consecutive_groups = @document.select(&:ignore_newline_not_beg?).map do |code_line|
- take_while_including(code_line.index..-1) do |line|
+ take_while_including(code_line.index..) do |line|
line.ignore_newline_not_beg?
end
end
@@ -243,7 +245,7 @@ module SyntaxSuggest
# expect(lines[1].to_s).to eq("")
def join_trailing_slash!
trailing_groups = @document.select(&:trailing_slash?).map do |code_line|
- take_while_including(code_line.index..-1) { |x| x.trailing_slash? }
+ take_while_including(code_line.index..) { |x| x.trailing_slash? }
end
join_groups(trailing_groups)
self
@@ -265,7 +267,7 @@ module SyntaxSuggest
groups.each do |lines|
line = lines.first
- # Handle the case of multiple groups in a a row
+ # Handle the case of multiple groups in a row
# if one is already replaced, move on
next if @document[line.index].empty?
@@ -277,7 +279,7 @@ module SyntaxSuggest
)
# Hide the rest of the lines
- lines[1..-1].each do |line|
+ lines[1..].each do |line|
# The above lines already have newlines in them, if add more
# then there will be double newline, use an empty line instead
@document[line.index] = CodeLine.new(line: "", index: line.index, lex: [])
@@ -291,7 +293,7 @@ module SyntaxSuggest
# Like `take_while` except when it stops
# iterating, it also returns the line
# that caused it to stop
- def take_while_including(range = 0..-1)
+ def take_while_including(range = 0..)
take_next_and_stop = false
@document[range].take_while do |line|
next if take_next_and_stop