Skip to content

Root of nvim-tree changed unexpectedly when jump to file in another directory with LSP on Windows #2981

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
ljie-PI opened this issue Nov 2, 2024 · 2 comments · Fixed by #2982
Assignees
Labels
bug Something isn't working OS Windows PowerShell specific to windows powershell

Comments

@ljie-PI
Copy link
Collaborator

ljie-PI commented Nov 2, 2024

Description

On windows, when I use LSP go to definition in another directory, the root of nvim-tree changed to that directory, even when the destination directory is a sub-directory of current directory.

Neovim version

NVIM v0.10.2
Build type: Release
LuaJIT 2.1.1713484068

Operating system and version

Windows 11

Windows variant

Command Prompt

nvim-tree version

82ab19e

Clean room replication

I'm on windows so don't have /tmp/nvt-min.lua.

Pasting my config here:

local status_ok, nvim_tree = pcall(require, "nvim-tree")
if not status_ok then
  return
end

local api_status_ok, nvim_tree_api = pcall(require, "nvim-tree.api")
if not api_status_ok then
  return
end

local function customized_on_attach(bufnr)
  local function opts(desc)
    return { desc = "nvim-tree: " .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
  end
  -- default mappings
  nvim_tree_api.config.mappings.default_on_attach(bufnr)
  -- custom mappings
  vim.keymap.set('n', 'c', nvim_tree_api.tree.change_root_to_node, opts('CD'))
  vim.keymap.set('n', 'l', nvim_tree_api.node.open.edit, opts('Open'))
  vim.keymap.set('n', '<CR>', nvim_tree_api.node.open.edit, opts('Open'))
  vim.keymap.set('n', 'o', nvim_tree_api.node.open.edit, opts('Open'))
  vim.keymap.set('n', 'h', nvim_tree_api.node.navigate.parent_close, opts('Close Directory'))
  vim.keymap.set('n', 'v', nvim_tree_api.node.open.vertical, opts('Open: Vertical Split'))
end

local keymap = vim.api.nvim_set_keymap
keymap("n", "<leader>e", ":NvimTreeToggle<CR>", { noremap = true, silent = true })

nvim_tree.setup {
  update_focused_file = {
    enable = true,
    update_cwd = true,
  },
  actions = {
    change_dir = {
      enable = false,
    }
  },
  renderer = {
    root_folder_modifier = ":t",
    icons = {
      glyphs = {
        default = "",
        symlink = "",
        folder = {
          arrow_open = "",
          arrow_closed = "",
          default = "",
          open = "",
          empty = "",
          empty_open = "",
          symlink = "",
          symlink_open = "",
        },
        git = {
          unstaged = "",
          staged = "S",
          unmerged = "",
          renamed = "",
          untracked = "U",
          deleted = "",
          ignored = "",
        },
      },
    },
  },
  diagnostics = {
    enable = true,
    show_on_dirs = true,
    icons = {
      hint = "",
      info = "",
      warning = "",
      error = "",
    },
  },
  view = {
    width = 45,
    side = "left",
  },
  on_attach = customized_on_attach,
}

Steps to reproduce

  1. Open a project with nvim
  2. Invoke "go to definition" provided by LSP, e.g.
image 3. Notice the root of the nvim-tree windows has changed to the subdirectory. image

Expected behavior

The root of nvim-tree doesn't change frequently if the directories are in the same project.

Actual behavior

root of the nvim-tree windows has changed

@ljie-PI ljie-PI added the bug Something isn't working label Nov 2, 2024
@ljie-PI
Copy link
Collaborator Author

ljie-PI commented Nov 2, 2024

With some debug message, I found the path passed from LSP is not normalized. The "c:" is lower case, while other place uses upper case drive identifier "C:". So the path_relative function doesn't return as expected.

image

@alex-courtis
Copy link
Member

Nice find... case sensitivity is hard to spot.

I'd be most grateful for a pull request; I've added you as a member of this repo. You can push a branch and always run CI.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working OS Windows PowerShell specific to windows powershell
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants