docs: clarify get_current_dir nil return and add telescope recipe
Problem: get_current_dir returns nil in several cases that were not documented, causing confusion when used in keymaps that open pickers like Telescope (#682). Also, posix_to_os_path could crash on Windows when no drive letter is found. Solution: expand get_current_dir docs to explain nil return cases, add a Telescope recipe with nil guards, and add a defensive nil check in posix_to_os_path. Cherry-picked from: stevearc/oil.nvim#727
This commit is contained in:
parent
582d9fc815
commit
eed6697ce2
3 changed files with 79 additions and 4 deletions
|
|
@ -7,6 +7,7 @@ Have a cool recipe to share? Open a pull request and add it to this doc!
|
|||
- [Toggle file detail view](#toggle-file-detail-view)
|
||||
- [Show CWD in the winbar](#show-cwd-in-the-winbar)
|
||||
- [Hide gitignored files and show git tracked hidden files](#hide-gitignored-files-and-show-git-tracked-hidden-files)
|
||||
- [Open Telescope file finder in the current oil directory](#open-telescope-file-finder-in-the-current-oil-directory)
|
||||
|
||||
<!-- /TOC -->
|
||||
|
||||
|
|
@ -125,3 +126,44 @@ require("oil").setup({
|
|||
},
|
||||
})
|
||||
```
|
||||
|
||||
## Open Telescope file finder in the current oil directory
|
||||
|
||||
When using `get_current_dir()` in a keymap that also opens another plugin's UI (like Telescope), always capture the directory in a local variable **before** the call that changes the buffer context. Passing `get_current_dir()` directly as an argument works because Lua evaluates arguments before calling the function, but any subsequent calls will see the new buffer.
|
||||
|
||||
```lua
|
||||
require("oil").setup({
|
||||
keymaps = {
|
||||
["<leader>ff"] = {
|
||||
desc = "Find files in the current directory",
|
||||
callback = function()
|
||||
local dir = require("oil").get_current_dir()
|
||||
if not dir then
|
||||
vim.notify("Could not get oil directory", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
require("telescope.builtin").find_files({ cwd = dir })
|
||||
end,
|
||||
},
|
||||
["<leader>fg"] = {
|
||||
desc = "Live grep in the current directory",
|
||||
callback = function()
|
||||
local dir = require("oil").get_current_dir()
|
||||
if not dir then
|
||||
vim.notify("Could not get oil directory", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
require("telescope.builtin").live_grep({ cwd = dir })
|
||||
end,
|
||||
},
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
If you need the directory after an operation that might change the current buffer, pass the buffer number explicitly:
|
||||
|
||||
```lua
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
-- ... some operation that changes the current buffer ...
|
||||
local dir = require("oil").get_current_dir(bufnr)
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue