Skip to content

Commit

Permalink
fix: Ensure doc standards and patterns are not stacked when switching…
Browse files Browse the repository at this point in the history
… filetype
  • Loading branch information
kkoomen committed Jan 1, 2020
1 parent ec27953 commit 2a1937b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
24 changes: 24 additions & 0 deletions autoload/doge.vim
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,29 @@ function! doge#command_complete(...) abort
return filter(copy(get(b:, 'doge_supported_doc_standards', [])), "v:val =~ '^'.a:1")
endfunction

""
" @public
" This function will be triggered on the FileType autocmd and will remove
" conflicting doc standards from the previous filetype.
function! doge#on_filetype_change() abort
if !exists('b:doge_prev_supported_doc_standards') && exists('b:doge_supported_doc_standards')
" Save the current supported doc standards
let b:doge_prev_supported_doc_standards = copy(get(b:, 'doge_supported_doc_standards', []))
elseif exists('b:doge_prev_supported_doc_standards') && exists('b:doge_supported_doc_standards')
" Remove all the doc standards from the previous filetype.
for l:doc in get(b:, 'doge_prev_supported_doc_standards', [])
if has_key(get(b:, 'doge_patterns', {}), l:doc)
unlet b:doge_patterns[l:doc]
endif

let l:doc_idx = index(get(b:, 'doge_supported_doc_standards', []), l:doc)
if l:doc_idx >= 0
call remove(b:doge_supported_doc_standards, l:doc_idx)
endif
endfor
let b:doge_prev_supported_doc_standards = copy(b:doge_supported_doc_standards)
endif
endfunction

let &cpoptions = s:save_cpo
unlet s:save_cpo
4 changes: 4 additions & 0 deletions doc/doge.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ doge#deactivate() *doge#deactivate()*
doge#command_complete() *doge#command_complete()*
Return a list of supported doc standards for the current buffer.

doge#on_filetype_change() *doge#on_filetype_change()*
This function will be triggered on the FileType autocmd and will remove
conflicting doc standards from the previous filetype.

doge#buffer#get_supported_doc_standards({defaults})
*doge#buffer#get_supported_doc_standards()*
'defaults': A list of supported doc standards that should be allowed.
Expand Down
1 change: 1 addition & 0 deletions doc/tags
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ doge#helpers#keyseq() doge.txt /*doge#helpers#keyseq()*
doge#helpers#placeholder() doge.txt /*doge#helpers#placeholder()*
doge#helpers#trim() doge.txt /*doge#helpers#trim()*
doge#indent#add() doge.txt /*doge#indent#add()*
doge#on_filetype_change() doge.txt /*doge#on_filetype_change()*
doge#pattern#custom() doge.txt /*doge#pattern#custom()*
doge#pattern#generate() doge.txt /*doge#pattern#generate()*
doge#python#file() doge.txt /*doge#python#file()*
Expand Down
1 change: 1 addition & 0 deletions plugin/doge.vim
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ augroup doge
autocmd TextChangedI * call doge#comment#update_interactive_comment_info()
autocmd InsertLeave * call doge#comment#deactivate_when_done()
autocmd TextChanged * call doge#comment#deactivate_when_done()
autocmd FileType * call doge#on_filetype_change()
augroup END

let &cpoptions = s:save_cpo
Expand Down
26 changes: 26 additions & 0 deletions test/autocmd-filetype.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# ==============================================================================
# Switch from PHP to JavaScript and make sure doc standards of PHP get removed.
# ==============================================================================

Execute (switch from PHP to JavaScript):
setfiletype php
AssertEqual exists('b:doge_prev_supported_doc_standards'), 1
AssertEqual has_key(b:doge_patterns, 'phpdoc'), 1
AssertEqual has_key(b:doge_patterns, 'jsdoc'), 0

Assert index(b:doge_supported_doc_standards, 'phpdoc') >= 0
Assert index(b:doge_supported_doc_standards, 'jsdoc') < 0

Assert index(b:doge_prev_supported_doc_standards, 'phpdoc') >= 0
Assert index(b:doge_prev_supported_doc_standards, 'jsdoc') < 0

# Change to JavaScript and make sure the phpdoc gets removed.
setfiletype javascript
AssertEqual 0, has_key(b:doge_patterns, 'phpdoc')
AssertEqual 1, has_key(b:doge_patterns, 'jsdoc')

Assert index(b:doge_supported_doc_standards, 'phpdoc') < 0
Assert index(b:doge_supported_doc_standards, 'jsdoc') >= 0

Assert index(b:doge_prev_supported_doc_standards, 'phpdoc') < 0
Assert index(b:doge_prev_supported_doc_standards, 'jsdoc') >= 0

0 comments on commit 2a1937b

Please sign in to comment.