From c0211c31d54e32139604a6151051ed0706f7cda1 Mon Sep 17 00:00:00 2001 From: yasuda Date: Wed, 28 Sep 2022 09:27:59 +0900 Subject: [PATCH 1/6] Drop localist modification for other quickfix list plugins --- ftplugin/markdown.vim | 8 -------- 1 file changed, 8 deletions(-) diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index 5e402269..c3834f79 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -405,14 +405,6 @@ function! s:Toc(...) else lopen endif - setlocal modifiable - for i in range(1, line('$')) - " this is the location-list data for the current item - let d = getloclist(0)[i-1] - call setline(i, d.text) - endfor - setlocal nomodified - setlocal nomodifiable execute 'normal! ' . l:cursor_header . 'G' endfunction From dcaddccfa1ca3abb66216a57f216425e82d24f8b Mon Sep 17 00:00:00 2001 From: yasuda Date: Wed, 28 Sep 2022 09:29:17 +0900 Subject: [PATCH 2/6] Stop removing heading hashes (`#`) to know heading level --- ftplugin/markdown.vim | 5 ----- 1 file changed, 5 deletions(-) diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index c3834f79..a0083d2e 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -192,11 +192,6 @@ function! s:GetHeaderList() let l:is_header = 0 endif if l:is_header ==# 1 && l:fenced_block ==# 0 && l:front_matter ==# 0 - " remove hashes from atx headers - if match(l:line, '^#') > -1 - let l:line = substitute(l:line, '\v^#*[ ]*', '', '') - let l:line = substitute(l:line, '\v[ ]*#*$', '', '') - endif " append line to list let l:level = s:GetHeaderLevel(i) let l:item = {'level': l:level, 'text': l:line, 'lnum': i, 'bufnr': bufnr} From acf364de468f58ed64dc3a35435423b7c4845e96 Mon Sep 17 00:00:00 2001 From: yasuda Date: Wed, 28 Sep 2022 11:58:54 +0900 Subject: [PATCH 3/6] Revert "Stop removing heading hashes (`#`) to know heading level" This reverts commit dcaddccfa1ca3abb66216a57f216425e82d24f8b. --- ftplugin/markdown.vim | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index a0083d2e..c3834f79 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -192,6 +192,11 @@ function! s:GetHeaderList() let l:is_header = 0 endif if l:is_header ==# 1 && l:fenced_block ==# 0 && l:front_matter ==# 0 + " remove hashes from atx headers + if match(l:line, '^#') > -1 + let l:line = substitute(l:line, '\v^#*[ ]*', '', '') + let l:line = substitute(l:line, '\v[ ]*#*$', '', '') + endif " append line to list let l:level = s:GetHeaderLevel(i) let l:item = {'level': l:level, 'text': l:line, 'lnum': i, 'bufnr': bufnr} From c133ce1e9e3821e8d163e227bd6520a4a17eddbf Mon Sep 17 00:00:00 2001 From: yasuda Date: Wed, 28 Sep 2022 11:59:38 +0900 Subject: [PATCH 4/6] Fix `:Toc` to show heading level as preceding `#` --- ftplugin/markdown.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index c3834f79..a1a68f20 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -377,7 +377,7 @@ function! s:Toc(...) endif endif " indent header based on level - let l:text = repeat(' ', h.level-1) . h.text + let l:text = repeat('#', h.level) . ' ' . h.text " keep track of the longest header size (heading level + title) let l:total_len = strdisplaywidth(l:text) if l:total_len > l:header_max_len From d4c72e420a95fe46ba05e17e798cbccd798c63cd Mon Sep 17 00:00:00 2001 From: yasuda Date: Wed, 28 Sep 2022 09:43:51 +0900 Subject: [PATCH 5/6] Fix TOC test --- test/folding-toc.vader | 18 +++++------ test/toc-autofit.vader | 6 ++-- test/toc.vader | 73 ++++++++++++++++++++++++------------------ 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/test/folding-toc.vader b/test/folding-toc.vader index 8d688f77..348f14ef 100644 --- a/test/folding-toc.vader +++ b/test/folding-toc.vader @@ -121,28 +121,28 @@ Execute (check TOC): let res = getloclist(0) let elem = res[0] AssertEqual elem.lnum, 1 - AssertEqual elem.text, 'chap 1' + AssertEqual elem.text, '# chap 1' let elem = res[1] AssertEqual elem.lnum, 15 - AssertEqual elem.text, ' chap 1.1' + AssertEqual elem.text, '## chap 1.1' let elem = res[2] AssertEqual elem.lnum, 25 - AssertEqual elem.text, ' chap 1.1.1' + AssertEqual elem.text, '### chap 1.1.1' let elem = res[3] AssertEqual elem.lnum, 30 - AssertEqual elem.text, 'chap 2' + AssertEqual elem.text, '# chap 2' let elem = res[4] AssertEqual elem.lnum, 34 - AssertEqual elem.text, ' chap 2.1' + AssertEqual elem.text, '## chap 2.1' let elem = res[5] AssertEqual elem.lnum, 41 - AssertEqual elem.text, 'chap 3' + AssertEqual elem.text, '# chap 3' let elem = res[6] AssertEqual elem.lnum, 45 - AssertEqual elem.text, 'chap 4' + AssertEqual elem.text, '# chap 4' let elem = res[7] AssertEqual elem.lnum, 50 - AssertEqual elem.text, ' chap 4.1' + AssertEqual elem.text, '## chap 4.1' Given markdown; --- @@ -175,5 +175,5 @@ Execute (check Toc of yaml front matter): AssertEqual len(res), 1 let elem = res[0] AssertEqual elem.lnum, 8 - AssertEqual elem.text, ' heading' + AssertEqual elem.text, '## heading' unlet g:vim_markdown_frontmatter diff --git a/test/toc-autofit.vader b/test/toc-autofit.vader index 26789975..79e43e82 100644 --- a/test/toc-autofit.vader +++ b/test/toc-autofit.vader @@ -45,9 +45,9 @@ Execute (toc window autofit width): :Toc let real_width = winwidth(0) :lclose - let expected_width = len(line) + 2*5 + 1 + 3 - 7 + let expected_width = len(line) + 1 + 3 AssertEqual real_width, expected_width set nonumber -" 2 spaces * 5 additional header levels + 1 space for first header + -" 3 spaces for line numbers - 7 chars ('###### ') that don't show up on the TOC +" 1 space for first header + 3 spaces for line numbers. +" 7 chars ('###### ') are removed, but added to `:Toc` again. diff --git a/test/toc.vader b/test/toc.vader index 51269526..efb32b73 100644 --- a/test/toc.vader +++ b/test/toc.vader @@ -28,11 +28,14 @@ test Execute (Toc setex headers): :Toc + :setl modifiable + :%s/\m^\([^|]*|\)\{2} // + :setl nomodifiable nomodified Expect (setex headers): - header 1 - header 2 - header 3 + # header 1 + ## header 2 + ### header 3 Given markdown; # header 1 @@ -49,11 +52,14 @@ test Execute (Toc atx headers): :Toc + :setl modifiable + :%s/\m^\([^|]*|\)\{2} // + :setl nomodifiable nomodified Expect (atx headers): - header 1 - header 2 - header 3 + # header 1 + ## header 2 + ### header 3 Given markdown; ATX tests. @@ -135,34 +141,37 @@ setex h2 Execute (Toc multiple headers): :Toc + :setl modifiable + :%s/\m^\([^|]*|\)\{2} // + :setl nomodifiable nomodified Expect (multiple headers): - h1 space - h1 nospace - h1 2 spaces - h1 trailing hash - h2 space - h2 nospace - h2 trailing hash - h3 space - h3 nospace - h3 trailing hash - h4 - h5 - h6 - h1 before h2 - h2 between h1s - h1 after h2 - setex h1 - setex h2 - setex h1 single punctuation - setex h1 punctuation longer than header - setex h1 before atx - atx h2 - atx h3 - atx h1 - setex h2 - atx h3 2 + # h1 space + # h1 nospace + # h1 2 spaces + # h1 trailing hash + ## h2 space + ## h2 nospace + ## h2 trailing hash + ### h3 space + ### h3 nospace + ### h3 trailing hash + #### h4 + ##### h5 + ###### h6 + # h1 before h2 + ## h2 between h1s + # h1 after h2 + # setex h1 + ## setex h2 + # setex h1 single punctuation + # setex h1 punctuation longer than header + # setex h1 before atx + ## atx h2 + ### atx h3 + # atx h1 + ## setex h2 + ### atx h3 2 Execute: :lclose From f04d8c36ec3a2435fe8c01254288ac07bcec185d Mon Sep 17 00:00:00 2001 From: yasuda Date: Thu, 27 Jul 2023 17:37:07 +0900 Subject: [PATCH 6/6] Change `:Toc` heading level `N#` --- ftplugin/markdown.vim | 2 +- test/folding-toc.vader | 18 ++++++------ test/toc-autofit.vader | 4 +-- test/toc.vader | 64 +++++++++++++++++++++--------------------- 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index a1a68f20..1695a56f 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -377,7 +377,7 @@ function! s:Toc(...) endif endif " indent header based on level - let l:text = repeat('#', h.level) . ' ' . h.text + let l:text = h.level . '# ' . h.text " keep track of the longest header size (heading level + title) let l:total_len = strdisplaywidth(l:text) if l:total_len > l:header_max_len diff --git a/test/folding-toc.vader b/test/folding-toc.vader index 348f14ef..026e0056 100644 --- a/test/folding-toc.vader +++ b/test/folding-toc.vader @@ -121,28 +121,28 @@ Execute (check TOC): let res = getloclist(0) let elem = res[0] AssertEqual elem.lnum, 1 - AssertEqual elem.text, '# chap 1' + AssertEqual elem.text, '1# chap 1' let elem = res[1] AssertEqual elem.lnum, 15 - AssertEqual elem.text, '## chap 1.1' + AssertEqual elem.text, '2# chap 1.1' let elem = res[2] AssertEqual elem.lnum, 25 - AssertEqual elem.text, '### chap 1.1.1' + AssertEqual elem.text, '3# chap 1.1.1' let elem = res[3] AssertEqual elem.lnum, 30 - AssertEqual elem.text, '# chap 2' + AssertEqual elem.text, '1# chap 2' let elem = res[4] AssertEqual elem.lnum, 34 - AssertEqual elem.text, '## chap 2.1' + AssertEqual elem.text, '2# chap 2.1' let elem = res[5] AssertEqual elem.lnum, 41 - AssertEqual elem.text, '# chap 3' + AssertEqual elem.text, '1# chap 3' let elem = res[6] AssertEqual elem.lnum, 45 - AssertEqual elem.text, '# chap 4' + AssertEqual elem.text, '1# chap 4' let elem = res[7] AssertEqual elem.lnum, 50 - AssertEqual elem.text, '## chap 4.1' + AssertEqual elem.text, '2# chap 4.1' Given markdown; --- @@ -175,5 +175,5 @@ Execute (check Toc of yaml front matter): AssertEqual len(res), 1 let elem = res[0] AssertEqual elem.lnum, 8 - AssertEqual elem.text, '## heading' + AssertEqual elem.text, '2# heading' unlet g:vim_markdown_frontmatter diff --git a/test/toc-autofit.vader b/test/toc-autofit.vader index 79e43e82..29fc433a 100644 --- a/test/toc-autofit.vader +++ b/test/toc-autofit.vader @@ -45,9 +45,9 @@ Execute (toc window autofit width): :Toc let real_width = winwidth(0) :lclose - let expected_width = len(line) + 1 + 3 + let expected_width = len(line) - len(matchstr(line, '^#\+ *')) + 1 + 3 + len('N# ') AssertEqual real_width, expected_width set nonumber " 1 space for first header + 3 spaces for line numbers. -" 7 chars ('###### ') are removed, but added to `:Toc` again. +" 7 chars ('###### ') are removed, and 3 char `N# ` are added. diff --git a/test/toc.vader b/test/toc.vader index efb32b73..0797dcba 100644 --- a/test/toc.vader +++ b/test/toc.vader @@ -33,9 +33,9 @@ Execute (Toc setex headers): :setl nomodifiable nomodified Expect (setex headers): - # header 1 - ## header 2 - ### header 3 + 1# header 1 + 2# header 2 + 3# header 3 Given markdown; # header 1 @@ -57,9 +57,9 @@ Execute (Toc atx headers): :setl nomodifiable nomodified Expect (atx headers): - # header 1 - ## header 2 - ### header 3 + 1# header 1 + 2# header 2 + 3# header 3 Given markdown; ATX tests. @@ -146,32 +146,32 @@ Execute (Toc multiple headers): :setl nomodifiable nomodified Expect (multiple headers): - # h1 space - # h1 nospace - # h1 2 spaces - # h1 trailing hash - ## h2 space - ## h2 nospace - ## h2 trailing hash - ### h3 space - ### h3 nospace - ### h3 trailing hash - #### h4 - ##### h5 - ###### h6 - # h1 before h2 - ## h2 between h1s - # h1 after h2 - # setex h1 - ## setex h2 - # setex h1 single punctuation - # setex h1 punctuation longer than header - # setex h1 before atx - ## atx h2 - ### atx h3 - # atx h1 - ## setex h2 - ### atx h3 2 + 1# h1 space + 1# h1 nospace + 1# h1 2 spaces + 1# h1 trailing hash + 2# h2 space + 2# h2 nospace + 2# h2 trailing hash + 3# h3 space + 3# h3 nospace + 3# h3 trailing hash + 4# h4 + 5# h5 + 6# h6 + 1# h1 before h2 + 2# h2 between h1s + 1# h1 after h2 + 1# setex h1 + 2# setex h2 + 1# setex h1 single punctuation + 1# setex h1 punctuation longer than header + 1# setex h1 before atx + 2# atx h2 + 3# atx h3 + 1# atx h1 + 2# setex h2 + 3# atx h3 2 Execute: :lclose