-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathinit.lua
232 lines (212 loc) · 5.7 KB
/
init.lua
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
local M = {}
local fn = vim.fn
local api = vim.api
local fmt = string.format
-- FIXME: remove this when lua autocommands are officially supported
-- Global store of callback for autocommand (and eventually mappings)
_G.__flutter_tools_callbacks = __flutter_tools_callbacks or {}
---@param name string
---@return string
function M.display_name(name, platform)
if not name then
return ""
end
local symbol = "•"
return symbol
.. " "
.. name
.. (platform and platform ~= "" and (" " .. symbol .. " ") .. platform or "")
end
---Create a neovim command
---@param name string
---@param rhs string
---@param modifiers string[]
function M.command(name, rhs, modifiers)
modifiers = modifiers or {}
local nargs = modifiers and modifiers.nargs or 0
vim.cmd(fmt("command! -nargs=%s %s %s", nargs, name, rhs))
end
--- if every item in a table is an empty value return true
function M.list_is_empty(tbl)
if not tbl then
return true
end
return table.concat(tbl) == ""
end
function M.buf_valid(bufnr, name)
local target = bufnr or name
if not target then
return false
end
if bufnr then
return api.nvim_buf_is_loaded(bufnr)
end
return vim.fn.bufexists(target) > 0
end
---Add a function to the global callback map
---@param f function
---@return number
local function _create(f)
table.insert(__flutter_tools_callbacks, f)
return #__flutter_tools_callbacks
end
function M._execute(id, args)
__flutter_tools_callbacks[id](args)
end
---Create a mapping
---@param mode string
---@param lhs string
---@param rhs string | function
---@param opts table
function M.map(mode, lhs, rhs, opts)
-- add functions to a global table keyed by their index
if type(rhs) == "function" then
local fn_id = _create(rhs)
rhs = string.format("<cmd>lua require('flutter-tools.utils')._execute(%s)<CR>", fn_id)
end
local buffer = opts.buffer
opts.silent = opts.silent ~= nil and opts.silent or true
opts.noremap = opts.noremap ~= nil and opts.noremap or true
opts.buffer = nil
if buffer and type(buffer) == "number" then
api.nvim_buf_set_keymap(buffer, mode, lhs, rhs, opts)
else
api.nvim_set_keymap(mode, lhs, rhs, opts)
end
end
---@class FlutterAutocmd
---@field events string[] list of autocommand events
---@field targets string[] list of autocommand patterns
---@field modifiers string[] e.g. nested, once
---@field command string | function
---Create an autocommand
---@param name string
---@param commands FlutterAutocmd[]
function M.augroup(name, commands)
vim.cmd("augroup " .. name)
vim.cmd("autocmd!")
for _, c in ipairs(commands) do
local command = c.command
if type(command) == "function" then
local fn_id = _create(command)
command = fmt("lua require('flutter-tools.utils')._execute(%s)", fn_id)
end
vim.cmd(
string.format(
"autocmd %s %s %s %s",
table.concat(c.events, ","),
table.concat(c.targets or {}, ","),
table.concat(c.modifiers or {}, " "),
command
)
)
end
vim.cmd("augroup END")
end
---Add a highlight group and an accompanying autocommand to refresh it
---if the colorscheme changes
---@param name string
---@param opts table
function M.highlight(name, opts)
local hls = {}
for k, v in pairs(opts) do
table.insert(hls, fmt("%s=%s", k, v))
end
local hl_cmd = fmt("highlight! %s %s", name, table.concat(hls, " "))
vim.cmd(hl_cmd)
M.augroup(name .. "Refresh", {
{
events = { "ColorScheme" },
targets = { "*" },
command = hl_cmd,
},
})
end
function M.fold(accumulator, callback, list)
for _, v in ipairs(list) do
accumulator = callback(accumulator, v)
end
return accumulator
end
---Find an item in a list based on a compare function
---@generic T
---@param compare fun(item: T): boolean
---@param list `T`
---@return `T`
function M.find(list, compare)
for _, item in ipairs(list) do
if compare(item) then
return item
end
end
end
---Merge two table but maintain metatables
---Priority is given to the second table
--- FIXME: this does not copy metatables
---@param t1 table
---@param t2 table
---@param skip string[]
---@return table
function M.merge(t1, t2, skip)
for k, v in pairs(t2) do
if not skip or not vim.tbl_contains(skip, k) then
if (type(v) == "table") and (type(t1[k] or false) == "table") then
M.merge(t1[k], t2[k])
else
t1[k] = v
end
end
end
return t1
end
function M.remove_newlines(str)
if not str or type(str) ~= "string" then
return str
end
return str:gsub("[\n\r]", "")
end
function M.executable(bin)
return fn.executable(bin) > 0
end
---Get the attribute value of a specified highlight
---@param name string
---@param attribute string
---@return string
function M.get_hl(name, attribute)
return fn.synIDattr(fn.hlID(name), attribute)
end
function M.open_command()
local path = require("flutter-tools.utils.path")
if path.is_mac then
return "open"
end
if path.is_linux then
return "xdg-open"
end
if path.is_windows then
return "explorer"
end
return nil, nil
end
---Create an lsp handler compatible with the new handler signature
---@see: https://github.com/neovim/neovim/pull/15504/
---@param func function
---@return function
function M.lsp_handler(func)
return function(...)
local config_or_client_id = select(4, ...)
local is_new = type(config_or_client_id) ~= "number"
if is_new then
func(...)
else
local err = select(1, ...)
local method = select(2, ...)
local result = select(3, ...)
local client_id = select(4, ...)
local bufnr = select(5, ...)
local config = select(6, ...)
func(err, result, { method = method, client_id = client_id, bufnr = bufnr }, config)
end
end
end
return M