stevearc / dressing.nvim

Neovim plugin to improve the default vim.ui interfaces
MIT License
1.7k stars 32 forks source link

NUI backend breaks with winbar #45

Closed ecosse3 closed 2 years ago

ecosse3 commented 2 years ago

Describe the bug A clear and concise description of what the bug is.

System information

require('dressing').setup({
  input = {
    -- Set to false to disable the vim.ui.input implementation
    enabled = true,

    -- Default prompt string
    default_prompt = "Input:",

    -- Can be 'left', 'right', or 'center'
    prompt_align = "left",

    -- When true, <Esc> will close the modal
    insert_only = true,

    -- These are passed to nvim_open_win
    anchor = "SW",
    border = EcoVim.ui.float.border or "rounded",
    -- 'editor' and 'win' will default to being centered
    relative = "cursor",

    -- These can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
    prefer_width = 40,
    width = nil,
    -- min_width and max_width can be a list of mixed types.
    -- min_width = {20, 0.2} means "the greater of 20 columns or 20% of total"
    max_width = { 140, 0.9 },
    min_width = { 20, 0.2 },

    -- Window transparency (0-100)
    winblend = 0,
    -- Change default highlight groups (see :help winhl)
    winhighlight = "",

    override = function(conf)
      -- This is the config that will be passed to nvim_open_win.
      -- Change values here to customize the layout
      return conf
    end,

    -- see :help dressing_get_config
    get_config = nil,
  },

  select = {
    -- Set to false to disable the vim.ui.select implementation
    enabled = true,

    -- Priority list of preferred vim.select implementations
    backend = { "telescope", "nui", "fzf", "builtin" },

    -- Options for nui Menu
    nui = {
      position = {
        row = 1,
        col = 0,
      },
      size = nil,
      relative = "cursor",
      border = {
        style = EcoVim.ui.float.border or "rounded",
        text = {
          top_align = "right",
        },
      },
      max_width = 80,
      max_height = 40,
    },

    -- see :help dressing_get_config
    get_config = function(opts)
      if opts.kind == 'codeaction' then
        return {
          backend = 'nui',
          nui = {
            relative = 'cursor',
            max_width = 80,
          }
        }
      end
    end
  },
})

To Reproduce Steps to reproduce the behavior:

  1. Use winbar of nvim 0.8

If possible, provide a minimal file that will trigger the issue (see tests/manual for examples of short ways to call vim.ui.*):

Somehow it works good with test. Just breaks when using my config and code actions/rename. image

Screenshots image

Additional context I have winbar configured with nvim 0.8 and then nui backed stops working and each time I try to use code action with vim.ui.select or rename with vim.ui.input it throws error.

Looks like winbar is trying to be attached to nui window and it doesn't have enough space or sth like that. I've added "DressingInput" filetype and "DressingSelect" filetype to excluded of winbar but it still doesn't work. I can tell that it works great with Telescope (which has filetype of TelescopePrompt) added to excluded filetypes.

My autocmd for winbar:

vim.api.nvim_create_autocmd({ "CursorMoved", "BufWinEnter", "BufFilePost" }, {
  callback = function()
    local winbar_filetype_exclude = {
      "help",
      "startify",
      "dashboard",
      "packer",
      "neogitstatus",
      "NvimTree",
      "Trouble",
      "alpha",
      "lir",
      "Outline",
      "spectre_panel",
      "toggleterm",
      "TelescopePrompt",
      "DressingInput",
      "DressingSelect",
    }

    if vim.tbl_contains(winbar_filetype_exclude, vim.bo.filetype) then
      vim.opt_local.winbar = nil
      return
    end

    local value = require("winbar").gps()

    if value == nil then
      value = require("winbar").filename()
    end

    vim.opt_local.winbar = value
  end,
})

I also realized that if the error shows, sometimes "Code actions" are showing and when I click enter everything seems to work fine. Just error is showing each time:

image

Then press enter and it shows correctly: image

Same with vim.ui.input.

Does anyone else have this issue when using winbar with nui prompt?

stevearc commented 2 years ago

Without a repro this will be difficult to address. I just pushed up a commit that exposes more of the underlying config options for nui, and additionally sets the filetype by default to be DressingSelect (previously it was empty, which you can verify with :set filetype?). Hopefully that should be enough to get your filetype exclusion working.

Some other exclusions you may want to use are

-- Inside a floating window
vim.api.nvim_win_get_config(0).relative ~= ""
-- Inside an unnamed buffer
vim.api.nvim_buf_get_name(0) == ""
-- Inside a non-normal buffer (e.g. quickfix, terminal, etc)
vim.api.nvim_buf_get_option(0, 'buftype') ~= ""
ecosse3 commented 2 years ago

Unfortunately now I have different issue with Select (code actions) and it breaks even without winbar implementation.

image

vim.ui.input with nui works fine without winbar. Still same issue when using winbar. I've checked and vim.bo.filetype is "DressingSelect" as expected now, but still breaks.

My config can be installed via https://github.com/ecosse3/nvim but unfortunately I'm not able to setup Docker (Alpine Linux) with Neovim Nightly for testing (just 0.7 is available).

stevearc commented 2 years ago

Apologies, there was a small bug with my last commit. The crash should not happen anymore, and you should be able to test if the winbar logic works now.

ecosse3 commented 2 years ago

@stevearc Thanks. Now NUI select works with code actions. Unfortunately, it has more height than expected:

image

With vim.ui.input I'm still experiencing the same issue. Error, and then after it works fine:

image

stevearc commented 2 years ago

You can adjust the height by changing the min_height value in the nui config area.

For the vim.ui.input error, you're just going to have to change how you set the winbar. Right now you're doing it on CursorMoved, so it runs as soon as the input float window is opened, and critically, before the filetype is set. This is why I recommended using some of those other exclusion rules, like unnamed buffers or floating windows. Probably the safest thing would be to check the height of the window before setting it. Or you could wrap the call to nvim_buf_set_option with pcall to silence the error.

ecosse3 commented 2 years ago

@stevearc Thanks, everything works as expected now. Checking if window is floating helped fixing this error.