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

feature: mini.files extra keymaps #2692

Open
1 task done
dusty-phillips opened this issue Mar 10, 2024 · 2 comments · May be fixed by #2695
Open
1 task done

feature: mini.files extra keymaps #2692

dusty-phillips opened this issue Mar 10, 2024 · 2 comments · May be fixed by #2695
Labels
enhancement New feature or request

Comments

@dusty-phillips
Copy link
Contributor

Did you check the docs?

  • I have read all the LazyVim docs

Is your feature request related to a problem? Please describe.

I prefer mini.files to neotree and have disabled neotree. Two actions I would like to invoke on my filesystem are not possible in the default mini.files extra: splitting windows and changing CWD.

Describe the solution you'd like

Mini.files provides documentation for mapping splits and setting cwd here:

https://github.com/echasnovski/mini.nvim/blob/e578f353d0b1d9f21a9640458d8e506c0c9acf5e/doc/mini-files.txt#L429

I would like LazyVim-sensible keybindings for these actions. Ideally, it would be possible to configure the keybindings without overriding config().

Describe alternatives you've considered

I can, of course, extend the existing LazyVim config to include these mini.files keymaps, but I felt they would be useful for other users as well.

Additional context

No response

@dusty-phillips dusty-phillips added the enhancement New feature or request label Mar 10, 2024
@dusty-phillips
Copy link
Contributor Author

dusty-phillips commented Mar 10, 2024

For reference, here is the config that I used to override the default one provided by lazvim (as far as I can tell, there's no way to say "call the default config" from inside the config call so I have to completely overwrite it?

(collapsed because I have a PR with a better version of this)
  config = function(_, opts)
    require("mini.files").setup(opts)

    local show_dotfiles = true
    local filter_show = function(fs_entry)
      return true
    end
    local filter_hide = function(fs_entry)
      return not vim.startswith(fs_entry.name, ".")
    end

    local toggle_dotfiles = function()
      show_dotfiles = not show_dotfiles
      local new_filter = show_dotfiles and filter_show or filter_hide
      require("mini.files").refresh({ content = { filter = new_filter } })
    end

    local map_split = function(buf_id, lhs, direction)
      local rhs = function()
        -- Make new window and set it as target
        local new_target_window
        local cur_target_window = require("mini.files").get_target_window()
        if cur_target_window ~= nil then
          vim.api.nvim_win_call(cur_target_window, function()
            vim.cmd(direction .. " split")
            new_target_window = vim.api.nvim_get_current_win()
          end)

          require("mini.files").set_target_window(new_target_window)
          require("mini.files").go_in({ close_on_file = true })
        end
      end

      local desc = "Split " .. direction
      vim.keymap.set("n", lhs, rhs, { buffer = buf_id, desc = desc })
    end

    local files_set_cwd = function()
      local cur_entry_path = MiniFiles.get_fs_entry().path
      local cur_directory = vim.fs.dirname(cur_entry_path)
      if cur_directory ~= nil then
        vim.fn.chdir(cur_directory)
      end
    end

    vim.api.nvim_create_autocmd("User", {
      pattern = "MiniFilesBufferCreate",
      callback = function(args)
        local buf_id = args.data.buf_id
        vim.keymap.set("n", "g.", toggle_dotfiles, { buffer = buf_id, desc = "Toggle dotfiles" })
      end,
    })

    vim.api.nvim_create_autocmd("User", {
      pattern = "MiniFilesBufferCreate",
      callback = function(args)
        local buf_id = args.data.buf_id
        -- Tweak keys to your liking
        map_split(buf_id, "gs", "belowright horizontal")
        map_split(buf_id, "gv", "belowright vertical")
      end,
    })

    vim.api.nvim_create_autocmd("User", {
      pattern = "MiniFilesBufferCreate",
      callback = function(args)
        vim.keymap.set("n", "gc", files_set_cwd, { buffer = args.data.buf_id, desc = "Set cwd" })
      end,
    })

    vim.api.nvim_create_autocmd("User", {
      pattern = "MiniFilesActionRename",
      callback = function(event)
        require("lazyvim.util").lsp.on_rename(event.data.from, event.data.to)
      end,
    })
  end,

Some notes:

  • I added a desc to the existing g. keybinding so it has a description in the menu. This is a no-brainer so I'll put up a PR for it.
  • I modified the mini.files-provided examples a bit because it was giving me some lua warnings. Mostly added some if clauses to ensure that something is actually there.
  • The gv keybinding collides with LazyVim. It seem to work in the mini.files buffer, but if you use g and look a the menu it has the "wrong" description.
  • The gc keybinding also collides with LazyVim, but seems to work in the mini.files buffer. It has the right description, but if you press g, wait for the menu, then press c, it will both change the cwd and show up ac menu that you have to escape.
  • I added a "go_in" call to the split view so that it open the files no split.
  • There's no way to customize the keybindings without overriding the config. Maybe it could have some extra mappings in opts?

@dusty-phillips
Copy link
Contributor Author

Created the linked PR to implement all of the above, though I'm cautious about the keybindings.

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