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