JoosepAlviste / nvim-ts-context-commentstring

Neovim treesitter plugin for setting the commentstring based on the cursor location in a file.
MIT License
1.13k stars 34 forks source link

plugin cause j/k position incorrect #73

Open defpis opened 1 year ago

defpis commented 1 year ago

Current behavior: 屏幕录制2023-08-21 17 51 23

Desired behavior: 屏幕录制2023-08-21 17 50 51

JoosepAlviste commented 11 months ago

Hey @defpis! I can't quite reproduce this:

https://github.com/JoosepAlviste/nvim-ts-context-commentstring/assets/9450943/b8d4e047-8047-42b9-93f0-e48af909755c

What exact keys are you pressing for the examples?

Could you create a minimal reproducible config using the minimal config? https://github.com/JoosepAlviste/nvim-ts-context-commentstring/blob/9bff161dfece6ecf3459e6e46ca42e49f9ed939f/utils/minimal_init.lua

defpis commented 11 months ago

Thanks for taking the time to check out the problem, any help would be appreciated. There is minimal config:

local options = { -- :help options
  backup = false, -- Don't create a backup file
  clipboard = 'unnamedplus', -- Allow NeoVim to access system clipboard
  conceallevel = 0, -- Make `` visible in Markdown files
  fileencoding = 'utf-8', -- Use `utf-8` as encoding
  hlsearch = true, -- Highlight all matches on previous search pattern
  ignorecase = true, -- Ignore case in search patterns
  mouse = 'a', -- Allow the mouse to be used in NeoVim
  showmode = false, -- Hide NeoVim modes in the status bar
  showtabline = 2, -- Always show tabs
  smartcase = true, -- Ignore `ignorecase` if search pattern contains upper case characters
  smartindent = true, -- Make indenting smarter on newline
  splitbelow = true, -- Force all horizontal splits to go below current window
  splitright = true, -- Force all vertical splits to go to the right of current window
  swapfile = false, -- Don't create a swapfile
  termguicolors = true, -- Set term gui colors
  timeoutlen = 1000, -- Time to wait for a mapped sequence to complete (in milliseconds)
  updatetime = 300, -- Faster completion (4000ms default)
  writebackup = false, -- If a file is being edited by another program (or was written to file while editing with another program), it is not allowed to be edited
  expandtab = true, -- Convert tabs to spaces
  shiftwidth = 2, -- The number of spaces inserted for each indentation
  tabstop = 2, -- Insert 2 spaces for a tab
  cursorline = true, -- Highlight the current line
  cursorlineopt = 'number', -- Hightlight the current line number
  number = true, -- Set numbered lines
  relativenumber = true, -- Set relative numbered lines
  signcolumn = 'yes', -- Always show the sign column, otherwise it would shift the text each time
  wrap = false, -- Display long lines as one
  scrolloff = 8, -- Minimal number of screen lines to keep above and below the cursor
  sidescrolloff = 8, -- Minimal number of screen columns to keep left and right the cursor
  cmdheight = 1, -- More space in the neovim command line for displaying messages
  pumheight = 10, -- Make popup menu smaller
  background = 'dark', -- Tell NeoVim what the background color is
  laststatus = 3, -- Use global statusline
}

for key, value in pairs(options) do
  vim.opt[key] = value
end

local lazy_path = vim.fn.stdpath('data') .. '/lazy/lazy.nvim'

if not vim.loop.fs_stat(lazy_path) then
  vim.fn.system({
    'git',
    'clone',
    '--filter=blob:none',
    'https://github.com/folke/lazy.nvim.git',
    '--branch=stable',
    lazy_path,
  })
end

vim.opt.rtp:prepend(lazy_path)

require('lazy').setup({
  {
    'gbprod/yanky.nvim',
    config = function()
      require('yanky').setup({
        preserve_cursor_position = { enabled = true },
        highlight = { on_put = false, on_yank = false },
      })
    end,
  },
  {
    'phaazon/hop.nvim',
    branch = 'v2',
    config = function()
      require('hop').setup({ keys = 'etovxqpdygfblzhckisuran' })
    end,
  },
  -- {
  --   'numToStr/Comment.nvim',
  --   config = function()
  --     require('Comment').setup()

  --     -- local ft = require('Comment.ft')
  --     -- ft.set('json', { '// %s', '/* %s */' })
  --   end,
  -- },
  {
    'kylechui/nvim-surround',
    config = function()
      require('nvim-surround').setup()
    end,
  },
  { 'nvim-treesitter/nvim-treesitter-textobjects', dependencies = { 'nvim-treesitter' } },
  {
    'nvim-treesitter/nvim-treesitter',
    dependencies = { 'JoosepAlviste/nvim-ts-context-commentstring' }, -- FIXME: May cause j/k position incorrect
    config = function()
      require('nvim-treesitter.configs').setup({
        ensure_installed = {
          'lua',
          'typescript',
          'tsx',
          'javascript',
          'html',
          'css',
          'scss',
          'json',
          'jsonc',
          'yaml',
          'markdown',
        },
        incremental_selection = {
          enable = true,
          keymaps = {
            init_selection = '<Leader><CR>',
            node_incremental = '<CR>',
            node_decremental = '<BS>',
            scope_incremental = '<Leader><CR>',
          },
        },
        textobjects = {
          select = {
            enable = true,
            lookahead = true,
            keymaps = {
              ['af'] = '@function.outer',
              ['if'] = '@function.inner',
              ['aa'] = '@parameter.outer',
              ['ia'] = '@parameter.inner',
            },
          },
        },
        context_commentstring = { enable = true },
      })
    end,
  },
})

both terminal and vscode cursor can't move correct position.

JoosepAlviste commented 11 months ago

Thanks for the minimal config!

I tried it out, but still can't reproduce the issue:

https://github.com/JoosepAlviste/nvim-ts-context-commentstring/assets/9450943/c0eb3a47-9009-4532-8059-6445143612ff

To be honest, I don't have any ideas as to what might be going wrong 😕 Maybe we could get someone else to try to reproduce it?

I don't think that this plugin should affect j/k in any way as it just sets up an autocommand on CursorHold and that's it. Maybe there's something outside of Vim that's affecting it somehow?

defpis commented 11 months ago

Thank you. There may be a problem with my computer environment. I try to find the reason by myself, I close this issue.

expipiplus1 commented 10 months ago

Can we reopen this please?

This is sensitive to updatetime. I can reproduce this with updatetime=100 and holding on an intermediate line for more than 100ms.

FWIW, disabling the CursorHold autocmd fixes it, but this is obviously undesireable (autocmd! context_commentstring_ft CursorHold)

JoosepAlviste commented 9 months ago

I was able to reproduce this now, when using the CursorHold autocommand. It looks like setting an option inside CursorHold is causing the issue, e.g., here:

https://github.com/JoosepAlviste/nvim-ts-context-commentstring/blob/ead792ccdace6a9d64a8119909af243ed0105465/lua/ts_context_commentstring/internal.lua#L89

I did a quick search, but couldn't find any other issues or questions about this behaviour, so I unfortunately don't have a good solution for this yet.

I can recommend using a commenting plugin integration, though, and disabling the autocommand like described in the integrations wiki page: https://github.com/JoosepAlviste/nvim-ts-context-commentstring/wiki/Integrations