From 5d689858df4d182a8ddf2188896ff23df81e2eb3 Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Thu, 28 Jun 2018 23:43:00 +0200 Subject: [PATCH 1/8] Improve comment-line command --- rc/core/comment.kak | 26 +++++++++++++++---- .../comment-multiple-lines-indented/cmd | 1 + .../comment-multiple-lines-indented/in | 4 +++ .../comment-multiple-lines-indented/out | 4 +++ .../comment-multiple-lines-indented/rc | 4 +++ .../comment-multiple-lines-unindented/cmd | 1 + .../comment-multiple-lines-unindented/in | 4 +++ .../comment-multiple-lines-unindented/out | 4 +++ .../comment-multiple-lines-unindented/rc | 4 +++ .../cmd | 1 + .../in | 3 +++ .../out | 3 +++ .../rc | 4 +++ 13 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 test/indent/comment/comment-multiple-lines-indented/cmd create mode 100644 test/indent/comment/comment-multiple-lines-indented/in create mode 100644 test/indent/comment/comment-multiple-lines-indented/out create mode 100644 test/indent/comment/comment-multiple-lines-indented/rc create mode 100644 test/indent/comment/comment-multiple-lines-unindented/cmd create mode 100644 test/indent/comment/comment-multiple-lines-unindented/in create mode 100644 test/indent/comment/comment-multiple-lines-unindented/out create mode 100644 test/indent/comment/comment-multiple-lines-unindented/rc create mode 100644 test/indent/comment/comment-multiple-lines-when-partially-commented/cmd create mode 100644 test/indent/comment/comment-multiple-lines-when-partially-commented/in create mode 100644 test/indent/comment/comment-multiple-lines-when-partially-commented/out create mode 100644 test/indent/comment/comment-multiple-lines-when-partially-commented/rc diff --git a/rc/core/comment.kak b/rc/core/comment.kak index 3e017ed0..433f38bd 100644 --- a/rc/core/comment.kak +++ b/rc/core/comment.kak @@ -150,16 +150,32 @@ define-command comment-line -docstring '(un)comment selected lines using line co try %{ # Keep non-empty lines execute-keys \A\s*\z + } catch %{ + } + + try %{ + set-register / "\A\Q%opt{comment_line}\E\h?" try %{ - # Select the comment characters and remove them - set-register / "\A\Q%opt{comment_line}\E\h*" - execute-keys sd - } catch %{ - # Comment the line + # See if there are any uncommented lines in the selection + execute-keys -draft + + # There are uncommented lines, so comment everything set-register '"' "%opt{comment_line} " + align-cursors-left execute-keys P + } catch %{ + # All lines were commented, so uncomment everything + execute-keys sd } } } } + +define-command align-cursors-left -docstring 'set all cursor (and anchor) columns to the column of the leftmost cursor' %{ + %sh{ + leftmost_column=$(echo "$kak_selections_desc" | tr ':' '\n' | cut -d',' -f1 | cut -d'.' -f2 | sort -n | head -n1) + aligned_selections=$(echo "$kak_selections_desc" | sed "s/\.[0-9]\+,/.$leftmost_column,/g") + echo "select $aligned_selections" + } +} diff --git a/test/indent/comment/comment-multiple-lines-indented/cmd b/test/indent/comment/comment-multiple-lines-indented/cmd new file mode 100644 index 00000000..fbad60f2 --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-indented/cmd @@ -0,0 +1 @@ +jXXX:comment-line diff --git a/test/indent/comment/comment-multiple-lines-indented/in b/test/indent/comment/comment-multiple-lines-indented/in new file mode 100644 index 00000000..d81dbd16 --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-indented/in @@ -0,0 +1,4 @@ +a: + b: 1 + + c: 2 diff --git a/test/indent/comment/comment-multiple-lines-indented/out b/test/indent/comment/comment-multiple-lines-indented/out new file mode 100644 index 00000000..46f89cd5 --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-indented/out @@ -0,0 +1,4 @@ +a: + # b: 1 + + # c: 2 diff --git a/test/indent/comment/comment-multiple-lines-indented/rc b/test/indent/comment/comment-multiple-lines-indented/rc new file mode 100644 index 00000000..4808655d --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-indented/rc @@ -0,0 +1,4 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/base/yaml.kak" +source "%val{runtime}/rc/core/comment.kak" +set buffer filetype yaml diff --git a/test/indent/comment/comment-multiple-lines-unindented/cmd b/test/indent/comment/comment-multiple-lines-unindented/cmd new file mode 100644 index 00000000..f67b66d6 --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-unindented/cmd @@ -0,0 +1 @@ +%:comment-line diff --git a/test/indent/comment/comment-multiple-lines-unindented/in b/test/indent/comment/comment-multiple-lines-unindented/in new file mode 100644 index 00000000..d81dbd16 --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-unindented/in @@ -0,0 +1,4 @@ +a: + b: 1 + + c: 2 diff --git a/test/indent/comment/comment-multiple-lines-unindented/out b/test/indent/comment/comment-multiple-lines-unindented/out new file mode 100644 index 00000000..9405433d --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-unindented/out @@ -0,0 +1,4 @@ +# a: +# b: 1 + +# c: 2 diff --git a/test/indent/comment/comment-multiple-lines-unindented/rc b/test/indent/comment/comment-multiple-lines-unindented/rc new file mode 100644 index 00000000..4808655d --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-unindented/rc @@ -0,0 +1,4 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/base/yaml.kak" +source "%val{runtime}/rc/core/comment.kak" +set buffer filetype yaml diff --git a/test/indent/comment/comment-multiple-lines-when-partially-commented/cmd b/test/indent/comment/comment-multiple-lines-when-partially-commented/cmd new file mode 100644 index 00000000..f67b66d6 --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-when-partially-commented/cmd @@ -0,0 +1 @@ +%:comment-line diff --git a/test/indent/comment/comment-multiple-lines-when-partially-commented/in b/test/indent/comment/comment-multiple-lines-when-partially-commented/in new file mode 100644 index 00000000..e048df42 --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-when-partially-commented/in @@ -0,0 +1,3 @@ +a: + # b: 1 + c: 2 diff --git a/test/indent/comment/comment-multiple-lines-when-partially-commented/out b/test/indent/comment/comment-multiple-lines-when-partially-commented/out new file mode 100644 index 00000000..f4bed302 --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-when-partially-commented/out @@ -0,0 +1,3 @@ +# a: +# # b: 1 +# c: 2 diff --git a/test/indent/comment/comment-multiple-lines-when-partially-commented/rc b/test/indent/comment/comment-multiple-lines-when-partially-commented/rc new file mode 100644 index 00000000..4808655d --- /dev/null +++ b/test/indent/comment/comment-multiple-lines-when-partially-commented/rc @@ -0,0 +1,4 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/base/yaml.kak" +source "%val{runtime}/rc/core/comment.kak" +set buffer filetype yaml From 8a1b557b5f39a7c5166f9686e8ebea380619f8f2 Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Fri, 29 Jun 2018 00:10:21 +0200 Subject: [PATCH 2/8] Add test to ensure whitespaces are preserved after uncomment --- test/indent/comment/uncomment-multiple-indented-lines/cmd | 1 + test/indent/comment/uncomment-multiple-indented-lines/in | 3 +++ test/indent/comment/uncomment-multiple-indented-lines/out | 3 +++ test/indent/comment/uncomment-multiple-indented-lines/rc | 4 ++++ 4 files changed, 11 insertions(+) create mode 100644 test/indent/comment/uncomment-multiple-indented-lines/cmd create mode 100644 test/indent/comment/uncomment-multiple-indented-lines/in create mode 100644 test/indent/comment/uncomment-multiple-indented-lines/out create mode 100644 test/indent/comment/uncomment-multiple-indented-lines/rc diff --git a/test/indent/comment/uncomment-multiple-indented-lines/cmd b/test/indent/comment/uncomment-multiple-indented-lines/cmd new file mode 100644 index 00000000..f67b66d6 --- /dev/null +++ b/test/indent/comment/uncomment-multiple-indented-lines/cmd @@ -0,0 +1 @@ +%:comment-line diff --git a/test/indent/comment/uncomment-multiple-indented-lines/in b/test/indent/comment/uncomment-multiple-indented-lines/in new file mode 100644 index 00000000..1bd4ba61 --- /dev/null +++ b/test/indent/comment/uncomment-multiple-indented-lines/in @@ -0,0 +1,3 @@ +# a: +# b: 1 +# c: 2 diff --git a/test/indent/comment/uncomment-multiple-indented-lines/out b/test/indent/comment/uncomment-multiple-indented-lines/out new file mode 100644 index 00000000..0969f779 --- /dev/null +++ b/test/indent/comment/uncomment-multiple-indented-lines/out @@ -0,0 +1,3 @@ +a: + b: 1 + c: 2 diff --git a/test/indent/comment/uncomment-multiple-indented-lines/rc b/test/indent/comment/uncomment-multiple-indented-lines/rc new file mode 100644 index 00000000..4808655d --- /dev/null +++ b/test/indent/comment/uncomment-multiple-indented-lines/rc @@ -0,0 +1,4 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/base/yaml.kak" +source "%val{runtime}/rc/core/comment.kak" +set buffer filetype yaml From c683b09287e1fb223af705819ede98c2e168e584 Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Fri, 29 Jun 2018 00:56:37 +0200 Subject: [PATCH 3/8] Rename align-cursors-left to align-selections-left --- rc/core/comment.kak | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rc/core/comment.kak b/rc/core/comment.kak index 433f38bd..ac2393cc 100644 --- a/rc/core/comment.kak +++ b/rc/core/comment.kak @@ -162,7 +162,7 @@ define-command comment-line -docstring '(un)comment selected lines using line co # There are uncommented lines, so comment everything set-register '"' "%opt{comment_line} " - align-cursors-left + align-selections-left execute-keys P } catch %{ # All lines were commented, so uncomment everything @@ -172,10 +172,10 @@ define-command comment-line -docstring '(un)comment selected lines using line co } } -define-command align-cursors-left -docstring 'set all cursor (and anchor) columns to the column of the leftmost cursor' %{ +define-command align-selections-left -docstring 'extend selections to the left to align with the leftmost selected column' %{ %sh{ leftmost_column=$(echo "$kak_selections_desc" | tr ':' '\n' | cut -d',' -f1 | cut -d'.' -f2 | sort -n | head -n1) - aligned_selections=$(echo "$kak_selections_desc" | sed "s/\.[0-9]\+,/.$leftmost_column,/g") + aligned_selections=$(echo "$kak_selections_desc" | sed -r "s/\.[0-9]+,/.$leftmost_column,/g") echo "select $aligned_selections" } } From d8a2588e9c207beda600388ced8e2457bbea472c Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Fri, 29 Jun 2018 01:05:00 +0200 Subject: [PATCH 4/8] another attempt to fix macOS issue --- rc/core/comment.kak | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rc/core/comment.kak b/rc/core/comment.kak index ac2393cc..c33436cb 100644 --- a/rc/core/comment.kak +++ b/rc/core/comment.kak @@ -175,7 +175,7 @@ define-command comment-line -docstring '(un)comment selected lines using line co define-command align-selections-left -docstring 'extend selections to the left to align with the leftmost selected column' %{ %sh{ leftmost_column=$(echo "$kak_selections_desc" | tr ':' '\n' | cut -d',' -f1 | cut -d'.' -f2 | sort -n | head -n1) - aligned_selections=$(echo "$kak_selections_desc" | sed -r "s/\.[0-9]+,/.$leftmost_column,/g") + aligned_selections=$(echo "$kak_selections_desc" | sed -E "s/\.[0-9]+,/.$leftmost_column,/g") echo "select $aligned_selections" } } From 7cfd0d1b39c6e133cf2193c0ade4f283ff987ca0 Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Fri, 29 Jun 2018 01:37:56 +0200 Subject: [PATCH 5/8] trigger travis --- rc/core/comment.kak | 1 - 1 file changed, 1 deletion(-) diff --git a/rc/core/comment.kak b/rc/core/comment.kak index c33436cb..add80977 100644 --- a/rc/core/comment.kak +++ b/rc/core/comment.kak @@ -150,7 +150,6 @@ define-command comment-line -docstring '(un)comment selected lines using line co try %{ # Keep non-empty lines execute-keys \A\s*\z - } catch %{ } try %{ From ba547ea4ddc292cb2a338611d9524950fd4229a6 Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Tue, 3 Jul 2018 22:32:04 +0200 Subject: [PATCH 6/8] Make tests a bit more interesting --- test/indent/comment/comment-multiple-lines-indented/cmd | 2 +- test/indent/comment/comment-multiple-lines-indented/in | 4 +++- test/indent/comment/comment-multiple-lines-indented/out | 4 +++- test/indent/comment/comment-multiple-lines-unindented/in | 4 +++- test/indent/comment/comment-multiple-lines-unindented/out | 4 +++- .../comment-multiple-lines-when-partially-commented/in | 5 ++++- .../comment-multiple-lines-when-partially-commented/out | 5 ++++- 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/test/indent/comment/comment-multiple-lines-indented/cmd b/test/indent/comment/comment-multiple-lines-indented/cmd index fbad60f2..2330d00f 100644 --- a/test/indent/comment/comment-multiple-lines-indented/cmd +++ b/test/indent/comment/comment-multiple-lines-indented/cmd @@ -1 +1 @@ -jXXX:comment-line +jXXXXX:comment-line diff --git a/test/indent/comment/comment-multiple-lines-indented/in b/test/indent/comment/comment-multiple-lines-indented/in index d81dbd16..f5e027ab 100644 --- a/test/indent/comment/comment-multiple-lines-indented/in +++ b/test/indent/comment/comment-multiple-lines-indented/in @@ -1,4 +1,6 @@ a: b: 1 + c: + d: 2 - c: 2 + e: 3 diff --git a/test/indent/comment/comment-multiple-lines-indented/out b/test/indent/comment/comment-multiple-lines-indented/out index 46f89cd5..34c257e6 100644 --- a/test/indent/comment/comment-multiple-lines-indented/out +++ b/test/indent/comment/comment-multiple-lines-indented/out @@ -1,4 +1,6 @@ a: # b: 1 + # c: + # d: 2 - # c: 2 + # e: 3 diff --git a/test/indent/comment/comment-multiple-lines-unindented/in b/test/indent/comment/comment-multiple-lines-unindented/in index d81dbd16..f5e027ab 100644 --- a/test/indent/comment/comment-multiple-lines-unindented/in +++ b/test/indent/comment/comment-multiple-lines-unindented/in @@ -1,4 +1,6 @@ a: b: 1 + c: + d: 2 - c: 2 + e: 3 diff --git a/test/indent/comment/comment-multiple-lines-unindented/out b/test/indent/comment/comment-multiple-lines-unindented/out index 9405433d..e9df3e3a 100644 --- a/test/indent/comment/comment-multiple-lines-unindented/out +++ b/test/indent/comment/comment-multiple-lines-unindented/out @@ -1,4 +1,6 @@ # a: # b: 1 +# c: +# d: 2 -# c: 2 +# e: 3 diff --git a/test/indent/comment/comment-multiple-lines-when-partially-commented/in b/test/indent/comment/comment-multiple-lines-when-partially-commented/in index e048df42..3e177da0 100644 --- a/test/indent/comment/comment-multiple-lines-when-partially-commented/in +++ b/test/indent/comment/comment-multiple-lines-when-partially-commented/in @@ -1,3 +1,6 @@ a: # b: 1 - c: 2 + c: + d: 2 + + e: 3 diff --git a/test/indent/comment/comment-multiple-lines-when-partially-commented/out b/test/indent/comment/comment-multiple-lines-when-partially-commented/out index f4bed302..4e6debd4 100644 --- a/test/indent/comment/comment-multiple-lines-when-partially-commented/out +++ b/test/indent/comment/comment-multiple-lines-when-partially-commented/out @@ -1,3 +1,6 @@ # a: # # b: 1 -# c: 2 +# c: +# d: 2 + +# e: 3 From 89280d0f203fa2b62317d9abd649c024e40244f0 Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Thu, 5 Jul 2018 23:29:53 +0200 Subject: [PATCH 7/8] Update according to bbtu --- rc/core/comment.kak | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rc/core/comment.kak b/rc/core/comment.kak index add80977..f4f1d9aa 100644 --- a/rc/core/comment.kak +++ b/rc/core/comment.kak @@ -138,12 +138,12 @@ define-command comment-block -docstring '(un)comment selections using block comm } define-command comment-line -docstring '(un)comment selected lines using line comments' %{ - %sh{ + evaluate-commands %sh{ if [ -z "${kak_opt_comment_line}" ]; then echo "fail \"The 'comment_line' option is empty, could not comment the line\"" fi } - evaluate-commands -draft %{ + evaluate-commands -save-regs '"/' -draft %{ # Select the content of the lines, without indentation execute-keys gi @@ -172,7 +172,7 @@ define-command comment-line -docstring '(un)comment selected lines using line co } define-command align-selections-left -docstring 'extend selections to the left to align with the leftmost selected column' %{ - %sh{ + evaluate-commands %sh{ leftmost_column=$(echo "$kak_selections_desc" | tr ':' '\n' | cut -d',' -f1 | cut -d'.' -f2 | sort -n | head -n1) aligned_selections=$(echo "$kak_selections_desc" | sed -E "s/\.[0-9]+,/.$leftmost_column,/g") echo "select $aligned_selections" From 4b18a15a9fbad3f3c08feb2f98a52255e5ba394c Mon Sep 17 00:00:00 2001 From: Maxim Baz Date: Thu, 5 Jul 2018 23:46:21 +0200 Subject: [PATCH 8/8] kak_selections_desc is now space-separated --- rc/core/comment.kak | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rc/core/comment.kak b/rc/core/comment.kak index f4f1d9aa..85121045 100644 --- a/rc/core/comment.kak +++ b/rc/core/comment.kak @@ -173,7 +173,7 @@ define-command comment-line -docstring '(un)comment selected lines using line co define-command align-selections-left -docstring 'extend selections to the left to align with the leftmost selected column' %{ evaluate-commands %sh{ - leftmost_column=$(echo "$kak_selections_desc" | tr ':' '\n' | cut -d',' -f1 | cut -d'.' -f2 | sort -n | head -n1) + leftmost_column=$(echo "$kak_selections_desc" | tr ' ' '\n' | cut -d',' -f1 | cut -d'.' -f2 | sort -n | head -n1) aligned_selections=$(echo "$kak_selections_desc" | sed -E "s/\.[0-9]+,/.$leftmost_column,/g") echo "select $aligned_selections" }