A plugin that allows you to paste code as snippet, based on treesitter queries which identify parts of the code that should be replaced with tabstops.
Requires nvim-0.10+
git clone \
https://github.com/mfussenegger/nvim-snippasta.git \
~/.config/nvim/pack/plugins/start/nvim-snippasta
The plugin exposes a single function which you can call via keymap
:
keymap.set({"n"}, "<leader>p", function() require("snippasta").paste() end)
The function tries to translate the text present in the v:register
register
into a snippet by replacing parts of it with tabstops and expand it using
vim.snippet.expand
.
To identify which parts of the text are converted to tabstops it uses
tree-sitter queries containing @tabstop
captures.
The queries themselves are not part of the plugin. You have to add them yourself. They're likely opinionated and I don't have the bandwidth to maintain a collection of them for each language.
To add the queries, create query files named tabstop.scm
in the query folder.
(~/.config/nvim/queries/<language>
). Read :help treesitter-query
for more
information.
There is a fallback to use a highlights.scm
in case you have no tabstop.scm
file for a given language. In this case it will use the @string
, @number
,
@boolean
and @variable.parameter
captures as potential tabstop.
Here are a few examples to give you some inspiration.
You can use :InspectTree
and :EditQuery
to help you write your own queries.
(variable_declaration
(assignment_statement
(variable_list
name: (identifier) @tabstop)
))
(table_constructor
(field
name: _
value: _ @tabstop))
(function_call
name: _
arguments: (arguments (_) @tabstop))
(assignment
left: (identifier) @tabstop)
(call
arguments: (argument_list [
(keyword_argument
value: (_) @tabstop)
(string) @tabstop
(integer) @tabstop
(true) @tabstop
(false) @tabstop
]))
(content
(CharData) @tabstop)