Empty else clause with only a comment seems to confuse syntaxsuggest
mame opened this issue · comments
class Foo
def foo
if cond?
foo
else
# comment
end
end
# ...
def bar
if @recv
end_is_missing_here
end
end
Expected: Around end_is_missing_here
is displayed
Actual:
error.rb: --> error.rb
Unmatched keyword, missing `end' ?
1 class Foo
2 def foo
> 3 if cond?
> 5 else
8 end
16 end
This might be a variant of #177.
Thanks for the report. I saw it come in. I've not had a chance to investigate yet. I'm guessing the logic from the fix last time either introduced the issue, or we need to introduce some new logic https://github.com/ruby/syntax_suggest/blob/main/lib/syntax_suggest/around_block_scan.rb#L259.
The comment or empty line has a problem because it's not evaluated and "removed" the same way a branch with code will be. It fails like this:
Block lines: 4..4 (add)
1 class Foo
2 def foo
3 if cond?
> 4 foo
5 else
6
7 end
8 end
9
10
11
12 def bar
13 if @recv
14 end_is_missing_here
15 end
16 end
Block lines: 3..5 (expand)
1 class Foo
2 def foo
> 3 if cond?
> 5 else
6
7 end
8 end
9
10
11
12 def bar
13 if @recv
14 end_is_missing_here
15 end
16 end
With those lines, then the kw/end pairs balance like this:
Block lines: 3..5 (expand)
1 class Foo
2 def foo
6
7 end
8 end
9
10
11
12 def bar
13 if @recv
14 end_is_missing_here
15 end
16 end
If you replace that line with any literal that's not a comment it will work because each side of the "else" is evaluated and removed and then when "indent expand" is called, it pulls in both the else and it's end since at that point it seems to the program they belong together:
Block lines: 4..4 (add)
1 class Foo
2 def foo
3 if cond?
> 4 foo
5 else
7 end
8 end
9
10
11
12 def bar
13 if @recv
14 end_is_missing_here
15 end
16 end
Block lines: 3..7 (expand)
1 class Foo
2 def foo
> 3 if cond?
> 5 else
> 7 end
8 end
9
10
11
12 def bar
13 if @recv
14 end_is_missing_here
15 end
16 end
The difference between #177 and this is that the problem is on the "indent" block expansion rather than the "neighbors" block expansion. I'm hoping we can apply similar logic to work around the issue.