1
0
mirror of https://github.com/jdhao/nvim-config.git synced 2025-06-08 14:14:33 +02:00

Compare commits

...

8 Commits

Author SHA1 Message Date
Li Peng
ad40e54076
Merge 074a53a66451d3445031633599c9aaa7a5c80f95 into 2383eb70755ce72e0375169051309c01dd33c480 2025-02-17 18:36:12 +00:00
jdhao
2383eb7075
switch back to nvim-cmp (#381) 2025-02-16 21:03:17 +01:00
jdhao
ab059bbb6d
use treesitter-textobjects instead (#380)
Note that we should keep the branch of nvim-treesitter and nvim-treesitter-textobjects the same. `main` is targeted at nightly neovim and `master` is for stable neovim. See also https://github.com/nvim-treesitter/nvim-treesitter/issues/4767
2025-02-16 20:15:41 +01:00
jdhao
93166c65a5
update colorscheme config (#379) 2025-02-16 19:52:34 +01:00
jdhao
4b8c490abd
fix gitsign word_diff flickering issue (#378)
I guess it is due to the reason that we are doing constant git fetch,
and somehow gitsigns word diff is affected.

Changing the gitsigns debounce or changing lualine refresh rate do not
seem to help. So gitsigns word diff is disabled.
2025-02-16 19:20:21 +01:00
jdhao
2242961925
Remove obsolete func (#377)
- remove function: SynGroup, HasColorscheme and
  GetGitBranch
- rewrite Inside_git_repo in lua instead
2025-02-16 19:13:24 +01:00
Li Peng
074a53a664 Only format C, CPP and Rust code 2025-01-04 09:01:31 +08:00
Li Peng
efcda5cf6f Setup LSP for Rust
Signed-off-by: Li Peng <lipeng@unisound.ai>
2025-01-03 14:46:53 +08:00
10 changed files with 129 additions and 63 deletions

View File

@ -10,23 +10,6 @@ function! s:Single_quote(str) abort
return "'" . substitute(copy(a:str), "'", "''", 'g') . "'"
endfunction
" Check the syntax group in the current cursor position, see
" https://stackoverflow.com/q/9464844/6064933 and
" https://jordanelver.co.uk/blog/2015/05/27/working-with-vim-colorschemes/
function! utils#SynGroup() abort
if !exists('*synstack')
return
endif
echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunction
" Check if a colorscheme exists in runtimepath.
" The following two functions are inspired by https://stackoverflow.com/a/5703164/6064933.
function! utils#HasColorscheme(name) abort
let l:pat = printf('colors/%s.vim', a:name)
return !empty(globpath(&runtimepath, l:pat))
endfunction
" Custom fold expr, adapted from https://vi.stackexchange.com/a/9094/15292
function! utils#VimFolds(lnum) abort
" get content of current line and the line below
@ -146,27 +129,6 @@ function! utils#iso_time(timestamp) abort
endfunction
" Check if we are inside a Git repo.
function! utils#Inside_git_repo() abort
let res = system('git rev-parse --is-inside-work-tree')
if match(res, 'true') == -1
return v:false
else
" Manually trigger a special user autocmd InGitRepo (used lazyloading.
doautocmd User InGitRepo
return v:true
endif
endfunction
function! utils#GetGitBranch()
let l:res = systemlist('git rev-parse --abbrev-ref HEAD')[0]
if match(l:res, 'fatal') != -1
return ''
else
return l:res
endif
endfunction
" Redirect command output to a register for later processing.
" Ref: https://stackoverflow.com/q/2573021/6064933 and https://unix.stackexchange.com/q/8101/221410 .
function! utils#CaptureCommandOutput(command) abort

View File

@ -1,5 +1,4 @@
--- This module will load a random colorscheme on nvim startup process.
local utils = require("utils")
local M = {}
@ -8,9 +7,14 @@ local M = {}
-- the same as the colorscheme name itself.
M.colorscheme_conf = {
onedark = function()
vim.cmd([[colorscheme onedark]])
-- Lua
require("onedark").setup {
style = "darker",
}
require("onedark").load()
end,
edge = function()
vim.g.edge_style = "default"
vim.g.edge_enable_italic = 1
vim.g.edge_better_performance = 1
@ -26,13 +30,14 @@ M.colorscheme_conf = {
-- foreground option can be material, mix, or original
vim.g.gruvbox_material_foreground = "original"
--background option can be hard, medium, soft
vim.g.gruvbox_material_background = "medium"
vim.g.gruvbox_material_background = "hard"
vim.g.gruvbox_material_enable_italic = 1
vim.g.gruvbox_material_better_performance = 1
vim.cmd([[colorscheme gruvbox-material]])
end,
everforest = function()
vim.g.everforest_background = "hard"
vim.g.everforest_enable_italic = 1
vim.g.everforest_better_performance = 1
@ -50,10 +55,11 @@ M.colorscheme_conf = {
end,
onedarkpro = function()
-- set colorscheme after options
vim.cmd("colorscheme onedark_vivid")
-- onedark_vivid does not enough contrast
vim.cmd("colorscheme onedark_dark")
end,
material = function()
vim.g.material_style = "oceanic"
vim.g.material_style = "darker"
vim.cmd("colorscheme material")
end,
arctic = function()
@ -67,6 +73,7 @@ M.colorscheme_conf = {
--- Use a random colorscheme from the pre-defined list of colorschemes.
M.rand_colorscheme = function()
local colorscheme = utils.rand_element(vim.tbl_keys(M.colorscheme_conf))
colorscheme = "gruvbox_material"
if not vim.tbl_contains(vim.tbl_keys(M.colorscheme_conf), colorscheme) then
local msg = "Invalid colorscheme: " .. colorscheme

View File

@ -8,7 +8,7 @@ gs.setup {
topdelete = { text = "" },
changedelete = { text = "" },
},
word_diff = true,
word_diff = false,
on_attach = function(bufnr)
local function map(mode, l, r, opts)
opts = opts or {}
@ -38,10 +38,10 @@ gs.setup {
end, { expr = true, desc = "previous hunk" })
-- Actions
map("n", "<leader>hp", gs.preview_hunk)
map("n", "<leader>hp", gs.preview_hunk, { desc = "preview hunk" })
map("n", "<leader>hb", function()
gs.blame_line { full = true }
end)
end, { desc = "blame hunk" })
end,
}

View File

@ -117,7 +117,7 @@ local custom_attach = function(client, bufnr)
end
end
local capabilities = require("cmp_nvim_lsp").default_capabilities()
local capabilities = vim.lsp.protocol.make_client_capabilities()
-- required by nvim-ufo
capabilities.textDocument.foldingRange = {
@ -278,6 +278,32 @@ if utils.executable("lua-language-server") then
}
end
-- settings for rust-analyzer is copied from https://rust-analyzer.github.io/manual.html#nvim-lsp
if utils.executable("rust-analyzer") then
lspconfig.rust_analyzer.setup {
on_attach = custom_attach,
settings = {
['rust-analyzer'] = {
imports = {
granularity = {
group = "module",
},
prefix = "self",
},
cargo = {
buildScripts = {
enable = true,
},
},
procMacro = {
enable = true
},
},
},
capabilities = capabilities,
}
end
-- Change diagnostic signs.
fn.sign_define("DiagnosticSignError", { text = "🆇", texthl = "DiagnosticSignError" })
fn.sign_define("DiagnosticSignWarn", { text = "⚠️", texthl = "DiagnosticSignWarn" })

View File

@ -42,7 +42,9 @@ local async_git_status_update = function()
async_cmd(ahead_cmd_str, handle_numeric_result("ahead_count"))
end
local function get_ahead_behind_info()
local function get_git_ahead_behind_info()
async_git_status_update()
local status = git_status_cache
if not status then
return ""
@ -63,9 +65,6 @@ local function get_ahead_behind_info()
return msg
end
local timer = vim.loop.new_timer()
timer:start(0, 1000, async_git_status_update)
local function spell()
if vim.o.spell then
return string.format("[SPELL]")
@ -210,6 +209,9 @@ require("lualine").setup {
section_separators = "",
disabled_filetypes = {},
always_divide_middle = true,
refresh = {
statusline = 500,
},
},
sections = {
lualine_a = {
@ -230,7 +232,7 @@ require("lualine").setup {
color = { gui = "italic,bold" },
},
{
get_ahead_behind_info,
get_git_ahead_behind_info,
color = { fg = "#E0C479" },
},
{

View File

@ -0,0 +1,44 @@
require("nvim-treesitter.configs").setup {
textobjects = {
select = {
enable = true,
-- Automatically jump forward to textobj, similar to targets.vim
lookahead = true,
keymaps = {
-- You can use the capture groups defined in textobjects.scm
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
-- You can optionally set descriptions to the mappings (used in the desc parameter of
-- nvim_buf_set_keymap) which plugins like which-key display
["ic"] = { query = "@class.inner", desc = "Select inner part of a class region" },
},
-- You can choose the select mode (default is charwise 'v')
--
-- Can also be a function which gets passed a table with the keys
-- * query_string: eg '@function.inner'
-- * method: eg 'v' or 'o'
-- and should return the mode ('v', 'V', or '<c-v>') or a table
-- mapping query_strings to modes.
selection_modes = {
["@function.inner"] = "V", -- linewise
["@function.outer"] = "V", -- linewise
["@class.outer"] = "V", -- linewise
["@class.inner"] = "V", -- linewise
["@parameter.outer"] = "v", -- charwise
},
-- If you set this to `true` (default is `false`) then any textobject is
-- extended to include preceding or succeeding whitespace. Succeeding
-- whitespace has priority in order to act similarly to eg the built-in
-- `ap`.
--
-- Can also be a function which gets passed a table with the keys
-- * query_string: eg '@function.inner'
-- * selection_mode: eg 'v'
-- and should return true or false
include_surrounding_whitespace = false,
},
},
}

View File

@ -54,6 +54,13 @@ api.nvim_create_autocmd({ "BufWritePre" }, {
end,
})
api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = {"*.c", "*.cc", "*.cpp", "*.h", "*.rs", "*.py"},
callback = function()
vim.lsp.buf.format { asnyc = false }
end,
})
-- Automatically reload the file if it is changed outside of Nvim, see https://unix.stackexchange.com/a/383044/221410.
-- It seems that `checktime` does not work in command line. We need to check if we are in command
-- line before executing this command, see also https://vi.stackexchange.com/a/20397/15292 .
@ -206,14 +213,16 @@ api.nvim_create_autocmd({ "VimEnter", "DirChanged" }, {
group = api.nvim_create_augroup("git_repo_check", { clear = true }),
pattern = "*",
desc = "check if we are inside Git repo",
command = "call utils#Inside_git_repo()",
callback = function()
utils.inside_git_repo()
end,
})
-- ref: https://vi.stackexchange.com/a/169/15292
api.nvim_create_autocmd("BufReadPre", {
group = api.nvim_create_augroup("large_file", { clear = true }),
pattern = "*",
desc = "check if we are inside Git repo",
desc = "optimize for large file",
callback = function(ev)
local file_size_limit = 524288 -- 0.5MB
local f = ev.file

View File

@ -143,9 +143,6 @@ keymap.set("x", "c", '"_c')
-- Remove trailing whitespace characters
keymap.set("n", "<leader><space>", "<cmd>StripTrailingWhitespace<cr>", { desc = "remove trailing space" })
-- check the syntax group of current cursor position
keymap.set("n", "<leader>st", "<cmd>call utils#SynGroup()<cr>", { desc = "check syntax group" })
-- Copy entire buffer.
keymap.set("n", "<leader>y", "<cmd>%yank<cr>", { desc = "yank entire buffer" })

View File

@ -23,7 +23,7 @@ end
local plugin_specs = {
-- auto-completion engine
{
"iguanacucumber/magazine.nvim",
"hrsh7th/nvim-cmp",
name = "nvim-cmp",
-- event = 'InsertEnter',
event = "VeryLazy",
@ -39,6 +39,7 @@ local plugin_specs = {
require("config.nvim-cmp")
end,
},
{
"neovim/nvim-lspconfig",
event = { "BufRead", "BufNewFile" },
@ -61,10 +62,14 @@ local plugin_specs = {
require("config.treesitter")
end,
},
-- Python-related text object
{ "jeetsukumaran/vim-pythonsense", ft = { "python" } },
{
"nvim-treesitter/nvim-treesitter-textobjects",
event = "VeryLazy",
branch = "master",
config = function()
require("config.treesitter-textobjects")
end,
},
{ "machakann/vim-swap", event = "VeryLazy" },
-- IDE for Lisp
@ -381,7 +386,7 @@ local plugin_specs = {
end
return false
end,
build = "cd app && npm install",
build = "cd app && npm install && git restore .",
ft = { "markdown" },
},

View File

@ -79,4 +79,18 @@ function M.is_compatible_version(expected_version)
return true
end
--- check if we are inside a git repo
--- @return boolean
function M.inside_git_repo()
local result = vim.system({ "git", "rev-parse", "--is-inside-work-tree" }, { text = true }):wait()
if result.code ~= 0 then
return false
end
-- Manually trigger a special user autocmd InGitRepo (used lazyloading.
vim.cmd([[doautocmd User InGitRepo]])
return true
end
return M