mhinz / vim-startify

:link: The fancy start screen for Vim.
MIT License
5.3k stars 186 forks source link

Funcref type fails to create list properly when written in Lua #498

Closed katawful closed 3 years ago

katawful commented 3 years ago

When setting g:startify_lists in Lua, if you include a function then Startify will fail to implement it like it would for Vimscript

Vim:

function! s:show_curr_repo()
  silent! !git rev-parse --is-inside-work-tree
  if v:shell_error == 0
    let git = 'git'
    let commits = systemlist(git .' log --oneline | head -n5')
    let git = 'G' . git[1:]
    return map(commits, '{"line": matchstr(v:val, "\\s\\zs.*"), "cmd": "'. git .' show ". matchstr(v:val, "^\\x\\+") }')
  else
    return 0
  endif
endfunction

let g:startify_lists = [
      \ { 'type': 'files',     'header': ['   Recent Global Files']},
      \ { 'type': 'dir',       'header': ['   Recent Files in: '. getcwd()]},
      \ { 'type': function('s:show_curr_repo'), 'header': ['   Commits in '. getcwd()]},
      \ { 'type': 'sessions',  'header': ['   Sessions']},
      \ { 'type': 'bookmarks', 'header': ['   Bookmarks']},
      \ { 'type': 'commands',  'header': ['   Commands']},
      \ ]

Lua:

local function startShowCurrRepo()
  vim.cmd("silent! !git rev-parse --is-inside-work-tree")
  if (vim.v.shell_error == 0) then
    local commits = vim.fn.systemlist("git log --oneline | head -n5")
    local output = {}
    local lineRegex = vim.regex("\\s\\zs.*")
    local showRegex = vim.regex("^\\x\\+")
    local iter = 1
    for k, v in ipairs(commits) do
      local index = lineRegex:match_str(v)
      local line = v:sub((index + 1))
      local show = v:sub(0, (index - 1))
      table.insert(output, {cmd = ("Git show " .. show), line = line})
      do local _ = (iter + 1) end
    end
    return output
  end
end
vim.g["startify_lists"] = {
  { header = {"   Recent Global Files"}, 
    type = "files"}, 
  { header = {("   Recent Files in: " .. vim.fn.getcwd())}, 
    type = "dir"}, 
  { header = {("   Commits in: " .. vim.fn.getcwd())}, 
    type = startShowCurrRepo()}, 
  { header = {"   Sessions"}, type = "sessions"},
  { header = {"   Bookmarks"}, type = "bookmarks"}, {header = {"   Commands"},
    type = "commands"}} 

Passing a Vimscript function to the Lua config causes the same problem

dominikhaid commented 3 years ago

Try changing

  { header = {("   Commits in: " .. vim.fn.getcwd())}, 
    type = startShowCurrRepo()}, 

to

  { header = {("   Commits in: " .. vim.fn.getcwd())}, 
    type = startShowCurrRepo}, 

By removing the () from the function itself will be past not the output of the function and that is what they expect in their code.

katawful commented 3 years ago

That worked, thanks!!!