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

How to disable in comments? #31

Closed
windowsrefund opened this issue Jun 11, 2023 · 11 comments
Closed

How to disable in comments? #31

windowsrefund opened this issue Jun 11, 2023 · 11 comments

Comments

@windowsrefund
Copy link

Is this even possible?

Thanks in advance

@altermo
Copy link
Owner

altermo commented Jun 11, 2023

First, update to the latest release.
Second:

require'ultimate-autopair'.setup({
  extensions={
    rules={rules={{'call',function(o)
      return o.col==1 or vim.treesitter.get_node({pos={o.linenr-1,o.col-2}}):type()~='comment'
    end
    }}}
  }
})

@windowsrefund
Copy link
Author

Thank you. I just updated to c17f0731db05f2a733b382718b91e0873683ed1b

stack traceback:
	...y/ultimate-autopair.nvim/lua/ultimate-autopair/debug.lua:104: in function <...y/ultimate-autopair.nvim/lua/ultimate-autopair/debug.lua:99>
	[C]: in function 'error'
	...s/neovim/0.9.0/share/nvim/runtime/lua/vim/treesitter.lua:121: in function 'get_parser'
	...s/neovim/0.9.0/share/nvim/runtime/lua/vim/treesitter.lua:398: in function 'get_node'
	...e/akosmin/.config/nvim/lua/plugins/ultimate-autopair.lua:15: in function 'check_rule'
	...autopair.nvim/lua/ultimate-autopair/extensions/rules.lua:54: in function 'check_rules'
	...autopair.nvim/lua/ultimate-autopair/extensions/rules.lua:73: in function <...autopair.nvim/lua/ultimate-autopair/extensions/rules.lua:69>
	[C]: in function 'xpcall'
	...y/ultimate-autopair.nvim/lua/ultimate-autopair/debug.lua:121: in function <...y/ultimate-autopair.nvim/lua/ultimate-autopair/debug.lua:120>
	...zy/ultimate-autopair.nvim/lua/ultimate-autopair/core.lua:40: in function <...zy/ultimate-autopair.nvim/lua/ultimate-autopair/core.lua:32>

Here's my config

  {
    'altermo/ultimate-autopair.nvim',
    enabled = true,

    event = { 'InsertEnter', 'CmdlineEnter' },
    config = function()
      require('ultimate-autopair').setup({
        -- Disable autopair in the command line: https://github.com/altermo/ultimate-autopair.nvim/issues/8
        cmap = false,
        extensions = {
          rules = {
            rules = { { 'call', function(o)
              return o.col == 1 or vim.treesitter.get_node({ pos = { o.linenr - 1, o.col - 2 } }):type() ~= 'comment'
            end
            } }
          },
          filetype = {
            p = 80,
            nft = {
              'TelescopePrompt',
              'text',
            },
          },
          -- Get Fly Mode working on strings: https://github.com/altermo/ultimate-autopair.nvim/issues/17
          fly = {
            nofilter = true,
          },
        },
        { '"', '"', fly = true, p = 11 },
        { "'", "'", fly = true, p = 11 },
      })
    end
  },
}

@altermo
Copy link
Owner

altermo commented Jun 12, 2023

Oh sorry, I forgot to check if tressitter parser is attached to the buffer, or if in cmd line mode.
In that end use:

require'ultimate-autopair'.setup({
  extensions={
    rules={rules={{'call',function(o)
      return o.incmd
        or o.col==1
        or not pcall(vim.treesitter.get_node,{pos={o.linenr-1,o.col-2}})
        or vim.treesitter.get_node({pos={o.linenr-1,o.col-2}}):type()~='comment'
    end
    }}}
  }
})

@windowsrefund
Copy link
Author

windowsrefund commented Jun 12, 2023

Surprisingly, the first one works as expected when inside a comment. However, when outside of a comment quotes (single and double) are not pairing.

The 2nd solution is just ignored from within a comment but otherwise, does not break usual behavior/expectations outside of a comment.

@altermo
Copy link
Owner

altermo commented Jun 12, 2023

So, is your problem solved?
If not, pleas describe in more detail what you want.

@windowsrefund
Copy link
Author

I thought my last message made it perfectly clear neither solution solves the problem. With all due respect, are you testing this as you go?

@altermo
Copy link
Owner

altermo commented Jun 12, 2023

Can you please describe in more detail what you want?

@windowsrefund
Copy link
Author

Did you read the topic? I don't understand how it's possible that this question is coming up now.

@altermo
Copy link
Owner

altermo commented Jun 12, 2023

That's what I don't understand, the example 2 I gave disables auto-pairing (and all other features) when inserting inside comments.

@windowsrefund
Copy link
Author

windowsrefund commented Jun 12, 2023

Interesting. It's working like a charm now and I'm not seeing the results I posted last night. Looks like we've got a winner here. Thanks again for providing this. This, along with being able to 'jump', are my favorite features of this autopair solution.

@windowsrefund
Copy link
Author

Just wanted to thank you again and explain something I just picked up on which helps me to now understand why I wasn't seeing consistent results.

Scenario 1: I am on any line in a .lua file and type o. On the new line, I type -- to begin a comment and then type any character that would normally autopair.

Scenario 2: I am on any line in a .lua file and type gcoto get dropped into a new line that is already commented (This is a mapping provided by Comment.nvim). In the now already commented line, I type any character that would normally autopair.

Scenario 3: In an already commented line, I type any character that would normally autopair.

Results:
Scenario 1: Fail, the character autopairs.

Scenario 2: Pass, the character does not autopair

Scenario 3: Pass, the character does not autopair

I suppose this has to do with deeper level treesitter stuff I don't quite grok and the solution provided is more than acceptable since I do try to make use of gco and gcO when I know I want to add comments. Just wanted to leave this here for anyone who goes down this particular road and may be confused about the results.

All the best.

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

No branches or pull requests

2 participants