Comments (19)
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.
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.
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.
Ok, thanks!
from ultimate-autopair.nvim.
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.
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.
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.
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.
That indeed works, thanks! And thanks for the :InspectTree
tip!!! That's a real nice one to know 👍🏻
from ultimate-autopair.nvim.
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.
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.
But its still not perfect... Guess I'll disable this one (completion of '
) for Rust.
from ultimate-autopair.nvim.
@svanharmelen Hmm ok. Is it possible to enable that rule for only rust filetypes?
from ultimate-autopair.nvim.
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)
- How to map keys myself? HOT 7
- Automatic newline and indentation for template literals in JavaScript HOT 6
- Bug: Autopair doesn't respect comments or strings in d language HOT 20
- Inserts `^]` character instead of whitespace when used with multicursors.nvim HOT 5
- Strange behaviour for > brackets HOT 13
- Fly mode conflict with treesitter HOT 2
- [Bug] Incorrect space-surround behaviour with HTML comments HOT 3
- Bug: Attempt to perform arithmetic on a nil value while editing a Markdown file HOT 2
- [Question] How to ignore a specific parser? HOT 1
- Builtin C-style block comment support HOT 2
- [Question] - JSX Support HOT 4
- alpha extension unexpected behavior HOT 2
- Support for lua `[=[...]=]` string? HOT 4
- [Performance] Holding on space in large markdown files freeze the editor HOT 3
- [Bug] Incorrect pair exiting behavior HOT 1
- [BUG] Wrong behavior when deleting asterisk in makdown files HOT 1
- Healthcheck fails saying parser is missing for 'lua' HOT 4
- [Feature Request] Specify pairs by regular expression HOT 2
- Way to prevent autocomplete if Esc is pressed? HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ultimate-autopair.nvim.