Skip to content

Commit 48ceb42

Browse files
authored
Merge pull request #25 from fedepujol/feature/config
Feature/config
2 parents fde95a2 + ef5bd48 commit 48ceb42

File tree

8 files changed

+182
-79
lines changed

8 files changed

+182
-79
lines changed

README.md

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,68 @@ Gain the power to move lines and blocks!
2020

2121
## :zap: Requirements
2222

23-
This plugin works with NeoVim v0.5 or later.
23+
This plugin works with Neovim v0.5 or later.
2424

2525
## :package: Installation
2626

27-
- [packer.nvim](https://github.com/wbthomason/packer.nvim)
27+
- [lazy](https://github.com/folke/lazy.nvim)
2828

2929
```lua
30-
use 'fedepujol/move.nvim'
30+
{
31+
'fedepujol/move.nvim',
32+
opts = {
33+
--- Config
34+
}
35+
}
3136
```
3237

33-
- [vim-plug](https://github.com/junegunn/vim-plug)
38+
- [paq](https://github.com/savq/paq-nvim)
3439

35-
```vim
36-
Plug 'fedepujol/move.nvim'
40+
```lua
41+
'fedepujol/move.nvim';
3742
```
3843

39-
- [paq](https://github.com/savq/paq-nvim)
44+
## :cog: Configuration
45+
You can use the default's (leaving the setup function empty)
46+
```lua
47+
require('move').setup({})
4048

49+
```
50+
51+
or customizing it
4152
```lua
42-
'fedepujol/move.nvim';
53+
require('move').setup({
54+
line = {
55+
enable = true, -- Enables line movement
56+
indent = true -- Toggles indentation
57+
},
58+
block = {
59+
enable = true, -- Enables block movement
60+
indent = true -- Toggles indentation
61+
},
62+
word = {
63+
enable = true, -- Enables word movement
64+
},
65+
char = {
66+
enable = false -- Enables char movement
67+
}
68+
})
69+
4370
```
71+
:information_source: By default, every option is enabled except char movement.
72+
:warning: Disabling line/block/word/char movements, will not generate the commands.
4473

4574
## :rocket: Usage
4675

4776
The plugin provides the following commands:
4877

49-
| Command | Description | Mode |
50-
| ---------- | ----------------------------------------------------- | ------ |
51-
| MoveLine | Moves a line up or down | Normal |
52-
| MoveHChar | Moves the character under the cursor, left or right | Normal |
53-
| MoveWord | Moves the word under the cursor forwards or backwards | Normal |
54-
| MoveBlock | Moves a selected block of text, up or down | Visual |
55-
| MoveHBlock | Moves a visual area, left or right | Visual |
78+
| Command | Description | Mode |
79+
| ---------- | --------------------------------------------------------- | ------ |
80+
| MoveLine | Moves a line up or down | Normal |
81+
| MoveHChar | Moves the character under the cursor, left or right | Normal |
82+
| MoveWord | Transpose the word under the cursor forwards or backwards | Normal |
83+
| MoveBlock | Moves a selected block of text, up or down | Visual |
84+
| MoveHBlock | Moves a visual area, left or right | Visual |
5685

5786
## :keyboard: Mappings
5887

lua/move.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local Config = require('move.config')
2+
local commands = require('move.commands')
3+
4+
local M = {}
5+
6+
M.setup = function(config)
7+
Config.apply(config)
8+
9+
commands.create_user_commands(Config.default_config)
10+
end
11+
12+
return M

lua/move/commands.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
local move_vert = require('move.core.vert')
2+
local move_hor = require('move.core.horiz')
3+
4+
local M = {}
5+
6+
---Creates User defined commands
7+
---@param cfg MoveConfig
8+
M.create_user_commands = function(cfg)
9+
-- Line Command
10+
if cfg.line.enable then
11+
vim.api.nvim_create_user_command("MoveLine",
12+
function(args)
13+
local dir = tonumber(args.args:gsub("[()]", ""), 10)
14+
move_vert.moveLine(dir, cfg.line.indent)
15+
end, { desc = "Move cursor line", force = true, nargs = 1 }
16+
)
17+
end
18+
19+
-- Block Command
20+
if cfg.block.enable then
21+
vim.api.nvim_create_user_command("MoveBlock",
22+
function(args)
23+
local dir = tonumber(args.args:gsub("[()]", ""), 10)
24+
move_vert.moveBlock(dir, args.line1, args.line2, cfg.block.indent)
25+
end, { desc = "Move visual block", force = true, nargs = 1, range = "%" }
26+
)
27+
end
28+
29+
-- Word Command
30+
if cfg.word.enable then
31+
vim.api.nvim_create_user_command('MoveWord',
32+
function(args)
33+
local dir = tonumber(args.args:gsub("[()]", ""), 10)
34+
move_hor.horzWord(dir)
35+
end, { desc = "Move word forwards or backwards", force = true, nargs = 1 }
36+
)
37+
end
38+
39+
-- Character Commands
40+
if cfg.char.enable then
41+
vim.api.nvim_create_user_command('MoveHChar',
42+
function(args)
43+
local dir = tonumber(args.args:gsub("[()]", ""), 10)
44+
move_hor.horzChar(dir)
45+
end, { desc = "Move the character under the cursor horizontally", force = true, nargs = 1 }
46+
)
47+
48+
vim.api.nvim_create_user_command('MoveHBlock',
49+
function(args)
50+
local dir = tonumber(args.args:gsub("[()]", ""), 10)
51+
move_hor.horzBlock(dir)
52+
end, { desc = "Move visual block horizontally", force = true, nargs = 1, range = "%" }
53+
)
54+
end
55+
end
56+
57+
return M

lua/move/config.lua

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---@class MWordConfig
2+
---@field enable boolean
3+
4+
---@class MLineConfig
5+
---@field enable boolean
6+
---@field indent boolean
7+
8+
---@class MBlockConfig
9+
---@field enable boolean
10+
---@field indent boolean
11+
12+
---@class MCharConfig
13+
---@field enable boolean
14+
15+
---@class MoveConfig
16+
---@field line MLineConfig
17+
---@field block MBlockConfig
18+
---@field word MWordConfig
19+
---@field char MCharConfig
20+
21+
local M = {}
22+
23+
---@class MoveConfig
24+
local default_config = {
25+
line = {
26+
enable = true,
27+
indent = true
28+
},
29+
block = {
30+
enable = true,
31+
indent = true
32+
},
33+
word = {
34+
enable = true,
35+
},
36+
char = {
37+
enable = false
38+
}
39+
}
40+
41+
---@type MoveConfig
42+
M.default_config = setmetatable({}, {
43+
__index = function(_, k)
44+
return default_config[k]
45+
end
46+
})
47+
48+
---@param config MoveConfig
49+
M.apply = function(config)
50+
default_config = vim.tbl_deep_extend('force', default_config, config or {})
51+
end
52+
53+
return M

lua/move/core/vert.lua

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ M.foldLastLine = -1
77

88
---Moves up or down the current cursor-line, mantaining the cursor over the line.
99
---@param dir number Movement direction. One of -1, 1.
10-
M.moveLine = function(dir)
10+
---@param indent boolean Enable auto-indent
11+
M.moveLine = function(dir, indent)
1112
-- Get the last line of current buffer
1213
local last_line = vim.fn.line('$')
1314

@@ -38,15 +39,19 @@ M.moveLine = function(dir)
3839

3940
local amount = utils.calc_indent(target + dir, dir)
4041
utils.swap_line(line, target + dir)
41-
utils.indent(amount, target + dir)
42+
43+
if indent then
44+
utils.indent(amount, target + dir)
45+
end
4246
end
4347
end
4448

4549
---Moves up or down a visual area mantaining the selection.
4650
---@param dir number Movement direction. One of -1, 1.
4751
---@param line1 number Initial line of the selected area.
4852
---@param line2 number End line of the selected area.
49-
M.moveBlock = function(dir, line1, line2)
53+
---@param indent boolean Enable auto-indent
54+
M.moveBlock = function(dir, line1, line2, indent)
5055
local vSRow = line1 or vim.fn.line('v')
5156
local vERow = line2 or vim.api.nvim_win_get_cursor(0)[1]
5257
local last_line = vim.fn.line('$')
@@ -63,11 +68,11 @@ M.moveBlock = function(dir, line1, line2)
6368

6469
-- Edges
6570
if vSRow == 0 and dir < 0 then
66-
vim.api.nvim_exec(':normal! ' .. vSRow .. 'ggV' .. vERow .. 'gg', false)
71+
vim.api.nvim_exec2(':normal! ' .. vSRow .. 'ggV' .. vERow .. 'gg', { output = false })
6772
return
6873
end
6974
if vERow == last_line and dir > 0 then
70-
vim.api.nvim_exec(':normal! ' .. (vSRow + 1) .. 'ggV' .. (vERow + dir) .. 'gg', false)
75+
vim.api.nvim_exec2(':normal! ' .. (vSRow + 1) .. 'ggV' .. (vERow + dir) .. 'gg', { output = false })
7176
return
7277
end
7378

@@ -90,7 +95,9 @@ M.moveBlock = function(dir, line1, line2)
9095
vim.cmd(':normal! zx')
9196
end
9297

93-
utils.indent_block(amount, (dir > 0 and vSRow + 2 or vSRow), vERow + dir)
98+
if indent then
99+
utils.indent_block(amount, (dir > 0 and vSRow + 2 or vSRow), vERow + dir)
100+
end
94101
utils.reselect_block(dir, vSRow, vERow)
95102
end
96103

lua/move/init.lua

Lines changed: 0 additions & 10 deletions
This file was deleted.

lua/move/utils/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ end
2121
---@param vSRow number Start row of Visual area.
2222
---@param vERow number End row of Visual area.
2323
M.reselect_block = function(dir, vSRow, vERow)
24-
vim.api.nvim_exec(':normal! \\e\\e', false)
25-
vim.api.nvim_exec(
24+
vim.api.nvim_exec2(':normal! \\e\\e', { output = false })
25+
vim.api.nvim_exec2(
2626
':normal! ' .. (dir > 0 and vSRow + 2 or vSRow) .. 'ggV' .. (vERow + dir) .. 'gg',
27-
false
27+
{ output = false }
2828
)
2929
end
3030

plugin/move.lua

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)