Browse Source

vim: Fix indentation of 'closing parens only lines'

This commit changes the indentation of lines which contain only closing
parentheses (`)`).

Example:

Change
```
add_library(mylib
    mysrc.c
    )
```
to:
```
add_library(mylib
    mysrc.c
)
```

There are edge cases, where the indentation still doesn't really work.

Example:

This, admittedly weird, piece of code (manually formatted to what I would
expect - this is already a personal preference ...):
```
if(4)
    if((0 OR 1
       )
      ) # could also be aligned with the `i` in `if`
        set(foobar
        )
    endif()
    set(foobar)
endif()
```
will be changed to:
```
if(4)
    if((0 OR 1
)
    )
    set(foobar
    )
endif()
set(foobar)
endif()
```
whereas with the previous vim indentation code the result would have been:
```
if(4)
    if((0 OR 1
        )
    )
set(foobar
    )
    endif()
    set(foobar)
endif()
```
which is not great but better than above. I hope that this is acceptable.


Note: Apart from the actual indentation fixes, I based the change on a version
of indent/cmake.vim I found in the debian/bookworm vim82 package which is newer
than the one in the cmake repository (with `Last Change:  2017 Sep 24` comment
instead of the cmake repo version with `Last Change:  2017 Aug 30`).

This vim82/debian version moved these two lines:
```
let s:keepcpo= &cpo
set cpo&vim
```
a bit further down (after an early exit check - which seems to make sense to
me).


Fixes: #22394
pull/350/head
Kai Tetzlaff 3 years ago
parent
commit
b14cfbe159
  1. 48
      Auxiliary/vim/indent/cmake.vim

48
Auxiliary/vim/indent/cmake.vim

@ -3,7 +3,7 @@
" Author: Andy Cedilnik <andy.cedilnik@kitware.com>
" Maintainer: Dimitri Merejkowsky <d.merej@gmail.com>
" Former Maintainer: Karthik Krishnan <karthik.krishnan@kitware.com>
" Last Change: 2017 Aug 30
" Last Change: 2022 Mar 22
"
" License: The CMake license applies to this file. See
" https://cmake.org/licensing
@ -14,9 +14,6 @@ if exists("b:did_indent")
endif
let b:did_indent = 1
let s:keepcpo= &cpo
set cpo&vim
setlocal indentexpr=CMakeGetIndent(v:lnum)
setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
@ -24,6 +21,8 @@ setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
if exists("*CMakeGetIndent")
finish
endif
let s:keepcpo= &cpo
set cpo&vim
fun! CMakeGetIndent(lnum)
let this_line = getline(a:lnum)
@ -54,32 +53,41 @@ fun! CMakeGetIndent(lnum)
let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
\ '\s*(' . cmake_regex_arguments .
\ '\(' . cmake_regex_comment . '\)\?$'
let cmake_indent_close_regex = '^' . cmake_regex_arguments .
\ ')\s*' .
\ '\(' . cmake_regex_comment . '\)\?$'
let cmake_closing_parens_line = '^\s*\()\+\)\s*$'
let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
" Add
if previous_line =~? cmake_indent_comment_line " Handle comments
let ind = ind
else
if previous_line =~? cmake_indent_begin_regex
let ind = ind + shiftwidth()
if this_line =~? cmake_closing_parens_line
if previous_line !~? cmake_indent_open_regex
let ind = ind - shiftwidth()
endif
if previous_line =~? cmake_indent_open_regex
let ind = ind + shiftwidth()
else
" Add
if previous_line =~? cmake_indent_comment_line " Handle comments
let ind = ind
else
if previous_line =~? cmake_indent_begin_regex
let ind = ind + shiftwidth()
endif
if previous_line =~? cmake_indent_open_regex
let ind = ind + shiftwidth()
endif
endif
endif
" Subtract
if this_line =~? cmake_indent_end_regex
let ind = ind - shiftwidth()
endif
if previous_line =~? cmake_indent_close_regex
let ind = ind - shiftwidth()
" Subtract
if this_line =~? cmake_indent_end_regex
let ind = ind - shiftwidth()
endif
if previous_line !~? cmake_closing_parens_line
if previous_line =~? cmake_indent_close_regex
let ind = ind - shiftwidth()
endif
endif
endif
return ind

Loading…
Cancel
Save