Giter Club home page Giter Club logo

Comments (19)

altermo avatar altermo commented on June 26, 2024

Currently no; the treesitter node lifetime is only created after the character is inserted. I'll look into implementing a temporary char insert to get the treesitter node in the next version of the plugin.

from ultimate-autopair.nvim.

mawkler avatar mawkler commented on June 26, 2024

I see. Thank you!

You seem to be correct in that Treesiter doesn't recognize the lifetime node until it's been created. However, it does also recognize the invalid lifetime with the extra apostrophe (<'a>') as a lifetime, but followed by an ERROR node as well. Could a fix be to detect if a lifetime node has just been typed that's followed by an apostrophe and an ERROR node, and to then remove the trailing apostrophe?

from ultimate-autopair.nvim.

altermo avatar altermo commented on June 26, 2024

No, the way the plugin is currently set up, it is not possible to do anything after the initial insertion.
However one could create an external program.
Here's an example of how to do this:

vim.api.nvim_create_autocmd('InsertCharPre',{callback=function ()
    local col=vim.fn.col'.'-1
    local line=vim.api.nvim_get_current_line()
    if vim.v.char~="'" then return end
    if line:sub(col,col)~="'" then return end
    local parser=vim.treesitter.get_parser()
    parser:parse()
    local node=vim.treesitter.get_node()
    if not node then return end
    local parent=node:parent()
    if not parent then return end
    if parent:type()~='lifetime' then return end
    vim.v.char=''
    vim.api.nvim_input('<Right>')
end})
require'ultimate-autopair'.setup{
    --your config
    config_internal_pairs={
        {"'","'",cond=function (fn)
            return not fn.in_node('lifetime')
        end}
    }
}

from ultimate-autopair.nvim.

mawkler avatar mawkler commented on June 26, 2024

Ok, thanks!

from ultimate-autopair.nvim.

svanharmelen avatar svanharmelen commented on June 26, 2024

Hey 👋🏻 I'm trying to switch from nvim-autopairs to UA and it seems to go pretty smooth except for 2 issues, one of them being this exact question 😏

I previously solved this is using the following config:

-- Define locals
local npairs   = require('nvim-autopairs')
local ts_conds = require('nvim-autopairs.ts-conds')

-- Configure Nvim-Autopairs
npairs.setup({
  check_ts = true,
  ts_config = {
    go = {},
    rust = {},
  }
})

-- Add custom rule for Rust lifetime params
npairs.get_rule("'")[2]:with_pair(ts_conds.is_not_ts_node({ 'type_arguments', 'bounded_type' }))

But I'm not sure how to port that to a condition in UA. I tried this, but that didn't work:

  {
    'altermo/ultimate-autopair.nvim',
    branch = 'v0.6',
    event = { 'InsertEnter', 'CmdlineEnter' },
    config = function()
      require('ultimate-autopair').setup {
        config_internal_pairs = {
          { "'", "'", multiline = false, suround = true, cond = function(fn) 
            return not fn.in_node { 'bounded_type', 'type_arguments' }
          end}
        }
      }
    end
  },

Does this additional info help? Or do you still don't see a good way to fix this in UA?

Thanks!

from ultimate-autopair.nvim.

altermo avatar altermo commented on June 26, 2024

Your UA config worked for me with this minimal config:

Click text to open and show minimal config

Open this file with nvim --clean and run :source (it will hang while installing everything).

for _,url in ipairs{'altermo/ultimate-autopair.nvim','nvim-treesitter/nvim-treesitter'} do
    local install_path=vim.fn.fnamemodify(url:gsub('.*/',''),':p')
    if vim.fn.isdirectory(install_path)==0 then
        vim.fn.system{'git','clone','https://github.com/'..url,install_path}
    end
    vim.opt.runtimepath:append(install_path)
