patch 9.1.1229: the comment plugin can be improved
Problem: the comment plugin can be improved
Solution: add comment text objects "ic" and "ac"
(Maxim Kim)
closes: #16938
Signed-off-by: Maxim Kim <habamax@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/testdir/test_plugin_comment.vim b/src/testdir/test_plugin_comment.vim
index 8e877ac..a0425d5 100644
--- a/src/testdir/test_plugin_comment.vim
+++ b/src/testdir/test_plugin_comment.vim
@@ -29,7 +29,6 @@
call assert_equal(["# vim9script", "", "# def Hello()", '# echo "Hello"', "# enddef"], result)
endfunc
-
func Test_basic_uncomment()
CheckScreendump
let lines =<< trim END
@@ -55,7 +54,7 @@
let result = readfile(output_file)
- call assert_equal(["# vim9script", "", "def Hello()", ' echo "Hello"', "enddef"], result)
+ call assert_equal(["# vim9script", "", "def Hello()", ' echo "Hello"', "enddef"], result)
endfunc
func Test_bothends_comment()
@@ -177,3 +176,341 @@
call assert_equal(["/* int main() { */", "\t/* if 1 { */", "\t /* return 0; */", "\t/* } */", " /* return 1; */", "/* } */"], result)
endfunc
+
+func Test_textobj_icomment()
+ CheckScreendump
+ let lines =<< trim END
+ for x in range(10):
+ print(x) # printing stuff
+ # print(x*x)
+ #print(x*x*x)
+ print(x*x*x*x) # printing stuff
+ print(x*x*x*x*x) # printing stuff
+ # print(x*x)
+ #print(x*x*x)
+
+ print(x*x*x*x*x)
+ END
+
+ let input_file = "test_textobj_icomment_input.py"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "dic..")
+ let output_file = "comment_textobj_icomment.py"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["for x in range(10):", " print(x) ", " print(x*x*x*x) ", " print(x*x*x*x*x) ", "", " print(x*x*x*x*x)"], result)
+endfunc
+
+func Test_textobj_icomment2()
+ CheckScreendump
+ let lines =<< trim END
+ #include <stdio.h>
+
+ int main() {
+ printf("hello"); /* hello world */ printf(" world\n");
+ /* if 1 {
+ return 1;
+ }*/
+
+ return 0;
+ }
+ END
+
+ let input_file = "test_textobj_icomment2_input.c"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "dic..")
+ let output_file = "comment_textobj_icomment2.c"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\"); printf(\" world\\n\");", " ", "", " return 0;", "}"], result)
+endfunc
+
+func Test_textobj_icomment3()
+ CheckScreendump
+ let lines =<< trim END
+ #include <stdio.h>
+
+ int main() {
+ printf("hello");/*hello world*/printf(" world\n");
+ return 0;
+ }
+ END
+
+ let input_file = "test_textobj_icomment3_input.c"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "jjjdic")
+ let output_file = "comment_textobj_icomment3.c"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\");printf(\" world\\n\");", " return 0;", "}"], result)
+endfunc
+
+func Test_textobj_acomment()
+ CheckScreendump
+ let lines =<< trim END
+ for x in range(10):
+ print(x) # printing stuff
+ # print(x*x)
+ #print(x*x*x)
+ print(x*x*x*x) # printing stuff
+ print(x*x*x*x*x) # printing stuff
+ # print(x*x)
+ #print(x*x*x)
+
+ print(x*x*x*x*x)
+ END
+
+ let input_file = "test_textobj_acomment_input.py"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "dac..")
+ let output_file = "comment_textobj_acomment.py"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["for x in range(10):", " print(x)", " print(x*x*x*x)", " print(x*x*x*x*x)", "", " print(x*x*x*x*x)"], result)
+endfunc
+
+func Test_textobj_acomment2()
+ CheckScreendump
+ let lines =<< trim END
+ #include <stdio.h>
+
+ int main() {
+ printf("hello"); /* hello world */ printf(" world\n");
+ /* if 1 {
+ return 1;
+ }*/
+
+ return 0;
+ }
+ END
+
+ let input_file = "test_textobj_acomment2_input.c"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "dac.")
+ let output_file = "comment_textobj_acomment2.c"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\");printf(\" world\\n\");", " return 0;", "}"], result)
+endfunc
+
+func Test_textobj_acomment3()
+ CheckScreendump
+ let lines =<< trim END
+ #include <stdio.h>
+
+ int main() {
+ printf("hello");/*hello world*/printf(" world\n");
+ return 0;
+ }
+ END
+
+ let input_file = "test_textobj_acomment3_input.c"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "jjjdac")
+ let output_file = "comment_textobj_acomment3.c"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\");printf(\" world\\n\");", " return 0;", "}"], result)
+endfunc
+
+func Test_textobj_firstline_comment()
+ CheckScreendump
+ let lines =<< trim END
+ /*#include <stdio.h>*/
+
+ int main() {}
+ END
+
+ let input_file = "test_textobj_firstlinecomment_input.c"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "dac")
+ let output_file = "comment_textobj_firstline_comment.c"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["int main() {}"], result)
+endfunc
+
+func Test_textobj_noleading_space_comment()
+ CheckScreendump
+ let lines =<< trim END
+ int main() {// main start
+ }/* main end */
+ END
+
+ let input_file = "test_textobj_noleading_space_input.c"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "dacdic")
+ let output_file = "comment_textobj_noleading_space_comment.c"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["int main() {", "}"], result)
+endfunc
+
+func Test_textobj_noleading_space_comment2()
+ CheckScreendump
+ let lines =<< trim END
+ int main() {// main start
+ } /* main end */
+ END
+
+ let input_file = "test_textobj_noleading_space_input2.c"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "dac.")
+ let output_file = "comment_textobj_noleading_space_comment2.c"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["int main() {", "}"], result)
+endfunc
+
+func Test_textobj_cursor_on_leading_space_comment()
+ CheckScreendump
+ let lines =<< trim END
+ int main() {
+ // multilple comments
+ // cursor is between them
+ }
+ END
+
+ let input_file = "test_textobj_cursor_on_leading_space_comment_input.c"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "jjdac")
+ let output_file = "comment_textobj_cursor_on_leading_space_comment.c"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["int main() {", "", "}"], result)
+endfunc
+
+func Test_textobj_conseq_comment()
+ CheckScreendump
+ let lines =<< trim END
+ int main() {
+ printf("hello"); // hello
+ // world
+ printf("world");
+ }
+ END
+
+ let input_file = "test_textobj_conseq_comment_input.c"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "dac")
+ let output_file = "comment_textobj_conseq_comment.c"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["int main() {", " printf(\"hello\");", " printf(\"world\");", "}"], result)
+endfunc
+
+func Test_textobj_conseq_comment2()
+ CheckScreendump
+ let lines =<< trim END
+ int main() {
+ printf("hello"); // hello
+
+ // world
+ printf("world");
+ }
+ END
+
+ let input_file = "test_textobj_conseq_comment_input2.c"
+ call writefile(lines, input_file, "D")
+
+ let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
+
+ call term_sendkeys(buf, "dac")
+ let output_file = "comment_textobj_conseq_comment2.c"
+ call term_sendkeys(buf, $":w {output_file}\<CR>")
+ defer delete(output_file)
+
+ call StopVimInTerminal(buf)
+
+ let result = readfile(output_file)
+
+ call assert_equal(["int main() {", " printf(\"hello\");", "", " // world", " printf(\"world\");", "}"], result)
+endfunc
diff --git a/src/version.c b/src/version.c
index 18bf939..7debc1a 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1229,
+/**/
1228,
/**/
1227,