Skip to content

Commit

Permalink
feat(rust): insert docblock above macros
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed May 5, 2023
1 parent 9849b60 commit 74b7ba1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion autoload/doge/preprocessors/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions autoload/doge/preprocessors/rust.vim
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test/filetypes/rust/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -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\<CR>
\<C-d>

Expect rust (generated comments with Arguments, Safety, Errors and Examples sections):
Expand All @@ -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> {}

0 comments on commit 74b7ba1

Please sign in to comment.