end
require'nvim-treesitter.configs'.setup{
  ensure_installed = {'rust'},
  sync_install = true,
}
require('ultimate-autopair').setup {
    config_internal_pairs = {
        { "'", "'", multiline = false, suround = true, cond = function(fn)
            return not fn.in_node { 'bounded_type', 'type_arguments' }
        end}
    }
}
vim.cmd.vnew()
vim.o.buftype='nofile'
vim.fn.setline(1,'let a: Vec<a>;')
vim.fn.setline(2,'//>>> PRESS THE \' KEY <<<')
vim.cmd.setf'rust'
vim.fn.cursor(1,12)
vim.api.nvim_input('i')
vim.o.cmdheight=2

Can you diagnose what may be causing it to not work for you?


Also, I already implemented a feature in the next version that makes this possible, but that won't be released for a long time.

from ultimate-autopair.nvim.

svanharmelen avatar svanharmelen commented on June 26, 2024

Thanks for your reaction @altermo! I tried your script and it indeed worked. But it feels finicky. For example, try this one:

require 'nvim-treesitter.configs'.setup {
  ensure_installed = { 'rust' },
  sync_install = true,
}
require('ultimate-autopair').setup {
  { '<', '>', fly = true, dosuround = true, multiline = false, space = true, surround = true },

  config_internal_pairs = {
    { "'", "'", multiline = false, suround = true,
      cond = function(fn) return not fn.in_node { 'bounded_type', 'type_arguments' } end,
    }
  },
}
vim.cmd.vnew()
vim.o.buftype = 'nofile'
vim.fn.setline(1, 'struct A<a> {')
vim.fn.setline(2, '    a: String<a>,')
vim.fn.setline(3, '}')
vim.fn.setline(4, '//>>> PRESS THE \' KEY <<<')
vim.cmd.setf 'rust'
vim.fn.cursor(1, 10)
vim.api.nvim_input('i')
vim.o.cmdheight = 2

I fails when inserting the ' on the first line, but if you move down to the second line, it does work for that one 🤷🏻

from ultimate-autopair.nvim.

altermo avatar altermo commented on June 26, 2024

That's because the failing node's type is type_parameters (rather than type_arguments), so just add it to the list of nodes to make it work.
Generally, if it fails somewhere, use :InspectTree to get the node and add it to the list of nodes.

from ultimate-autopair.nvim.

svanharmelen avatar svanharmelen commented on June 26, 2024

That indeed works, thanks! And thanks for the :InspectTree tip!!! That's a real nice one to know 👍🏻

from ultimate-autopair.nvim.

mawkler avatar mawkler commented on June 26, 2024

Thank you, that works for me as well!

require('ultimate-autopair').setup({
  { '<', '>', fly = true, dosuround = true, multiline = false, space = true, surround = true },
  config_internal_pairs = {
    { "'", "'", multiline = false, surround = true, cond = function(fn)
        return not fn.in_node({ 'bounded_type', 'type_parameters' })
      end,
    }
  },
})

Perhaps this should be added to the README or the Wiki?

from ultimate-autopair.nvim.

svanharmelen avatar svanharmelen commented on June 26, 2024

I actually added 'bounded_type', 'reference_type', 'type_arguments', 'type_parameters' to make it work in other places I encountered it as well...

from ultimate-autopair.nvim.

svanharmelen avatar svanharmelen commented on June 26, 2024

But its still not perfect... Guess I'll disable this one (completion of ') for Rust.

from ultimate-autopair.nvim.

mawkler avatar mawkler commented on June 26, 2024

@svanharmelen Hmm ok. Is it possible to enable that rule for only rust filetypes?

from ultimate-autopair.nvim.

altermo avatar altermo commented on June 26, 2024

cond takes a function so early-return true if the file type is rust. (fn.get_ft() is treesitter-injected-lang aware)
The config would be:

config_internal_pairs = {
  { "'", "'", multiline = false, surround = true, cond = function(fn)
      if fn.get_ft() ~= 'rust' then return true end
      return not fn.in_node({ 'bounded_type', 'reference_type', 'type_arguments', 'type_parameters' })
    end,
  },
},

from ultimate-autopair.nvim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.