Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

If using floating mode and creating a new file the floating window becomes the created file. #2702

Closed
niksingh710 opened this issue Mar 10, 2024 · 2 comments
Labels

Comments

@niksingh710
Copy link

Description

If Nvim tree is used in floating mode.
and a new file is created e.g test.lua now the nvimTree floating window is the new buffer test.lua

Neovim version

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1702233742

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

Operating system and version

6.7.9-zen1-1-zen Arch linux

Windows variant

No response

nvim-tree version

041dbd1

Clean room replication

-- Plugin manager
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)
vim.opt.sessionoptions = { "buffers", "curdir", "tabpages", "winsize", "help", "globals", "skiprtp", "folds" }
require("lazy").setup({
  {
    "nvim-tree/nvim-tree.lua",
    -- enabled = false,
    cmd = { "NvimTreeToggle" },
    dependencies = {
      "nvim-tree/nvim-web-devicons",
    },
    keys = {
      { "<leader>e", "<cmd>NvimTreeToggle<CR>", desc = "Explorer" },
    },

    init = function()
      -- disable netrw at the very start of your init.lua
      vim.g.loaded_netrw = 1
      vim.g.loaded_netrwPlugin = 1
      -- auto cmd for last window
      vim.api.nvim_create_autocmd("BufEnter", {
        nested = true,
        callback = function()
          if #vim.api.nvim_list_wins() == 1 and vim.api.nvim_buf_get_name(0):match("NvimTree_") ~= nil then
            vim.cmd("quit")
          end
        end,
      })
      -- will fall back to last buffer if closed one buffer
      -- doesnot let nvim tree window to be the last window
      vim.api.nvim_create_autocmd("BufEnter", {
        nested = true,
        callback = function()
          local api = require("nvim-tree.api")

          -- Only 1 window with nvim-tree left: we probably closed a file buffer
          if #vim.api.nvim_list_wins() == 1 and api.tree.is_tree_buf() then
            -- Required to let the close event complete. An error is thrown without this.
            vim.defer_fn(function()
              -- close nvim-tree: will go to the last hidden buffer used before closing
              api.tree.toggle({ find_file = true, focus = true })
              -- re-open nivm-tree
              api.tree.toggle({ find_file = true, focus = true })
              -- nvim-tree is still the active window. Go to the previous window.
              vim.cmd("wincmd p")
            end, 0)
          end
        end,
      })
    end,

    opts = {
      filters = { custom = { "^.git$" } },
      hijack_cursor = false,

      actions = {
        open_file = {
          quit_on_open = true,
        },
      },
      view = {
        adaptive_size = true,
        side = "right",
        width = 30,
        float = {
          enable = true,
          quit_on_focus_loss = false,
        },
      },
      git = {
        enable = true,
        ignore = false,
      },
      system_open = {
        cmd = "xdg-open",
      },
      renderer = {
        root_folder_label = ":t",
        highlight_git = true,
        highlight_opened_files = "none",

        indent_markers = {
          enable = true,
          inline_arrows = true,
          icons = {
            corner = "",
            edge = "",
            item = "",
            none = " ",
          },
        },
      },
      diagnostics = {
        enable = true,
        show_on_dirs = true,
        show_on_open_dirs = false,
        debounce_delay = 50,
        severity = {
          -- min = vim.diagnostic.severity.WARNING, -- I don't like warning in my file explorer
          min = vim.diagnostic.severity.ERROR,
          max = vim.diagnostic.severity.ERROR,
        },
      },
    },

    config = function(_, opts)
      local api = require("nvim-tree.api")

      local gwidth = vim.api.nvim_list_uis()[1].width
      local gheight = vim.api.nvim_list_uis()[1].height
      local width = 100
      local height = 40
      opts.view.width = width
      opts.view.float.open_win_config = {
        relative = "editor",
        width = width,
        height = height,
        row = (gheight - height) * 0.4,
        col = (gwidth - width) * 0.5,
      }

      local function attach(bufnr)
        -- This will make sure that newly created file get's open to edit
        api.events.subscribe(api.events.Event.FileCreated, function(file)
          vim.cmd("edit " .. file.fname)
        end)

        -- functions used to map
        local function options(desc)
          return {
            desc = "nvim-tree: " .. desc,
            buffer = bufnr,
            noremap = true,
            silent = true,
            nowait = true,
          }
        end

        local function set(mode, data)
          for key, value in pairs(data) do
            vim.keymap.set(mode, key, value[1], value[2])
          end
        end

        local normal = {
          h = { api.node.navigate.parent_close, options("Close Directory") },
          l = { api.node.open.edit, options("Open") },
          H = { api.tree.collapse_all, options("Close Directory") },
          L = { api.tree.expand_all, options("Expand All") },
          v = { api.node.open.vertical, options("Open: Vertical Split") },
          s = { api.node.open.horizontal, options("Open: Horizontal Split") },
          C = { api.tree.change_root_to_node, options("CD") },
          O = { api.node.run.system, options("Run System") },
          y = { api.fs.copy.node, options("Copy") },
          c = { api.fs.copy.filename, options("Copy Name") },
          ["?"] = { api.tree.toggle_help, options("Help") },
        }

        api.config.mappings.default_on_attach(bufnr)
        set("n", normal)

        vim.keymap.set("n", "P", function() -- Special fn to print node PATH
          local node = api.tree.get_node_under_cursor()
          print(node.absolute_path)
        end, options("Print Node Path"))
      end
      opts.on_attach = attach

      require("nvim-tree").setup(opts)

      -- Opens telescope to find a directory and focus on it
      function find_directory_and_focus()
        local actions = require("telescope.actions")
        local action_state = require("telescope.actions.state")

        local function open_nvim_tree(prompt_bufnr, _)
          actions.select_default:replace(function()
            actions.close(prompt_bufnr)
            local selection = action_state.get_selected_entry()
            api.tree.open()
            api.tree.find_file(selection.cwd .. "/" .. selection.value)
          end)
          return true
        end

        local ok, telescope = pcall(require, "telescope.builtin")
        if not ok then
          print("Telescope is not installed")
          return
        end
        telescope.find_files({
          find_command = { "fd", "--type", "directory", "--hidden", "--exclude", ".git/*" },
          attach_mappings = open_nvim_tree,
        })
      end

      vim.keymap.set("n", "fd", find_directory_and_focus)
    end,
  },
})

