From ca238449c981cc0bca7079e73997e503e655c9c0 Mon Sep 17 00:00:00 2001 From: niuzhaolong Date: Mon, 19 May 2025 12:58:17 +0800 Subject: [PATCH] init commit --- .gitignore | 1 + init.vim | 40 +++++++++++ lua/auto-pairs.lua | 132 +++++++++++++++++++++++++++++++++++ lua/config/lazy.lua | 35 ++++++++++ lua/plugins/brackets-fix.lua | 6 ++ lua/plugins/colorscheme.lua | 11 +++ lua/plugins/search-move.lua | 5 ++ 7 files changed, 230 insertions(+) create mode 100644 .gitignore create mode 100644 init.vim create mode 100644 lua/auto-pairs.lua create mode 100644 lua/config/lazy.lua create mode 100644 lua/plugins/brackets-fix.lua create mode 100644 lua/plugins/colorscheme.lua create mode 100644 lua/plugins/search-move.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e033bc6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +lazy-lock.json diff --git a/init.vim b/init.vim new file mode 100644 index 0000000..c81d816 --- /dev/null +++ b/init.vim @@ -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 :nohlsearch + +lua << EOF +require("config.lazy") +-- local auto_pairs = require('auto-pairs') +-- auto_pairs.setup() +EOF diff --git a/lua/auto-pairs.lua b/lua/auto-pairs.lua new file mode 100644 index 0000000..ee55590 --- /dev/null +++ b/lua/auto-pairs.lua @@ -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 .. '' + 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('', true, true, true) + end + end + return right +end + + +-- 主函数,处理自动补全 +function M.setup() + vim.keymap.set('i', '', 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 '' + end + end + + return '' +end + +return M diff --git a/lua/config/lazy.lua b/lua/config/lazy.lua new file mode 100644 index 0000000..ef4c1e0 --- /dev/null +++ b/lua/config/lazy.lua @@ -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 }, +}) diff --git a/lua/plugins/brackets-fix.lua b/lua/plugins/brackets-fix.lua new file mode 100644 index 0000000..7531537 --- /dev/null +++ b/lua/plugins/brackets-fix.lua @@ -0,0 +1,6 @@ +-- 用于括号、引号和格式化补全 + +return { + "tpope/vim-endwise", + "jiangmiao/auto-pairs", +} diff --git a/lua/plugins/colorscheme.lua b/lua/plugins/colorscheme.lua new file mode 100644 index 0000000..a9c8f43 --- /dev/null +++ b/lua/plugins/colorscheme.lua @@ -0,0 +1,11 @@ +-- 配色方案 +-- +return { + { "catppuccin/nvim", + name = "catppuccin", + priority = 1000, + config = function() + vim.cmd([[colorscheme catppuccin-frappe]]) + end, + }, +} diff --git a/lua/plugins/search-move.lua b/lua/plugins/search-move.lua new file mode 100644 index 0000000..6ed9e3d --- /dev/null +++ b/lua/plugins/search-move.lua @@ -0,0 +1,5 @@ +-- 用于搜索和快速移动 +-- +return { + {"mileszs/ack.vim", }, -- 提供工作方式类似grep的:Ack :Ack!命令 +}