Skip to content

Working with other plugins

NWang edited this page Feb 13, 2023 · 11 revisions

NOTICE:

  • You should always check for the latest README/docs before referencing the example code provided below.
  • I have added the commit hash (at the time I'm writing this) for each plugin. Remember to update the commit hash in future updates.

Work with SmiteshP/nvim-navic@7e9d2b2

In short, to avoid Neovim got frozen after restoring from a session, you should NOT update your WinBars(i.e. by setting vim.wo.winbar) on WinEnter event. Instead, it's recommended to use CursorHold command since that:

  1. It's triggered much less often than CursorMoved, and thus providing good performance.
  2. It's definitely not called in the middle of :source foobar.vim, so we can avoid the problem I just mentioned.
vim.api.nvim_create_autocmd({
  'CursorHold',
  'InsertEnter',
}, {
  group = curfile_augroup,
  callback = function()
    if -- the current filetype/buftype should be excluded.
    then return end

    -- do your stuff here. (mine for example)
    local winbar_prefix = string.format('  %s%s  ', vim.fn.expand('%:t'), vim.bo.mod and '*' or '')
    local guess = 11
    while
      vim.api.nvim_win_get_width(vim.api.nvim_get_current_win())
      - winbar_prefix:len()
      < navic.get_location({ depth_limit = guess, highlight = false }):len() - 10
    do
      guess = guess-1
    end
    local winbar_content = navic.get_location({ depth_limit = guess }) or ''

    -- finally, set the value of winbar.
    vim.wo.winbar = winbar_prefix .. winbar_content .. (winbar_content:len()>1 and ' 🐈' or '🐈')
  end,
})

Work with ibhagwan/fzf-luacddb1c3

In short, this is the working example: (more explanation below)

opts.actions = {
  ['default'] = function(selected)
    if repos[selected[1]] == vim.fn.getcwd(-1, -1) then return end
    local no_DirChanged_DirChangedPre = repos[selected[1]] == vim.fn.getcwd(-1, 0)
    if no_DirChanged_DirChangedPre then require('suave').store_session(true) end
    vim.cmd('cd ' .. repos[selected[1]])
    if no_DirChanged_DirChangedPre then require('suave').restore_session(true) end
  end
}

details

Before I encountered an issue(see #18), I thought everything should work fine by those autocmds I provided as they had been working for months without any problem. Then I realized that there is a special case:

The two events DirChanged and DirChangedPre won't be triggered, say you try to switch from Project A to Project B, from a tabpage where its tabpage current-directory(:h current-directory) has been set to the root of Project B.

That is: Instead of just calling :cd, you also need to call store_session(true) and restore_session(true) manually when ^ happen, i.e. when destination == tabpage current-directory.

Work with nanozuki/tabby.nvim@9065c65

You can restore those tabpage names, which are generated/specified in the following ways, for nanozuki/tabby.nvim:

To list all the tabpage names, you can write something like this:

for tabnr, tabh in pairs(vim.api.nvim_list_tabpages()) do
  local tabname = require('tabby.feature.tab_name').get_raw(tabh)
  -- process the tab name...
end

Thus, your suave.setup will look like this: @174287c

suave.setup {
  store_hooks = {
    after_mksession = {
      function (data)
        -- always reset.
        data.tabby = {}
        for tabnr, tabh in pairs(vim.api.nvim_list_tabpages()) do
          data.tabby[tabnr] = require('tabby.feature.tab_name').get_raw(tabh)
        end
      end,
    },
  },
  restore_hooks = {
    after_source = {
      function (data)
        if not data or not data.tabby
        then return end

        for tabnr, tabh in pairs(vim.api.nvim_list_tabpages()) do
          if data.tabby[tabnr] then
            require('tabby.feature.tab_name').set(tabh, data.tabby[tabnr])
          end
        end
      end,
    },
  },
}