Skip to content

Commit 168815d

Browse files
authored
fix if/else folding bug (#468)
* rework folding spec helper * remove elixirElseBlock * remove unsupported else fold spec
1 parent c1c3dca commit 168815d

File tree

3 files changed

+63
-57
lines changed

3 files changed

+63
-57
lines changed

spec/folding/basic_spec.rb

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,46 @@
33
require 'spec_helper'
44

55
describe 'Basic folding' do
6-
def self.it_folds_lines(content, lines, tags = nil)
7-
it("folds #{lines} lines on \n#{content}", tags) do
8-
expect(content).to fold_lines(lines, tags)
6+
def self.fold(content)
7+
it("properly folds \n#{content}") do
8+
expect(content).to fold_lines
99
end
1010
end
1111

12-
it_folds_lines(<<~EOF, 2)
13-
defmodule M do
14-
end
12+
fold <<~EOF
13+
defmodule M do # fold
14+
end # fold
1515
"not in fold"
1616
EOF
1717

18-
it_folds_lines(<<~EOF, 4)
19-
defmodule M do
20-
def some_func do
21-
end
22-
end
18+
fold <<~EOF
19+
defmodule M do # fold
20+
def some_func do # fold
21+
end # fold
22+
end # fold
2323
"not in fold"
2424
EOF
2525

26-
it_folds_lines(<<~EOF, 2, on_line: 2)
26+
fold <<~EOF
2727
defmodule M do
28-
def some_func do
29-
end
28+
def some_func do # fold
29+
end # fold
3030
end
3131
"not in fold"
3232
EOF
3333

34-
it_folds_lines(<<~EOF, 2)
35-
if true do
36-
end
34+
fold <<~EOF
35+
if true do # fold
36+
end # fold
3737
"not in fold"
3838
EOF
3939

40-
it_folds_lines(<<~EOF, 3, on_line: 3)
41-
if true do
42-
nil
43-
else
44-
nil
45-
end
46-
"not in fold"
47-
EOF
48-
49-
it_folds_lines(<<~EOF, 5, skip: "broken")
50-
if true do
51-
nil
52-
else
53-
nil
54-
end
40+
fold <<~EOF
41+
if true do # fold
42+
nil # fold
43+
else # fold
44+
nil # fold
45+
end # fold
5546
"not in fold"
5647
EOF
5748
end
58-

spec/spec_helper.rb

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
require 'vimrunner/rspec'
77

88
class Buffer
9+
FOLD_PLACEHOLDER = '<!-- FOLD -->'.freeze
10+
911
def initialize(vim, type)
1012
@file = ".fixture.#{type}"
1113
@vim = vim
@@ -60,15 +62,15 @@ def syntax(content, pattern)
6062
syngroups.gsub!(/["'\[\]]/, '').split(', ')
6163
end
6264

63-
def fold_and_delete(content, opts)
64-
start_line = opts[:on_line]
65+
def fold_and_replace(content, fold_on_line)
6566
with_file content do
6667
@vim.command("set foldmethod=syntax")
6768

6869
@vim.normal("zO")
69-
@vim.normal("#{start_line}G")
70+
@vim.normal("#{fold_on_line}G")
7071
@vim.normal("zc")
71-
@vim.normal("dd")
72+
@vim.normal("cc#{FOLD_PLACEHOLDER}")
73+
@vim.normal(":.s/\s*//<CR>")
7274
end
7375
end
7476

@@ -135,14 +137,14 @@ def self.new
135137
end
136138

137139
failure_message do |code|
138-
<<~EOM
139-
Expected
140+
<<~EOM
141+
Expected
140142
141-
#{@typed}
142-
to be indented as
143+
#{@typed}
144+
to be indented as
143145
144-
#{code}
145-
EOM
146+
#{code}
147+
EOM
146148
end
147149
end
148150

@@ -204,27 +206,42 @@ def self.new
204206
end
205207
end
206208

207-
RSpec::Matchers.define :fold_lines do |lines, opts|
209+
RSpec::Matchers.define :fold_lines do
208210
buffer = Buffer.new(VIM, :ex)
209-
opts ||= {}
210-
start_line = opts[:on_line] ||= 1
211-
212-
after = nil
213211

214212
match do |code|
215-
after = buffer.fold_and_delete(code, opts)
213+
@code = code
214+
215+
pattern = /# fold\s*$/
216+
217+
placeholder_set = false
218+
@expected = code.each_line.reduce([]) do |acc, line|
219+
if line =~ pattern
220+
if !placeholder_set
221+
placeholder_set = true
222+
acc << (Buffer::FOLD_PLACEHOLDER + "\n")
223+
end
224+
else
225+
acc << line
226+
end
227+
228+
acc
229+
end.join
230+
231+
fold_on_line = code.each_line.find_index { |l| l =~ pattern } + 1
232+
@actual = buffer.fold_and_replace(code, fold_on_line)
216233

217-
code.lines.count - after.lines.count == lines
234+
@expected == @actual
218235
end
219236

220237
failure_message do |code|
221238
<<~EOF
222-
expected
223-
#{code}
224-
to fold #{lines} lines at line #{start_line},
225-
but after folding at line #{start_line} and deleting a line I got
226-
#{after}
227-
back
239+
Folded
240+
241+
#{@code}
242+
and unexpectedly got
243+
244+
#{@actual}
228245
EOF
229246
end
230247
end

syntax/elixir.vim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ syn match elixirAtomInterpolated ':\("\)\@=' contains=elixirString
8282
syn match elixirString "\(\w\)\@<!?\%(\\\(x\d{1,2}\|\h{1,2}\h\@!\>\|0[0-7]{0,2}[0-7]\@!\>\|[^x0MC]\)\|(\\[MC]-)+\w\|[^\s\\]\)"
8383

8484
syn region elixirBlock matchgroup=elixirBlockDefinition start="\<do\>:\@!" end="\<end\>" contains=ALLBUT,@elixirNotTop fold
85-
syn region elixirElseBlock matchgroup=elixirBlockDefinition start="\<else\>:\@!" end="\<end\>" contains=ALLBUT,@elixirNotTop fold
8685
syn region elixirAnonymousFunction matchgroup=elixirBlockDefinition start="\<fn\>" end="\<end\>" contains=ALLBUT,@elixirNotTop fold
8786

8887
syn region elixirArguments start="(" end=")" contained contains=elixirOperator,elixirAtom,elixirPseudoVariable,elixirAlias,elixirBoolean,elixirVariable,elixirUnusedVariable,elixirNumber,elixirDocString,elixirAtomInterpolated,elixirRegex,elixirString,elixirStringDelimiter,elixirRegexDelimiter,elixirInterpolationDelimiter,elixirSigil,elixirAnonymousFunction,elixirComment

0 commit comments

Comments
 (0)