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

Swap operands

Open
#313 4 comments 4 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.
imagine you write out something like this

df["column_1"] - df["long_column_name"].a_couple().of_transformations()

But you realize that you actually want to swap these.

you've gotten okay at vim motions, so you F-D0P, but then realize you've still got a ways to go:

- df["long_column_name"].a_couple().of_transformations()df["column_1"] 

Thankfully your cursor's in a convenient place, so you can easily add a minus sign in the middle:

- df["long_column_name"].a_couple().of_transformations()-df["column_1"] 

You've still got to 0dw to clear up the leading - . Finally, you're in a state where your format-on-save hooks will clean things up:

df["long_column_name"].a_couple().of_transformations()-df["column_1"] 

Describe the solution you'd like
I'd like to be able to add an @operand mapping to my swap configuration, e.g.:

		swap = {
			enable = true,
			swap_next = {
				["<leader>o"] = "@operand",
			},
			swap_previous = {
				["<leader>O"] = "@operand",
			},
		},

Describe alternatives you've considered

I guess there might be a swapping-specific plugin that I could look for. If it doesn't exist, I suppose I could try to implement it, although I'd rather try to implement it in this project if it's possible (I don't know what's involved in creating a textobject for treesitter)

Additional context
nil

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 tracing the existing swap configuration and the Tree-sitter textobject implementation mentioned in the issue. Determine how an @operand mapping would be registered for swap_next and swap_previous. Done means operand expressions can be swapped through the shown Lua mappings without the extra manual cleanup described.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, neovim
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.