From 0b6b23e0cc8acdb7501ae69aae33f17b38893d76 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 11 Jul 2019 14:52:01 +0200 Subject: [PATCH 01/11] Update based on current Vim runtime version --- indent/cmake.vim | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/indent/cmake.vim b/indent/cmake.vim index 33e583d..845bdd7 100644 --- a/indent/cmake.vim +++ b/indent/cmake.vim @@ -3,7 +3,7 @@ " Author: Andy Cedilnik " Maintainer: Dimitri Merejkowsky " Former Maintainer: Karthik Krishnan -" Last Change: 2017 Aug 30 +" Last Change: 2017 Sep 24 " " Licence: 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) From 9a302656eea783e7a3399b56534259eb405478a4 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 11 Jul 2019 14:53:24 +0200 Subject: [PATCH 02/11] Use indent of opening parenthesis after closing one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously it would use indent 11 for lines after: set_property(TARGET foo APPEND PROPERTY INCLUDE_DIRECTORIES ${BAR}) set_property(…) Now it uses: set_property(TARGET foo APPEND PROPERTY INCLUDE_DIRECTORIES ${BAR}) set_property(…) --- indent/cmake.vim | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/indent/cmake.vim b/indent/cmake.vim index 845bdd7..62a4078 100644 --- a/indent/cmake.vim +++ b/indent/cmake.vim @@ -55,7 +55,7 @@ fun! CMakeGetIndent(lnum) \ '\(' . cmake_regex_comment . '\)\?$' let cmake_indent_close_regex = '^' . cmake_regex_arguments . - \ ')\s*' . + \ '\zs)\s*' . \ '\(' . cmake_regex_comment . '\)\?$' let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*(' @@ -77,8 +77,17 @@ fun! CMakeGetIndent(lnum) if this_line =~? cmake_indent_end_regex let ind = ind - shiftwidth() endif - if previous_line =~? cmake_indent_close_regex - let ind = ind - shiftwidth() + " Use indent of the line with the opening parenthesis. + let m = matchstrpos(previous_line, cmake_indent_close_regex) + if !empty(m[0]) + " Go to closing parenthesis. + call cursor(lnum, m[1], m[2]) + let pairpos = searchpairpos('(', '', ')', 'bW', '', 0, 500) + if pairpos[0] != 0 + let ind = indent(pairpos[0]) + else + let ind = ind - shiftwidth() + endif endif return ind From 5efa173c2583cbad802f9e74c603f6c0095349f0 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 11 Jul 2019 15:02:56 +0200 Subject: [PATCH 03/11] use script-local variables (only defined once) --- indent/cmake.vim | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/indent/cmake.vim b/indent/cmake.vim index 62a4078..ae20c76 100644 --- a/indent/cmake.vim +++ b/indent/cmake.vim @@ -24,6 +24,28 @@ endif let s:keepcpo= &cpo set cpo&vim +let s:or = '\|' +" Regular expressions used by line indentation function. +let s:cmake_regex_comment = '#.*' +let s:cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*' +let s:cmake_regex_quoted = '"\([^"\\]\|\\.\)*"' +let s:cmake_regex_arguments = '\(' . s:cmake_regex_quoted . + \ s:or . '\$(' . s:cmake_regex_identifier . ')' . + \ s:or . '[^()\\#"]' . s:or . '\\.' . '\)*' + +let s:cmake_indent_comment_line = '^\s*' . s:cmake_regex_comment +let s:cmake_indent_blank_regex = '^\s*$' +let s:cmake_indent_open_regex = '^\s*' . s:cmake_regex_identifier . + \ '\s*(' . s:cmake_regex_arguments . + \ '\(' . s:cmake_regex_comment . '\)\?$' + +let s:cmake_indent_close_regex = '^' . s:cmake_regex_arguments . + \ '\zs)\s*' . + \ '\(' . s:cmake_regex_comment . '\)\?$' + +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*(' + fun! CMakeGetIndent(lnum) let this_line = getline(a:lnum) @@ -39,46 +61,24 @@ fun! CMakeGetIndent(lnum) let ind = indent(lnum) - let or = '\|' - " Regular expressions used by line indentation function. - let cmake_regex_comment = '#.*' - let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*' - let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"' - let cmake_regex_arguments = '\(' . cmake_regex_quoted . - \ or . '\$(' . cmake_regex_identifier . ')' . - \ or . '[^()\\#"]' . or . '\\.' . '\)*' - - let cmake_indent_comment_line = '^\s*' . cmake_regex_comment - let cmake_indent_blank_regex = '^\s*$' - let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier . - \ '\s*(' . cmake_regex_arguments . - \ '\(' . cmake_regex_comment . '\)\?$' - - let cmake_indent_close_regex = '^' . cmake_regex_arguments . - \ '\zs)\s*' . - \ '\(' . cmake_regex_comment . '\)\?$' - - 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 + if previous_line =~? s:cmake_indent_comment_line " Handle comments let ind = ind else - if previous_line =~? cmake_indent_begin_regex + if previous_line =~? s:cmake_indent_begin_regex let ind = ind + shiftwidth() endif - if previous_line =~? cmake_indent_open_regex + if previous_line =~? s:cmake_indent_open_regex let ind = ind + shiftwidth() endif endif " Subtract - if this_line =~? cmake_indent_end_regex + if this_line =~? s:cmake_indent_end_regex let ind = ind - shiftwidth() endif " Use indent of the line with the opening parenthesis. - let m = matchstrpos(previous_line, cmake_indent_close_regex) + let m = matchstrpos(previous_line, s:cmake_indent_close_regex) if !empty(m[0]) " Go to closing parenthesis. call cursor(lnum, m[1], m[2]) From dda2f7038d167481834812ae6e8924c802efec78 Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Tue, 16 Jul 2019 15:47:10 +0200 Subject: [PATCH 04/11] tests: add indent-tests (no references yet) --- test/indent1.cmake | 10 ++++++++++ test/indent2.cmake | 19 +++++++++++++++++++ test/indent3.cmake | 6 ++++++ test/indent4.cmake | 12 ++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 test/indent1.cmake create mode 100644 test/indent2.cmake create mode 100644 test/indent3.cmake create mode 100644 test/indent4.cmake diff --git a/test/indent1.cmake b/test/indent1.cmake new file mode 100644 index 0000000..a2629da --- /dev/null +++ b/test/indent1.cmake @@ -0,0 +1,10 @@ +set_property(TARGET foo APPEND PROPERTY + INCLUDE_DIRECTORIES ${BAR} + +message(STATUS "Hello World") + +# expected: +#set_property(TARGET foo APPEND PROPERTY +# INCLUDE_DIRECTORIES ${BAR} +# +# message(STATUS "Hello World") diff --git a/test/indent2.cmake b/test/indent2.cmake new file mode 100644 index 0000000..f6e5ec9 --- /dev/null +++ b/test/indent2.cmake @@ -0,0 +1,19 @@ +set_property(TARGET foo APPEND PROPERTY + INCLUDE_DIRECTORIES ${BAR}) +message(STATUS "Hello World") + + +set_property(TARGET foo APPEND PROPERTY + INCLUDE_DIRECTORIES ${BAR}) +message(STATUS "Hello World") + + +# expected: +#set_property(TARGET foo APPEND PROPERTY +# INCLUDE_DIRECTORIES ${BAR}) +#message(STATUS "Hello World") +# +# +#set_property(TARGET foo APPEND PROPERTY +# INCLUDE_DIRECTORIES ${BAR}) +#message(STATUS "Hello World") diff --git a/test/indent3.cmake b/test/indent3.cmake new file mode 100644 index 0000000..6072877 --- /dev/null +++ b/test/indent3.cmake @@ -0,0 +1,6 @@ +set_property(TARGET foo APPEND PROPERTY INCLUDE_DIRECTORIES ${BAR}) +message(STATUS "Hello World") + +# expected +#set_property(TARGET foo APPEND PROPERTY INCLUDE_DIRECTORIES ${BAR}) +#message(STATUS "Hello World") diff --git a/test/indent4.cmake b/test/indent4.cmake new file mode 100644 index 0000000..37c1030 --- /dev/null +++ b/test/indent4.cmake @@ -0,0 +1,12 @@ +set_property(TARGET foo APPEND PROPERTY +# with comment + INCLUDE_DIRECTORIES ${BAR}) + +message(STATUS "Hello World") + +# expected +#set_property(TARGET foo APPEND PROPERTY +# # with comment +# INCLUDE_DIRECTORIES ${BAR}) +# +#message(STATUS "Hello World") From c1b6f47ca965321d1da946b4b8c8794f191c20be Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Wed, 17 Jul 2019 11:41:40 +0200 Subject: [PATCH 05/11] indent: rewrite, now command-args are align to open parenthesis --- indent/cmake.vim | 59 ++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/indent/cmake.vim b/indent/cmake.vim index ae20c76..594aa7b 100644 --- a/indent/cmake.vim +++ b/indent/cmake.vim @@ -21,6 +21,7 @@ setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE( if exists("*CMakeGetIndent") finish endif + let s:keepcpo= &cpo set cpo&vim @@ -34,7 +35,6 @@ let s:cmake_regex_arguments = '\(' . s:cmake_regex_quoted . \ s:or . '[^()\\#"]' . s:or . '\\.' . '\)*' let s:cmake_indent_comment_line = '^\s*' . s:cmake_regex_comment -let s:cmake_indent_blank_regex = '^\s*$' let s:cmake_indent_open_regex = '^\s*' . s:cmake_regex_identifier . \ '\s*(' . s:cmake_regex_arguments . \ '\(' . s:cmake_regex_comment . '\)\?$' @@ -49,10 +49,20 @@ let s:cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\ fun! CMakeGetIndent(lnum) let this_line = getline(a:lnum) - " Find a non-blank line above the current line. - let lnum = a:lnum - let lnum = prevnonblank(lnum - 1) - let previous_line = getline(lnum) + let lnum = a:lnum - 1 + + " Find a non-blank/non-comment line above the current line. + while lnum > 0 + let lnum = prevnonblank(lnum) + let previous_line = getline(lnum) + + " ignore comments (# only, lua-like comment TODO) + if previous_line !~? s:cmake_indent_comment_line + break + endif + + let lnum -= 1 + endwhile " Hit the start of the file, use zero indent. if lnum == 0 @@ -61,34 +71,25 @@ fun! CMakeGetIndent(lnum) let ind = indent(lnum) - " Add - if previous_line =~? s:cmake_indent_comment_line " Handle comments - let ind = ind - else - if previous_line =~? s:cmake_indent_begin_regex - let ind = ind + shiftwidth() - endif - if previous_line =~? s:cmake_indent_open_regex - let ind = ind + shiftwidth() - endif + 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 + let ind = s[1] + 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') + if pairpos[0] != 0 + let ind = indent(pairpos[0]) + endif endif - " Subtract - if this_line =~? s:cmake_indent_end_regex + if previous_line =~? s:cmake_indent_begin_regex " control begin block + let ind = ind + shiftwidth() + elseif this_line =~? s:cmake_indent_end_regex " control end block let ind = ind - shiftwidth() endif - " Use indent of the line with the opening parenthesis. - let m = matchstrpos(previous_line, s:cmake_indent_close_regex) - if !empty(m[0]) - " Go to closing parenthesis. - call cursor(lnum, m[1], m[2]) - let pairpos = searchpairpos('(', '', ')', 'bW', '', 0, 500) - if pairpos[0] != 0 - let ind = indent(pairpos[0]) - else - let ind = ind - shiftwidth() - endif - endif return ind endfun From baa205af25deb08a175bff3a12ed14e61cee87c7 Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Wed, 17 Jul 2019 13:27:06 +0200 Subject: [PATCH 06/11] indent: add some references and tests --- test/.vimrc | 13 ++++++++++++- test/indent1.cmake | 10 +++------- test/indent1.cmake.html.ref | 10 ++++++++++ test/indent2.cmake | 15 ++------------- test/indent2.cmake.html.ref | 12 ++++++++++++ test/indent3.cmake | 4 ---- test/indent3.cmake.html.ref | 6 ++++++ test/indent4.cmake | 32 ++++++++++++++++++++++++-------- test/indent4.cmake.html.ref | 32 ++++++++++++++++++++++++++++++++ test/run-test.sh | 9 ++++++++- 10 files changed, 109 insertions(+), 34 deletions(-) create mode 100644 test/indent1.cmake.html.ref create mode 100644 test/indent2.cmake.html.ref create mode 100644 test/indent3.cmake.html.ref create mode 100644 test/indent4.cmake.html.ref diff --git a/test/.vimrc b/test/.vimrc index 7aa0135..d561df2 100644 --- a/test/.vimrc +++ b/test/.vimrc @@ -3,10 +3,21 @@ " remove user's .vimrc - what else? set runtimepath-=~/.vimrc -" add .. as vim-plugin-path (for syntax) +" add .. as vim-plugin-path (for syntax and indent) set runtimepath^=../ " nocompat is needed for html-output set nocompatible +source ../syntax/cmake.vim +source ../indent/cmake.vim + +set expandtab +set nocopyindent +set nopreserveindent +set nosmartindent +set softtabstop=0 +set shiftwidth=4 +set tabstop=4 + syntax on diff --git a/test/indent1.cmake b/test/indent1.cmake index a2629da..605c33c 100644 --- a/test/indent1.cmake +++ b/test/indent1.cmake @@ -1,10 +1,6 @@ set_property(TARGET foo APPEND PROPERTY - INCLUDE_DIRECTORIES ${BAR} +INCLUDE_DIRECTORIES ${BAR} -message(STATUS "Hello World") +# closing parens "forgotten" -# expected: -#set_property(TARGET foo APPEND PROPERTY -# INCLUDE_DIRECTORIES ${BAR} -# -# message(STATUS "Hello World") +message(STATUS "Hello World") diff --git a/test/indent1.cmake.html.ref b/test/indent1.cmake.html.ref new file mode 100644 index 0000000..760afd6 --- /dev/null +++ b/test/indent1.cmake.html.ref @@ -0,0 +1,10 @@ + +
+set_property(TARGET foo APPEND PROPERTY
+             INCLUDE_DIRECTORIES ${BAR}
+
+             # closing parens "forgotten"
+
+             message(STATUS "Hello World")
+
+ diff --git a/test/indent2.cmake b/test/indent2.cmake index f6e5ec9..22b5451 100644 --- a/test/indent2.cmake +++ b/test/indent2.cmake @@ -1,19 +1,8 @@ set_property(TARGET foo APPEND PROPERTY - INCLUDE_DIRECTORIES ${BAR}) +INCLUDE_DIRECTORIES ${BAR}) message(STATUS "Hello World") set_property(TARGET foo APPEND PROPERTY - INCLUDE_DIRECTORIES ${BAR}) +INCLUDE_DIRECTORIES ${BAR}) message(STATUS "Hello World") - - -# expected: -#set_property(TARGET foo APPEND PROPERTY -# INCLUDE_DIRECTORIES ${BAR}) -#message(STATUS "Hello World") -# -# -#set_property(TARGET foo APPEND PROPERTY -# INCLUDE_DIRECTORIES ${BAR}) -#message(STATUS "Hello World") diff --git a/test/indent2.cmake.html.ref b/test/indent2.cmake.html.ref new file mode 100644 index 0000000..fb7d4d7 --- /dev/null +++ b/test/indent2.cmake.html.ref @@ -0,0 +1,12 @@ + +
+set_property(TARGET foo APPEND PROPERTY
+             INCLUDE_DIRECTORIES ${BAR})
+message(STATUS "Hello World")
+
+
+set_property(TARGET foo APPEND PROPERTY
+             INCLUDE_DIRECTORIES ${BAR})
+message(STATUS "Hello World")
+
+ diff --git a/test/indent3.cmake b/test/indent3.cmake index 6072877..e8e953b 100644 --- a/test/indent3.cmake +++ b/test/indent3.cmake @@ -1,6 +1,2 @@ set_property(TARGET foo APPEND PROPERTY INCLUDE_DIRECTORIES ${BAR}) message(STATUS "Hello World") - -# expected -#set_property(TARGET foo APPEND PROPERTY INCLUDE_DIRECTORIES ${BAR}) -#message(STATUS "Hello World") diff --git a/test/indent3.cmake.html.ref b/test/indent3.cmake.html.ref new file mode 100644 index 0000000..823f02e --- /dev/null +++ b/test/indent3.cmake.html.ref @@ -0,0 +1,6 @@ + +
+set_property(TARGET foo APPEND PROPERTY INCLUDE_DIRECTORIES ${BAR})
+message(STATUS "Hello World")
+
+ diff --git a/test/indent4.cmake b/test/indent4.cmake index 37c1030..af406ab 100644 --- a/test/indent4.cmake +++ b/test/indent4.cmake @@ -1,12 +1,28 @@ set_property(TARGET foo APPEND PROPERTY -# with comment - INCLUDE_DIRECTORIES ${BAR}) +INCLUDE_DIRECTORIES ${BAR}) + +message(STATUS "Hello World") + +set_property(TARGET foo APPEND PROPERTY # with comment +INCLUDE_DIRECTORIES ${BAR} "()") + +if(VAR) +set_property(TARGET foo APPEND PROPERTY # with comment +INCLUDE_DIRECTORIES ${BAR}) message(STATUS "Hello World") -# expected -#set_property(TARGET foo APPEND PROPERTY -# # with comment -# INCLUDE_DIRECTORIES ${BAR}) -# -#message(STATUS "Hello World") +set_property(TARGET foo APPEND PROPERTY +# with comment +INCLUDE_DIRECTORIES ${BAR}) +endif() + +if(VAR) +set_property(TARGET +HELLO +HELLO) +endif() + +message(STATUS "Hello" #[[Bracket Comment]] "second") + +add_custom_command() # TODO this will wrongly align to ( due to bracket-comment diff --git a/test/indent4.cmake.html.ref b/test/indent4.cmake.html.ref new file mode 100644 index 0000000..9e249ea --- /dev/null +++ b/test/indent4.cmake.html.ref @@ -0,0 +1,32 @@ + +
+set_property(TARGET foo APPEND PROPERTY
+             INCLUDE_DIRECTORIES ${BAR})
+
+message(STATUS "Hello World")
+
+set_property(TARGET foo APPEND PROPERTY # with comment
+             INCLUDE_DIRECTORIES ${BAR} "()")
+
+if(VAR)
+    set_property(TARGET foo APPEND PROPERTY # with comment
+                 INCLUDE_DIRECTORIES ${BAR})
+
+    message(STATUS "Hello World")
+
+    set_property(TARGET foo APPEND PROPERTY
+                 # with comment
+                 INCLUDE_DIRECTORIES ${BAR})
+endif()
+
+if(VAR)
+    set_property(TARGET
+                 HELLO
+                 HELLO)
+endif()
+
+message(STATUS "Hello" #[[Bracket Comment]] "second")
+
+        add_custom_command() # TODO this will wrongly align to ( due to bracket-comment
+
+ diff --git a/test/run-test.sh b/test/run-test.sh index 35fa48b..025e954 100755 --- a/test/run-test.sh +++ b/test/run-test.sh @@ -2,8 +2,15 @@ TMP=$(mktemp) +if [[ $1 == *"indent"* ]] +then + INDENT="norm gg=G" +else + INDENT="norm gg" +fi + # generate html-file with local .vimrc and local syntax highlighting and only that! -vim -u .vimrc -n -es -c TOhtml -c "w! $TMP" -c 'qa!' $1.cmake >/dev/null 2>&1 +vim -u .vimrc -n -es -c "$INDENT" -c "w! $TMP.cmake" -c TOhtml -c "w! $TMP" -c 'qa!' $1.cmake >/dev/null 2>&1 # extract the body of the html-file sed -i -n -e '//,$p' $TMP From 735ac305bc1f1bbef9f2d59e681738bad84ea664 Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Wed, 17 Jul 2019 13:52:39 +0200 Subject: [PATCH 07/11] indent: do not align with parenthesis if first word is on the next line --- indent/cmake.vim | 6 +++++- test/indent4.cmake | 7 +++++++ test/indent4.cmake.html.ref | 7 +++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/indent/cmake.vim b/indent/cmake.vim index 594aa7b..0902f62 100644 --- a/indent/cmake.vim +++ b/indent/cmake.vim @@ -75,7 +75,11 @@ fun! CMakeGetIndent(lnum) 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 - let ind = s[1] + if strlen(previous_line) == s[1] " if ( is the last char on this line, do not align with ( but use sw() + let ind += shiftwidth() + else + let ind = s[1] + endif endif elseif previous_line =~? s:cmake_indent_close_regex " close parenthesis call cursor(lnum, strlen(previous_line)) diff --git a/test/indent4.cmake b/test/indent4.cmake index af406ab..71d2538 100644 --- a/test/indent4.cmake +++ b/test/indent4.cmake @@ -23,6 +23,13 @@ HELLO HELLO) endif() +if(VAR) +set_property( +TARGET +HELLO +HELLO) +endif() + message(STATUS "Hello" #[[Bracket Comment]] "second") add_custom_command() # TODO this will wrongly align to ( due to bracket-comment diff --git a/test/indent4.cmake.html.ref b/test/indent4.cmake.html.ref index 9e249ea..c6a6c23 100644 --- a/test/indent4.cmake.html.ref +++ b/test/indent4.cmake.html.ref @@ -25,6 +25,13 @@ HELLO) endif() +if(VAR) + set_property( + TARGET + HELLO + HELLO) +endif() + message(STATUS "Hello" #[[Bracket Comment]] "second") add_custom_command() # TODO this will wrongly align to ( due to bracket-comment From 80dbacf15088f870d1bac2870d5af87a05d0d27d Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Wed, 17 Jul 2019 16:48:46 +0200 Subject: [PATCH 08/11] indent: fix #15 and update README --- README.md | 35 +++++++++++++++++++++++++++++++++++ indent/cmake.vim | 20 +++++++++++++++----- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ff251ac..49a118a 100644 --- a/README.md +++ b/README.md @@ -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/ diff --git a/indent/cmake.vim b/indent/cmake.vim index 0902f62..3ca1e9e 100644 --- a/indent/cmake.vim +++ b/indent/cmake.vim @@ -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) @@ -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 From 2d346d212481f96b33eb911332b1c9f108d4a03b Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Mon, 22 Jul 2019 10:48:46 +0200 Subject: [PATCH 09/11] indent: by default old indent-behavior (non-aligned) --- README.md | 12 ++++++--- indent/cmake.vim | 6 ++--- test/indent1.cmake.html.ref | 6 ++--- test/indent2.cmake.html.ref | 4 +-- test/indent4-align.cmake | 1 + test/indent4-align.cmake.html.ref | 44 +++++++++++++++++++++++++++++++ test/indent4-align.vim | 1 + test/indent4.cmake | 5 ++++ test/indent4.cmake.html.ref | 21 +++++++++------ test/run-test.sh | 9 ++++++- 10 files changed, 89 insertions(+), 20 deletions(-) create mode 120000 test/indent4-align.cmake create mode 100644 test/indent4-align.cmake.html.ref create mode 100644 test/indent4-align.vim diff --git a/README.md b/README.md index 49a118a..be465a6 100644 --- a/README.md +++ b/README.md @@ -25,12 +25,16 @@ With Vundle ## Indentation -There is also an indent file which does some intelligent alignment. +There is also an indent file which can do some intelligent alignment. + +### Control-statements 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. +### Command arguments + +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: @@ -50,7 +54,9 @@ add_custom_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: +This is achieved by letting `g:cmake_indent_align_command_arguments` to be 1. + +By setting it to 0 (the default) in your vimrc-file the standard behavior is achieved. ```cmake add_custom_target(TARGET name diff --git a/indent/cmake.vim b/indent/cmake.vim index 3ca1e9e..f0e0cbd 100644 --- a/indent/cmake.vim +++ b/indent/cmake.vim @@ -46,8 +46,8 @@ 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 +if !exists('g:cmake_indent_align_command_arguments') + let g:cmake_indent_align_command_arguments = 0 endif fun! CMakeGetIndent(lnum) @@ -84,7 +84,7 @@ fun! CMakeGetIndent(lnum) 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 + if g:cmake_indent_align_command_arguments == 0 " old behavior let ind += shiftwidth() else let ind = s[1] diff --git a/test/indent1.cmake.html.ref b/test/indent1.cmake.html.ref index 760afd6..c3cfbe0 100644 --- a/test/indent1.cmake.html.ref +++ b/test/indent1.cmake.html.ref @@ -1,10 +1,10 @@
 set_property(TARGET foo APPEND PROPERTY
-             INCLUDE_DIRECTORIES ${BAR}
+    INCLUDE_DIRECTORIES ${BAR}
 
-             # closing parens "forgotten"
+    # closing parens "forgotten"
 
-             message(STATUS "Hello World")
+    message(STATUS "Hello World")
 
diff --git a/test/indent2.cmake.html.ref b/test/indent2.cmake.html.ref index fb7d4d7..c976b82 100644 --- a/test/indent2.cmake.html.ref +++ b/test/indent2.cmake.html.ref @@ -1,12 +1,12 @@
 set_property(TARGET foo APPEND PROPERTY
-             INCLUDE_DIRECTORIES ${BAR})
+    INCLUDE_DIRECTORIES ${BAR})
 message(STATUS "Hello World")
 
 
 set_property(TARGET foo APPEND PROPERTY
-             INCLUDE_DIRECTORIES ${BAR})
+    INCLUDE_DIRECTORIES ${BAR})
 message(STATUS "Hello World")
 
diff --git a/test/indent4-align.cmake b/test/indent4-align.cmake new file mode 120000 index 0000000..aabc365 --- /dev/null +++ b/test/indent4-align.cmake @@ -0,0 +1 @@ +indent4.cmake \ No newline at end of file diff --git a/test/indent4-align.cmake.html.ref b/test/indent4-align.cmake.html.ref new file mode 100644 index 0000000..f4eefee --- /dev/null +++ b/test/indent4-align.cmake.html.ref @@ -0,0 +1,44 @@ + +
+set_property(TARGET foo APPEND PROPERTY
+             INCLUDE_DIRECTORIES ${BAR})
+
+message(STATUS "Hello World")
+
+set_property(TARGET foo APPEND PROPERTY # with comment
+             INCLUDE_DIRECTORIES ${BAR} "()")
+
+if(VAR)
+    set_property(TARGET foo APPEND PROPERTY # with comment
+                 INCLUDE_DIRECTORIES ${BAR})
+
+    message(STATUS "Hello World")
+
+    set_property(TARGET foo APPEND PROPERTY
+                 # with comment
+                 INCLUDE_DIRECTORIES ${BAR})
+endif()
+
+if(VAR)
+    set_property(TARGET
+                 HELLO
+                 HELLO)
+endif()
+
+if(VAR)
+    set_property(
+        TARGET
+        HELLO
+        HELLO)
+endif()
+
+add_custom_command(tout # )
+                   #add_custom_command(
+                   hallo
+                   )
+
+message(STATUS "Hello" #[[Bracket Comment]] "second")
+
+        add_custom_command() # TODO this will wrongly align to ( due to bracket-comment
+
+ diff --git a/test/indent4-align.vim b/test/indent4-align.vim new file mode 100644 index 0000000..04e9c7e --- /dev/null +++ b/test/indent4-align.vim @@ -0,0 +1 @@ +let g:cmake_indent_align_command_arguments = 1 diff --git a/test/indent4.cmake b/test/indent4.cmake index 71d2538..90d47dd 100644 --- a/test/indent4.cmake +++ b/test/indent4.cmake @@ -30,6 +30,11 @@ HELLO HELLO) endif() +add_custom_command(tout # ) +#add_custom_command( +hallo +) + message(STATUS "Hello" #[[Bracket Comment]] "second") add_custom_command() # TODO this will wrongly align to ( due to bracket-comment diff --git a/test/indent4.cmake.html.ref b/test/indent4.cmake.html.ref index c6a6c23..0422263 100644 --- a/test/indent4.cmake.html.ref +++ b/test/indent4.cmake.html.ref @@ -1,28 +1,28 @@
 set_property(TARGET foo APPEND PROPERTY
-             INCLUDE_DIRECTORIES ${BAR})
+    INCLUDE_DIRECTORIES ${BAR})
 
 message(STATUS "Hello World")
 
 set_property(TARGET foo APPEND PROPERTY # with comment
-             INCLUDE_DIRECTORIES ${BAR} "()")
+    INCLUDE_DIRECTORIES ${BAR} "()")
 
 if(VAR)
     set_property(TARGET foo APPEND PROPERTY # with comment
-                 INCLUDE_DIRECTORIES ${BAR})
+        INCLUDE_DIRECTORIES ${BAR})
 
     message(STATUS "Hello World")
 
     set_property(TARGET foo APPEND PROPERTY
-                 # with comment
-                 INCLUDE_DIRECTORIES ${BAR})
+        # with comment
+        INCLUDE_DIRECTORIES ${BAR})
 endif()
 
 if(VAR)
     set_property(TARGET
-                 HELLO
-                 HELLO)
+        HELLO
+        HELLO)
 endif()
 
 if(VAR)
