-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathutils.lua
More file actions
164 lines (133 loc) · 4.25 KB
/
utils.lua
File metadata and controls
164 lines (133 loc) · 4.25 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
local fn = vim.fn
local api = vim.api
local unpack = table.unpack or unpack
local pack = table.pack or vim.F.pack_len
local M = {}
local last_changedtick = vim.b.changedtick
---@param func fun(...: unknown): any the callback
---@param delay integer delay in milliseconds
---@return fun(...: unknown)
function M.debounce(func, delay)
local timer_id
return function(...)
if timer_id then fn.timer_stop(timer_id) end
local args = pack(...)
timer_id = fn.timer_start(delay, function()
return func(unpack(args, 1, args.n))
end)
end
end
function M.str_to_lines(str)
return fn.split(str, "\n")
end
function M.lines_to_str(lines)
return fn.join(lines, "\n")
end
function M.remove_matching_suffix(str, suffix)
if not M.ends_with(str, suffix) then return str end
return str:sub(1, -#suffix - 1)
end
function M.remove_matching_prefix(str, prefix)
if not M.starts_with(str, prefix) then return str end
return str:sub(#prefix + 1)
end
function M.subset(tbl, from, to)
return { unpack(tbl, from, to) }
end
---returns the directory of the running script
---@return string
function M.script_dir()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*/)") or "./"
end
---returns the directory of the root of the module
---@return string
function M.module_dir()
-- HACK: This only works if this file is not moved!
return M.script_dir() .. "../.."
end
function M.prequire(...)
local status, lib = pcall(require, ...)
if status then return lib end
return nil
end
function M.pumvisible()
local cmp = M.prequire("cmp")
if cmp then
return cmp.visible()
else
return vim.fn.pumvisible() > 0
end
end
function M.current_position()
return { fn.line("."), fn.col(".") }
end
function M.ends_with(str, suffix)
if str == "" then return true end
return str:sub(-#suffix) == suffix
end
function M.starts_with(str, prefix)
if str == "" then return true end
return str:sub(1, #prefix) == prefix
end
function M.is_end_of_line()
return fn.col(".") == fn.col("$")
end
function M.end_of_line()
return api.nvim_buf_get_text(0, fn.line(".") - 1, fn.col(".") - 1, fn.line(".") - 1, fn.col("$"), {})[1]
end
function M.document_changed()
local current_changedtick = last_changedtick
last_changedtick = vim.b.changedtick
return last_changedtick > current_changedtick
end
function M.selected_text()
local mode = vim.fn.mode()
if mode ~= "v" and mode ~= "V" and mode ~= "" then return "" end
local a_orig = vim.fn.getreg("a")
vim.cmd([[silent! normal! "aygv]])
local text = vim.fn.getreg("a")
vim.fn.setreg("a", a_orig)
return text
end
function M.set(array)
local set = {}
local uniqueValues = {}
for _, value in ipairs(array) do
if not set[value] then
set[value] = true
table.insert(uniqueValues, value)
end
end
return uniqueValues
end
---Selects a given range of text
---@param range table
---@param selection_mode? 'charwise'|'linewise'|'blockwise'|'v'|'V'|'<C-v>'
function M.select_range(range, selection_mode)
local start_row, start_col, end_row, end_col = range[1][1], range[1][2], range[2][1], range[2][2]
local v_table = { charwise = "v", linewise = "V", blockwise = "<C-v>" }
selection_mode = selection_mode or "charwise"
-- Normalise selection_mode
selection_mode = v_table[selection_mode] or selection_mode
-- enter visual mode if normal or operator-pending (no) mode
-- Why? According to https://learnvimscriptthehardway.stevelosh.com/chapters/15.html
-- If your operator-pending mapping ends with some text visually selected, Vim will operate on that text.
-- Otherwise, Vim will operate on the text between the original cursor position and the new position.
local mode = api.nvim_get_mode()
if mode.mode ~= selection_mode then
-- Call to `nvim_replace_termcodes()` is needed for sending appropriate command to enter blockwise mode
selection_mode = vim.api.nvim_replace_termcodes(selection_mode, true, true, true)
api.nvim_cmd({ cmd = "normal", bang = true, args = { selection_mode } }, {})
end
api.nvim_win_set_cursor(0, { start_row, start_col - 1 })
vim.cmd("normal! o")
api.nvim_win_set_cursor(0, { end_row, end_col - 1 })
end
function M.read_file_into_buffer(file_path)
local content = vim.fn.readfile(file_path)
local bufnr = vim.api.nvim_create_buf(false, true)
api.nvim_buf_set_lines(bufnr, 0, -1, false, content)
return bufnr
end
return M