https://github.com/wansmer/binary-swap.nvim
Neovim plugin for swapping operands and operators in binary expressions
https://github.com/wansmer/binary-swap.nvim
editing-support formating neovim neovim-plugin treesitter
Last synced: 4 months ago
JSON representation
Neovim plugin for swapping operands and operators in binary expressions
- Host: GitHub
- URL: https://github.com/wansmer/binary-swap.nvim
- Owner: Wansmer
- License: mit
- Created: 2022-11-12T15:19:18.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-06T10:53:12.000Z (almost 3 years ago)
- Last Synced: 2024-07-31T20:51:38.078Z (almost 2 years ago)
- Topics: editing-support, formating, neovim, neovim-plugin, treesitter
- Language: Lua
- Homepage:
- Size: 12.7 KB
- Stars: 17
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Binary-Swap: swap operands in binary expressions
Small plugin for neovim 0.8+ powered on [treesitter](https://github.com/nvim-treesitter/nvim-treesitter) for swapping operands and operators in binary expressions: `123 > 0` to `0 < 123` and `1 + 2` to `2 + 1`
- Comparison operations;
- Mathematical operations;
https://user-images.githubusercontent.com/46977173/201508787-1b9604a1-1d0a-4feb-86d2-8b5417f4f679.mov
# Installation
With [packer.nvim]():
```lua
use({
'Wansmer/binary-swap.nvim',
setup = function ()
vim.keymap.set('n', 'YOUR PREFER KEYS', function ()
require('binary-swap').swap_operands()
end)
vim.keymap.set('n', 'YOUR PREFER KEYS', function ()
require('binary-swap').swap_operands_with_operator()
end)
end
})
```
Binary-wap doesn't set up keymaps by default and no required additional settings.
There are two methods available outside:
1. `require('binary-swap').swap_operands()` – swap only operands
(e.g., `MAX_VALUE >= getCurrentValue()` will transform to `getCurrentValue() >= MAX_VALUE`, here **operator** `>=` is not changed);
2. `require('binary-swap').swap_operands_with_operator()` – swap operands and operator to opposite if possible. (e.g., `MAX_VALUE >= getCurrentValue()` transforms to `getCurrentValue() <= MAX_VALUE`)
## Note
I considered a few different languages and at each of them node with binary expression node has type `binary_expression`. Plugin searches this type by default because have no reason to add additional options to set up. If in your favorite language, this type is different, feel free to open an issue.