Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ carry over.
each other in the editing experience.
--]]

-- Oil is the directory explorer. Disable netrw before plugins and startup
-- arguments are processed so it cannot replace Oil's directory buffers.
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

-- Set <space> as the leader key
-- See `:help mapleader`
-- NOTE: Must happen before plugins are required. Others, the wrong leader key
Expand Down
151 changes: 140 additions & 11 deletions nvim/lua/core/lazy-plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ local plugins = {
enabled = true,
show_file_path = true,
show_symbols = true,
exclude_filetype = {
'help',
'oil', -- Conflicts with oil.nvim winbar
'startify',
'dashboard',
'packer',
'neogitstatus',
'NvimTree',
'Trouble',
'alpha',
'lir',
'Outline',
'spectre_panel',
'toggleterm',
'qf',
},
}
},

Expand Down Expand Up @@ -125,20 +141,133 @@ local plugins = {
-- end,
-- },

-- https://github.com/prichrd/netrw.nvim
-- Adds a layer of ✨bling✨ and config to your favorite file explorer.
-- File explorer that lets you edit your filesystem like a normal Neovim buffer.
{
'prichrd/netrw.nvim',
opts = {
use_devicons = true,
icons = {
symlink = "",
directory = "",
file = ""
},
},
'stevearc/oil.nvim',
config = function()
-- Show CWD in the winbar. In Neovim, the winbar is a status line displayed
-- at the top of each window.
-- Here, we declare a global function, `get_oil_winbar`, to retrieve the
-- current working directory
function _G.get_oil_winbar()
local bufnr = vim.api.nvim_win_get_buf(vim.g.statusline_winid)
local dir = require("oil").get_current_dir(bufnr)
if dir then
return vim.fn.fnamemodify(dir, ":~")
else
-- If there is no current directory (e.g. over ssh), just show the buffer name
return vim.api.nvim_buf_get_name(0)
end
end

local oil = require("oil")
---@module 'oil'
---@type oil.SetupOpts
local opts = {
columns = { "icon", },
delete_to_trash = true,
view_options = {
show_hidden = true,
},
-- Open directory buffers from BufEnter below. This avoids Oil's WinNew
-- parent-window race when Neovim starts with a directory argument.
default_file_explorer = false,
-- Keymaps in oil buffer. Can be any value that `vim.keymap.set` accepts OR a table of keymap
-- options with a `callback` (e.g. { callback = function() ... end, desc = "", mode = "n" })
-- Additionally, if it is a string that matches "actions.<name>",
-- it will use the mapping at require("oil.actions").<name>
-- Set to `false` to remove a keymap
-- See :help oil-actions for a list of all available actions
keymaps = {
["g?"] = { "actions.show_help", mode = "n" },
["<CR>"] = "actions.select",
["<C-s>"] = { "actions.select", opts = { vertical = true } },
["<C-h>"] = false, -- disabled
["<C-t>"] = { "actions.select", opts = { tab = true } },
["<C-p>"] = { "actions.select", opts = { horizontal = true } },
["<C-c>"] = { "actions.close", mode = "n" },
["<C-l>"] = "actions.refresh",
["-"] = { "actions.parent", mode = "n" },
["_"] = { "actions.open_cwd", mode = "n" },
["`"] = { "actions.cd", mode = "n" },
["g~"] = { "actions.cd", opts = { scope = "tab" }, mode = "n" },
["gs"] = { "actions.change_sort", mode = "n" },
["gx"] = "actions.open_external",
["g."] = { "actions.toggle_hidden", mode = "n" },
["g\\"] = { "actions.toggle_trash", mode = "n" },
-- Default keymaps I replaced:
-- ["<C-h>"] = { "actions.select", opts = { horizontal = true } },
-- ["<C-p>"] = "actions.preview",
},
win_options = {
winbar = "%!v:lua.get_oil_winbar()",
}
}
oil.setup(opts)

vim.keymap.set("n", "-", "<CMD>Oil<CR>", { desc = "Open parent directory with :Oil" })
vim.keymap.set("n", "<space>-", oil.toggle_float)

vim.api.nvim_create_autocmd("BufEnter", {
desc = "Open directory buffers with Oil after the window exists",
callback = function()
if vim.bo.filetype == "oil" then
return
end

local path = vim.api.nvim_buf_get_name(0)
if path == "" or vim.fn.isdirectory(path) ~= 1 then
return
end

vim.schedule(function()
oil.open(path)
end)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scheduled Oil opens wrong window

Medium Severity

The directory BufEnter handler captures only path, then calls oil.open(path) inside vim.schedule. When the callback runs, Oil uses the then-current window, not the window that entered the directory buffer, so a quick buffer or window switch can open the explorer in the wrong place or trigger Oil errors.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a408243. Configure here.

end,
})

-- winbar.nvim clears the winbar for excluded filetypes. Restore Oil's
-- own winbar after those callbacks run.
local oil_winbar_events = {
"DirChanged",
"CursorMoved",
"BufWinEnter",
"BufFilePost",
"InsertEnter",
"BufWritePost",
}
vim.api.nvim_create_autocmd(oil_winbar_events, {
desc = "Restore Oil's directory winbar",
callback = function()
if vim.bo.filetype == "oil" then
vim.opt_local.winbar = "%!v:lua.get_oil_winbar()"
end
end,
})
end,
-- Optional dependencies
dependencies = { "nvim-tree/nvim-web-devicons" },
-- Use based on preference for dev icons.
-- dependencies = { { "nvim-mini/mini.icons", opts = {} } },
-- Lazy loading is not recommended because it is very tricky to make it work correctly in all situations.
lazy = false,
},

