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