diff --git a/autoload/doge/preprocessors/javascript.vim b/autoload/doge/preprocessors/javascript.vim index 4fdde52d..3c446f0b 100644 --- a/autoload/doge/preprocessors/javascript.vim +++ b/autoload/doge/preprocessors/javascript.vim @@ -3,7 +3,7 @@ set cpoptions&vim " Alter the insert position for JavaScript functions. function! doge#preprocessors#javascript#insert_position(lnum_insert_pos) abort - " In Java some functions may have the ES7 decorators above them. + " In Java, some functions may have the ES7 decorators above them. " If this is the case we want to insert above this. " " Example: diff --git a/autoload/doge/preprocessors/rust.vim b/autoload/doge/preprocessors/rust.vim new file mode 100644 index 00000000..ff9a3037 --- /dev/null +++ b/autoload/doge/preprocessors/rust.vim @@ -0,0 +1,38 @@ +let s:save_cpo = &cpoptions +set cpoptions&vim + +" Alter the insert position for JavaScript functions. +function! doge#preprocessors#rust#insert_position(lnum_insert_pos) abort + " In Rust, some functions may have #[...] macros above them. + " If this is the case we want to insert above this. + " + " Example: + " #[doc(alias = "x")] + " #[doc(alias = "big")] + " pub struct BigX; + + " Go to the beginning of the line. + call execute('normal! ^') + + let l:offset = 1 + let l:has_macros = 0 + while doge#helpers#trim(getline(line('.') - l:offset)) =~# '\m^#[' + + " Assume that a user won't have more than 20 macros on a function. + " When we reach 20 lines or more, return and do nothing. + if l:offset > 20 + return a:lnum_insert_pos + endif + + let l:has_macros = 1 + let l:offset += 1 + + endwhile + + return l:has_macros == v:true + \ ? a:lnum_insert_pos - l:offset + 1 + \ : a:lnum_insert_pos +endfunction + +let &cpoptions = s:save_cpo +unlet s:save_cpo diff --git a/test/filetypes/rust/functions.vader b/test/filetypes/rust/functions.vader index a0b748ff..6feafdc6 100644 --- a/test/filetypes/rust/functions.vader +++ b/test/filetypes/rust/functions.vader @@ -63,9 +63,11 @@ Expect rust (generated comments with Arguments and Examples sections): # Functions with errors and safety section # ============================================================================== Given rust (impl methods with Errors and Safety section): + #[macro_export] pub unsafe fn foo(foo: usize) -> Result<(), FooError> {} Do (trigger doge): + :2\ \ Expect rust (generated comments with Arguments, Safety, Errors and Examples sections): @@ -88,4 +90,5 @@ Expect rust (generated comments with Arguments, Safety, Errors and Examples sect /// ``` /// [TODO:example] /// ``` + #[macro_export] pub unsafe fn foo(foo: usize) -> Result<(), FooError> {}