From 0600724a8ba23fcef20e995d0f79269d8de75069 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 14 Jan 2019 23:42:57 +1100 Subject: [PATCH] Improve some corner cases in c-family auto-indenting --- rc/core/c-family.kak | 12 ++++++++---- test/indent/c-family/deindent-if-body/in | 8 ++++++++ test/indent/c-family/deindent-if-body/out | 10 ++++++++++ test/indent/c-family/deindent-if-body/state | 1 - test/indent/c-family/indent-if-body/in | 6 ++++++ test/indent/c-family/indent-if-body/out | 8 ++++++++ test/indent/c-family/indent-if-body/state | 1 - 7 files changed, 40 insertions(+), 6 deletions(-) delete mode 100644 test/indent/c-family/deindent-if-body/state delete mode 100644 test/indent/c-family/indent-if-body/state diff --git a/rc/core/c-family.kak b/rc/core/c-family.kak index c57ef586..1e8c4a94 100644 --- a/rc/core/c-family.kak +++ b/rc/core/c-family.kak @@ -34,8 +34,9 @@ define-command -hidden c-family-indent-on-newline %< evaluate-commands -draft -i # if previous line is part of a comment, do nothing execute-keys -draft /\* ^\h*[^/*\h] > catch %< - # else if previous line closed a paren, copy indent of the opening paren line - execute-keys -draft k 1s(\))(\h+\w+)*\h*(\;\h*)?$ mJ 1 + # else if previous line closed a paren (possibly followed by words and a comment), + # copy indent of the opening paren line + execute-keys -draft k 1s(\))(\h+\w+)*\h*(\;\h*)?(?://[^\n]+)?\n\z mJ 1 > catch %< # else indent new lines with the same level as the previous one execute-keys -draft K @@ -47,9 +48,12 @@ define-command -hidden c-family-indent-on-newline %< evaluate-commands -draft -i # indent after a label try %< execute-keys -draft k s[a-zA-Z0-9_-]+:\h*$ j > # indent after a statement not followed by an opening brace - try %< execute-keys -draft k \b(if|else|for|while)\h*(\(.*?\)\h*)?$ j > + try %< execute-keys -draft k s\)\h*(?://[^\n]+)?\n\z \ + mB \A\b(if|else|for|while)\b j > # deindent after a single line statement end - try %< execute-keys -draft K \;\h*$ K s\b(if|else|for|while)\h*(\(.*?\)\h*)?$|.\z 1 > + try %< execute-keys -draft K \;\h*(//[^\n]+)?$ \ + K s\)(\h+\w+)*\h*(//[^\n]+)?\n([^\n]*\n){2}\z \ + MB \A\b(if|else|for|while)\b 1 > # align to the opening parenthesis or opening brace (whichever is first) # on a previous line if its followed by text on the same line try %< evaluate-commands -draft %< diff --git a/test/indent/c-family/deindent-if-body/in b/test/indent/c-family/deindent-if-body/in index 3a64ea8b..5b2cc138 100644 --- a/test/indent/c-family/deindent-if-body/in +++ b/test/indent/c-family/deindent-if-body/in @@ -1,2 +1,10 @@ if (int i = bar()) foo();%( ) + +if (bar(a, b, + c, d)) + foo(); // comment%( ) + +if (bar(a, b, + c, d)) // comment + foo();%( ) diff --git a/test/indent/c-family/deindent-if-body/out b/test/indent/c-family/deindent-if-body/out index 62a0612f..543b5aa1 100644 --- a/test/indent/c-family/deindent-if-body/out +++ b/test/indent/c-family/deindent-if-body/out @@ -1,3 +1,13 @@ if (int i = bar()) foo(); baz + +if (bar(a, b, + c, d)) + foo(); // comment +baz + +if (bar(a, b, + c, d)) // comment + foo(); +baz diff --git a/test/indent/c-family/deindent-if-body/state b/test/indent/c-family/deindent-if-body/state deleted file mode 100644 index 88f47c8a..00000000 --- a/test/indent/c-family/deindent-if-body/state +++ /dev/null @@ -1 +0,0 @@ -3.4,3.4 diff --git a/test/indent/c-family/indent-if-body/in b/test/indent/c-family/indent-if-body/in index 1b99552b..0ac6763a 100644 --- a/test/indent/c-family/indent-if-body/in +++ b/test/indent/c-family/indent-if-body/in @@ -1 +1,7 @@ if (int i = bar())%( ) + +if (bar(a, b, + c, d))%( ) + +if (bar(a, b, + c, d)) // comment%( ) diff --git a/test/indent/c-family/indent-if-body/out b/test/indent/c-family/indent-if-body/out index d4596331..3b4b466f 100644 --- a/test/indent/c-family/indent-if-body/out +++ b/test/indent/c-family/indent-if-body/out @@ -1,2 +1,10 @@ if (int i = bar()) foo + +if (bar(a, b, + c, d)) + foo + +if (bar(a, b, + c, d)) // comment + foo diff --git a/test/indent/c-family/indent-if-body/state b/test/indent/c-family/indent-if-body/state deleted file mode 100644 index 82542193..00000000 --- a/test/indent/c-family/indent-if-body/state +++ /dev/null @@ -1 +0,0 @@ -2.8,2.8