neovim cmp intergration for props and emits #4495
RayGuo-ergou
started this conversation in
General
Replies: 3 comments 3 replies
-
There's an -- To see more information :h cmp-config.sources
sources = cmp.config.sources({
{
name = 'nvim_lsp',
---@param entry cmp.Entry
---@param ctx cmp.Context
entry_filter = function(entry, ctx)
-- Check if the buffer type is 'vue'
if ctx.filetype ~= 'vue' then
return true
end
local cursor_before_line = ctx.cursor_before_line
-- For events
if cursor_before_line:sub(-1) == '@' then
return entry.completion_item.label:match('^@')
-- For props also exclude events with `:on-` prefix
elseif cursor_before_line:sub(-1) == ':' then
return entry.completion_item.label:match('^:') and not entry.completion_item.label:match('^:on%-')
else
return true
end
end -- <--- HERE
},
}) |
Beta Was this translation helpful? Give feedback.
1 reply
-
I found that local function is_in_start_tag()
local ts_utils = require('nvim-treesitter.ts_utils')
local node = ts_utils.get_node_at_cursor()
if not node then
return false
end
local node_to_check = { 'start_tag', 'self_closing_tag', 'directive_attribute' }
return vim.tbl_contains(node_to_check, node:type())
end |
Beta Was this translation helpful? Give feedback.
0 replies
-
It seems that after the latest updates, it no longer works. @RayGuo-ergou can you confirm? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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