fix: neovim

This commit is contained in:
Ryan Yin
2023-07-10 22:50:19 +08:00
parent 21b3d4ad37
commit 7cc49c29f1
62 changed files with 1998 additions and 2600 deletions
+193
View File
@@ -0,0 +1,193 @@
local cli = {}
local helper = require('core.helper')
function cli:env_init()
self.module_path = helper.path_join(self.config_path, 'lua', 'modules')
self.lazy_dir = helper.path_join(self.data_path, 'lazy')
package.path = package.path
.. ';'
.. self.rtp
.. '/lua/vim/?.lua;'
.. self.module_path
.. '/?.lua;'
local shared = assert(loadfile(helper.path_join(self.rtp, 'lua', 'vim', 'shared.lua')))
_G.vim = shared()
end
function cli:get_all_packages()
local pack = require('core.pack')
local p = io.popen('find "' .. cli.module_path .. '" -type f')
if not p then
return
end
for file in p:lines() do
if file:find('package.lua') then
local module = file:match(cli.module_path .. '/(.+).lua$')
require(module)
end
end
p:close()
local lazy_keyword = {
'keys',
'ft',
'cmd',
'event',
'lazy',
}
local function generate_node(tbl, list)
local node = tbl[1]
list[node] = {}
list[node].type = tbl.dev and 'Local Plugin' or 'Remote Plugin'
local check_lazy = function(t, data)
vim.tbl_filter(function(k)
if vim.tbl_contains(lazy_keyword, k) then
data.load = type(t[k]) == 'table' and table.concat(t[k], ',') or t[k]
return true
end
return false
end, vim.tbl_keys(t))
end
check_lazy(tbl, list[node])
if tbl.dependencies then
for _, v in pairs(tbl.dependencies) do
if type(v) == 'string' then
v = { v }
end
list[v[1]] = {
from_depend = true,
load_after = node,
}
list[v[1]].type = v.dev and 'Local Plugin' or 'Remote Plugin'
check_lazy(v, list[v[1]])
end
end
end
local list = {}
for _, data in pairs(pack.repos or {}) do
if type(data) == string then
data = { data }
end
generate_node(data, list)
end
return list
end
function cli:boot_strap()
helper.blue('🔸 Search plugin management lazy.nvim in local')
if helper.isdir(self.lazy_dir) then
helper.green('🔸 Found lazy.nvim skip download')
return
end
helper.run_git('lazy.nvim', 'git clone https://github.com/folke/lazy.nvim ' .. self.lazy_dir, 'Install')
helper.success('lazy.nvim')
end
function cli:installer(type)
cli:boot_strap()
local packages = cli:get_all_packages()
---@diagnostic disable-next-line: unused-local, param-type-mismatch
local res = {}
for name, v in pairs(packages or {}) do
if v.type:find('Remote') then
local non_user_name = vim.split(name, '/')[2]
local path = self.lazy_dir .. helper.path_sep .. non_user_name
if helper.isdir(path) and type == 'install' then
helper.purple('\t🥯 Skip already in plugin ' .. name)
else
local url = 'git clone https://github.com/'
local cmd = type == 'install' and url .. name .. ' ' .. path or 'git -C ' .. path .. ' pull'
local failed = helper.run_git(name, cmd, type)
table.insert(res, failed)
end
else
helper.purple('\t🥯 Skip local plugin ' .. name)
end
end
if not vim.tbl_contains(res, true) then
helper.green('🎉 Congratulations Config install or update success. Enjoy ^^')
return
end
helper.red('Some plugins not install or update success please run install again')
end
function cli.install()
cli:installer('install')
end
function cli.update()
cli:installer('update')
end
function cli.clean()
os.execute('rm -rf ' .. cli.lazy_dir)
end
function cli.doctor(pack_name)
local list = cli:get_all_packages()
if not list then
return
end
helper.yellow('🔹 Total: ' .. vim.tbl_count(list) + 1 .. ' Plugins')
local packs = pack_name and { [pack_name] = list[pack_name] } or list
for k, v in pairs(packs) do
helper.blue('\t' .. '' .. k)
if v.type then
helper.write('purple')('\tType: ')
helper.write('white')(v.type)
print()
end
if v.load then
helper.write('purple')('\tLoad: ')
helper.write('white')(v.load)
print()
end
if v.from_depend then
helper.write('purple')('\tDepend: ')
helper.write('white')(v.load_after)
print()
end
end
end
function cli.modules()
local p = io.popen('find "' .. cli.module_path .. '" -type d')
if not p then
return
end
local res = {}
for dict in p:lines() do
dict = vim.split(dict, helper.path_sep)
if dict[#dict] ~= 'modules' then
table.insert(res, dict[#dict])
end
end
helper.green('Found ' .. #res .. ' Modules in Local')
for _, v in pairs(res) do
helper.write('yellow')('\t' .. v)
print()
end
end
function cli:meta(arg)
return function(data)
self[arg](data)
end
end
return cli
@@ -0,0 +1,97 @@
local helper = {}
helper.path_sep = package.config:sub(1, 1) == '\\' and '\\' or '/'
function helper.path_join(...)
return table.concat({ ... }, helper.path_sep)
end
function helper.data_path()
local cli = require('core.cli')
if cli.config_path then
return cli.config_path
end
return vim.fn.stdpath('data')
end
function helper.config_path()
local cli = require('core.cli')
if cli.data_path then
return cli.data_path
end
return vim.fn.stdpath('config')
end
local function get_color(color)
local tbl = {
black = '\027[90m',
red = '\027[91m',
green = '\027[92m',
yellow = '\027[93m',
blue = '\027[94m',
purple = '\027[95m',
cyan = '\027[96m',
white = '\027[97m',
}
return tbl[color]
end
local function color_print(color)
local rgb = get_color(color)
return function(text)
print(rgb .. text .. '\027[m')
end
end
function helper.write(color)
local rgb = get_color(color)
return function(text)
io.write(rgb .. text .. '\027[m')
end
end
function helper.success(msg)
color_print('green')('\t🍻 ' .. msg .. ' Success ‼️ ')
end
function helper.error(msg)
color_print('red')(msg)
end
function helper.run_git(name, cmd, type)
local pip = assert(io.popen(cmd .. ' 2>&1'))
color_print('green')('\t🍻 ' .. type .. ' ' .. name)
local failed = false
for line in pip:lines() do
if line:find('fatal') then
failed = true
end
io.write('\t ' .. line)
io.write('\n')
end
pip:close()
return failed
end
local function exists(file)
local ok, _, code = os.rename(file, file)
if not ok then
if code == 13 then
return true
end
end
return ok
end
--- Check if a directory exists in this path
function helper.isdir(path)
return exists(path .. '/')
end
setmetatable(helper, {
__index = function(_, k)
return color_print(k)
end,
})
return helper
@@ -0,0 +1,51 @@
local g, fn = vim.g, vim.fn
local helper = require('core.helper')
-- remove check is windows because I only use mac or linux
local cache_dir = helper.path_join(vim.fn.stdpath('cache'), 'nvim')
-- Create cache dir and subs dir
local createdir = function()
local data_dir = {
cache_dir .. 'backup',
cache_dir .. 'session',
cache_dir .. 'swap',
cache_dir .. 'tags',
cache_dir .. 'undo',
}
-- There only check once that If cache_dir exists
-- Then I don't want to check subs dir exists
if fn.isdirectory(cache_dir) == 0 then
os.execute('mkdir -p ' .. cache_dir)
for _, v in pairs(data_dir) do
if fn.isdirectory(v) == 0 then
os.execute('mkdir -p ' .. v)
end
end
end
end
createdir()
--disable_distribution_plugins
g.loaded_gzip = 1
g.loaded_tar = 1
g.loaded_tarPlugin = 1
g.loaded_zip = 1
g.loaded_zipPlugin = 1
g.loaded_getscript = 1
g.loaded_getscriptPlugin = 1
g.loaded_vimball = 1
g.loaded_vimballPlugin = 1
g.loaded_matchit = 1
g.loaded_matchparen = 1
g.loaded_2html_plugin = 1
g.loaded_logiPat = 1
g.loaded_rrhelper = 1
g.loaded_netrw = 1
g.loaded_netrwPlugin = 1
g.loaded_netrwSettings = 1
g.loaded_netrwFileHandlers = 1
require('core.pack'):boot_strap()
require('core.options')
require('keymap')
@@ -0,0 +1,115 @@
local keymap = {}
local opts = {}
function opts:new(instance)
instance = instance
or {
options = {
silent = false,
nowait = false,
expr = false,
noremap = false,
},
}
setmetatable(instance, self)
self.__index = self
return instance
end
function keymap.silent(opt)
return function()
opt.silent = true
end
end
function keymap.noremap(opt)
return function()
opt.noremap = true
end
end
function keymap.expr(opt)
return function()
opt.expr = true
end
end
function keymap.remap(opt)
return function()
opt.remap = true
end
end
function keymap.nowait(opt)
return function()
opt.nowait = true
end
end
function keymap.new_opts(...)
local args = { ... }
local o = opts:new()
if #args == 0 then
return o.options
end
for _, arg in pairs(args) do
if type(arg) == 'string' then
o.options.desc = arg
else
arg(o.options)()
end
end
return o.options
end
function keymap.cmd(str)
return '<cmd>' .. str .. '<CR>'
end
-- visual
function keymap.cu(str)
return '<C-u><cmd>' .. str .. '<CR>'
end
--@private
local keymap_set = function(mode, tbl)
vim.validate({
tbl = { tbl, 'table' },
})
local len = #tbl
if len < 2 then
vim.notify('keymap must has rhs')
return
end
local options = len == 3 and tbl[3] or keymap.new_opts()
vim.keymap.set(mode, tbl[1], tbl[2], options)
end
local function map(mod)
return function(tbl)
vim.validate({
tbl = { tbl, 'table' },
})
if type(tbl[1]) == 'table' and type(tbl[2]) == 'table' then
for _, v in pairs(tbl) do
keymap_set(mod, v)
end
else
keymap_set(mod, tbl)
end
end
end
keymap.nmap = map('n')
keymap.imap = map('i')
keymap.cmap = map('c')
keymap.vmap = map('v')
keymap.xmap = map('x')
keymap.tmap = map('t')
return keymap
@@ -0,0 +1,91 @@
local opt = vim.opt
local cache_dir = vim.env.HOME .. '/.cache/nvim/'
opt.relativenumber = true
opt.termguicolors = true
opt.hidden = true
opt.magic = true
opt.virtualedit = 'block'
-- sync content between vim & system clipboard
opt.clipboard = 'unnamedplus'
opt.wildignorecase = true
opt.swapfile = false
opt.directory = cache_dir .. 'swap/'
opt.undodir = cache_dir .. 'undo/'
opt.backupdir = cache_dir .. 'backup/'
opt.viewdir = cache_dir .. 'view/'
opt.spellfile = cache_dir .. 'spell/en.uft-8.add'
opt.history = 2000
opt.timeout = true
opt.ttimeout = true
opt.timeoutlen = 500
opt.ttimeoutlen = 10
opt.updatetime = 100
opt.redrawtime = 1500
opt.ignorecase = true
opt.smartcase = true
opt.infercase = true
if vim.fn.executable('rg') == 1 then
opt.grepformat = '%f:%l:%c:%m,%f:%l:%m'
opt.grepprg = 'rg --vimgrep --no-heading --smart-case'
end
opt.completeopt = 'menu,menuone,noselect'
opt.showmode = false
opt.shortmess = 'aoOTIcF'
opt.scrolloff = 2
opt.sidescrolloff = 5
opt.ruler = false
opt.showtabline = 0
opt.winwidth = 30
opt.pumheight = 15
opt.showcmd = false
opt.cmdheight = 0
opt.laststatus = 3
opt.list = true
opt.listchars = 'tab:»·,nbsp:+,trail:·,extends:→,precedes:←'
opt.pumblend = 10
opt.winblend = 10
opt.undofile = true
opt.smarttab = true
opt.expandtab = true
opt.autoindent = true
opt.tabstop = 2
opt.shiftwidth = 2
-- wrap
opt.linebreak = true
opt.whichwrap = 'h,l,<,>,[,],~'
opt.breakindentopt = 'shift:2,min:20'
opt.showbreak = ''
opt.foldlevelstart = 99
opt.foldmethod = 'marker'
opt.number = true
opt.signcolumn = 'yes'
opt.spelloptions = 'camel'
opt.textwidth = 100
opt.colorcolumn = '100'
if vim.loop.os_uname().sysname == 'Darwin' then
vim.g.clipboard = {
name = 'macOS-clipboard',
copy = {
['+'] = 'pbcopy',
['*'] = 'pbcopy',
},
paste = {
['+'] = 'pbpaste',
['*'] = 'pbpaste',
},
cache_enabled = 0,
}
vim.g.python_host_prog = '/usr/bin/python'
vim.g.python3_host_prog = '/usr/local/bin/python3'
end
@@ -0,0 +1,62 @@
local uv, api, fn = vim.loop, vim.api, vim.fn
local pack = {}
pack.__index = pack
function pack:load_modules_packages()
local modules_dir = self.helper.path_join(self.config_path, 'lua', 'modules')
self.repos = {}
local list = vim.fs.find('package.lua', { path = modules_dir, type = 'file', limit = 10 })
if #list == 0 then
return
end
local disable_modules = {}
if fn.exists('g:disable_modules') == 1 then
disable_modules = vim.split(vim.g.disable_modules, ',', { trimempty = true })
end
for _, f in pairs(list) do
local _, pos = f:find(modules_dir)
f = f:sub(pos - 6, #f - 4)
if not vim.tbl_contains(disable_modules, f) then
require(f)
end
end
end
function pack:boot_strap()
self.helper = require('core.helper')
self.data_path = self.helper.data_path()
self.config_path = self.helper.config_path()
local lazy_path = self.helper.path_join(self.data_path, 'lazy', 'lazy.nvim')
local state = uv.fs_stat(lazy_path)
if not state then
local cmd = '!git clone https://github.com/folke/lazy.nvim ' .. lazy_path
api.nvim_command(cmd)
end
vim.opt.runtimepath:prepend(lazy_path)
local lazy = require('lazy')
local opts = {
lockfile = self.helper.path_join(self.data_path, 'lazy-lock.json'),
}
self:load_modules_packages()
lazy.setup(self.repos, opts)
for k, v in pairs(self) do
if type(v) ~= 'function' then
self[k] = nil
end
end
end
function pack.package(repo)
if not pack.repos then
pack.repos = {}
end
table.insert(pack.repos, repo)
end
return pack