Improve rust comment closing handling
/// foo ///%( ) /// foo /// %( ) With `c<ret>bar<esc>`, /// 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.
This commit is contained in:
parent
dd6684a17d
commit
974684aa68
|
@ -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 <a-x> s ^\h*//[!/]{0,2}\h* <ret> y gh j P
|
||||
try %[ # line comment
|
||||
evaluate-commands -draft -save-regs '/"' %[
|
||||
# copy the commenting prefix
|
||||
execute-keys -save-regs '' k <a-x> s ^\h*//[!/]{0,2}\h* <ret> y
|
||||
try %[
|
||||
# if the previous comment isn't empty, create a new one
|
||||
execute-keys <a-x><a-K>^\h*//[!/]{0,2}$<ret> j<a-x>s^\h*<ret>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 //.*<ret> d
|
||||
]
|
||||
]
|
||||
] catch %[ # block comment
|
||||
# if the previous line isn't within a comment scope, break
|
||||
execute-keys -draft k<a-x> <a-k>^(\h*/\*|\h+\*(?!/))<ret>
|
||||
|
||||
# find comment opening, validate it was not closed, and check its using star prefixes
|
||||
execute-keys -draft <a-?>/\*<ret><a-H> <a-K>\*/<ret> <a-k>\A\h*/\*([^\n]*\n\h*\*)*[^\n]*\n\h*.\z<ret>
|
||||
|
||||
try %[
|
||||
# if the previous line is opening the comment, insert star preceeded by space
|
||||
execute-keys -draft k<a-x><a-k>^\h*/\*<ret>
|
||||
execute-keys -draft i*<space><esc>
|
||||
] catch %[
|
||||
try %[
|
||||
# if the next line is a comment line insert a star
|
||||
execute-keys -draft j<a-x><a-k>^\h+\*<ret>
|
||||
execute-keys -draft i*<space><esc>
|
||||
] catch %[
|
||||
try %[
|
||||
# if the previous line is an empty comment line, close the comment scope
|
||||
execute-keys -draft k<a-x><a-k>^\h+\*\h+$<ret> <a-x>1s\*(\h*)<ret>c/<esc>
|
||||
] catch %[
|
||||
# if the previous line is a non-empty comment line, add a star
|
||||
execute-keys -draft i*<space><esc>
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
# trim trailing whitespace on the previous line
|
||||
try %[ execute-keys -draft s\h+$<ret> d ]
|
||||
# align the new star with the previous one
|
||||
execute-keys K<a-x>1s^[^*]*(\*)<ret>&
|
||||
]
|
||||
} catch %`
|
||||
# preserve previous line indent
|
||||
try %{ execute-keys -draft <semicolon> K <a-&> }
|
||||
|
|
6
test/indent/rust/block-comment-close/in
Normal file
6
test/indent/rust/block-comment-close/in
Normal file
|
@ -0,0 +1,6 @@
|
|||
/* foo
|
||||
* %( )
|
||||
|
||||
/* foo
|
||||
*%( )
|
||||
|
8
test/indent/rust/block-comment-close/out
Normal file
8
test/indent/rust/block-comment-close/out
Normal file
|
@ -0,0 +1,8 @@
|
|||
/* foo
|
||||
*/
|
||||
bar
|
||||
|
||||
/* foo
|
||||
*
|
||||
* bar
|
||||
|
1
test/indent/rust/block-comment/cmd
Normal file
1
test/indent/rust/block-comment/cmd
Normal file
|
@ -0,0 +1 @@
|
|||
c<ret>bar<esc>
|
12
test/indent/rust/block-comment/in
Normal file
12
test/indent/rust/block-comment/in
Normal file
|
@ -0,0 +1,12 @@
|
|||
/* foo%( )
|
||||
|
||||
/*! foo%( )
|
||||
|
||||
/*!! foo%( )
|
||||
|
||||
/** foo%( )
|
||||
|
||||
/*** foo%( )
|
||||
|
||||
println!("hello world"); /* foo%( )
|
||||
|
18
test/indent/rust/block-comment/out
Normal file
18
test/indent/rust/block-comment/out
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* foo
|
||||
* bar
|
||||
|
||||
/*! foo
|
||||
* bar
|
||||
|
||||
/*!! foo
|
||||
* bar
|
||||
|
||||
/** foo
|
||||
* bar
|
||||
|
||||
/*** foo
|
||||
* bar
|
||||
|
||||
println!("hello world"); /* foo
|
||||
bar
|
||||
|
3
test/indent/rust/block-comment/rc
Normal file
3
test/indent/rust/block-comment/rc
Normal file
|
@ -0,0 +1,3 @@
|
|||
source "%val{runtime}/colors/default.kak"
|
||||
source "%val{runtime}/rc/filetype/rust.kak"
|
||||
set buffer filetype rust
|
1
test/indent/rust/line-comment-close/cmd
Normal file
1
test/indent/rust/line-comment-close/cmd
Normal file
|
@ -0,0 +1 @@
|
|||
c<ret>bar<esc>
|
26
test/indent/rust/line-comment-close/in
Normal file
26
test/indent/rust/line-comment-close/in
Normal file
|
@ -0,0 +1,26 @@
|
|||
// foo
|
||||
// %( )
|
||||
|
||||
// foo
|
||||
//%( )
|
||||
|
||||
//! %( )
|
||||
|
||||
//!%( )
|
||||
|
||||
//!! %( )
|
||||
|
||||
//!!%( )
|
||||
|
||||
/// %( )
|
||||
|
||||
///%( )
|
||||
|
||||
//// %( )
|
||||
|
||||
////%( )
|
||||
|
||||
println!("hello world"); // %( )
|
||||
|
||||
println!("hello world"); //%( )
|
||||
|
33
test/indent/rust/line-comment-close/out
Normal file
33
test/indent/rust/line-comment-close/out
Normal file
|
@ -0,0 +1,33 @@
|
|||
// foo
|
||||
//
|
||||
// bar
|
||||
|
||||
// foo
|
||||
bar
|
||||
|
||||
//!
|
||||
//! bar
|
||||
|
||||
bar
|
||||
|
||||
//!!
|
||||
//!! bar
|
||||
|
||||
bar
|
||||
|
||||
///
|
||||
/// bar
|
||||
|
||||
bar
|
||||
|
||||
////
|
||||
//// bar
|
||||
|
||||
bar
|
||||
|
||||
println!("hello world"); //
|
||||
bar
|
||||
|
||||
println!("hello world"); //
|
||||
bar
|
||||
|
3
test/indent/rust/line-comment-close/rc
Normal file
3
test/indent/rust/line-comment-close/rc
Normal file
|
@ -0,0 +1,3 @@
|
|||
source "%val{runtime}/colors/default.kak"
|
||||
source "%val{runtime}/rc/filetype/rust.kak"
|
||||
set buffer filetype rust
|
1
test/indent/rust/line-comment/cmd
Normal file
1
test/indent/rust/line-comment/cmd
Normal file
|
@ -0,0 +1 @@
|
|||
c<ret>bar<esc>
|
3
test/indent/rust/line-comment/rc
Normal file
3
test/indent/rust/line-comment/rc
Normal file
|
@ -0,0 +1,3 @@
|
|||
source "%val{runtime}/colors/default.kak"
|
||||
source "%val{runtime}/rc/filetype/rust.kak"
|
||||
set buffer filetype rust
|
Loading…
Reference in New Issue
Block a user