Skip to content

Commit 9bd90e2

Browse files
committed
(#2) Fix multiple "-spec" clauses (if there is no "when")
1 parent d794a24 commit 9bd90e2

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

Diff for: indent/erlang.vim

+49-11
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ endfunction
5656
" Line tokenizer library {{{1
5757
" ======================
5858

59-
" Indtokens are "indentation tokens".
59+
" Indtokens are "indentation tokens". See their exact format in the
60+
" documentaiton of the s:GetTokensFromLine function.
6061

6162
" Purpose:
6263
" Calculate the new virtual column after the given segment of a line.
@@ -120,8 +121,9 @@ endfunction
120121
" indtokens = [indtoken]
121122
" indtoken = [token, vcol, col]
122123
" token = string (examples: 'begin', '<quoted_atom>', '}')
123-
" vcol = integer (the virtual column of the first character of the token)
124-
" col = integer
124+
" vcol = integer (the virtual column of the first character of the token;
125+
" counting starts from 0)
126+
" col = integer (counting starts from 0)
125127
function! s:GetTokensFromLine(line, string_continuation, atom_continuation,
126128
\tabstop)
127129

@@ -665,11 +667,14 @@ endfunction
665667
" stack: [token]
666668
" token: string
667669
" stored_vcol: integer
670+
" lnum: the line number of the "end of clause" mark (or 0 if we hit the
671+
" beginning of the file)
672+
" i: the index of the "end of clause" token within its own line
668673
" Returns:
669674
" result: [should_return, indent]
670675
" should_return: bool -- if true, the caller should return `indent` to Vim
671676
" indent -- integer
672-
function! s:BeginningOfClauseFound(stack, token, stored_vcol)
677+
function! s:BeginningOfClauseFound(stack, token, stored_vcol, lnum, i)
673678
if !empty(a:stack) && a:stack[0] ==# 'when'
674679
call s:Log(' BeginningOfClauseFound: "when" found in stack')
675680
call s:Pop(a:stack)
@@ -687,13 +692,45 @@ function! s:BeginningOfClauseFound(stack, token, stored_vcol)
687692
return [1, a:stored_vcol + &sw]
688693
elseif a:stack[0] ==# ';'
689694
call s:Pop(a:stack)
690-
if empty(a:stack)
691-
call s:Log(' Stack is ["->", ";"], so LTI is in a function head ' .
692-
\'-> return')
693-
return [0, a:stored_vcol]
694-
else
695+
696+
if !empty(a:stack)
695697
return [1, s:UnexpectedToken(a:token, a:stack)]
696698
endif
699+
700+
if a:lnum ==# 0
701+
" Set lnum and i to be NextIndToken-friendly
702+
let lnum = 1
703+
let i = -1
704+
else
705+
let lnum = a:lnum
706+
let i = a:i
707+
endif
708+
709+
" Are we after a "-spec func() ...;" clause?
710+
let [next1_indtoken, next1_lnum, next1_i] = s:NextIndToken(lnum, i)
711+
if !empty(next1_indtoken) && next1_indtoken[0] =~# '-'
712+
let [next2_indtoken, next2_lnum, next2_i] =
713+
\s:NextIndToken(next1_lnum, next1_i)
714+
if !empty(next2_indtoken) && next2_indtoken[0] =~# 'spec'
715+
let [next3_indtoken, next3_lnum, next3_i] =
716+
\s:NextIndToken(next2_lnum, next2_i)
717+
if !empty(next3_indtoken)
718+
let [next4_indtoken, next4_lnum, next4_i] =
719+
\s:NextIndToken(next3_lnum, next3_i)
720+
if !empty(next4_indtoken)
721+
" Yes, we are.
722+
call s:Log(' Stack is ["->", ";"], so LTI is in a "-spec" ' .
723+
\'attribute -> return')
724+
return [1, next4_indtoken[1]]
725+
endif
726+
endif
727+
endif
728+
endif
729+
730+
call s:Log(' Stack is ["->", ";"], so LTI is in a function head ' .
731+
\'-> return')
732+
return [1, a:stored_vcol]
733+
697734
else
698735
return [1, s:UnexpectedToken(a:token, a:stack)]
699736
endif
@@ -762,7 +799,7 @@ function! s:ErlangCalcIndent2(lnum, stack)
762799
" Hit the start of the file
763800
if lnum ==# 0
764801
let [ret, res] = s:BeginningOfClauseFound(stack, 'beginning_of_file',
765-
\stored_vcol)
802+
\stored_vcol, 0, 0)
766803
if ret | return res | endif
767804

768805
return 0
@@ -781,7 +818,8 @@ function! s:ErlangCalcIndent2(lnum, stack)
781818
endif
782819

783820
if token ==# '<end_of_clause>'
784-
let [ret, res] = s:BeginningOfClauseFound(stack, token, stored_vcol)
821+
let [ret, res] = s:BeginningOfClauseFound(stack, token, stored_vcol,
822+
\lnum, i)
785823
if ret | return res | endif
786824

787825
if stored_vcol ==# -1

Diff for: test_indent.erl

+2-2
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,8 @@ func() ->
509509

510510
% Overloading
511511
-spec func(A, B) -> ReturnType;
512-
(A,
513-
B) -> ReturnType.
512+
(A,
513+
B) -> ReturnType.
514514
func() ->
515515
ok.
516516

0 commit comments

Comments
 (0)