From 46add6c3017d3e4035dc10ffa9cb54221d8dfe1a Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 1 Nov 2023 21:22:00 +0100 Subject: [PATCH] feat: Add (optin) support for formatting tables without borders (#643) --- README.md | 21 +++++++++++++++++++++ doc/vim-markdown.txt | 24 ++++++++++++++++++++++++ ftplugin/markdown.vim | 15 ++++++++++++++- test/table-format.vader | 15 +++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 08abbfde..250d3e5a 100644 --- a/README.md +++ b/README.md @@ -349,6 +349,27 @@ The following options control which syntax extensions will be turned on. They ar let g:vim_markdown_edit_url_in = 'tab' +### Borderless tables + +- `g:vim_markdown_borderless_table` + + Add support for borderless tables, such as: + ``` + header 1|header 2 + --|-- + data 1|data 2 + ``` + if set to `1`: + + let g:vim_markdown_borderless_table = 1 + + the table would be formatted as usual: + ``` + | header 1 | header 2 | + |----------|----------| + | data 1 | data 2 | + ``` + ## Mappings The following work on normal and visual modes: diff --git a/doc/vim-markdown.txt b/doc/vim-markdown.txt index 438a23dd..4aa8270c 100644 --- a/doc/vim-markdown.txt +++ b/doc/vim-markdown.txt @@ -490,6 +490,30 @@ Change how to open new files ~ > let g:vim_markdown_edit_url_in = 'tab' < +------------------------------------------------------------------------------- + *vim-markdown-support-borderless-tables* +Support borderless tables ~ + + *g:vim_markdown_borderless_table* +- 'g:vim_markdown_borderless_table' + + Add support for borderless tables, such as: +> + header 1|header 2 + --|-- + data 1|data 2 +< + if set to 1: +> + let g:vim_markdown_borderless_table = 1 +< + the table would be formatted as usual: +> + | header 1 | header 2 | + |----------|----------| + | data 1 | data 2 | +< + =============================================================================== *vim-markdown-mappings* Mappings ~ diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index 3a4c1ad6..de239db4 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -538,6 +538,19 @@ endfunction " function! s:TableFormat() let l:pos = getpos('.') + + if get(g:, 'vim_markdown_borderless_table', 0) + " add `|` to the beginning of the line if it isn't present + normal! { + call search('|') + execute 'silent .,''}s/\v^(\s{0,})\|?([^\|])/\1|\2/e' + + " add `|` to the end of the line if it isn't present + normal! { + call search('|') + execute 'silent .,''}s/\v([^\|])\|?(\s{0,})$/\1|\2/e' + endif + normal! { " Search instead of `normal! j` because of the table at beginning of file edge case. call search('|') @@ -765,7 +778,7 @@ endif command! -buffer -range=% HeaderDecrease call s:HeaderDecrease(, ) command! -buffer -range=% HeaderIncrease call s:HeaderDecrease(, , 1) command! -buffer -range=% SetexToAtx call s:SetexToAtx(, ) -command! -buffer TableFormat call s:TableFormat() +command! -buffer -range TableFormat call s:TableFormat() command! -buffer Toc call s:Toc() command! -buffer Toch call s:Toc('horizontal') command! -buffer Tocv call s:Toc('vertical') diff --git a/test/table-format.vader b/test/table-format.vader index 82ce3f80..f7028cb0 100644 --- a/test/table-format.vader +++ b/test/table-format.vader @@ -68,3 +68,18 @@ Expect (preserve colons to align text): | left | right | center | | |:-----|------:|:------:|:--| | left | right | center | | + +Given markdown (borderless table); + left |right| center + :- | --: |:---: + left |right| center + +Execute (format borderless table): + let g:vim_markdown_borderless_table = 1 + TableFormat + unlet g:vim_markdown_borderless_table + +Expect (table with borders): + | left | right | center | + |:-----|------:|:------:| + | left | right | center |