-- https://github.com/prichrd/netrw.nvim
-- Adds a layer of ✨bling✨ and config to your favorite file explorer.
-- INFO: Disabled in favor of oil.nvim as default explorer
-- {
-- 'prichrd/netrw.nvim',
-- opts = {
-- use_devicons = true,
-- icons = {
-- symlink = "",
-- directory = "",
-- file = ""
-- },
-- },
-- },

-- https://github.com/sindrets/diffview.nvim
{ 'sindrets/diffview.nvim' },

Expand Down
36 changes: 33 additions & 3 deletions nvim/lua/core/vim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,28 @@ vim.api.nvim_create_autocmd("FileType", {
end,
})

vim.g.netrw_sort_by = "name"
vim.g.netrw_sort_direction = "reverse"
vim.g.netrw_sort_sequence =
[[\(\.bak\|\~\|\.o\|\.h\|\.info\|\.swp\|\.obj\)[*@]\=$,*]]

-- For reference, this is the default sorting sequence
-- [\/]$,*,\(\.bak\|\~\|\.o\|\.h\|\.info\|\.swp\|\.obj\)[*@]\=$

-- Oil replaces netrw's `:Explore` command and keeps `:E` as a shorthand.
local function open_oil(opts)
local path = opts.args ~= "" and opts.args or nil
require("oil").open(path)
end

local oil_command_opts = {
force = true,
nargs = "?",
complete = "dir",
}
vim.api.nvim_create_user_command("Explore", open_oil, oil_command_opts)
vim.api.nvim_create_user_command("E", open_oil, oil_command_opts)

-- To change the default tree list mode in `netrw`, set the `g:netrw_liststyle` variable.
-- The possible values for `g:netrw_liststyle` are:
--
Expand Down Expand Up @@ -273,8 +295,16 @@ highlights_question_answer()
-- `[target-group]`: Syntax highlighting group you want to apply properties to.
-- `[source-group]`: Syntax highlighting group it will inherit/link from.

-- This forces `:E` to map to `:Explore` even if another plugin has already
-- overwritten that command.
vim.api.nvim_create_user_command("E", "Explore", { force = true })
vim.api.nvim_create_user_command("PrintWinbar", function()
local winbar = vim.wo.winbar

print(vim.inspect({
raw = winbar,
rendered = vim.api.nvim_eval_statusline(winbar, {
winid = vim.api.nvim_get_current_win(),
use_winbar = true,
}).str,
}))
end, {})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug PrintWinbar command committed

Low Severity

A new :PrintWinbar user command dumps raw and rendered winbar values via vim.inspect and nvim_eval_statusline. It is not part of the Oil explorer behavior described in the PR and reads like temporary winbar debugging left in core/vim.lua.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a408243. Configure here.


return {}