dhruvasagar / vim-table-mode

VIM Table Mode for instant table creation.
2.11k stars 96 forks source link

Tableize pattern for non CSV #213

Closed slarrain closed 2 years ago

slarrain commented 2 years ago

So, I have an interesting use case for VTM.

I have a bunch of TAGS on different files/notes. I can generate a list of Tags with the :VimwikiGenerateTagLinks with the following format.

# Generated Tags

## Test0

- [Kredito](Kredito#Kredito)
- [Legalbot](Legalbot#Legalbot)

## TestA

- [TestA1](TestA1#TestA1)

## TestB

- [TestA2](TestA2#TestA2)

Now what I would like to be able to do, is to create a Table with that format. So that I can effectively create tables from tags. I was thinking of the Tableize command, because it can have an {pattern} argument. Is this possible? What would be the pattern in this case? Or there is no pattern that would allow to convert that into tables?

dhruvasagar commented 2 years ago

@slarrain Can you share how that table should finally look like ?

dhruvasagar commented 2 years ago

@slarrain The {pattern} is meant to match the delimiter that separates each column value.

slarrain commented 2 years ago

Thank's for the quick reply.

I was thinking of being able to auto generate from the snippet above something like the following:


| Lead                 | TestA            | TestB            |
|----------------------|------------------|------------------|
| [Kredito](Kredito)   | [TestA1](TestA1) | [TestA2](TestA2) |
| [Legalbot](Legalbot) |                  |                  |

What would be the use case? This functionality would allow you to use a sort of Kanban Board like table. To the task (or Lead or whatever) you put a tag (:tag:) or update one. Then :VimwikirebuildTags and then :VimwikiGenerateTagLinks will create the snippet from the issue. And then being able to create or update the table with the new tags will make it just like a Kanban Board. And I think is just a pattern thing, but I'm not aware of the insides of the package.

dhruvasagar commented 2 years ago

@slarrain This can't be done natively by Tabelize or it's variants because, like I said, {pattern} currently is a regex that defines delimiters for each column value (like for csv that is a ,).

However, i'll try to share a code snippet using Table Mode APIs that can get this desired behavior.

dhruvasagar commented 2 years ago

Following is a snippet that should work :

function! functions#TableizeMarkdown() range
  let line = a:firstline
  while line < a:lastline
    " First subheading
    if getline(line) =~# '^##'
      break
    endif
    let line += 1
  endwhile

  let lines = getline(line, a:lastline)
  let columns = []
  let column = []
  for line in lines
    if empty(line) | continue | endif

    if !empty(column) && line =~# '^##' " Column Header
      call add(columns, column)
      let column = []
    endif

    call add(column, line)
  endfor
  call add(columns, column) " last column

  let rows = []
  for icol in range(len(columns))
    let row = []
    for irow in range(len(columns))
      if irow < len(columns) && icol < len(columns[irow])
        let val = substitute(columns[irow][icol], '^##\|^- ', '', '')
        call add(row, val)
      else
        call add(row, '')
      endif
    endfor
    call add(rows, row)
  endfor

  exec ':' . a:firstline . ',' . a:lastline 'delete'

  let lines = map(rows, {_, row -> '|' . join(row, '|') . '|'})
  call insert(lines, '', 1)
  call append(a:firstline - 1, lines)
  call tablemode#table#AddBorder(a:firstline + 1)
  call tablemode#table#Realign(a:firstline)
endfunction
dhruvasagar commented 2 years ago

@slarrain ^