nvim-treesitter / nvim-treesitter/nvim-treesitter-textobjects

Feature Request: Duplicating Nodes

Open
#780 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Tree-sitter Query
Stars
2.8k
Forks
271
Avg merge
8d 8h
Merged PRs (30d)
1

Description

Is your feature request related to a problem? Please describe.

This library answers a huge felt need wanting to work with elements of the syntax by selecting them, deleting, swapping them around, etc.

But one itch that I find is still unscratched is the ability to duplicate nodes.

I often find I am working with an array of objects in TypeScript (which would be treated as @parameter) and I want to quickly make a duplicate of a node and then modify it. For example, I might have an array like this:

const people = [
    {
        name: "Bill Smith",
        age: 25,
    },
    {
        name: "Frank Jones",
        age: 30,
    },
];

And I want to duplicate Frank Jones' record. To do that I can easily select and/or yank the record, and then paste it after. The problem with this is that I have to do a little dancing around the trailing comma and it takes a few steps. The same thing goes for inline arrays of function parameters etc.

https://github.com/user-attachments/assets/5fbc0872-672e-4adf-90fc-9039e74cc055

Of course there's other options to do this like using V to do line-wise visual mode etc, but it always requires a bit of thinking and a few keystrokes to see how to duplicate nodes in different situations.

Describe the solution you'd like

What I would love to do would be have an action like move or swap that would allow me to use a keybinding to quickly duplicate something like a @parameter or a @statement. The same could be done for any nodes that are in an array on the tree.

lua <<EOF
require'nvim-treesitter.configs'.setup {
  textobjects = {
    copy = {
      enable = true,
      copy_next = {
        ["<leader>c"] = "@parameter.inner",
      },
    },
  },
}
EOF

In a way this would be similar to how many editors allow you to quickly duplicate files and then make adjustments. It would be so nice to be able to just quickly duplicate a @function, a @statement, or a @parameter in our syntax tree and then tweak it, rather than worry about yanking and pasting.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing textobject actions referenced in the request, especially move and swap, along with the textobjects configuration entry point. Define how a copy action should behave for nodes such as @parameter, @statement, and @function, including nodes in arrays. Done means a keybinding can duplicate these nodes without manual yank-and-paste adjustments.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, typescript
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.