Empty method definition with only a comment seems to confuse syntaxsuggest
mame opened this issue · comments
class C
def foo
# comment
end
def bar
"some literal"
end
def baz
end
def qux
end
def quux
end
end
end # extra end
Expected:
$ ruby error.rb
error.rb: --> error.rb
Unmatched `end', missing keyword (`do', `def`, `if`, etc.) ?
> 1 class C
> 18 end
> 19 end # extra end
error.rb:19: syntax error, unexpected `end' (SyntaxError)
end # extra end
^~~
Actual:
$ ruby error.rb
error.rb: --> error.rb
Unmatched `end', missing keyword (`do', `def`, `if`, etc.) ?
1 class C
> 4 end
> 10 def baz
> 11 end
18 end
error.rb:19: syntax error, unexpected `end' (SyntaxError)
end # extra end
^~~
I haven't identified the exact condition to reproduce this issue, but I am wondering that an empty method definition with only comments would confuse the heuristics.
Very interesting. Thanks for the report. I do some special stuff to handle comments
syntax_suggest/lib/syntax_suggest/clean_document.rb
Lines 70 to 85 in e0c53bb
class C
def foo
end
def bar
"some literal"
end
def baz
end
def qux
end
def quux
end
end
end # extra end
If you want to see individual steps you can run it with this flag:
$ SYNTAX_SUGGEST_DEBUG=1 ruby scratch.rb
The first few steps aren't that interesting, but step 5 is:
Block lines: 5..9 (expand)
1 class C
2 def foo
3
4 end
> 5
> 9
10 def baz
11 end
12
13 def qux
14 end
15
16 def quux
17 end
18 end
19 end # extra end
On the next expansion it pops lines 5-9 and expands in both directions them to 3-12:
Block lines: 3..12 (expand)
1 class C
2 def foo
> 3
> 4 end
> 10 def baz
> 11 end
> 12
13 def qux
14 end
15
16 def quux
17 end
18 end
19 end # extra end
So the issue looks like it stops that expansion up before it should.
syntax_suggest/lib/syntax_suggest/block_expand.rb
Lines 55 to 60 in e0c53bb
I'll have to play with it some. Either we can change the input document (hiding comment lines for example) or we can change some logic in that scanner. Usually fixing one problem breaks an existing case, but often there's some good path forward.