doc: clarify get_current_dir nil return and add Telescope recipe

Expand get_current_dir docs to explain when nil is returned (non-oil
buffer, non-files adapter, buffer context change). Add inline code
examples showing the capture-before-call pattern for Telescope keymaps.

Update the existing Telescope keymap example in oil.txt to guard against
nil. Add a dedicated Telescope recipe in recipes.md with find_files and
live_grep examples.

Add a defensive nil guard in posix_to_os_path for edge-case Windows
paths where the drive letter match fails.

Closes #682
This commit is contained in:
kubasync-clanker[bot] 2026-02-18 20:03:52 +00:00
parent f55b25e493
commit 1580de4c66
3 changed files with 79 additions and 4 deletions

View file

@ -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)
```