Skip to content

Commit

Permalink
fix(cpp): Prevent @return tag to be added when returning void (issue #5)
Browse files Browse the repository at this point in the history
Former-commit-id: 05f208d
  • Loading branch information
kkoomen committed Jul 10, 2019
1 parent 7ea3015 commit ec4fdd9
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 16 deletions.
14 changes: 14 additions & 0 deletions autoload/doge/preprocessors/cpp.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

" A callback function being called after the tokens have been extracted. This
" function will adjust the input if needed.
function! doge#preprocessors#cpp#tokens(tokens) abort
" See https://github.com/kkoomen/vim-doge/issues/5
if has_key(a:tokens, 'returnType') && !empty(a:tokens['returnType']) && a:tokens['returnType'] ==# 'void'
let a:tokens['returnType'] = ''
endif
endfunction

let &cpoptions = s:save_cpo
unlet s:save_cpo
3 changes: 1 addition & 2 deletions autoload/doge/token.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ function! s:token_replace(tokens, text) abort
let l:conditional_pattern = printf('\m#(%s|\(.*\))', l:token)
if l:text =~# l:conditional_pattern
let l:conditional_pattern_replacement_value = '\1'
if (type(l:token_value) == v:t_string && empty(l:token_value))
\ || (type(l:token_value) == v:t_list && len(l:token_value) < 1)
if (type(l:token_value) == v:t_string && empty(l:token_value)) || (type(l:token_value) == v:t_list && len(l:token_value) < 1)
let l:conditional_pattern_replacement_value = ''
let l:empty_conditional_pattern_value = 1
endif
Expand Down
16 changes: 8 additions & 8 deletions ftplugin/cpp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ let s:parameters_match_pattern = '\m\%(\%(const\)\s\+\)\?\%(\%([[:alnum:]_]\+(.\
" template<class F, class... Args>
" decltype(auto) PerfectForward(F fun, Args&&... args) {}
call add(b:doge_patterns, {
\ 'match': '\m^\%(template\s*<[[:alnum:][:space:]_<>.,]*>\s\+\)\?\%(static\s\+\)\?\%(decltype(auto[&*]*)\|auto[&*]*\)\s\+\%([[:alnum:]_:]\+\)\s*(\(.\{-}\))\s*\%(\%([[:alnum:]_:]\+\s*(\(.\{-}\))\)*\s\+\)\?\%(\s*->\s*[[:alnum:]_:&*.]\+\%(<[[:alnum:][:space:]_(),]\+>\%([[:alnum:]_:&*.]\+\)*\)*\)\?\s*[;{]',
\ 'match_group_names': ['parameters'],
\ 'match': '\m^\%(template\s*<[[:alnum:][:space:]_<>.,]*>\s\+\)\?\%(static\s\+\)\?\%(decltype(auto[&*]*)\|auto[&*]*\)\s\+\%([[:alnum:]_:]\+\)\s*(\(.\{-}\))\s*\%(\%([[:alnum:]_:]\+\s*(\%(.\{-}\))\)*\s\+\)\?\s*->\s*\([[:alnum:]_:&*.]\+\%(<[[:alnum:][:space:]_(),]\+>\%([[:alnum:]_:&*.]\+\)*\)*\)\s*[;{]',
\ 'match_group_names': ['parameters', 'returnType'],
\ 'parameters': {
\ 'match': s:parameters_match_pattern,
\ 'match_group_names': ['name'],
Expand All @@ -52,9 +52,9 @@ call add(b:doge_patterns, {
\ 'doxygen': [
\ '/**',
\ ' * TODO',
\ ' *',
\ '#(parameters| *)',
\ '#(parameters| * {parameters})',
\ ' * @return TODO',
\ '#(returnType| * @return TODO)',
\ ' */',
\ ],
\ },
Expand All @@ -78,8 +78,8 @@ call add(b:doge_patterns, {
" template<typename T, typename... Args>
" static T* create(Args&& ... args) {}
call add(b:doge_patterns, {
\ 'match': '\m^\%(\%(template\s*<[[:alnum:][:space:]_<>.,]*>\|const\|inline\)\s\+\)\?\%(static\s\+\)\?\%([[:alnum:]_:&*]\+\s*\%(<[[:alnum:][:space:]_<>.,]\+>\)\?\|[[:alnum:]_]\+(.\{-})\)\s\+\%([[:alnum:]_:]\+\)\s*(\(.\{-}\))\s*[;{]',
\ 'match_group_names': ['parameters'],
\ 'match': '\m^\%(\%(template\s*<[[:alnum:][:space:]_<>.,]*>\|const\|inline\)\s\+\)\?\%(static\s\+\)\?\([[:alnum:]_:&*]\+\s*\%(<[[:alnum:][:space:]_<>.,]\+>\)\?\|[[:alnum:]_]\+(.\{-})\)\s\+\%([[:alnum:]_:]\+\)\s*(\(.\{-}\))\s*[;{]',
\ 'match_group_names': ['returnType', 'parameters'],
\ 'parameters': {
\ 'match': s:parameters_match_pattern,
\ 'match_group_names': ['name'],
Expand All @@ -93,9 +93,9 @@ call add(b:doge_patterns, {
\ 'doxygen': [
\ '/**',
\ ' * TODO',
\ ' *',
\ '#(parameters| *)',
\ '#(parameters| * {parameters})',
\ ' * @return TODO',
\ '#(returnType| * @return TODO)',
\ ' */',
\ ],
\ },
Expand Down
27 changes: 27 additions & 0 deletions test/filetypes/cpp/auto-functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Expect cpp (generated comment with @param and @return tags):
{
//
}

# ==============================================================================
# Auto-functions with parameters with advanced type hints.
# ==============================================================================
Expand Down Expand Up @@ -51,6 +52,32 @@ Expect cpp (generated comment with @param and @return tags):
//
}

# ==============================================================================
# Auto-functions with parameters with advanced type hints and void return type.
# ==============================================================================
Given cpp (auto-function with parameters with advanced type hints with void return type using return type 'auto'):
template<auto n>
auto f(std::enable_if<is_foreach_iterator<T>, T>::type& it_begin, T& _end) -> void // inline comment
{
//
}

Do (trigger doge):
\<C-d>

Expect cpp (generated comment with @param and @return tags):
/**
* TODO
*
* @param it_begin TODO
* @param _end TODO
*/
template<auto n>
auto f(std::enable_if<is_foreach_iterator<T>, T>::type& it_begin, T& _end) -> void // inline comment
{
//
}

# ==============================================================================
# Auto-functions with advanced syntax.
# ==============================================================================
Expand Down
1 change: 0 additions & 1 deletion test/filetypes/cpp/function-declarations.vader
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Expect cpp (generated comment with @param and @return tags):
*
* @param text TODO
* @param node TODO
* @return TODO
*/
void Emitter::append_token(const std::string& text /* inline comment */, const AST_Node* node);

Expand Down
4 changes: 0 additions & 4 deletions test/filetypes/cpp/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ Do (trigger doge):
Expect cpp (generated comment with nothing but the text 'TODO' and @return tag):
/**
* TODO
*
* @return TODO
*/
void Emitter::append_token(/* inline comment */) // another comment
{
Expand All @@ -39,7 +37,6 @@ Expect cpp (generated comment with @param and @return tags):
*
* @param text TODO
* @param node TODO
* @return TODO
*/
void Emitter::append_token(const std::string& text /* inline comment */, const AST_Node* node) // another comment
{
Expand All @@ -63,7 +60,6 @@ Expect cpp (generated comment with @param and @return tags):
* TODO
*
* @param p TODO
* @return TODO
*/
void f(A* p = this)
{
Expand Down
1 change: 0 additions & 1 deletion test/filetypes/cpp/template-functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ Expect cpp (generated comment with @param and @return tags):
*
* @param i TODO
* @param args TODO
* @return TODO
*/
template<class...T> void h(int i = 0, T... args) {
//
Expand Down

0 comments on commit ec4fdd9

Please sign in to comment.