-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvert.lua
More file actions
85 lines (71 loc) · 2.12 KB
/
vert.lua
File metadata and controls
85 lines (71 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
local utils = require('move.utils')
local M = {}
-- Desc:
-- Moves up or down the current cursor line
-- mantaining the cursor over the line
-- Parameter:
-- dir -> Movement direction (-1, +1)
M.moveLine = function(dir)
-- Get the last line of current buffer
local last_line = vim.fn.line('$')
-- Get current cursor position
local cursor_position = vim.api.nvim_win_get_cursor(0)
local line = cursor_position[1]
if dir == nil then
error('Missing offset', 3)
end
-- Edges
if line == 1 and dir < 0 then
return
end
if line == last_line and dir > 0 then
return
end
-- General Case
if line >= 1 and line <= last_line then
local amount = utils.calc_indent(line + dir, dir)
utils.swap_line(line, line + dir)
utils.indent(amount, line + dir)
end
end
-- Desc:
-- Moves up or down a visual area
-- mantaining the selection
-- Parameter:
-- dir -> Movement direction (-1, +1)
-- line1 -> Initial line of the seleted area
-- line2 -> End line of the selected area
M.moveBlock = function(dir, line1, line2)
local vSRow = line1 or vim.fn.line('v')
local vERow = line2 or vim.api.nvim_win_get_cursor(0)[1]
local last_line = vim.fn.line('$')
-- Zero-based and end exclusive
vSRow = vSRow - 1
if vSRow > vERow then
local aux = vSRow
vSRow = vERow
vERow = aux
end
-- Edges
if vSRow == 0 and dir < 0 then
vim.api.nvim_exec(':normal! '..vSRow..'ggV'..(vERow)..'gg', false)
return
end
if vERow == last_line and dir > 0 then
vim.api.nvim_exec(':normal! '..(vSRow + 1)..'ggV'..(vERow + dir)..'gg', false)
return
end
local vBlock = vim.api.nvim_buf_get_lines(0, vSRow, vERow, true)
if dir < 0 then
local vTarget = utils.get_target(vSRow - 1, vSRow)
table.insert(vBlock, vTarget[1])
elseif dir > 0 then
local vTarget = utils.get_target(vERow, vERow + 1)
table.insert(vBlock, 1, vTarget[1])
end
local amount = utils.calc_indent((dir > 0 and vERow or vSRow + 1) + dir, dir)
utils.move_range(vBlock, (dir > 0 and vSRow or vSRow - 1), (dir > 0 and vERow + 1 or vERow))
utils.indent_block(amount, (dir > 0 and vSRow + 2 or vSRow), vERow + dir)
utils.reselect_block(dir, vSRow, vERow)
end
return M