From 974684aa684e9704b17a58fbb99c7c2803a261b4 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 9 Jul 2020 01:13:00 +0800 Subject: [PATCH] Improve rust comment closing handling /// foo ///%( ) /// foo /// %( ) With `cbar`, /// foo bar /// foo /// /// bar Based on c-family block comment handling, this patch also add rust block comment indentation. This affects `o` behavior on empty comment but it allows a way more efficient way to clear comments. --- rc/filetype/rust.kak | 47 ++++++++++++++++++- .../rust/{comment => block-comment-close}/cmd | 0 test/indent/rust/block-comment-close/in | 6 +++ test/indent/rust/block-comment-close/out | 8 ++++ .../rust/{comment => block-comment-close}/rc | 0 test/indent/rust/block-comment/cmd | 1 + test/indent/rust/block-comment/in | 12 +++++ test/indent/rust/block-comment/out | 18 +++++++ test/indent/rust/block-comment/rc | 3 ++ test/indent/rust/line-comment-close/cmd | 1 + test/indent/rust/line-comment-close/in | 26 ++++++++++ test/indent/rust/line-comment-close/out | 33 +++++++++++++ test/indent/rust/line-comment-close/rc | 3 ++ test/indent/rust/line-comment/cmd | 1 + test/indent/rust/{comment => line-comment}/in | 0 .../indent/rust/{comment => line-comment}/out | 0 test/indent/rust/line-comment/rc | 3 ++ 17 files changed, 160 insertions(+), 2 deletions(-) rename test/indent/rust/{comment => block-comment-close}/cmd (100%) create mode 100644 test/indent/rust/block-comment-close/in create mode 100644 test/indent/rust/block-comment-close/out rename test/indent/rust/{comment => block-comment-close}/rc (100%) create mode 100644 test/indent/rust/block-comment/cmd create mode 100644 test/indent/rust/block-comment/in create mode 100644 test/indent/rust/block-comment/out create mode 100644 test/indent/rust/block-comment/rc create mode 100644 test/indent/rust/line-comment-close/cmd create mode 100644 test/indent/rust/line-comment-close/in create mode 100644 test/indent/rust/line-comment-close/out create mode 100644 test/indent/rust/line-comment-close/rc create mode 100644 test/indent/rust/line-comment/cmd rename test/indent/rust/{comment => line-comment}/in (100%) rename test/indent/rust/{comment => line-comment}/out (100%) create mode 100644 test/indent/rust/line-comment/rc diff --git a/rc/filetype/rust.kak b/rc/filetype/rust.kak index eac61115..ecfee8ef 100644 --- a/rc/filetype/rust.kak +++ b/rc/filetype/rust.kak @@ -99,9 +99,52 @@ define-command -hidden rust-trim-indent %{ define-command -hidden rust-indent-on-new-line %~ evaluate-commands -draft -itersel %< - # copy // comments prefix and following white spaces try %{ - execute-keys -draft k s ^\h*//[!/]{0,2}\h* y gh j P + try %[ # line comment + evaluate-commands -draft -save-regs '/"' %[ + # copy the commenting prefix + execute-keys -save-regs '' k s ^\h*//[!/]{0,2}\h* y + try %[ + # if the previous comment isn't empty, create a new one + execute-keys ^\h*//[!/]{0,2}$ js^\h*P + ] catch %[ + # TODO figure out a way to not delete empty comment in current line + # if there is no space and text in the previous comment, remove it completely + execute-keys s //.* d + ] + ] + ] catch %[ # block comment + # if the previous line isn't within a comment scope, break + execute-keys -draft k ^(\h*/\*|\h+\*(?!/)) + + # find comment opening, validate it was not closed, and check its using star prefixes + execute-keys -draft /\* \*/ \A\h*/\*([^\n]*\n\h*\*)*[^\n]*\n\h*.\z + + try %[ + # if the previous line is opening the comment, insert star preceeded by space + execute-keys -draft k^\h*/\* + execute-keys -draft i* + ] catch %[ + try %[ + # if the next line is a comment line insert a star + execute-keys -draft j^\h+\* + execute-keys -draft i* + ] catch %[ + try %[ + # if the previous line is an empty comment line, close the comment scope + execute-keys -draft k^\h+\*\h+$ 1s\*(\h*)c/ + ] catch %[ + # if the previous line is a non-empty comment line, add a star + execute-keys -draft i* + ] + ] + ] + + # trim trailing whitespace on the previous line + try %[ execute-keys -draft s\h+$ d ] + # align the new star with the previous one + execute-keys K1s^[^*]*(\*)& + ] } catch %` # preserve previous line indent try %{ execute-keys -draft K } diff --git a/test/indent/rust/comment/cmd b/test/indent/rust/block-comment-close/cmd similarity index 100% rename from test/indent/rust/comment/cmd rename to test/indent/rust/block-comment-close/cmd diff --git a/test/indent/rust/block-comment-close/in b/test/indent/rust/block-comment-close/in new file mode 100644 index 00000000..2553fd94 --- /dev/null +++ b/test/indent/rust/block-comment-close/in @@ -0,0 +1,6 @@ + /* foo + * %( ) + + /* foo + *%( ) + diff --git a/test/indent/rust/block-comment-close/out b/test/indent/rust/block-comment-close/out new file mode 100644 index 00000000..4cdd2f36 --- /dev/null +++ b/test/indent/rust/block-comment-close/out @@ -0,0 +1,8 @@ + /* foo + */ +bar + + /* foo + * + * bar + diff --git a/test/indent/rust/comment/rc b/test/indent/rust/block-comment-close/rc similarity index 100% rename from test/indent/rust/comment/rc rename to test/indent/rust/block-comment-close/rc diff --git a/test/indent/rust/block-comment/cmd b/test/indent/rust/block-comment/cmd new file mode 100644 index 00000000..8682d51e --- /dev/null +++ b/test/indent/rust/block-comment/cmd @@ -0,0 +1 @@ +cbar diff --git a/test/indent/rust/block-comment/in b/test/indent/rust/block-comment/in new file mode 100644 index 00000000..c38e3e41 --- /dev/null +++ b/test/indent/rust/block-comment/in @@ -0,0 +1,12 @@ + /* foo%( ) + + /*! foo%( ) + + /*!! foo%( ) + + /** foo%( ) + + /*** foo%( ) + + println!("hello world"); /* foo%( ) + diff --git a/test/indent/rust/block-comment/out b/test/indent/rust/block-comment/out new file mode 100644 index 00000000..e6c24302 --- /dev/null +++ b/test/indent/rust/block-comment/out @@ -0,0 +1,18 @@ + /* foo + * bar + + /*! foo + * bar + + /*!! foo + * bar + + /** foo + * bar + + /*** foo + * bar + + println!("hello world"); /* foo + bar + diff --git a/test/indent/rust/block-comment/rc b/test/indent/rust/block-comment/rc new file mode 100644 index 00000000..64064c25 --- /dev/null +++ b/test/indent/rust/block-comment/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/rust.kak" +set buffer filetype rust diff --git a/test/indent/rust/line-comment-close/cmd b/test/indent/rust/line-comment-close/cmd new file mode 100644 index 00000000..8682d51e --- /dev/null +++ b/test/indent/rust/line-comment-close/cmd @@ -0,0 +1 @@ +cbar diff --git a/test/indent/rust/line-comment-close/in b/test/indent/rust/line-comment-close/in new file mode 100644 index 00000000..6832ae64 --- /dev/null +++ b/test/indent/rust/line-comment-close/in @@ -0,0 +1,26 @@ + // foo + // %( ) + + // foo + //%( ) + + //! %( ) + + //!%( ) + + //!! %( ) + + //!!%( ) + + /// %( ) + + ///%( ) + + //// %( ) + + ////%( ) + + println!("hello world"); // %( ) + + println!("hello world"); //%( ) + diff --git a/test/indent/rust/line-comment-close/out b/test/indent/rust/line-comment-close/out new file mode 100644 index 00000000..dc8f04e4 --- /dev/null +++ b/test/indent/rust/line-comment-close/out @@ -0,0 +1,33 @@ + // foo + // + // bar + + // foo + bar + + //! + //! bar + + bar + + //!! + //!! bar + + bar + + /// + /// bar + + bar + + //// + //// bar + + bar + + println!("hello world"); // + bar + + println!("hello world"); // + bar + diff --git a/test/indent/rust/line-comment-close/rc b/test/indent/rust/line-comment-close/rc new file mode 100644 index 00000000..64064c25 --- /dev/null +++ b/test/indent/rust/line-comment-close/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/rust.kak" +set buffer filetype rust diff --git a/test/indent/rust/line-comment/cmd b/test/indent/rust/line-comment/cmd new file mode 100644 index 00000000..8682d51e --- /dev/null +++ b/test/indent/rust/line-comment/cmd @@ -0,0 +1 @@ +cbar diff --git a/test/indent/rust/comment/in b/test/indent/rust/line-comment/in similarity index 100% rename from test/indent/rust/comment/in rename to test/indent/rust/line-comment/in diff --git a/test/indent/rust/comment/out b/test/indent/rust/line-comment/out similarity index 100% rename from test/indent/rust/comment/out rename to test/indent/rust/line-comment/out diff --git a/test/indent/rust/line-comment/rc b/test/indent/rust/line-comment/rc new file mode 100644 index 00000000..64064c25 --- /dev/null +++ b/test/indent/rust/line-comment/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/rust.kak" +set buffer filetype rust