Steps to reproduce

nvim --clean -u ./minimal.lua

Expected behavior

Should close the window where nvimTree was opened and focus the newly created file.

Actual behavior

output

@niksingh710 niksingh710 added the bug Something isn't working label Mar 10, 2024
@alex-courtis
Copy link
Member

In future please use a https://github.com/nvim-tree/nvim-tree.lua/wiki/Troubleshooting#clean-room-replication instead of a complex setup with the (known problematic) lazy plugin manager.

Replicated:

vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.cmd([[set packpath=/tmp/nvt-min/site]])
local package_root = "/tmp/nvt-min/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
  require("packer").startup({
    {
      "wbthomason/packer.nvim",
      "nvim-tree/nvim-tree.lua",
      "nvim-tree/nvim-web-devicons",
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
    },
    config = {
      package_root = package_root,
      compile_path = install_path .. "/plugin/packer_compiled.lua",
      display = { non_interactive = true },
    },
  })
end
if vim.fn.isdirectory(install_path) == 0 then
  print("Installing nvim-tree and dependencies.")
  vim.fn.system({ "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path })
end
load_plugins()
require("packer").sync()
vim.cmd([[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]])
vim.opt.termguicolors = true
vim.opt.cursorline = true

local function on_attach(bufnr)
  local api = require("nvim-tree.api")

  -- This will make sure that newly created file get's open to edit
  api.events.subscribe(api.events.Event.FileCreated, function(file)
    vim.cmd("edit " .. file.fname)
  end)

  api.config.mappings.default_on_attach(bufnr)
end

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
  require("nvim-tree").setup({
    on_attach = on_attach,
    view = {
      float = {
        enable = true,
      },
    },
  })
end

The issue is that FileCreated event - it's editing the new file in the nvim-tree window.

@alex-courtis alex-courtis added awaiting feedback reproduced Issue confirmed and removed bug Something isn't working labels Mar 14, 2024
@alex-courtis
Copy link
Member

I'll close this one down; please reopen or comment if you have further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants