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
|
local M = {}
--- Shorten Function Names
local fn = vim.fn
function M.executable(name)
if fn.executable(name) > 0 then
return true
end
return false
end
--------------------------------------------------
--- Check whether a feature exists in Nvim
--- @feat: string
--- the feature name, like `nvim-0.7` or `unix`.
--- return: bool
M.has = function(feat)
if fn.has(feat) == 1 then
return true
end
return false
end
--------------------------------------------------
-- Format on save
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
require("null-ls").setup({
-- you can reuse a shared lspconfig on_attach callback here
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
-- on 0.8, you should use vim.lsp.buf.format({ bufnr = bufnr }) instead
vim.lsp.buf.formatting_seq_sync()
end,
})
end
end,
})
--------------------------------------------------
---Determine if a value of any type is empty
---@param item any
---@return boolean?
function M.empty(item)
if not item then return true end
local item_type = type(item)
if item_type == 'string' then return item == '' end
if item_type == 'number' then return item <= 0 end
if item_type == 'table' then return vim.tbl_isempty(item) end
return item ~= nil
end
--------------------------------------------------
--- Create a dir if it does not exist
function M.may_create_dir(dir)
local res = fn.isdirectory(dir)
if res == 0 then
fn.mkdir(dir, "p")
end
end
--------------------------------------------------
--- Toggle cmp completion
vim.g.cmp_toggle_flag = false -- initialize
local normal_buftype = function()
return vim.api.nvim_buf_get_option(0, "buftype") ~= "prompt"
end
M.toggle_completion = function()
local ok, cmp = pcall(require, "cmp")
if ok then
local next_cmp_toggle_flag = not vim.g.cmp_toggle_flag
if next_cmp_toggle_flag then
print("completion on")
else
print("completion off")
end
cmp.setup({
enabled = function()
vim.g.cmp_toggle_flag = next_cmp_toggle_flag
if next_cmp_toggle_flag then
return normal_buftype
else
return next_cmp_toggle_flag
end
end,
})
else
print("completion not available")
end
end
--------------------------------------------------
--- Make sure using latest neovim version
function M.get_nvim_version()
local actual_ver = vim.version()
local nvim_ver_str = string.format("%d.%d.%d", actual_ver.major, actual_ver.minor, actual_ver.patch)
return nvim_ver_str
end
function M.add_pack(name)
local status, error = pcall(vim.cmd, "packadd " .. name)
return status
end
--------------------------------------------------
--- Toggle autopairs on/off (requires "windwp/nvim-autopairs")
function M.Toggle_autopairs()
local ok, autopairs = pcall(require, "nvim-autopairs")
if ok then
if autopairs.state.disabled then
autopairs.enable()
print("autopairs on")
else
autopairs.disable()
print("autopairs off")
end
else
print("autopairs not available")
end
end
--------------------------------------------------
--- Make vim-rooter message disappear after making it's changes
--vim.cmd([[
--let timer = timer_start(1000, 'LogTrigger', {})
--func! LogTrigger(timer)
-- silent!
--endfunc
--]])
--
--vim.cmd([[
--function! ConfigureChDir()
-- echo ('')
--endfunction
--" Call after vim-rooter changes the root dir
--autocmd User RooterChDir :sleep! | call LogTrigger(timer) | call ConfigureChDir()
--]])
--------------------------------------------------
-- Set bare dotfiles repository git environment variables dynamically
-- Set git enviornment variables
--function M.Set_git_env_vars()
-- local git_dir_job = vim.fn.jobstart({ "git", "rev-parse", "--git-dir" })
-- local command_status = vim.fn.jobwait({ git_dir_job })[1]
-- if command_status > 0 then
-- vim.env.GIT_DIR = vim.fn.expand("$HOME/.cfg")
-- vim.env.GIT_WORK_TREE = vim.fn.expand("~")
-- else
-- vim.env.GIT_DIR = nil
-- vim.env.GIT_WORK_TREE = nil
-- end
-- -- Launch terminal emulator with Git environment variables set
-- --require("toggleterm").exec(string.format([[%s %s]], os.getenv("SHELL"), "-i"))
--end
------
local prev_cwd = ""
function M.Set_git_env_vars()
local cwd = vim.fn.getcwd()
if cwd ~= prev_cwd then
prev_cwd = cwd
local git_dir_job = vim.fn.jobstart({ "git", "rev-parse", "--git-dir" })
local command_status = vim.fn.jobwait({ git_dir_job })[1]
if command_status > 0 then
vim.env.GIT_DIR = vim.fn.expand("$HOME/.cfg")
vim.env.GIT_WORK_TREE = vim.fn.expand("~")
else
vim.env.GIT_DIR = nil
vim.env.GIT_WORK_TREE = nil
end
end
end
vim.cmd [[augroup my_git_env_vars]]
vim.cmd [[ autocmd!]]
vim.cmd [[ autocmd BufEnter * lua require('user.mods').Set_git_env_vars()]]
vim.cmd [[ autocmd VimEnter * lua require('user.mods').Set_git_env_vars()]]
vim.cmd [[augroup END]]
--------------------------------------------------
return M
|