@@ -21,6 +21,7 @@ setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
2121if exists (" *CMakeGetIndent" )
2222 finish
2323endif
24+
2425let s: keepcpo= &cpo
2526set cpo &vim
2627
@@ -34,7 +35,6 @@ let s:cmake_regex_arguments = '\(' . s:cmake_regex_quoted .
3435 \ s: or . ' [^()\\#"]' . s: or . ' \\.' . ' \)*'
3536
3637let s: cmake_indent_comment_line = ' ^\s*' . s: cmake_regex_comment
37- let s: cmake_indent_blank_regex = ' ^\s*$'
3838let s: cmake_indent_open_regex = ' ^\s*' . s: cmake_regex_identifier .
3939 \ ' \s*(' . s: cmake_regex_arguments .
4040 \ ' \(' . s: cmake_regex_comment . ' \)\?$'
@@ -49,10 +49,20 @@ let s:cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\
4949fun ! CMakeGetIndent (lnum)
5050 let this_line = getline (a: lnum )
5151
52- " Find a non-blank line above the current line.
53- let lnum = a: lnum
54- let lnum = prevnonblank (lnum - 1 )
55- let previous_line = getline (lnum)
52+ let lnum = a: lnum - 1
53+
54+ " Find a non-blank/non-comment line above the current line.
55+ while lnum > 0
56+ let lnum = prevnonblank (lnum)
57+ let previous_line = getline (lnum)
58+
59+ " ignore comments (# only, lua-like comment TODO)
60+ if previous_line !~? s: cmake_indent_comment_line
61+ break
62+ endif
63+
64+ let lnum -= 1
65+ endwhile
5666
5767 " Hit the start of the file, use zero indent.
5868 if lnum == 0
@@ -61,34 +71,25 @@ fun! CMakeGetIndent(lnum)
6171
6272 let ind = indent (lnum)
6373
64- " Add
65- if previous_line = ~? s: cmake_indent_comment_line " Handle comments
66- let ind = ind
67- else
68- if previous_line = ~? s: cmake_indent_begin_regex
69- let ind = ind + shiftwidth ()
70- endif
71- if previous_line = ~? s: cmake_indent_open_regex
72- let ind = ind + shiftwidth ()
73- endif
74+ if previous_line = ~? s: cmake_indent_open_regex " open parenthesis
75+ if previous_line !~? s: cmake_indent_close_regex " closing parenthesis is not on the same line
76+ call cursor (lnum, 1 )
77+ let s = searchpos (' (' ) " find first ( which is by cmake-design not a string
78+ let ind = s [1 ]
79+ endif
80+ elseif previous_line = ~? s: cmake_indent_close_regex " close parenthesis
81+ call cursor (lnum, strlen (previous_line))
82+ let pairpos = searchpos (s: cmake_indent_open_regex , ' nbz' )
83+ if pairpos[0 ] != 0
84+ let ind = indent (pairpos[0 ])
85+ endif
7486 endif
7587
76- " Subtract
77- if this_line = ~? s: cmake_indent_end_regex
88+ if previous_line = ~? s: cmake_indent_begin_regex " control begin block
89+ let ind = ind + shiftwidth ()
90+ elseif this_line = ~? s: cmake_indent_end_regex " control end block
7891 let ind = ind - shiftwidth ()
7992 endif
80- " Use indent of the line with the opening parenthesis.
81- let m = matchstrpos (previous_line, s: cmake_indent_close_regex )
82- if ! empty (m [0 ])
83- " Go to closing parenthesis.
84- call cursor (lnum, m [1 ], m [2 ])
85- let pairpos = searchpairpos (' (' , ' ' , ' )' , ' bW' , ' ' , 0 , 500 )
86- if pairpos[0 ] != 0
87- let ind = indent (pairpos[0 ])
88- else
89- let ind = ind - shiftwidth ()
90- endif
91- endif
9293
9394 return ind
9495endfun
0 commit comments