summaryrefslogtreecommitdiff
path: root/lib/syntax_suggest
diff options
context:
space:
mode:
authorschneems <[email protected]>2023-03-08 08:51:00 -0600
committerHiroshi SHIBATA <[email protected]>2023-04-06 15:45:28 +0900
commite5236471c3ce194199a6ceb70012eb2ca243407e (patch)
tree83455ffeecb919833e32998ed83fd018092cbd1c /lib/syntax_suggest
parent33cfd262fcfe65737b6d4cde416a24cd81406885 (diff)
[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 'lib/syntax_suggest')
-rw-r--r--lib/syntax_suggest/clean_document.rb7
-rw-r--r--lib/syntax_suggest/code_line.rb6
2 files changed, 6 insertions, 7 deletions
diff --git a/lib/syntax_suggest/clean_document.rb b/lib/syntax_suggest/clean_document.rb
index b572189259..08a465dfb0 100644
--- a/lib/syntax_suggest/clean_document.rb
+++ b/lib/syntax_suggest/clean_document.rb
@@ -110,7 +110,7 @@ module SyntaxSuggest
@document.join
end
- # Remove comments and whitespace only lines
+ # Remove comments
#
# replace with empty newlines
#
@@ -156,8 +156,9 @@ module SyntaxSuggest
#
def clean_sweep(source:)
source.lines.map do |line|
- if line.match?(/^\s*(#[^{].*)?$/) # https://rubular.com/r/LLE10D8HKMkJvs
- $/
+ if line.match?(/^\s*#([^{].*)?$/) # https://rubular.com/r/LLE10D8HKMkJvs
+ whitespace = /^(?<whitespace>\s*)#([^{].*)?$/.match(line).named_captures["whitespace"] || ""
+ whitespace + $/
else
line
end
diff --git a/lib/syntax_suggest/code_line.rb b/lib/syntax_suggest/code_line.rb
index dc738ab128..d771a2c0dd 100644
--- a/lib/syntax_suggest/code_line.rb
+++ b/lib/syntax_suggest/code_line.rb
@@ -48,11 +48,9 @@ module SyntaxSuggest
strip_line = line.dup
strip_line.lstrip!
- if strip_line.empty?
- @empty = true
- @indent = 0
+ if (@empty = strip_line.empty?)
+ @indent = line.length - 1 # Newline removed from strip_line is not "whitespace"
else
- @empty = false
@indent = line.length - strip_line.length
end