rmagatti / goto-preview

A small Neovim plugin for previewing definitions using floating windows.
Apache License 2.0
835 stars 27 forks source link

[FEATURE] Add a wiki for repo #70

Closed jrwrigh closed 2 years ago

jrwrigh commented 2 years ago

Basically as an option to add some config examples/advanced configurations. For example, I have now setup goto-preview to behave similarly to telescope in regards to "expanding" preview windows into full windows. You can press <CR>, <C-v>, <C-x>, <C-t> to expand the preview into the current (main) window, vertical split, horizontal split, and a new tab, respectively.

local gtp = require('goto-preview')
local select_to_edit_map = {
  default    = "edit",
  horizontal = "new",
  vertical   = "vnew",
  tab        = "tabedit",
}

local function open_file(orig_window, filename, cursor_position, command)
  if orig_window ~= 0 and orig_window ~= nil then
    vim.api.nvim_set_current_win(orig_window)
  end
  pcall(vim.cmd, string.format('%s %s', command, filename))
  vim.api.nvim_win_set_cursor(0, cursor_position)
end

local function open_preview(preview_win, type)
  return function()
    local command         = select_to_edit_map[type]
    local orig_window     = vim.api.nvim_win_get_config(preview_win).win
    local cursor_position = vim.api.nvim_win_get_cursor(preview_win)
    local filename        = vim.api.nvim_buf_get_name(0)

    vim.api.nvim_win_close(preview_win, gtp.conf.force_close)
    open_file(orig_window, filename, cursor_position, command)

    local buffer = vim.api.nvim_get_current_buf()
    vim.api.nvim_buf_del_keymap(buffer, 'n', '<C-v>')
    vim.api.nvim_buf_del_keymap(buffer, 'n', '<CR>')
    vim.api.nvim_buf_del_keymap(buffer, 'n', '<C-x>')
    vim.api.nvim_buf_del_keymap(buffer, 'n', '<C-t>')
  end
end

local function post_open_hook(buf, win)
  vim.keymap.set('n', '<C-v>', open_preview(win, "vertical"),   { buffer = buf })
  vim.keymap.set('n', '<CR>',  open_preview(win, "default"),    { buffer = buf })
  vim.keymap.set('n', '<C-x>', open_preview(win, "horizontal"), { buffer = buf })
  vim.keymap.set('n', '<C-t>', open_preview(win, "tab"),        { buffer = buf })
end

require('goto-preview').setup {
  post_open_hook = post_open_hook,
}

I don't see any particular reason for this to be "officially" incorporated into the plugin, but I think it'd be nice to share with others as an option.

rmagatti commented 2 years ago

Hey, yeah this is good stuff. I've created the wiki with just the main page (no info for now). After your PR is merged you'll be able to edit the Wiki as well!

jrwrigh commented 2 years ago

The PR was merged, but I'm still unable to edit the wiki.

rmagatti commented 2 years ago

@jrwrigh I misinformed you before. I have edited permissions so wiki editing is public now.

jrwrigh commented 2 years ago

No worries! I now have write access to the wiki. I'll go ahead and add my stuff.