-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplacer.lua
More file actions
94 lines (76 loc) · 2.54 KB
/
Copy pathreplacer.lua
File metadata and controls
94 lines (76 loc) · 2.54 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
86
87
88
89
90
91
92
93
94
local mapping = require('toggle.mapping')
-- TODO provide luadocs for replacers
local M = {}
M.__get_cword_replacer = function()
local word = vim.fn.expand('<cword>')
return {
can_handle = function()
return mapping.__has_mapping(word)
end,
replace = function(get_mapping)
vim.api.nvim_command('normal! ciw' .. get_mapping(word))
end,
}
end
M.__get_cWORD_replacer = function()
local word = vim.fn.expand('<cWORD>')
return {
can_handle = function()
return mapping.__has_mapping(word)
end,
replace = function(get_mapping)
vim.api.nvim_command('normal! ciW' .. get_mapping(word))
end,
}
end
M.__get_character_replacer = function()
local coords = vim.api.nvim_win_get_cursor(0)
local line = vim.api.nvim_get_current_line()
local character = line:sub(coords[2] + 1, coords[2] + 1)
return {
can_handle = function()
return mapping.__has_mapping(character)
end,
replace = function(get_mapping)
vim.api.nvim_command('normal! r' .. get_mapping(character))
end,
}
end
M.__get_end_of_word_replacer = function()
local current_cursor_position = vim.fn.getcurpos()
vim.api.nvim_command('normal! e') -- jump to the end of word
local end_of_word_column = vim.api.nvim_win_get_cursor(0)[2]
vim.fn.setpos('.', current_cursor_position) -- restore cursor position
local line = vim.api.nvim_get_current_line()
local end_of_word_under_cursor = line:sub(current_cursor_position[3], end_of_word_column + 1)
return {
can_handle = function()
return mapping.__has_mapping(end_of_word_under_cursor)
end,
replace = function(get_mapping)
vim.api.nvim_command('normal! ce' .. get_mapping(end_of_word_under_cursor))
end,
}
end
M.__visual_mode_replacer = function()
local function is_visual_mode()
local mode = vim.api.nvim_get_mode().mode
return (mode == 'v') or (mode == 'V')
end
local selected_text = ''
if is_visual_mode() then
local vstart = vim.fn.getpos('.')
local vend = vim.fn.getpos('v')
local lines = vim.fn.getregion(vstart, vend)
selected_text = lines[1]
end
return {
can_handle = function()
return is_visual_mode() and mapping.__has_mapping(selected_text)
end,
replace = function(get_mapping)
vim.api.nvim_command('norm! c' .. get_mapping(selected_text))
end,
}
end
return M