Skip to content

New File getting opened in floating window. #2743

Closed
@niksingh710

Description

@niksingh710

Description

As it has been getting discussed, #2739 (reply in thread)

Creation of any new file opens the buffer into the nvim floating window.

#2739 (reply in thread)

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"

Operating system and version

Arch Linux 6.8.4-zen1-1-zen

Windows variant

No response

nvim-tree version

81eb8d5

Clean room replication

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
      "JMarkin/nvim-tree.lua-float-preview",
    },
    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

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
  local api = require("nvim-tree.api")
  local function attach(bufnr)
    local FloatPreview = require("float-preview")

    FloatPreview.attach_nvimtree(bufnr)
    local close_wrap = FloatPreview.close_wrap

    -- 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 = { close_wrap(api.node.navigate.parent_close), options("Close Directory") },
      l = { close_wrap(api.node.open.edit), options("Open") },
      H = { close_wrap(api.tree.collapse_all), options("Close Directory") },
      L = { close_wrap(api.tree.expand_all), options("Expand All") },
      v = { close_wrap(api.node.open.vertical), options("Open: Vertical Split") },
      s = { close_wrap(api.node.open.horizontal), options("Open: Horizontal Split") },
      C = { close_wrap(api.tree.change_root_to_node), options("CD") },
      O = { close_wrap(api.node.run.system), options("Run System") },
      y = { close_wrap(api.fs.copy.node), options("Copy") },
      c = { close_wrap(api.fs.copy.filename), options("Copy Name") },
      K = { FloatPreview.toggle, options("Copy Name") },
      ["?"] = { close_wrap(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

  HEIGHT_PADDING = 10
  WIDTH_PADDING = 15

  require("nvim-tree").setup({
    filters = { custom = { "^.git$" } },
    hijack_cursor = false,

    actions = {
      open_file = {
        eject = false,
        quit_on_open = true,
      },
    },

    on_attach = attach,
    view = {
      adaptive_size = false,
      side = "right",

      width = function()
        return vim.opt.columns:get() - WIDTH_PADDING * 2
      end,
      float = {
        enable = true,
        quit_on_focus_loss = false,
        open_win_config = function()
          local screen_w = vim.opt.columns:get()
          local screen_h = vim.opt.lines:get() - vim.opt.cmdheight:get()
          local window_w_f = (screen_w - WIDTH_PADDING * 2) / 2
          local window_w = math.floor(window_w_f)
          local window_h = screen_h - HEIGHT_PADDING * 2
          local center_x = WIDTH_PADDING - 1
          local center_y = ((vim.opt.lines:get() - window_h) / 2) - vim.opt.cmdheight:get()

          return {
            border = "single",
            relative = "editor",
            row = center_y,
            col = center_x,
            width = window_w,
            height = window_h,
          }
        end,
      },
    },
  })
  require("float-preview").setup({
    window = {
      wrap = false,
      trim_height = false,
      open_win_config = function()
        HEIGHT_PADDING = 10
        WIDTH_PADDING = 15
        local screen_w = vim.opt.columns:get()
        local screen_h = vim.opt.lines:get() - vim.opt.cmdheight:get()
        local window_w_f = (screen_w - WIDTH_PADDING * 2 - 1) / 2
        local window_w = math.floor(window_w_f)
        local window_h = screen_h - HEIGHT_PADDING * 2
        local center_x = window_w_f + WIDTH_PADDING + 2
        local center_y = ((vim.opt.lines:get() - window_h) / 2) - vim.opt.cmdheight:get()

        return {
          style = "minimal",
          relative = "editor",
          border = "single",
          row = center_y,
          col = center_x,
          width = window_w,
          height = window_h,
        }
      end,
    },
    mapping = {
      -- scroll down float buffer
      down = { "<C-d>" },
      -- scroll up float buffer
      up = { "<C-e>", "<C-u>" },
      -- enable/disable float windows
      toggle = { "'" },
    },
  })
end

-- UNCOMMENT this block for diagnostics issues, substituting pattern and cmd as appropriate.
-- Requires diagnostics.enable = true in setup.
--[[
vim.api.nvim_create_autocmd("FileType", {
  pattern = "lua",
  callback = function()
    vim.lsp.start { cmd = { "lua-language-server" } }
  end,
})
]]

Steps to reproduce

  1. nvim -nu nvt-min.lua
  2. :NvimTreeToggle
  3. a then type your filename
  4. The file buffer is opened in the nvim Floating window

Expected behavior

The floating window should be closed and the buffer of the newly created file should be focused.

Actual behavior

output.mp4

Activity

alex-courtis

alex-courtis commented on Apr 16, 2024

@alex-courtis
Member

Reproduced.

Removed this and the issue goes away:

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

What's the purpose of that event?

niksingh710

niksingh710 commented on Apr 16, 2024

@niksingh710
Author

It resolves the issue kindda.
I had that snippet to instantly open the file in edit mode as soon as created.
i guess i need to add a vim.cmd to close nvimTree before opening the buffer in edit mode.

Is there any way i can check if NvimTree is in floating mode or not?

alex-courtis

alex-courtis commented on Apr 20, 2024

@alex-courtis
Member

Is there any way i can check if NvimTree is in floating mode or not?

It will float depending on your view.float.enable setting, although I don't think that's quite what your asking.

Perhaps you are after :help nvim-tree-api.tree.is_visible()

alex-courtis

alex-courtis commented on Apr 20, 2024

@alex-courtis
Member

or perhaps :help nvim-tree-api.tree.winid()

niksingh710

niksingh710 commented on Apr 23, 2024

@niksingh710
Author

Thanks for the help @alex-courtis.

After seeing the help options you mentioned, I came up with the snippet below That closes NvimTree if new file is created.

-- This will make sure that newly created file get's open to edit
api.events.subscribe(api.events.Event.FileCreated, function(file)
local win_id = api.tree.winid()
if win_id ~= nil then
  vim.cmd([[NvimTreeClose]])
end
vim.cmd("edit " .. file.fname)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @alex-courtis@niksingh710

        Issue actions

          New File getting opened in floating window. · Issue #2743 · nvim-tree/nvim-tree.lua