Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom actions for finder? #1523

Open
jaidetree opened this issue Jan 30, 2025 · 2 comments · May be fixed by #1525
Open

Custom actions for finder? #1523

jaidetree opened this issue Jan 30, 2025 · 2 comments · May be fixed by #1525
Labels
enhancement New feature or request

Comments

@jaidetree
Copy link

jaidetree commented Jan 30, 2025

Is your feature request related to a problem? Please describe.
I created a telescope plugin for myself that takes a filepath and presents options to open it in the current window, new tab, vsplit left, vsplit right, split above, and split below. Ideally, I would like to get the filepath that was selected from the lspsaga finder UI to open up my telescope picker to choose the desired action. This would normalize my bindings across neovim while reducing steps to remember.

Describe the solution you'd like
There's two options that could work:

  1. Support something like the following:
    require("lspsaga").setup({
      finder = {
        keys = {
          "<cr>" = function (filepath) ... end
        },
      },
    })
  2. Create module-level functions for common actions that one could replace with their own implementations. Less ideal monkey-patching actions but it would work, and I have an emacs-inspired advising system that would make it more manageable.
    telescope = require('telescope')
    telescope.actions.open = function (filepath) 
      telescope._extensions.find_menu.find_menu({ filepath = filepath })
    end 

Describe alternatives you've considered
I looked at the finder implementation and saw that it's mostly iterating the config settings for keys, so was thinking I could do something like the following:

   require("lspsaga").setup({
     finder = {
       keys = {
         "Telescope file_menu filepath=" = "<CR>",
       },
     },
   })

But that doesn't seem to work, I'll have to check but maybe unsupported commands are stripped from the config?

I could fork it and make my changes, but then I'll have to be on top of your upstream changes and I'd much rather try and find a way to make lspsaga better than create a worse version of it.

Additional context

Open to alternative ideas here!

@jaidetree jaidetree added the enhancement New feature or request label Jan 30, 2025
@jaidetree
Copy link
Author

Found a discussion that's nearly identical https://github.com/orgs/nvimdev/discussions/1495#discussion-7301203

That said, it sparked an alternative idea:

require("lspsaga").setup({
  finder = {
    keys = {
      open = "<cr>" -- Set Enter to open the file
    },
    actions = {
      open = function (filepath) ... end
    }
  },
})
  • It wouldn't change the existing keys configuration shape
  • It would be trivial to implement:
    if config.finder.actions[action] {
      config.finder.actions[action](fname)
    } else {
      vim.cmd[action](fname)
    }
  • That conditional would be applied
    vim.cmd[action](fname)
  • The conditional would also apply
    vim.cmd[action](fname)
  • This system should even support new\custom actions if I understand this right!
  • I think it's a reasonable compromise to prevent redefining the actions defined in
    local black = { 'close', 'toggle_or_open', 'go_peek', 'quit', 'shuttle' }

If you're on board with that route I'm happy to draft a PR. Thoughts?

jaidetree added a commit to jaidetree/lspsaga.nvim that referenced this issue Jan 31, 2025
Problem:
There are use cases where you may wish to call a function instead of
mapping a command directly to a key action for the finder UI.

Solution:
Supports configuring a `lspsaga.config.finder.actions` object mapping
action names to callable functions.

```lua
require('lspsaga').setup({
  finder = {
    keys = {
      open = "<CR>",
    },
    actions = {
      open = function (fname)
        print("Selected filename", fname)
      end
    },
  }
})
```

Additionally, this wraps the final `api.nvim_win_set_cursor(0, pos)`
call in `xpcall` given that this may fail if the function does not
finish synchronously, like spawning a telescope interface.

Fixes nvimdev#1523
@jaidetree jaidetree linked a pull request Jan 31, 2025 that will close this issue
@jaidetree
Copy link
Author

Decided to try drafting a PR anyway given it's a pretty minimal amount of changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant