Skip to content

Commit

Permalink
indent: fix #15 and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
pboettch committed Jul 17, 2019
1 parent 5f8fa16 commit d501b0e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,41 @@ With Vundle
" inside .vimrc
Plugin 'pboettch/vim-cmake-syntax'

## Indentation

There is also an indent file which does some intelligent alignment.

For control-keywords (`if`, `while`, `foreach`, `macro`, etc) it automatically adds a
`shiftwidth()` (and substracts it for a `end`-keyword).

For commands (so everything which has arguments between parenthesis `(...)`) is tries to do the following.

Either it aligns all arguments on a new line in the same column as the opening parenthesis if the first argument is on the
same line as the command:

```cmake
add_custom_target(TARGET name
DEPENDS target
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
```

or it indents it with one additional `shiftwidth()` if the first argument is on a new line

```cmake
add_custom_target(
TARGET name
DEPENDS target
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
```

By setting `g:cmake_indent_no_command_argument_align` to 1 in your vimrc-file the old behavior is activated:

```cmake
add_custom_target(TARGET name
DEPENDS target
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
```

## Test

There is a ever growing test-suite based on ctest located in test/
Expand Down
20 changes: 15 additions & 5 deletions indent/cmake.vim
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ let s:cmake_indent_close_regex = '^' . s:cmake_regex_arguments .
let s:cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
let s:cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('

if !exists('g:cmake_indent_no_command_argument_align')
let g:cmake_indent_no_command_argument_align = 0
endif

fun! CMakeGetIndent(lnum)
let this_line = getline(a:lnum)

Expand Down Expand Up @@ -74,16 +78,22 @@ fun! CMakeGetIndent(lnum)
if previous_line =~? s:cmake_indent_open_regex " open parenthesis
if previous_line !~? s:cmake_indent_close_regex " closing parenthesis is not on the same line
call cursor(lnum, 1)
let s = searchpos('(') " find first ( which is by cmake-design not a string
if strlen(previous_line) == s[1] " if ( is the last char on this line, do not align with ( but use sw()
let s = searchpos('(\s*$') " find '(' with nothing or only spaces until the end of the line
if s[0] == lnum
let ind += shiftwidth()
else
let ind = s[1]
else " an argument after the keyword
call cursor(lnum, 1)
let s = searchpos('(') " find position of first '('
if g:cmake_indent_no_command_argument_align == 1 " old behavior
let ind += shiftwidth()
else
let ind = s[1]
endif
endif
endif
elseif previous_line =~? s:cmake_indent_close_regex " close parenthesis
call cursor(lnum, strlen(previous_line))
let pairpos = searchpos(s:cmake_indent_open_regex, 'nbz')
let pairpos = searchpos(s:cmake_indent_open_regex, 'nbz') " find corresponding open paren
if pairpos[0] != 0
let ind = indent(pairpos[0])
endif
Expand Down

0 comments on commit d501b0e

Please sign in to comment.