From 11d98a07dc5ad7e8af8971f1ba5352cc7140b1dc Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sun, 27 Sep 2020 15:52:42 +0800 Subject: [PATCH 1/6] Rust align open paren for if and for --- rc/filetype/rust.kak | 2 +- test/indent/rust/on-open-paren/in | 9 +++++++++ test/indent/rust/on-open-paren/out | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/rc/filetype/rust.kak b/rc/filetype/rust.kak index d7ff4952..a06ae7d1 100644 --- a/rc/filetype/rust.kak +++ b/rc/filetype/rust.kak @@ -174,7 +174,7 @@ define-command -hidden rust-indent-on-opening-curly-brace %[ # align indent with opening paren when { is entered on a new line after the closing paren try %[ execute-keys -draft h ) M \A\(.*\)\h*\n\h*\{\z s \A|.\z 1 ] # dedent standalone { after impl and related block without any { in between - try %< execute-keys -draft hh impl|fn|struct|enum|union \{ ll ^\h*\{$ > + try %< execute-keys -draft hh impl|fn|struct|enum|union|if|for \{ ll ^\h*\{$ > _ ] diff --git a/test/indent/rust/on-open-paren/in b/test/indent/rust/on-open-paren/in index 1ca71d62..661f2331 100644 --- a/test/indent/rust/on-open-paren/in +++ b/test/indent/rust/on-open-paren/in @@ -33,3 +33,12 @@ union X where T: Debug %( ) + if foo() + && bar() + %( ) + + for x in group + .iter() + .sorted_by(|a, b| Ord::cmp(&a.0, &b.0)) + %( ) + diff --git a/test/indent/rust/on-open-paren/out b/test/indent/rust/on-open-paren/out index de125ea7..60973f8a 100644 --- a/test/indent/rust/on-open-paren/out +++ b/test/indent/rust/on-open-paren/out @@ -33,3 +33,12 @@ union X where T: Debug { + if foo() + && bar() + { + + for x in group + .iter() + .sorted_by(|a, b| Ord::cmp(&a.0, &b.0)) + { + From 5c8dfcdfa946d9018150e67a32f472b2e80a9cd7 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sun, 27 Sep 2020 15:53:15 +0800 Subject: [PATCH 2/6] Rust improve align after partial statement --- rc/filetype/rust.kak | 15 ++++--- test/indent/rust/after-variable/cmd | 1 + test/indent/rust/after-variable/in | 6 +++ test/indent/rust/after-variable/out | 9 +++++ test/indent/rust/after-variable/rc | 3 ++ test/indent/rust/after-where/in | 2 - test/indent/rust/after-where/out | 3 -- test/indent/rust/line-start-with-operator/bar | 36 +++++++++++++++++ test/indent/rust/line-start-with-operator/cmd | 1 + test/indent/rust/line-start-with-operator/in | 26 +++++++++++++ test/indent/rust/line-start-with-operator/out | 39 +++++++++++++++++++ test/indent/rust/line-start-with-operator/rc | 3 ++ 12 files changed, 131 insertions(+), 13 deletions(-) create mode 100644 test/indent/rust/after-variable/cmd create mode 100644 test/indent/rust/after-variable/in create mode 100644 test/indent/rust/after-variable/out create mode 100644 test/indent/rust/after-variable/rc create mode 100644 test/indent/rust/line-start-with-operator/bar create mode 100644 test/indent/rust/line-start-with-operator/cmd create mode 100644 test/indent/rust/line-start-with-operator/in create mode 100644 test/indent/rust/line-start-with-operator/out create mode 100644 test/indent/rust/line-start-with-operator/rc diff --git a/rc/filetype/rust.kak b/rc/filetype/rust.kak index a06ae7d1..f2fbfee1 100644 --- a/rc/filetype/rust.kak +++ b/rc/filetype/rust.kak @@ -148,16 +148,15 @@ define-command -hidden rust-indent-on-new-line %~ } catch %` # preserve previous line indent try %{ execute-keys -draft K } - # indent after lines ending with { or ( - try %[ execute-keys -draft k [{(]\h*$ j ] # indent after lines ending with [{(].+ and move first parameter to own line try %< execute-keys -draft [c[({],[)}] \A[({][^\n]+\n[^\n]*\n?\z L i > - # indent lines with a standalone where - try %+ execute-keys -draft k ^\h*where\h*$ j + - # dedent after lines starting with . and ending with , or ; - try %_ execute-keys -draft k ^\h*\..*[,]\h*$ j _ - # deindent closing brace(s) when after cursor - try %= execute-keys -draft ^\h*[})] gh / [})] m 1 = + # indent after non-empty lines not starting with operator and not ending with , or ; + # XXX s [^\h]* is workaround for broken negate match #3766, use this later + try %< execute-keys -draft k s [^\h].+ \A[-+*/&|^})] [,](\h*/[/*].*|)$ j > + # dedent after lines starting with . and ending with } or ) or , or ; + try %_ execute-keys -draft k ^\h*\..*[}),]\h*$ j _ + # align to opening curly brace or paren when newline is inserted before a single closing + try %< execute-keys -draft ^\h*[)}] h m 1 > # todo dedent additional unmatched parenthesis # try %& execute-keys -draft k s \((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\) l Gl s\) %sh{ # count previous selections length diff --git a/test/indent/rust/after-variable/cmd b/test/indent/rust/after-variable/cmd new file mode 100644 index 00000000..932ef40d --- /dev/null +++ b/test/indent/rust/after-variable/cmd @@ -0,0 +1 @@ +c.baz() diff --git a/test/indent/rust/after-variable/in b/test/indent/rust/after-variable/in new file mode 100644 index 00000000..59951f6d --- /dev/null +++ b/test/indent/rust/after-variable/in @@ -0,0 +1,6 @@ + foo%( ) + + Foo(bar)%( ) + + Foo { bar }%( ) + diff --git a/test/indent/rust/after-variable/out b/test/indent/rust/after-variable/out new file mode 100644 index 00000000..4f5315a0 --- /dev/null +++ b/test/indent/rust/after-variable/out @@ -0,0 +1,9 @@ + foo + .baz() + + Foo(bar) + .baz() + + Foo { bar } + .baz() + diff --git a/test/indent/rust/after-variable/rc b/test/indent/rust/after-variable/rc new file mode 100644 index 00000000..64064c25 --- /dev/null +++ b/test/indent/rust/after-variable/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/after-where/in b/test/indent/rust/after-where/in index 412f13d3..d935a00c 100644 --- a/test/indent/rust/after-where/in +++ b/test/indent/rust/after-where/in @@ -1,5 +1,3 @@ - impl X for T where%( ) - impl X for T where%( ) diff --git a/test/indent/rust/after-where/out b/test/indent/rust/after-where/out index 3cc8383e..caa373e4 100644 --- a/test/indent/rust/after-where/out +++ b/test/indent/rust/after-where/out @@ -1,6 +1,3 @@ - impl X for T where - bar - impl X for T where bar diff --git a/test/indent/rust/line-start-with-operator/bar b/test/indent/rust/line-start-with-operator/bar new file mode 100644 index 00000000..3aa5220d --- /dev/null +++ b/test/indent/rust/line-start-with-operator/bar @@ -0,0 +1,36 @@ + .foo + bar + + + foo + bar + + - foo + bar + + * foo + bar + + / foo + bar + + & foo + bar + + | foo + bar + + ^ foo + bar + + && foo + bar + + || foo + bar + + < foo + bar + + > foo + bar + diff --git a/test/indent/rust/line-start-with-operator/cmd b/test/indent/rust/line-start-with-operator/cmd new file mode 100644 index 00000000..8682d51e --- /dev/null +++ b/test/indent/rust/line-start-with-operator/cmd @@ -0,0 +1 @@ +cbar diff --git a/test/indent/rust/line-start-with-operator/in b/test/indent/rust/line-start-with-operator/in new file mode 100644 index 00000000..0b17056b --- /dev/null +++ b/test/indent/rust/line-start-with-operator/in @@ -0,0 +1,26 @@ + .foo%( ) + + .foo()%( ) + + + foo%( ) + + - foo%( ) + + * foo%( ) + + / foo%( ) + + & foo%( ) + + | foo%( ) + + ^ foo%( ) + + && foo%( ) + + || foo%( ) + + < foo%( ) + + > foo%( ) + diff --git a/test/indent/rust/line-start-with-operator/out b/test/indent/rust/line-start-with-operator/out new file mode 100644 index 00000000..f81bfdec --- /dev/null +++ b/test/indent/rust/line-start-with-operator/out @@ -0,0 +1,39 @@ + .foo + bar + + .foo() + bar + + + foo + bar + + - foo + bar + + * foo + bar + + / foo + bar + + & foo + bar + + | foo + bar + + ^ foo + bar + + && foo + bar + + || foo + bar + + < foo + bar + + > foo + bar + diff --git a/test/indent/rust/line-start-with-operator/rc b/test/indent/rust/line-start-with-operator/rc new file mode 100644 index 00000000..64064c25 --- /dev/null +++ b/test/indent/rust/line-start-with-operator/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/rust.kak" +set buffer filetype rust From 785cbaeaed59d4ec78ad2935b5ef2ac340e2ac03 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sun, 27 Sep 2020 23:34:08 +0800 Subject: [PATCH 3/6] Add rust test for empty line indent --- rc/filetype/rust.kak | 2 +- test/indent/rust/empty-line/cmd | 1 + test/indent/rust/empty-line/in | 2 ++ test/indent/rust/empty-line/out | 3 +++ test/indent/rust/empty-line/rc | 3 +++ 5 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 test/indent/rust/empty-line/cmd create mode 100644 test/indent/rust/empty-line/in create mode 100644 test/indent/rust/empty-line/out create mode 100644 test/indent/rust/empty-line/rc diff --git a/rc/filetype/rust.kak b/rc/filetype/rust.kak index f2fbfee1..51b5ef67 100644 --- a/rc/filetype/rust.kak +++ b/rc/filetype/rust.kak @@ -151,7 +151,7 @@ define-command -hidden rust-indent-on-new-line %~ # indent after lines ending with [{(].+ and move first parameter to own line try %< execute-keys -draft [c[({],[)}] \A[({][^\n]+\n[^\n]*\n?\z L i > # indent after non-empty lines not starting with operator and not ending with , or ; - # XXX s [^\h]* is workaround for broken negate match #3766, use this later + # XXX simplify this into a single without s try %< execute-keys -draft k s [^\h].+ \A[-+*/&|^})] [,](\h*/[/*].*|)$ j > # dedent after lines starting with . and ending with } or ) or , or ; try %_ execute-keys -draft k ^\h*\..*[}),]\h*$ j _ diff --git a/test/indent/rust/empty-line/cmd b/test/indent/rust/empty-line/cmd new file mode 100644 index 00000000..8682d51e --- /dev/null +++ b/test/indent/rust/empty-line/cmd @@ -0,0 +1 @@ +cbar diff --git a/test/indent/rust/empty-line/in b/test/indent/rust/empty-line/in new file mode 100644 index 00000000..e4ed2188 --- /dev/null +++ b/test/indent/rust/empty-line/in @@ -0,0 +1,2 @@ + %( ) + diff --git a/test/indent/rust/empty-line/out b/test/indent/rust/empty-line/out new file mode 100644 index 00000000..4567eae7 --- /dev/null +++ b/test/indent/rust/empty-line/out @@ -0,0 +1,3 @@ + + bar + diff --git a/test/indent/rust/empty-line/rc b/test/indent/rust/empty-line/rc new file mode 100644 index 00000000..64064c25 --- /dev/null +++ b/test/indent/rust/empty-line/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/rust.kak" +set buffer filetype rust From df68a77ed2612ec1d73c7de767fa698a98302c61 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Mon, 28 Sep 2020 00:03:53 +0800 Subject: [PATCH 4/6] Rust reindent where to match block --- rc/filetype/rust.kak | 2 ++ test/indent/rust/before-where/cmd | 1 + test/indent/rust/before-where/in | 2 ++ test/indent/rust/before-where/out | 4 ++++ test/indent/rust/before-where/rc | 3 +++ 5 files changed, 12 insertions(+) create mode 100644 test/indent/rust/before-where/cmd create mode 100644 test/indent/rust/before-where/in create mode 100644 test/indent/rust/before-where/out create mode 100644 test/indent/rust/before-where/rc diff --git a/rc/filetype/rust.kak b/rc/filetype/rust.kak index 51b5ef67..b1024424 100644 --- a/rc/filetype/rust.kak +++ b/rc/filetype/rust.kak @@ -146,6 +146,8 @@ define-command -hidden rust-indent-on-new-line %~ execute-keys K1s^[^*]*(\*)& ] } catch %` + # re-indent previous line if it starts with where to match previous block + try %+ execute-keys -draft k ^\h*where\b hh impl|fn|struct|enum|union 1 + # preserve previous line indent try %{ execute-keys -draft K } # indent after lines ending with [{(].+ and move first parameter to own line diff --git a/test/indent/rust/before-where/cmd b/test/indent/rust/before-where/cmd new file mode 100644 index 00000000..fe3daa48 --- /dev/null +++ b/test/indent/rust/before-where/cmd @@ -0,0 +1 @@ +cwherebar diff --git a/test/indent/rust/before-where/in b/test/indent/rust/before-where/in new file mode 100644 index 00000000..0c1161c1 --- /dev/null +++ b/test/indent/rust/before-where/in @@ -0,0 +1,2 @@ + impl X for T%( ) + diff --git a/test/indent/rust/before-where/out b/test/indent/rust/before-where/out new file mode 100644 index 00000000..caa373e4 --- /dev/null +++ b/test/indent/rust/before-where/out @@ -0,0 +1,4 @@ + impl X for T + where + bar + diff --git a/test/indent/rust/before-where/rc b/test/indent/rust/before-where/rc new file mode 100644 index 00000000..64064c25 --- /dev/null +++ b/test/indent/rust/before-where/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/rust.kak" +set buffer filetype rust From 56e12013c850523a8de983bb4ecc6f66d45a4815 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Mon, 28 Sep 2020 00:09:12 +0800 Subject: [PATCH 5/6] Rust indent keyword check boundary --- rc/filetype/rust.kak | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rc/filetype/rust.kak b/rc/filetype/rust.kak index b1024424..d4b9826c 100644 --- a/rc/filetype/rust.kak +++ b/rc/filetype/rust.kak @@ -147,7 +147,7 @@ define-command -hidden rust-indent-on-new-line %~ ] } catch %` # re-indent previous line if it starts with where to match previous block - try %+ execute-keys -draft k ^\h*where\b hh impl|fn|struct|enum|union 1 + + try %+ execute-keys -draft k ^\h*where\b hh ^\h*\b(impl|fn|struct|enum|union)\b 1 + # preserve previous line indent try %{ execute-keys -draft K } # indent after lines ending with [{(].+ and move first parameter to own line @@ -175,7 +175,7 @@ define-command -hidden rust-indent-on-opening-curly-brace %[ # align indent with opening paren when { is entered on a new line after the closing paren try %[ execute-keys -draft h ) M \A\(.*\)\h*\n\h*\{\z s \A|.\z 1 ] # dedent standalone { after impl and related block without any { in between - try %< execute-keys -draft hh impl|fn|struct|enum|union|if|for \{ ll ^\h*\{$ > + try %< execute-keys -draft hh ^\h*\b(impl|fn|struct|enum|union|if|for)\b \{ ll ^\h*\{$ > _ ] From bfca07da4d52b821c67a5b4e3eb056df9d98bd55 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 30 Sep 2020 00:37:35 +0800 Subject: [PATCH 6/6] Rust not to indent on hash --- rc/filetype/rust.kak | 2 +- test/indent/rust/line-start-with-operator/in | 2 ++ test/indent/rust/line-start-with-operator/out | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/rc/filetype/rust.kak b/rc/filetype/rust.kak index d4b9826c..9117de8e 100644 --- a/rc/filetype/rust.kak +++ b/rc/filetype/rust.kak @@ -154,7 +154,7 @@ define-command -hidden rust-indent-on-new-line %~ try %< execute-keys -draft [c[({],[)}] \A[({][^\n]+\n[^\n]*\n?\z L i > # indent after non-empty lines not starting with operator and not ending with , or ; # XXX simplify this into a single without s - try %< execute-keys -draft k s [^\h].+ \A[-+*/&|^})] [,](\h*/[/*].*|)$ j > + try %< execute-keys -draft k s [^\h].+ \A[-+*/&|^})#] [,](\h*/[/*].*|)$ j > # dedent after lines starting with . and ending with } or ) or , or ; try %_ execute-keys -draft k ^\h*\..*[}),]\h*$ j _ # align to opening curly brace or paren when newline is inserted before a single closing diff --git a/test/indent/rust/line-start-with-operator/in b/test/indent/rust/line-start-with-operator/in index 0b17056b..063f771e 100644 --- a/test/indent/rust/line-start-with-operator/in +++ b/test/indent/rust/line-start-with-operator/in @@ -24,3 +24,5 @@ > foo%( ) + #[derive(Debug)]%( ) + diff --git a/test/indent/rust/line-start-with-operator/out b/test/indent/rust/line-start-with-operator/out index f81bfdec..f270d0d9 100644 --- a/test/indent/rust/line-start-with-operator/out +++ b/test/indent/rust/line-start-with-operator/out @@ -37,3 +37,6 @@ > foo bar + #[derive(Debug)] + bar +