-
Notifications
You must be signed in to change notification settings - Fork 5
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
Feature request: Rust lifetime syntax support #68
Comments
Currently no; the treesitter node |
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 ( |
No, the way the plugin is currently set up, it is not possible to do anything after the initial insertion. 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}
}
} |
Ok, thanks! |
Hey 👋🏻 I'm trying to switch from I previously solved this is using the following config:
But I'm not sure how to port that to a condition in UA. I tried this, but that didn't work:
Does this additional info help? Or do you still don't see a good way to fix this in UA? Thanks! |
Your UA config worked for me with this minimal config: Click text to open and show minimal configOpen this file with 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?
|
Thanks for your reaction @altermo! I tried your script and it indeed worked. But it feels finicky. For example, try this one:
I fails when inserting the |
That's because the failing node's type is |
That indeed works, thanks! And thanks for the |
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? |
I actually added |
But its still not perfect... Guess I'll disable this one (completion of |
@svanharmelen Hmm ok. Is it possible to enable that rule for only rust filetypes? |
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,
},
}, |
Why not disable |
@altermo I see that you disabled Rust completely in 035d92e. How come you didn't go with your solution above instead? It has been working great for me. |
I think it's a good default to have this one disabled for Rust. While it maybe configurable to some extend to work as one would like, it remains a somewhat flaky and incomplete experience for me. |
Edit: never mind, the fix still works |
@mawkler I don't and I totally don't miss it to be honest. Of course you can always "undo" the default and enable it for Rust if you want by adding this to you config: config_internal_pairs = {
{ "'", "'", alpha = true, multiline=false, nft = { 'tex' }, suround = true,
cond = function(fn) return not fn.in_lisp() or fn.in_string() end
}
}, |
Hi! Thanks for making this awesome plugin!
This is a tricky one, but I would love it if this plugin handled creating Rust lifetime syntax. They can look like this.
Currently when I type this with this plugin I get the following, since the apostrophe gets paired:
In Rust you also use apostrophes (
'
) to typechar
s, which are different from strings which are typed with double quotes ("
), so simply disabling ultimate-autopairs would be a shame because the pairing is useful when typingchar
s.Would it be possible to use Treesitter to determine if you're typing a lifetime or if you're typing a
char
?The text was updated successfully, but these errors were encountered: