init commit

This commit is contained in:
niuzhaolong 2025-05-19 12:58:17 +08:00
commit ca238449c9
7 changed files with 230 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
lazy-lock.json

40
init.vim Normal file
View File

@ -0,0 +1,40 @@
let mapleader = "\\"
set autoindent
set expandtab
set softtabstop =4
set shiftwidth =4
set shiftround
set backspace =indent,eol,start
set hidden
set laststatus =2
set display =lastline
set showmode
set showcmd
set incsearch
set hlsearch
set ttyfast
set lazyredraw
set list
" if &shell =~# 'fish$'
" set shell=/bin/zsh
" endif
set noeb
set vb t_vb=
set foldmethod =marker
nnoremap <silent> <C-L> :nohlsearch<CR><C-L>
lua << EOF
require("config.lazy")
-- local auto_pairs = require('auto-pairs')
-- auto_pairs.setup()
EOF

132
lua/auto-pairs.lua Normal file
View File

@ -0,0 +1,132 @@
-- 不能正常工作的括号补全代码
-- 完全由ai生成qwq
--
local M = {}
-- 定义需要自动补全的字符对
local pairs_map = {
['('] = ')',
['['] = ']',
['{'] = '}',
["'"] = "'",
['"'] = '"',
['`'] = '`',
['<'] = '>',
}
-- 检查光标前是否是转义字符
local function is_escaped()
local col = vim.api.nvim_win_get_cursor(0)[2]
local line = vim.api.nvim_get_current_line()
local count = 0
while col > 0 and line:sub(col, col) == '\\' do
count = count + 1
col = col - 1
end
return count % 2 == 1
end
-- 检查是否在注释或字符串中
local function is_in_comment_or_string()
local synstack = vim.fn.synstack(vim.fn.line('.'), vim.fn.col('.'))
for _, id in ipairs(synstack) do
local syn = vim.fn.synIDattr(id, 'name')
if string.find(syn, 'comment') or string.find(syn, 'string') then
return true
end
end
return false
end
local function should_auto_pair(left, right)
local line = vim.api.nvim_get_current_line()
local col = vim.api.nvim_win_get_cursor(0)[2]
local next_char = line:sub(col + 1, col + 1)
-- 特殊处理引号:如果下一个字符是字母/数字,不自动补全
if left == "'" or left == '"' or left == '`' then
if next_char:match('%w') then
return false
end
end
return next_char == '' or next_char:match('%s') or next_char == right
end
local function auto_pair(left)
local right = pairs_map[left]
if should_auto_pair(left, right) then
local keys = left .. right .. '<Left>'
return vim.api.nvim_replace_termcodes(keys, true, true, true)
end
return left
end
-- 修改右括号处理函数
local function handle_right_pair(right)
local line = vim.api.nvim_get_current_line()
local col = vim.api.nvim_win_get_cursor(0)[2]
local next_char = line:sub(col + 1, col + 1)
-- 只有当下一个字符确实是配对的右括号时才跳过
if next_char == right then
-- 检查前面是否有未闭合的左括号
local before_cursor = line:sub(1, col)
local after_cursor = line:sub(col + 1)
-- 计算前面左括号和后面右括号的数量
local left_count = select(2, before_cursor:gsub(vim.pesc(pairs_map[right]), ''))
local right_count = select(2, after_cursor:gsub(vim.pesc(right), ''))
-- 只有当前面有未闭合的左括号时才跳过
if left_count > right_count then
return vim.api.nvim_replace_termcodes('<Right>', true, true, true)
end
end
return right
end
-- 主函数,处理自动补全
function M.setup()
vim.keymap.set('i', '<BS>', function()
return M.auto_pairs_bs()
end, {expr = true, noremap = true})
for left, right in pairs(pairs_map) do
vim.keymap.set('i', left, function()
if is_escaped() or is_in_comment_or_string(left) then
return left
end
return auto_pair(left)
end, {expr = true, noremap = true})
-- 设置右符号
vim.keymap.set('i', right, function()
if is_escaped() or is_in_comment_or_string() then
return right
end
return handle_right_pair(right)
end, {expr = true, noremap = true})
end
end
-- 退格键处理
function M.auto_pairs_bs()
local line = vim.fn.getline('.')
local col = vim.fn.col('.') -- 获取 1-based 列号
local prev_char = line:sub(col-1, col-1)
local next_char = line:sub(col, col)
for left, right in pairs(pairs_map) do
if prev_char == left and next_char == right then
return '<Del><BS>'
end
end
return '<BS>'
end
return M

35
lua/config/lazy.lua Normal file
View File

@ -0,0 +1,35 @@
-- Bootstrap lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
if vim.v.shell_error ~= 0 then
vim.api.nvim_echo({
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
{ out, "WarningMsg" },
{ "\nPress any key to exit..." },
}, true, {})
vim.fn.getchar()
os.exit(1)
end
end
vim.opt.rtp:prepend(lazypath)
-- Make sure to setup `mapleader` and `maplocalleader` before
-- loading lazy.nvim so that mappings are correct.
-- This is also a good place to setup other settings (vim.opt)
vim.g.mapleader = "\\"
vim.g.maplocalleader = "\\"
-- Setup lazy.nvim
require("lazy").setup({
spec = {
-- import your plugins
{ import = "plugins" },
},
-- Configure any other settings here. See the documentation for more details.
-- colorscheme that will be used when installing plugins.
install = { colorscheme = { "habamax" } },
-- automatically check for plugin updates
checker = { enabled = true },
})

View File

@ -0,0 +1,6 @@
-- 用于括号、引号和格式化补全
return {
"tpope/vim-endwise",
"jiangmiao/auto-pairs",
}

View File

@ -0,0 +1,11 @@
-- 配色方案
--
return {
{ "catppuccin/nvim",
name = "catppuccin",
priority = 1000,
config = function()
vim.cmd([[colorscheme catppuccin-frappe]])
end,
},
}

View File

@ -0,0 +1,5 @@
-- 用于搜索和快速移动
--
return {
{"mileszs/ack.vim", }, -- 提供工作方式类似grep的:Ack :Ack!命令
}