@@ -32,8 +32,13 @@
         HELLO)
 endif()
 
+add_custom_command(tout # )
+    #add_custom_command(
+    hallo
+    )
+
 message(STATUS "Hello" #[[Bracket Comment]] "second")
 
-        add_custom_command() # TODO this will wrongly align to ( due to bracket-comment
+    add_custom_command() # TODO this will wrongly align to ( due to bracket-comment
 
diff --git a/test/run-test.sh b/test/run-test.sh index 025e954..596aa0b 100755 --- a/test/run-test.sh +++ b/test/run-test.sh @@ -9,8 +9,15 @@ else INDENT="norm gg" fi +if [ -e "$1.vim" ] +then + OPTION_FILE="source $1.vim" +else + OPTION_FILE="" +fi + # generate html-file with local .vimrc and local syntax highlighting and only that! -vim -u .vimrc -n -es -c "$INDENT" -c "w! $TMP.cmake" -c TOhtml -c "w! $TMP" -c 'qa!' $1.cmake >/dev/null 2>&1 +vim -u .vimrc -n -es -c "$OPTION_FILE" -c "$INDENT" -c "w! $TMP.cmake" -c TOhtml -c "w! $TMP" -c 'qa!' $1.cmake >/dev/null 2>&1 # extract the body of the html-file sed -i -n -e '//,$p' $TMP From 7c77415b1b284c40c680f8d82d5b27db8c36a7e7 Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Mon, 22 Jul 2019 10:51:24 +0200 Subject: [PATCH 10/11] indent: option to allow comments to be aligned to 0-column --- README.md | 5 +++ indent/cmake.vim | 10 ++++- test/indent4-align-comment-zero-col.cmake | 40 +++++++++++++++++ ...ent4-align-comment-zero-col.cmake.html.ref | 44 +++++++++++++++++++ test/indent4-align-comment-zero-col.vim | 2 + test/indent4-comment-zero-col.cmake | 1 + test/indent4-comment-zero-col.cmake.html.ref | 44 +++++++++++++++++++ test/indent4-comment-zero-col.vim | 1 + 8 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 test/indent4-align-comment-zero-col.cmake create mode 100644 test/indent4-align-comment-zero-col.cmake.html.ref create mode 100644 test/indent4-align-comment-zero-col.vim create mode 120000 test/indent4-comment-zero-col.cmake create mode 100644 test/indent4-comment-zero-col.cmake.html.ref create mode 100644 test/indent4-comment-zero-col.vim diff --git a/README.md b/README.md index be465a6..9d1e828 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,11 @@ add_custom_target(TARGET name WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) ``` +### Comments + +By setting `g:cmake_indent_align_comments_to_first_column` to 1 (default: 0) comment will always be aligned to the +zero column - otherwise they will aligned as normal line with a statement. + ## Test There is a ever growing test-suite based on ctest located in test/ diff --git a/indent/cmake.vim b/indent/cmake.vim index f0e0cbd..4a0b356 100644 --- a/indent/cmake.vim +++ b/indent/cmake.vim @@ -50,6 +50,10 @@ if !exists('g:cmake_indent_align_command_arguments') let g:cmake_indent_align_command_arguments = 0 endif +if !exists('g:cmake_indent_align_comments_to_first_column') + let g:cmake_indent_align_comments_to_first_column = 0 +endif + fun! CMakeGetIndent(lnum) let this_line = getline(a:lnum) @@ -102,7 +106,11 @@ fun! CMakeGetIndent(lnum) if previous_line =~? s:cmake_indent_begin_regex " control begin block let ind = ind + shiftwidth() elseif this_line =~? s:cmake_indent_end_regex " control end block - let ind = ind - shiftwidth() + let ind = ind - shiftwidth() + elseif this_line =~? s:cmake_indent_comment_line + if g:cmake_indent_align_comments_to_first_column == 1 + let ind = 0 + endif endif return ind diff --git a/test/indent4-align-comment-zero-col.cmake b/test/indent4-align-comment-zero-col.cmake new file mode 100644 index 0000000..90d47dd --- /dev/null +++ b/test/indent4-align-comment-zero-col.cmake @@ -0,0 +1,40 @@ +set_property(TARGET foo APPEND PROPERTY +INCLUDE_DIRECTORIES ${BAR}) + +message(STATUS "Hello World") + +set_property(TARGET foo APPEND PROPERTY # with comment +INCLUDE_DIRECTORIES ${BAR} "()") + +if(VAR) +set_property(TARGET foo APPEND PROPERTY # with comment +INCLUDE_DIRECTORIES ${BAR}) + +message(STATUS "Hello World") + +set_property(TARGET foo APPEND PROPERTY +# with comment +INCLUDE_DIRECTORIES ${BAR}) +endif() + +if(VAR) +set_property(TARGET +HELLO +HELLO) +endif() + +if(VAR) +set_property( +TARGET +HELLO +HELLO) +endif() + +add_custom_command(tout # ) +#add_custom_command( +hallo +) + +message(STATUS "Hello" #[[Bracket Comment]] "second") + +add_custom_command() # TODO this will wrongly align to ( due to bracket-comment diff --git a/test/indent4-align-comment-zero-col.cmake.html.ref b/test/indent4-align-comment-zero-col.cmake.html.ref new file mode 100644 index 0000000..b71d823 --- /dev/null +++ b/test/indent4-align-comment-zero-col.cmake.html.ref @@ -0,0 +1,44 @@ + +
+set_property(TARGET foo APPEND PROPERTY
+             INCLUDE_DIRECTORIES ${BAR})
+
+message(STATUS "Hello World")
+
+set_property(TARGET foo APPEND PROPERTY # with comment
+             INCLUDE_DIRECTORIES ${BAR} "()")
+
+if(VAR)
+    set_property(TARGET foo APPEND PROPERTY # with comment
+                 INCLUDE_DIRECTORIES ${BAR})
+
+    message(STATUS "Hello World")
+
+    set_property(TARGET foo APPEND PROPERTY
+# with comment
+                 INCLUDE_DIRECTORIES ${BAR})
+endif()
+
+if(VAR)
+    set_property(TARGET
+                 HELLO
+                 HELLO)
+endif()
+
+if(VAR)
+    set_property(
+        TARGET
+        HELLO
+        HELLO)
+endif()
+
+add_custom_command(tout # )
+#add_custom_command(
+                   hallo
+                   )
+
+message(STATUS "Hello" #[[Bracket Comment]] "second")
+
+        add_custom_command() # TODO this will wrongly align to ( due to bracket-comment
+
+ diff --git a/test/indent4-align-comment-zero-col.vim b/test/indent4-align-comment-zero-col.vim new file mode 100644 index 0000000..d389041 --- /dev/null +++ b/test/indent4-align-comment-zero-col.vim @@ -0,0 +1,2 @@ +let g:cmake_indent_align_command_arguments = 1 +let g:cmake_indent_align_comments_to_first_column = 1 diff --git a/test/indent4-comment-zero-col.cmake b/test/indent4-comment-zero-col.cmake new file mode 120000 index 0000000..aabc365 --- /dev/null +++ b/test/indent4-comment-zero-col.cmake @@ -0,0 +1 @@ +indent4.cmake \ No newline at end of file diff --git a/test/indent4-comment-zero-col.cmake.html.ref b/test/indent4-comment-zero-col.cmake.html.ref new file mode 100644 index 0000000..2870e35 --- /dev/null +++ b/test/indent4-comment-zero-col.cmake.html.ref @@ -0,0 +1,44 @@ + +
+set_property(TARGET foo APPEND PROPERTY
+    INCLUDE_DIRECTORIES ${BAR})
+
+message(STATUS "Hello World")
+
+set_property(TARGET foo APPEND PROPERTY # with comment
+    INCLUDE_DIRECTORIES ${BAR} "()")
+
+if(VAR)
+    set_property(TARGET foo APPEND PROPERTY # with comment
+        INCLUDE_DIRECTORIES ${BAR})
+
+    message(STATUS "Hello World")
+
+    set_property(TARGET foo APPEND PROPERTY
+# with comment
+        INCLUDE_DIRECTORIES ${BAR})
+endif()
+
+if(VAR)
+    set_property(TARGET
+        HELLO
+        HELLO)
+endif()
+
+if(VAR)
+    set_property(
+        TARGET
+        HELLO
+        HELLO)
+endif()
+
+add_custom_command(tout # )
+#add_custom_command(
+    hallo
+    )
+
+message(STATUS "Hello" #[[Bracket Comment]] "second")
+
+    add_custom_command() # TODO this will wrongly align to ( due to bracket-comment
+
+ diff --git a/test/indent4-comment-zero-col.vim b/test/indent4-comment-zero-col.vim new file mode 100644 index 0000000..4cc8878 --- /dev/null +++ b/test/indent4-comment-zero-col.vim @@ -0,0 +1 @@ +let g:cmake_indent_align_comments_to_first_column = 1 From 7edeedb8b9ca35250b66edb7e33ff7c545e273d3 Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Wed, 24 Jul 2019 10:07:25 +0200 Subject: [PATCH 11/11] indent: fix end-of-control-block indent if block is empty --- indent/cmake.vim | 4 +++- test/if-endif-indent.cmake | 2 ++ test/if-endif-indent.cmake.html.ref | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/if-endif-indent.cmake create mode 100644 test/if-endif-indent.cmake.html.ref diff --git a/indent/cmake.vim b/indent/cmake.vim index 4a0b356..57343c4 100644 --- a/indent/cmake.vim +++ b/indent/cmake.vim @@ -105,7 +105,9 @@ fun! CMakeGetIndent(lnum) if previous_line =~? s:cmake_indent_begin_regex " control begin block let ind = ind + shiftwidth() - elseif this_line =~? s:cmake_indent_end_regex " control end block + endif + + if this_line =~? s:cmake_indent_end_regex " control end block let ind = ind - shiftwidth() elseif this_line =~? s:cmake_indent_comment_line if g:cmake_indent_align_comments_to_first_column == 1 diff --git a/test/if-endif-indent.cmake b/test/if-endif-indent.cmake new file mode 100644 index 0000000..1c4eb32 --- /dev/null +++ b/test/if-endif-indent.cmake @@ -0,0 +1,2 @@ +if(HELLO) +endif() diff --git a/test/if-endif-indent.cmake.html.ref b/test/if-endif-indent.cmake.html.ref new file mode 100644 index 0000000..062a963 --- /dev/null +++ b/test/if-endif-indent.cmake.html.ref @@ -0,0 +1,6 @@ + +
+if(HELLO)
+endif()
+
+