Skip to content

Commit e8d59d1

Browse files
committed
don't change indent of doc blocks, strings, and comments
Fixes #505 Fixes #346
1 parent 008b224 commit e8d59d1

File tree

5 files changed

+78
-12
lines changed

5 files changed

+78
-12
lines changed

autoload/elixir/indent.vim

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function! elixir#indent#indent(lnum)
2121

2222
let handlers = [
2323
\'top_of_file',
24+
\'starts_with_string_continuation',
2425
\'following_trailing_binary_operator',
2526
\'starts_with_pipe',
2627
\'starts_with_binary_operator',
@@ -31,9 +32,14 @@ function! elixir#indent#indent(lnum)
3132
\]
3233
for handler in handlers
3334
call s:debug('testing handler elixir#indent#handle_'.handler)
34-
let context = {'lnum': lnum, 'text': text, 'prev_nb_lnum': prev_nb_lnum, 'prev_nb_text': prev_nb_text}
35+
let context = {'lnum': lnum, 'text': text, 'first_nb_char_idx': match(text, '\w'), 'prev_nb_lnum': prev_nb_lnum, 'prev_nb_text': prev_nb_text}
3536
let indent = function('elixir#indent#handle_'.handler)(context)
36-
if indent != -1
37+
if indent == -2
38+
" Keep indent the same
39+
call s:debug('line '.lnum.': elixir#indent#handle_'.handler.' returned -2; returning indent of -1')
40+
call cursor(curs_lnum, curs_col)
41+
return -1
42+
elseif indent != -1
3743
call s:debug('line '.lnum.': elixir#indent#handle_'.handler.' returned '.indent)
3844
call cursor(curs_lnum, curs_col)
3945
return indent
@@ -98,7 +104,11 @@ endfunction
98104
" Returns 0 or 1 based on whether or not the given line number and column
99105
" number pair is a string or comment
100106
function! s:is_string_or_comment(line, col)
101-
return synIDattr(synID(a:line, a:col, 1), "name") =~ '\%(String\|Comment\)'
107+
return s:syntax_name(a:line, a:col) =~ '\%(String\|Comment\)'
108+
endfunction
109+
110+
function! s:syntax_name(line, col)
111+
return synIDattr(synID(a:line, a:col, 1), "name")
102112
endfunction
103113

104114
" Skip expression for searchpair. Returns 0 or 1 based on whether the value
@@ -154,6 +164,14 @@ function! elixir#indent#handle_top_of_file(context)
154164
end
155165
endfunction
156166

167+
function! elixir#indent#handle_starts_with_string_continuation(context)
168+
if s:syntax_name(a:context.lnum, a:context.first_nb_char_idx) =~ '\(String\|Comment\)$'
169+
return -2
170+
else
171+
return -1
172+
end
173+
endfunction
174+
157175
function! elixir#indent#handle_follow_prev_nb(context)
158176
return s:get_base_indent(a:context.prev_nb_lnum, a:context.prev_nb_text)
159177
endfunction

spec/indent/comment_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,16 @@ def run(task) when task in [:t1, :t2] do
4141
{:error, :timed_out_waiting_for_lock}
4242
end
4343
EOF
44+
45+
it "bulk indenting comments" do
46+
expect(<<~EOF).to be_elixir_indentation
47+
defmodule Test do
48+
# SELECT *
49+
# FROM table
50+
# WHERE column = 123
51+
# AND another_column = 456
52+
end
53+
EOF
54+
end
4455
end
4556

spec/indent/documentation_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,25 @@
1010
"""
1111
end
1212
EOF
13+
14+
it "bulk indenting doc blocks" do
15+
expect(<<~EOF).to be_elixir_indentation
16+
defmodule Test do
17+
@doc """
18+
do not reindent
19+
any indent that i do
20+
please
21+
"""
22+
end
23+
EOF
24+
end
25+
26+
i <<~EOF
27+
defmodule Test do
28+
@doc """
29+
it should
30+
have reasonable
31+
default start indent when typed
32+
"""
33+
EOF
1334
end

spec/indent/string_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'Indenting strings' do
6+
it "bulk indenting strings" do
7+
expect(<<~EOF).to be_elixir_indentation
8+
defp sql do
9+
"""
10+
SELECT *
11+
FROM table
12+
WHERE column = 123
13+
AND another_column = 456
14+
"""
15+
end
16+
EOF
17+
end
18+
end

spec/spec_helper.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def initialize(vim, type)
1313

1414
def reindent(content)
1515
with_file content do
16-
cmd = 'ggVG999<<' # remove all indentation
16+
min_indent = content.each_line.map { |line| line[/\s*/].size }.min
17+
cmd = "ggVG:s/\\s\\{0,#{min_indent}}//" # remove all indentation
1718
cmd += 'gg=G' # force vim to indent the file
1819
@vim.normal cmd
1920
end
@@ -144,6 +145,8 @@ def self.new
144145
to be indented as
145146
146147
#{code}
148+
149+
when typed
147150
EOM
148151
end
149152
end
@@ -169,6 +172,8 @@ def self.new
169172
to be indented as
170173
171174
#{code}
175+
176+
when bulk indented
172177
EOM
173178
end
174179
end
@@ -284,14 +289,7 @@ def ip(str)
284289

285290
def gen_tests(method, str)
286291
send method, "\n#{str}" do
287-
reload = -> do
288-
VIM.add_plugin(File.expand_path('..', __dir__), 'ftdetect/elixir.vim')
289-
received = ExBuffer.new.reindent(str)
290-
puts received
291-
str == received
292-
end
293-
actual = ExBuffer.new.reindent(str)
294-
expect(actual).to eq(str)
292+
expect(str).to be_elixir_indentation
295293
end
296294

297295
send method, "typed: \n#{str}" do

0 commit comments

Comments
 (0)