You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi neovim users, I want to introduce a nvim-cmp integration I am using for props and emits inspired by this post.
Basically changes:
to:
Pass this into your `nvim-cmp` setup opts:
-- To see more information :h cmp-config.sourcessources=cmp.config.sources({
{
name='nvim_lsp',
---@paramentrycmp.Entry---@paramctxcmp.Contextentry_filter=function(entry, ctx)
-- Check if the buffer type is 'vue'ifctx.filetype~='vue' thenreturntrueendlocalcursor_before_line=ctx.cursor_before_line-- For eventsifcursor_before_line:sub(-1) =='@' thenreturnentry.completion_item.label:match('^@')
-- For props also exclude events with `:on-` prefixelseifcursor_before_line:sub(-1) ==':' thenreturnentry.completion_item.label:match('^:') andnotentry.completion_item.label:match('^:on%-')
elsereturntrueendend
},
})
The regex performance is quite good since it only checks the beginning characters. However, this approach can still lead to issues such as a string union.
For example, with this TypeScript type:
typeEmailAddresses='a@b.c'|'d@e.f'
You will lose the items in nvim-cmp if you only type @. To address this, you can add Treesitter to check if the cursor is in a starting tag. ( I am not a fan of complex regex )
To use a local buffer variable to cache the result of the Treesitter is recommended to avoid performance issues as the function will call for every single entry. Here is an example of how to do it:
-- check if in start taglocalfunctionis_in_start_tag()
localts_utils=require('nvim-treesitter.ts_utils')
localnode=ts_utils.get_node_at_cursor()
ifnotnodethenreturnfalseendlocalnode_to_check= { 'start_tag', 'self_closing_tag', 'directive_attribute' }
returnvim.tbl_contains(node_to_check, node:type())
endentry_filter=function(entry, ctx)
-- Use a buffer-local variable to cache the result of the Treesitter checklocalbufnr=ctx.bufnrlocalcached_is_in_start_tag=vim.b[bufnr]._vue_ts_cached_is_in_start_tagifcached_is_in_start_tag==nilthenvim.b[bufnr]._vue_ts_cached_is_in_start_tag=is_in_start_tag()
end-- If not in start tag, return trueifvim.b[bufnr]._vue_ts_cached_is_in_start_tag==falsethenreturntrueend-- rest of the codeend
In your cmp configuration, register an event to clear the cache when the menu is closed:
-- To see more information :h cmp-config.sourcessources=cmp.config.sources({
{
name='nvim_lsp',
---@paramentrycmp.Entry---@paramctxcmp.Contextentry_filter=function(entry, ctx)
-- Check if the buffer type is 'vue'ifctx.filetype~='vue' thenreturntrueendlocalcursor_before_line=ctx.cursor_before_line-- For eventsifcursor_before_line:sub(-1) =='@' thenreturnentry.completion_item.label:match('^@')
-- For props also exclude events with `:on-` prefixelseifcursor_before_line:sub(-1) ==':' thenreturnentry.completion_item.label:match('^:') andnotentry.completion_item.label:match('^:on%-')
elsereturntrueendend-- <--- HERE
},
})
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi neovim users, I want to introduce a
nvim-cmp
integration I am using for props and emits inspired by this post.Basically changes:
to:
Pass this into your `nvim-cmp` setup opts:
The regex performance is quite good since it only checks the beginning characters. However, this approach can still lead to issues such as a string union.
For example, with this TypeScript type:
You will lose the items in nvim-cmp if you only type
@
. To address this, you can add Treesitter to check if the cursor is in a starting tag. ( I am not a fan of complex regex )To use a local buffer variable to cache the result of the Treesitter is recommended to avoid performance issues as the function will call for every single entry. Here is an example of how to do it:
In your cmp configuration, register an event to clear the cache when the menu is closed:
Beta Was this translation helpful? Give feedback.
All reactions