stevearc / oil.nvim

Neovim file explorer: edit your filesystem like a buffer
MIT License
3.4k stars 92 forks source link

feature request: compact directory entries #346

Closed sonntag closed 2 months ago

sonntag commented 3 months ago

Did you check existing requests?

Describe the feature

VSCode has an option in the tree browser to compact folders, wherein if a folder only has a single child they get combined into a single tree element. The nvim-tree plugin also has a similar feature outlined here.

I was think it would be cool to have a similar option in oil. Basically, if we open up the directory with the following tree:

❯ tree
.
├── foo
│   └── bar
│       └── baz.txt
├── multiple
│   ├── a.txt
│   ├── b.txt
│   └── c.txt
└── one
    └── two
        ├── a.txt
        └── b.txt

The oil buffer would have the following content:

multiple/
one/two/
foo/bar/baz.txt

Provide background

I commonly work in Java-based languages where it is common to have directories with either only a single directory or a single file. This feature would make it faster to navigate down the directory tree in these cases, and also make these single directory cases move obvious when navigating an unfamiliar directory structure.

What is the significance of this feature?

nice to have

Additional details

No response

wojciech-kulik commented 2 months ago

I agree, navigation through empty folders in Java/Kotlin is pretty annoying. I wrote a simple binding to make it faster:

vim.api.nvim_create_autocmd("FileType", {
  group = group,
  pattern = "oil",
  callback = function(event)
    local function navigateDeeper()
      vim.cmd("norm! G")
      require("oil").select()

      vim.defer_fn(function()
        if vim.fn.line("$") == 2 then
          navigateDeeper()
        end
      end, 50)
    end

    vim.keymap.set("n", "<leader><cr>", function()
      navigateDeeper()
    end, { buffer = event.buf })
  end,
})

It's not perfect, because some delay is required.

stevearc commented 2 months ago

This is unlikely to ever happen. There are fundamental UX challenges around what happens if you delete or edit one of these compact directories? If you have foo/bar/baz/, how would you enter the foo/bar/ directory? On top of the UX, this would add significant complexity to the resolution logic in the code.

wojciech-kulik commented 2 months ago

This is unlikely to ever happen. There are fundamental UX challenges around what happens if you delete or edit one of these compact directories? If you have foo/bar/baz/, how would you enter the foo/bar/ directory? On top of the UX, this would add significant complexity to the resolution logic in the code.

Instead of changing the core of navigation you could just provide an action to navigate quickly through empty dirs, something like the code I provided, but without delays :).

stevearc commented 2 months ago

I would accept a PR to add such an action. Should be relatively straightforward; just check if the entry under the cursor is a directory and, if so, check if it has a single child dir. Repeat until the number of children != 1 and enter that directory.

sonntag commented 2 months ago

If you have foo/bar/baz/, how would you enter the foo/bar/ directory?

I mean the general idea is you wouldn't enter the foo/bar/ directory. If you needed to create a new file at that level, you could add a new entry at the root level and append the same foo/bar/ directory to that entry. E.g. in the above example you could add a new line to the buffer that says foo/bar/cat.txt and the existing file create logic would handle it properly. The buffer would then also change to only show foo/bar/ since bar now has 2 files.

We could also easily support a toggle operation to toggle between the compact and expanded views, which would allow users to navigate to these directories if they need to (similar to how g. toggles hidden directories).