Make auto_tags preserve existing tag section structure

This commit is contained in:
Jiaming Chen
2023-07-03 19:44:54 -07:00
committed by Tinmarino
parent 65b9f0e68b
commit acc92fb648
2 changed files with 111 additions and 43 deletions
+8 -3
View File
@@ -360,22 +360,28 @@ function! vimwiki#tags#generate_tags(create, ...) abort
" make a dictionary { tag_name: [tag_links, ...] }
let tags_entries = {}
for tagname in self.specific_tags
let tags_entries[tagname] = []
endfor
for entries in values(metadata)
for entry in entries
if has_key(tags_entries, entry.tagname)
call add(tags_entries[entry.tagname], [entry.link, entry.description])
else
if need_all_tags
let tags_entries[entry.tagname] = [[entry.link, entry.description]]
endif
endif
endfor
unlet entry " needed for older vims with sticky type checking since name is reused
endfor
let tagnames = need_all_tags ? sort(keys(tags_entries)) : self.specific_tags
let lines = []
let bullet = repeat(' ', vimwiki#lst#get_list_margin()).vimwiki#lst#default_symbol().' '
let current_dir = vimwiki#base#current_subdir()
for tagname in sort(keys(tags_entries))
if need_all_tags || index(self.specific_tags, tagname) != -1
for tagname in tagnames
if len(lines) > 0
call add(lines, '')
endif
@@ -418,7 +424,6 @@ function! vimwiki#tags#generate_tags(create, ...) abort
call add(lines, bullet . substitute(link_tpl, '__LinkUrl__', taglink, ''))
endif
endfor
endif
endfor
return lines
+63
View File
@@ -0,0 +1,63 @@
# Test how vimwiki#tags#generate_tags behaves when updating existing tag link sections
Before (Setup test wiki files):
call writefile([":usedtag:", ":othertag:"], expand("~/testmarkdown/Test-Tag-tagged.md"))
edit ~/testmarkdown/Test-Tag-links.md
After (Cleanup files):
%delete
call system("rm $HOME/testmarkdown/.vimwiki_tags")
call system("rm $HOME/testmarkdown/Test-Tag-tagged.md")
call system("rm $HOME/testmarkdown/Test-Tag-links.md")
Do (Create preexisting tag links with unused tag):
I
# Generated Tags\<CR>
\<CR>
## unusedtag\<CR>
\<CR>
## usedtag\<CR>
\<ESC>
:VimwikiRebuildTags!\<CR>
:call vimwiki#tags#generate_tags(0)\<CR>
Expect (Keeps unused tag header):
# Generated Tags
## unusedtag
## usedtag
- [Test-Tag-tagged](Test-Tag-tagged)
Do (Create preexisting tag subheadings out of alphabetical order):
I
# Generated Tags\<CR>
\<CR>
## z\<CR>
\<CR>
## usedtag\<CR>
\<CR>
## a\<CR>
\<CR>
# Other Stuff
\<ESC>
:VimwikiRebuildTags!\<CR>
:call vimwiki#tags#generate_tags(0)\<CR>
Expect (Existing tag subheading order is preserved):
# Generated Tags
## z
## usedtag
- [Test-Tag-tagged](Test-Tag-tagged)
## a
# Other Stuff