summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2024-06-11 19:33:33 +1000
committerMaxime Coste <mawww@kakoune.org>2024-06-11 19:33:33 +1000
commit17c25cc86a6bf4779f58fe2d2a5c13147bb27f2b (patch)
treefb3eff03e900644329c918900e5ac7b8f39ce348
parenta35a7dc4a42ef0a0d83d5db7faeee7ad87ad0d24 (diff)
parent9c9aa2cf95a1503cddcba69aed8b5f1a234927d6 (diff)
Merge remote-tracking branch 'sjjf/python_comment_paras'
-rw-r--r--rc/filetype/python.kak50
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/cmd1
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/in1
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/out2
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/rc3
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/cmd1
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/in1
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/out2
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/rc3
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/exit-block-indented/cmd1
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/exit-block-indented/in4
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/exit-block-indented/out3
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/exit-block-indented/rc3
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/exit-block/cmd1
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/exit-block/in4
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/exit-block/out3
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/exit-block/rc3
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/cmd1
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/in4
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/out5
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/rc3
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/not-a-comment/cmd1
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/not-a-comment/in4
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/not-a-comment/out5
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/not-a-comment/rc3
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/cmd1
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/in3
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/out4
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/rc3
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/paragraph-break/cmd1
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/paragraph-break/in4
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/paragraph-break/out5
-rw-r--r--test/regression/5147-python-block-comment-paragraphs/paragraph-break/rc3
33 files changed, 130 insertions, 6 deletions
diff --git a/rc/filetype/python.kak b/rc/filetype/python.kak
index 65da1a89..4b1a9c76 100644
--- a/rc/filetype/python.kak
+++ b/rc/filetype/python.kak
@@ -167,14 +167,52 @@ define-command -hidden python-insert-on-new-line %{ evaluate-commands -itersel -
execute-keys <semicolon>
try %{
evaluate-commands -draft -save-regs '/"' %{
- # copy the commenting prefix
- execute-keys -save-regs '' k x1s^\h*(#+\h*)<ret> y
+ # Handle block comment continuation/termination.
+ #
+ # This code applies in the context where we have a new line
+ # inserted, and the previous line is a block comment (i.e. a line
+ # that matches '^\h*#+\h*\S*$'). We assume that the comment will
+ # be continued, so we copy the prefix (all leading whitespace, any
+ # '#' characters, and then any additional whitespace before the
+ # next non-whitespace character) into the new line.
+ #
+ # PEP8's text on block comments says they can include an empty
+ # comment (i.e. '^\h*#+\h*$') as a paragraph separator - we
+ # leave a single empty comment as-is, to allow paragraph breaks,
+ # but if we see two consecutive empty comments we exit the
+ # comment block and delete the empty comments.
+ # Reference: https://peps.python.org/pep-0008/#block-comments
+ #
+ # first, make sure we're in the right context - don't wrap this
+ # in a try/catch so we fail out immediately
+ execute-keys -draft kxs^\h*#+\h*<ret>
+
+ # now handle the coment continuation logic
try %{
- # if the previous comment isn't empty, create a new one
- execute-keys x<a-K>^\h*#+\h*$<ret> jxs^\h*<ret>P
+ # try and match a regular block comment, copying the prefix
+ execute-keys -draft -save-regs '' k x 1s^(\h*#+\h*)\S.*$ <ret> y
+ execute-keys -draft P
} catch %{
- # if there is no text in the previous comment, remove it completely
- execute-keys d
+ try %{
+ # try and match a regular block comment followed by a single
+ # empty comment line
+ execute-keys -draft -save-regs '' kKx 1s^(\h*#+\h*)\S+\n\h*#+\h*$ <ret> y
+ execute-keys -draft P
+ } catch %{
+ try %{
+ # try and match a pair of empty comment lines, and delete
+ # them if we match
+ execute-keys -draft kKx <a-k> ^\h*#+\h*\n\h*#+\h*$ <ret> <a-d>
+ } catch %{
+ # finally, we need a special case for a new line inserted
+ # into a file that consists of a single empty comment - in
+ # that case we can't expect to copy the trailing whitespace,
+ # so we add our own
+ execute-keys -draft -save-regs '' k x1s^(\h*#+)\h*$<ret> y
+ execute-keys -draft P
+ execute-keys -draft i<space>
+ }
+ }
}
}
diff --git a/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/cmd b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/cmd
new file mode 100644
index 00000000..60ee9918
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/cmd
@@ -0,0 +1 @@
+gjA<ret>
diff --git a/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/in b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/in
new file mode 100644
index 00000000..641ef36f
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/in
@@ -0,0 +1 @@
+ #
diff --git a/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/out b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/out
new file mode 100644
index 00000000..fee7ebff
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/out
@@ -0,0 +1,2 @@
+ #
+ #
diff --git a/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/rc b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/rc
new file mode 100644
index 00000000..a098fc21
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file-indented/rc
@@ -0,0 +1,3 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/filetype/python.kak"
+set buffer filetype python
diff --git a/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/cmd b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/cmd
new file mode 100644
index 00000000..60ee9918
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/cmd
@@ -0,0 +1 @@
+gjA<ret>
diff --git a/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/in b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/in
new file mode 100644
index 00000000..792d6005
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/in
@@ -0,0 +1 @@
+#
diff --git a/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/out b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/out
new file mode 100644
index 00000000..0f3b43f3
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/out
@@ -0,0 +1,2 @@
+#
+#
diff --git a/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/rc b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/rc
new file mode 100644
index 00000000..a098fc21
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/empty-start-of-file/rc
@@ -0,0 +1,3 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/filetype/python.kak"
+set buffer filetype python
diff --git a/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/cmd b/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/cmd
new file mode 100644
index 00000000..60ee9918
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/cmd
@@ -0,0 +1 @@
+gjA<ret>
diff --git a/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/in b/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/in
new file mode 100644
index 00000000..0092d9ec
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/in
@@ -0,0 +1,4 @@
+ # A new line after a pair of empty comment lines should exit the
+ # block comment.
+ #
+ #
diff --git a/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/out b/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/out
new file mode 100644
index 00000000..36283e91
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/out
@@ -0,0 +1,3 @@
+ # A new line after a pair of empty comment lines should exit the
+ # block comment.
+
diff --git a/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/rc b/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/rc
new file mode 100644
index 00000000..a098fc21
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/exit-block-indented/rc
@@ -0,0 +1,3 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/filetype/python.kak"
+set buffer filetype python
diff --git a/test/regression/5147-python-block-comment-paragraphs/exit-block/cmd b/test/regression/5147-python-block-comment-paragraphs/exit-block/cmd
new file mode 100644
index 00000000..60ee9918
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/exit-block/cmd
@@ -0,0 +1 @@
+gjA<ret>
diff --git a/test/regression/5147-python-block-comment-paragraphs/exit-block/in b/test/regression/5147-python-block-comment-paragraphs/exit-block/in
new file mode 100644
index 00000000..582a11c7
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/exit-block/in
@@ -0,0 +1,4 @@
+# A new line after a pair of empty comment lines should exit the
+# block comment.
+#
+#
diff --git a/test/regression/5147-python-block-comment-paragraphs/exit-block/out b/test/regression/5147-python-block-comment-paragraphs/exit-block/out
new file mode 100644
index 00000000..21bc5075
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/exit-block/out
@@ -0,0 +1,3 @@
+# A new line after a pair of empty comment lines should exit the
+# block comment.
+
diff --git a/test/regression/5147-python-block-comment-paragraphs/exit-block/rc b/test/regression/5147-python-block-comment-paragraphs/exit-block/rc
new file mode 100644
index 00000000..a098fc21
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/exit-block/rc
@@ -0,0 +1,3 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/filetype/python.kak"
+set buffer filetype python
diff --git a/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/cmd b/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/cmd
new file mode 100644
index 00000000..60ee9918
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/cmd
@@ -0,0 +1 @@
+gjA<ret>
diff --git a/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/in b/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/in
new file mode 100644
index 00000000..f3c7955e
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/in
@@ -0,0 +1,4 @@
+ # If we're not in a comment at all, make sure we do the right thing.
+ # (one empty line and a 4-space prefixed line follows)
+
+
diff --git a/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/out b/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/out
new file mode 100644
index 00000000..9748afb0
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/out
@@ -0,0 +1,5 @@
+ # If we're not in a comment at all, make sure we do the right thing.
+ # (one empty line and a 4-space prefixed line follows)
+
+
+
diff --git a/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/rc b/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/rc
new file mode 100644
index 00000000..a098fc21
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/not-a-comment-indented/rc
@@ -0,0 +1,3 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/filetype/python.kak"
+set buffer filetype python
diff --git a/test/regression/5147-python-block-comment-paragraphs/not-a-comment/cmd b/test/regression/5147-python-block-comment-paragraphs/not-a-comment/cmd
new file mode 100644
index 00000000..60ee9918
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/not-a-comment/cmd
@@ -0,0 +1 @@
+gjA<ret>
diff --git a/test/regression/5147-python-block-comment-paragraphs/not-a-comment/in b/test/regression/5147-python-block-comment-paragraphs/not-a-comment/in
new file mode 100644
index 00000000..bdfda4a1
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/not-a-comment/in
@@ -0,0 +1,4 @@
+# If we're not in a comment at all, make sure we do the right thing.
+# (two empty lines follow)
+
+
diff --git a/test/regression/5147-python-block-comment-paragraphs/not-a-comment/out b/test/regression/5147-python-block-comment-paragraphs/not-a-comment/out
new file mode 100644
index 00000000..881aeaa0
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/not-a-comment/out
@@ -0,0 +1,5 @@
+# If we're not in a comment at all, make sure we do the right thing.
+# (two empty lines follow)
+
+
+
diff --git a/test/regression/5147-python-block-comment-paragraphs/not-a-comment/rc b/test/regression/5147-python-block-comment-paragraphs/not-a-comment/rc
new file mode 100644
index 00000000..a098fc21
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/not-a-comment/rc
@@ -0,0 +1,3 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/filetype/python.kak"
+set buffer filetype python
diff --git a/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/cmd b/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/cmd
new file mode 100644
index 00000000..60ee9918
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/cmd
@@ -0,0 +1 @@
+gjA<ret>
diff --git a/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/in b/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/in
new file mode 100644
index 00000000..fb95ee15
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/in
@@ -0,0 +1,3 @@
+ # A new line after a single empty comment line should leave the comment in
+ # place as a possible paragraph separator.
+ #
diff --git a/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/out b/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/out
new file mode 100644
index 00000000..31a0fc7e
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/out
@@ -0,0 +1,4 @@
+ # A new line after a single empty comment line should leave the comment in
+ # place as a possible paragraph separator.
+ #
+ #
diff --git a/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/rc b/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/rc
new file mode 100644
index 00000000..a098fc21
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/paragraph-break-indented/rc
@@ -0,0 +1,3 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/filetype/python.kak"
+set buffer filetype python
diff --git a/test/regression/5147-python-block-comment-paragraphs/paragraph-break/cmd b/test/regression/5147-python-block-comment-paragraphs/paragraph-break/cmd
new file mode 100644
index 00000000..60ee9918
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/paragraph-break/cmd
@@ -0,0 +1 @@
+gjA<ret>
diff --git a/test/regression/5147-python-block-comment-paragraphs/paragraph-break/in b/test/regression/5147-python-block-comment-paragraphs/paragraph-break/in
new file mode 100644
index 00000000..c856556f
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/paragraph-break/in
@@ -0,0 +1,4 @@
+# A new line after a single empty comment line should leave the empty comment
+# in place as a possible paragraph separator, starting a new comment with the
+# prefix copied.
+#
diff --git a/test/regression/5147-python-block-comment-paragraphs/paragraph-break/out b/test/regression/5147-python-block-comment-paragraphs/paragraph-break/out
new file mode 100644
index 00000000..56da68fd
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/paragraph-break/out
@@ -0,0 +1,5 @@
+# A new line after a single empty comment line should leave the empty comment
+# in place as a possible paragraph separator, starting a new comment with the
+# prefix copied.
+#
+#
diff --git a/test/regression/5147-python-block-comment-paragraphs/paragraph-break/rc b/test/regression/5147-python-block-comment-paragraphs/paragraph-break/rc
new file mode 100644
index 00000000..a098fc21
--- /dev/null
+++ b/test/regression/5147-python-block-comment-paragraphs/paragraph-break/rc
@@ -0,0 +1,3 @@
+source "%val{runtime}/colors/default.kak"
+source "%val{runtime}/rc/filetype/python.kak"
+set buffer filetype python