gen740 / SmoothCursor.nvim

MIT License
342 stars 13 forks source link

It seems that `disabled_filetypes` is not working with lazy.nvim #21

Closed nyngwang closed 1 year ago

nyngwang commented 1 year ago

Hi, am still enjoying this plugin :) But here is a little problem, I have the following config:

use {
  'gen740/SmoothCursor.nvim',
  config = function()
    require('smoothcursor').setup {
      fancy = {
        enable = true,
      },
      threshold = 1,
      enabled_filetypes = nil,
      disabled_filetypes = { 'fzf', 'neo-tree', 'lazy' }, -- Here I've added `'lazy'`,
                                                          -- which is the filetype of the floating
                                                          -- menu provided by lazy.nvim.
    }
    vim.keymap.set('n', '<Leader>ac', function () vim.cmd('SmoothCursorToggle') end, NOREF_NOERR_TRUNC)
  end
}

but it seems that I still get the beautiful cursor on the floating menu of lazy.nvim:

It has no problem with fzf and neo-tree though, so I think maybe this is due to some tricky conflicts between plugins and/or the "lazy loading" nature(?) of lazy.nvim, not sure...

Is this a big deal? Well... this creates a kinda brutal shaking effect during syncing/updating/etc. with lazy.nvim. When I disabled the cursor manually the effect disappeared.

Thanks for your reading~

https://user-images.githubusercontent.com/24765272/209715367-37cc0b17-1a82-4919-8b4a-026e141b19df.mov

https://user-images.githubusercontent.com/24765272/209716177-876ac4ab-7ba8-45f7-bdb1-27504d447436.mov

gen740 commented 1 year ago

Could you make a minimum reproduction code? There may be something wrong with the filetype detection.

gen740 commented 1 year ago

I guess lazy.nvim open float window with "ft" unset. Thus file detection of this plugin does not work. Currently, this plugin cannot be disabled when "ft=nil".

gen740 commented 1 year ago

I created a PR (#22). Try disabled_filetypes = { 'fzf', 'neo-tree', 'lazy', '' }. This will also disable unnamed filetype.

nyngwang commented 1 year ago

To @gen740:

I guess lazy.nvim open float window with "ft" unset. [...]

I also made the same assumption like you did :) It seems that the filetype is too late to be set. (I have another plugin suave.lua that I needed to deal with some autocmd with quickfix list and I encountered a similar issue.)

I created a PR (https://github.com/gen740/SmoothCursor.nvim/pull/22). [...]

This works! This is indeed an elegant solution for me. I think you can safely close this one. Thank you so much :)

nyngwang commented 1 year ago

@gen740 Sorry for tagging you again! But I just realized that the problem still exists after #22.

But here is the good news: with many hours of testing I found a way to bypass it and accidentally create a beautiful fly-in effect(maybe worth to add it into README):

config = function()
  require('smoothcursor').setup {
    fancy = {
      enable = true,
    },
    threshold = 1,
  }
  -- Always disable the cursor to create fly-in effect.
  -- Use `TermOpen` since `filetype`, `buftype` might haven't been set in `BufEnter`.
  vim.api.nvim_create_autocmd({ 'BufLeave', 'TermOpen' }, {
    group = 'colors.lua',
    pattern = '*',
    callback = function () vim.cmd('SmoothCursorStop') end
  })
  -- Strangely, we can use `BufWinLeave` to detect filetype `lazy`.
  vim.api.nvim_create_autocmd({ 'BufWinLeave' }, {
    group = 'colors.lua',
    pattern = '*',
    callback = function ()
      -- print(vim.bo.filetype)
      if vim.bo.filetype == 'lazy' then vim.cmd('SmoothCursorStop') end
    end
  })
  -- Enable the cursor anyway.
  vim.api.nvim_create_autocmd({ 'BufEnter' }, {
    group = 'colors.lua',
    pattern = '*',
    callback = function () vim.cmd('SmoothCursorStart') end
  })
end

https://user-images.githubusercontent.com/24765272/209850003-fd42f222-eaa9-4f02-8055-5bd7f9267a5d.mov

gen740 commented 1 year ago

TBH, I disabled this fly-in effect intended by switching the buffer which stores cursor position. So you can achieve the same effect by overwriting SmoothCursor autogroup like this.

  vim.api.nvim_create_augroup('SmoothCursor', { clear = true })
  local callback = require('smoothcursor.callback')

  vim.api.nvim_create_autocmd({ 'BufEnter' }, {
    group = 'SmoothCursor',
    callback = function()
      -- callback.switch_buf() -- disable switching buffer
      callback.detect_filetype()
      callback.set_buffer_to_prev_pos()
      callback.sc_callback()
    end,
  })

  vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
    group = 'SmoothCursor',
    callback = function()
      callback.sc_callback()
    end,
  })

  vim.api.nvim_create_autocmd({ 'BufLeave' }, {
    group = 'SmoothCursor',
    callback = function()
      callback.unplace_signs()
    end,
  })

Be sure this code is executed after smoothcurser.setup().

But, I recommend you use the following configuration.

  vim.api.nvim_create_autocmd({ 'BufLeave' }, {
    group = 'colors.lua',
    pattern = '*',
    callback = function()
      require('smoothcursor.callback').buffer_set_all(0)
    end,
  })

this will achieve a nice fly-in effect from the top. Of course, you can set buffer_set_all(vim.fn.line('$')) to make a fly-in effect from the bottom.

nyngwang commented 1 year ago

@gen740 Would you mind providing an option for the fly-in effect(either from top or bottom is OK) for me🙏? While I'm OK with my current setup(and also your recommended way), it would be more clean if I can opt-in this from the setup so I can remove those nvim_create_autocmds I just mentioned.

(I understood that you might be busy recently so you could decide whether or not to accept this feature request. I'm also willing to contribute but I usually found that authors can do things more concisely than I did...)

And thanks for your reading, as always!

gen740 commented 1 year ago

Merged #27.

nyngwang commented 1 year ago

To @gen740:

Hi, sorry again for tagging you on this. Your work on #27 is wonderful and working :) But we would probably need to reopen this issue since I realized that the problem hasn't been fixed with #22: passing an empty string '' to disabled_filetypes = { ... } doesn't solve the problem and it will also disable this plugin on VimStart. It would be great if disabled_filetypes can work with 'lazy'. This is not in a hurry, since the problem can be workaround with the following two autocmds currently:

require('smoothcursor').setup {
  -- ...
  -- disabled_filetypes = { 'fzf', 'neo-tree', 'lazy', '' } -- to enable this plugin on `VimStart`.
}

-- Always enable on `BufEnter`.
vim.api.nvim_create_autocmd({ 'BufEnter' }, {
  pattern = '*',
  callback = function () vim.cmd('SmoothCursorStart') end
})
-- Strangely, we can use `BufWinLeave` to detect filetype `lazy`.
vim.api.nvim_create_autocmd({ 'BufWinLeave' }, {
  pattern = '*',
  callback = function ()
    if vim.bo.filetype == 'lazy' then vim.cmd('SmoothCursorStop') end
  end
})
gen740 commented 1 year ago

I see the problem. Lazy.nvim does not fire any buffer-related autocommand.