# Tag generation and navigation
# Note: The Generate must be in Execute

Execute (Setup):
  set sw=4
  AssertEqual 4, &sw
  call system("rm $HOME/testmarkdown/.vimwiki_tags")


######################################################################
Do (Create File Content with default tags {{{1):
  :edit $HOME/testmarkdown/Test-Tag-default.md\<CR>
  I
  :tag1:tag2:\<CR>
  \<Esc>
  :write\<CR>
  :VimwikiRebuildTags!\<CR>
  :VimwikiGenerateTagLinks\<CR>
  gg


Execute (Generate tags for default):
  edit $HOME/testmarkdown/Test-Tag-default.md
  AssertEqual 'VimwikiTag', SyntaxAt(1, 2)
  1
  VimwikiGenerateTagLinks
  write


Expect (Tags default):
  :tag1:tag2:


  # Generated Tags

  ## tag1

  - [Test-Tag-default](Test-Tag-default)

  ## tag2

  - [Test-Tag-default](Test-Tag-default)


Execute (Clean tag file for default):
  call system("rm $HOME/testmarkdown/.vimwiki_tags")


Do (Create File Content with default tags):
  :edit $HOME/testmarkdown/Test-Tag-default.md\<CR>
  ggdG
  I
  # Generated Tags\<Cr>
  \<Cr>
  # Heading with my tags\<Cr>
  \<Cr>
  :tag1:tag2:\<CR>
  \<Cr>
  Dummy content 1
  \<Cr>
  # Another heading\<Cr>
  \<Cr>
  Dummy content 2
  \<Esc>
  :write\<CR>
  :VimwikiRebuildTags!\<CR>
  :VimwikiGenerateTagLinks\<CR>
  gg


Expect (Tags default with Generated Tags heading):
  # Generated Tags

  ## tag1

  - [Heading with my tags](Test-Tag-default#heading-with-my-tags)

  ## tag2

  - [Heading with my tags](Test-Tag-default#heading-with-my-tags)

  # Heading with my tags

  :tag1:tag2:

  Dummy content 1
  # Another heading

  Dummy content 2


Execute (Clean file for tag default):
  call system("rm $HOME/testmarkdown/.vimwiki_tags")
  call system("rm $HOME/testmarkdown/Test-Tag-default.md")



######################################################################
Execute (Change delimiter <tag1|tag2> {{{1):
  let g:vimwiki_tag_format = {'pre_mark': '<', 'post_mark': '>', 'sep': '|'}
  unlet g:vimwiki_syntaxlocal_vars
  call vimwiki#vars#init()
  edit $HOME/testmarkdown/Test-Tag.md
  AssertEqual $HOME . '/testmarkdown/Test-Tag.md', expand('%')
  AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax')
  AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr')


Do (Create File Content with <>):
  :edit $HOME/testmarkdown/Test-Tag.md\<CR>
  I
  <tag-bar-1>\<CR>
  \<CR>
  # A header\<CR>
  \<CR>
  <tag-bar-2|tag-bar-3>\<CR>
  \<CR>
  # Another header\<CR>
  \<CR>
  Words here.
  \<Esc>
  :write\<CR>
  :VimwikiRebuildTags!\<CR>
  gg


Execute (Generate tags):
  edit $HOME/testmarkdown/Test-Tag.md
  AssertEqual 'VimwikiTag', SyntaxAt(1, 1)
  VimwikiGenerateTagLinks
  set tw=200


Expect (Correctly generated tags section {{{3):
  <tag-bar-1>

  # A header

  <tag-bar-2|tag-bar-3>

  # Another header

  Words here.

  # Generated Tags

  ## tag-bar-1

  - [Test-Tag](Test-Tag)

  ## tag-bar-2

  - [A header](Test-Tag#a-header)

  ## tag-bar-3

  - [A header](Test-Tag#a-header)


Do (Write a quick tag for a quick jump):
  :edit $HOME/testmarkdown/Test-Tag.md\<CR>
  ggdG
  I
  [go1](Test-Tag#tag-bar-1)\<Cr>
  [go2](#tag-bar-1)\<Cr>
  bla\<Cr>
  <tag-bar-1>\<Esc>
  ggl\<Cr>A __HERE1__\<Esc>
  ggjl\<Cr>A __HERE2__\<Esc>


Expect (Good jump {{{3):
  [go1](Test-Tag#tag-bar-1)
  [go2](#tag-bar-1)
  bla
  <tag-bar-1> __HERE1__ __HERE2__


Execute (Clean Test-Tag and .vimwiki_tags -2):
  let g:vimwiki_tag_format = {}
  unlet g:vimwiki_syntaxlocal_vars
  call vimwiki#vars#init()
  call system("rm $HOME/testmarkdown/Test.md")
  call system("rm $HOME/testmarkdown/.vimwiki_tags")
  call system("rm $HOME/testmarkdown/Test-Tag.md")
  call DeleteHiddenBuffers()


######################################################################
Execute (Default tag generation {{{1):
  edit $HOME/testmarkdown/Test-Tag.md
  AssertEqual $HOME . '/testmarkdown/Test-Tag.md', expand('%')
  AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax')
  AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr')
  set tw=200


Do (Single file Part1):
  :edit $HOME/testmarkdown/Test-Tag.md\<Cr>
  ggdGO
  :single-tag:\<Esc>
  :write\<Cr>
  :VimwikiRebuildTags!\<Cr>


Execute (Generate tags):
  edit $HOME/testmarkdown/Test-Tag.md
  AssertEqual 'VimwikiTag', SyntaxAt(1, 1)
  VimwikiGenerateTagLinks
  write


Expect (Single tags toc):
  :single-tag:


  # Generated Tags

  ## single-tag

  - [Test-Tag](Test-Tag)


Do (Create File Content):
  :edit $HOME/testmarkdown/Test-Tag.md\<CR>
  ggdGO
  :top-tag:\<CR>
  \<CR>
  # A header\<CR>
  \<CR>
  :test-tag:\<CR>
  \<CR>
  # Another header\<CR>
  \<CR>
  Words here.\<CR>
  If tag isn't within 2 lines of header then it has a direct link instead of\<CR>
  a link to the header.\<CR>
  \<CR>
  :second-tag:
  \<Esc>
  :write\<CR>
  :VimwikiRebuildTags\<CR>


Execute (Edit tags file):
  edit $HOME/testmarkdown/.vimwiki_tags
  AssertEqual $HOME . '/testmarkdown/.vimwiki_tags', expand('%')
  AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax')
  AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr')


# Note: tags file uses tabs
Expect (Correctly formatted tags file):
  !_TAG_FILE_FORMAT	2
  !_TAG_FILE_SORTED	1
  !_TAG_OUTPUT_MODE	vimwiki-tags
  !_TAG_PROGRAM_AUTHOR	Vimwiki
  !_TAG_PROGRAM_NAME	Vimwiki Tags
  !_TAG_PROGRAM_URL	https://github.com/vimwiki/vimwiki
  !_TAG_PROGRAM_VERSION	2023.05.12
  second-tag	Test-Tag.md	13;"	vimwiki:Test-Tag\tTest-Tag#second-tag\tTest-Tag#second-tag
  test-tag	Test-Tag.md	5;"	vimwiki:Test-Tag\tTest-Tag#a-header\tA header
  top-tag	Test-Tag.md	1;"	vimwiki:Test-Tag\tTest-Tag\tTest-Tag


Execute (Generate tags):
  edit $HOME/testmarkdown/Test-Tag.md
  VimwikiGenerateTagLinks


Expect (Correctly generated tags section):
  :top-tag:

  # A header

  :test-tag:

  # Another header

  Words here.
  If tag isn't within 2 lines of header then it has a direct link instead of
  a link to the header.

  :second-tag:


  # Generated Tags

  ## second-tag

  - [second-tag](Test-Tag#second-tag)

  ## test-tag

  - [A header](Test-Tag#a-header)

  ## top-tag

  - [Test-Tag](Test-Tag)


Execute (Clean Test-Tag and .vimwiki_tags -1 ):
  call system("rm $HOME/testmarkdown/Test.md")
  call system("rm $HOME/testmarkdown/.vimwiki_tags")
  call system("rm $HOME/testmarkdown/Test-Tag.md")
  call DeleteHiddenBuffers()

# vim: sw=2:foldlevel=30:foldmethod=marker:

######################################################################
Execute (Check first tags file):
  call system("mkdir -p $HOME/testmarkdown/subdir1/subdir11")
  edit $HOME/testmarkdown/Test-Tag-1.md
  AssertEqual $HOME . '/testmarkdown/Test-Tag-1.md', expand('%')
  AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax')
  AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr')


Do (Build first tags file):
  :edit $HOME/testmarkdown/Test-Tag-1.md\<Cr>
  ggI
  # A Header\<Cr>
  :header-tag-1:\<Cr>
  \<Cr>
  # Another Header\<Cr>
  :header-tag-2:\<Cr>
  \<Cr>
  :standalone-tag-1:
  \<Esc>
  :write\<Cr>
  :VimwikiRebuildTags!\<CR>


Execute (Check second tags file):
  edit $HOME/testmarkdown/subdir1/Test-Tag-2.md
  AssertEqual $HOME . '/testmarkdown/subdir1/Test-Tag-2.md', expand('%')
  AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax')
  AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr')


Do (Build second tags file):
  :edit $HOME/testmarkdown/subdir1/Test-Tag-2.md\<Cr>
  ggI
  # A Header\<Cr>
  :header-tag-1:\<Cr>
  \<Cr>
  # Another Header\<Cr>
  :header-tag-2:\<Cr>
  \<Cr>
  :standalone-tag-1:
  \<Esc>
  :write\<Cr>
  :VimwikiRebuildTags!\<CR>


Execute (Build tag links in third file):
  edit $HOME/testmarkdown/subdir1/subdir11/Test-Tag-Links.md
  AssertEqual $HOME . '/testmarkdown/subdir1/subdir11/Test-Tag-Links.md', expand('%')
  AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax')
  AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr')
  VimwikiGenerateTagLinks
  write


Expect (Tag links relative to current file):
  

  # Generated Tags

  ## header-tag-1
  
  - [A Header](../../Test-Tag-1#a-header)
  - [A Header](../Test-Tag-2#a-header)

  ## header-tag-2

  - [Another Header](../../Test-Tag-1#another-header)
  - [Another Header](../Test-Tag-2#another-header)

  ## standalone-tag-1

  - [standalone-tag-1](../../Test-Tag-1#standalone-tag-1)
  - [standalone-tag-1](../Test-Tag-2#standalone-tag-1)


Do (Delete some existing links to test updating generated tag links):
  :edit $HOME/testmarkdown/subdir1/subdir11/Test-Tag-Links.md\<Cr>
  7G
  dd
  12G
  6dd
  :write\<Cr>
  :call vimwiki#tags#generate_tags(0)\<Cr>


Expect (Only update generated tag links for tags already existing in the file):
  

  # Generated Tags

  ## header-tag-1
  
  - [A Header](../../Test-Tag-1#a-header)
  - [A Header](../Test-Tag-2#a-header)

  ## header-tag-2

  - [Another Header](../../Test-Tag-1#another-header)
  - [Another Header](../Test-Tag-2#another-header)


Execute (Clean relative tag setup):
  call system("rm $HOME/testmarkdown/.vimwiki_tags")
  call system("rm -rf $HOME/testmarkdown/subdir1")
  call system("rm $HOME/testmarkdown/Test-Tag-1.md")


######################################################################
Execute (Change delimiter #tag1content1|content2#tag2 as for issue #1316 {{{1):
  let g:vimwiki_tag_format = {'pre_mark': '#tag1', 'post_mark': '#tag2', 'sep': '|'}
  unlet g:vimwiki_syntaxlocal_vars
  call vimwiki#vars#init()
  edit $HOME/testmarkdown/Test-Tag-1316.md
  AssertEqual $HOME . '/testmarkdown/Test-Tag-1316.md', expand('%')
  AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax')
  AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr')


Do (Create File Content with #tag1 #tag2 as for issue #1316):
  :edit $HOME/testmarkdown/Test-Tag-issue-1316.md\<CR>
  I
# Line1: Heading
  # A header\<CR>
# Line2: Tag not in header
  bla #tag1content01|content02#tag2\<CR>
# Line3: Tag in header
  #tag1content11|content12#tag2\<CR>
# Line4: empty
  \<CR>
# Line5: dummy content
  Words here.
  \<Esc>
  :write\<CR>
  :VimwikiRebuildTags!\<CR>
  gg


Execute (Generate tags for issue #1316):
  edit $HOME/testmarkdown/Test-Tag-issue-1316.md
  AssertEqual 'VimwikiTag', SyntaxAt(2, 5)
  1
  VimwikiGenerateTagLinks
  write


Expect (Single tags toc):
  # A header
  bla #tag1content01|content02#tag2
  #tag1content11|content12#tag2

  Words here.

  # Generated Tags

  ## content01

  - [A header](Test-Tag-issue-1316#a-header)

  ## content02

  - [A header](Test-Tag-issue-1316#a-header)

  ## content11

  - [tag2](Test-Tag-issue-1316#tag1content11content12tag2)

  ## content12

  - [tag2](Test-Tag-issue-1316#tag1content11content12tag2)


Execute (Clean file for tag #1316):
  call system("rm $HOME/testmarkdown/.vimwiki_tags")
  call system("rm $HOME/testmarkdown/Test-Tag-issue-1316.md")


Execute (Change delimiter #first_tag #vim #mo as for issue #1316 (2) {{{1):
  let g:vimwiki_tag_format = {'pre_mark': '#\w\@=', 'post_mark': '', 'sep': '[#[:space:]]\+', 'in': '[^#:[:space:]]\+' }
  unlet g:vimwiki_syntaxlocal_vars
  call vimwiki#vars#init()
  edit $HOME/testmarkdown/Test-Tag-issue-1316-bis.md
  AssertEqual $HOME . '/testmarkdown/Test-Tag-issue-1316-bis.md', expand('%')
  AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax')
  AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr')


Do (Create File Content with #tag1 #tag2 as for issue #1316):
  :edit $HOME/testmarkdown/Test-Tag-issue-1316-bis.md\<CR>
  I
# Line1: Heading
  # A header\<CR>
# Line2: Tag not in header
  bla #first_tag\<Cr>
# Line3: Tag in header
  #vim #mono\<Cr>
# Line4: empty
  \<CR>
# Line5: dummy content
  Words here.
  \<Esc>
  :write\<CR>
  :VimwikiRebuildTags!\<CR>
  gg


Execute (Assert some file info for #1316):
  edit $HOME/testmarkdown/Test-Tag-issue-1316-bis.md
  AssertEqual $HOME . '/testmarkdown/Test-Tag-issue-1316-bis.md', expand('%')
  AssertEqual 'markdown', vimwiki#vars#get_wikilocal('syntax')
  AssertEqual 1, vimwiki#vars#get_bufferlocal('wiki_nr')
  AssertEqual 'VimwikiTag', SyntaxAt(2, 6)


Do (Append something at first_tag):
  :edit $HOME/testmarkdown/Test-Tag-issue-1316-bis.md\<CR>
# Some bug with loclist for vim7.3 and vim8.0
  :if v:version > 810\<Cr>
    VimwikiSearchTags first_tag\<Cr>
  else\<Cr>
    2\<Cr>
  endif\<Cr>
  A Content1\<Esc>


Do (Append something at mono tag):
  :edit $HOME/testmarkdown/Test-Tag-issue-1316-bis.md\<CR>
  :if v:version > 810\<Cr>
    VimwikiSearchTags mono\<Cr>
  else\<Cr>
    3\<Cr>
  endif\<Cr>
  A Content2\<Esc>


Expect (Content appended at tags):
  # A header
  bla #first_tag Content1
  #vim #mono Content2

  Words here.


Execute (Clean file for tag #1316):
  call system("rm $HOME/testmarkdown/.vimwiki_tags")
  call system("rm $HOME/testmarkdown/Test-Tag-issue-1316-bis.md")


Execute (Clean):
  call system("rm $HOME/testmarkdown/.vimwiki_tags")
  unlet g:vimwiki_tag_format
  unlet g:vimwiki_syntaxlocal_vars
  call vimwiki#vars#init()


Do (Md: #1034: Create File with tag in code for issue #1034):
  :edit $HOME/testmarkdown/Test-Tag-issue-1034.md\<CR>
  I
# Line1: Heading
  Some line\<Cr>
# Line2: Tag not in header
  ```bash\<Cr>
# Line3: Tag in header
  this is bash code with :tag1:\<Cr>
# Line4: empty
  ```\<Cr>
# Line5: tag2
  and :tag2:\<Cr>
  \<Esc>
  :write\<CR>
  :VimwikiRebuildTags!\<CR>


Do (Md: #1034: Append something at mono tag):
  :edit $HOME/testmarkdown/Test-Tag-issue-1034.md\<CR>
  ggdG
  :read $HOME/testmarkdown/.vimwiki_tags\<CR>
  G
  :g/^!/d\<CR>
  oContent\<Esc>


Expect (Md: #1034: Can see tag2 but not tag1 as in code):
  tag2	Test-Tag-issue-1034.md	5;"	vimwiki:Test-Tag-issue-1034\tTest-Tag-issue-1034#tag2\tTest-Tag-issue-1034#tag2
  Content


Do (Wiki: #1034: Create File with tag in code for issue #1034):
  :edit $HOME/testwiki/Test-Tag-issue-1034.wiki\<CR>
  I
# Line1: Heading
  Some line\<Cr>
# Line2: Tag not in header
  {{{bash\<Cr>
# Line3: Tag in header
  this is bash code with :tag1:\<Cr>
# Line4: empty
  }}}\<Cr>
# Line5: tag2
  and :tag2:\<Cr>
  \<Esc>
  :write\<CR>
  :VimwikiRebuildTags!\<CR>


Do (Wiki: #1034: Append something at mono tag):
  :edit $HOME/testwiki/Test-Tag-issue-1034.wiki\<CR>
  ggdG
  :read $HOME/testwiki/.vimwiki_tags\<CR>
  G
  :g/^!/d\<CR>
  oContent\<Esc>


Expect (Wiki: #1034: Can see tag2 but not tag1 as in code):
  tag2	Test-Tag-issue-1034.wiki	5;"	vimwiki:Test-Tag-issue-1034\tTest-Tag-issue-1034#tag2\tTest-Tag-issue-1034#tag2
  Content


Execute (#1034: Clean file for tag #1034):
  call system("rm $HOME/testmarkdown/.vimwiki_tags")
  call system("rm $HOME/testwiki/.vimwiki_tags")
  call system("rm $HOME/testmarkdown/Test-Tag-issue-1013.md")
  call system("rm $HOME/testwiki/Test-Tag-issue-1013.wiki")
