Skip to content

Instantly share code, notes, and snippets.

@gavxin
Last active June 19, 2023 09:20
Show Gist options
  • Save gavxin/f0223455d8fa76f81295d727fe636f8d to your computer and use it in GitHub Desktop.
Save gavxin/f0223455d8fa76f81295d727fe636f8d to your computer and use it in GitHub Desktop.
neovim config
-- Please place this file to following location
-- ~/.config/nvim/init.lua on Mac, Linux
-- ~/AppData/Local/nvim/init.lua on Windows
--------------------------------------------------------------
-- basic configs
vim.o.ignorecase = true
vim.o.smartcase = true
vim.o.expandtab = true
vim.o.tabstop = 2
vim.o.shiftwidth = 2
vim.opt.number = true
vim.opt.cc = { 80, 110, }
vim.wo.signcolumn = "yes"
vim.g.mapleader = ","
vim.o.termguicolors = true
vim.wo.cursorline = true
--------------------------------------------------------------
-- package manager
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
print("Cloning lazy.nvim ...")
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable", -- latest stable release
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
--------------------------------------------------------------
--
require("lazy").setup({
{
"folke/tokyonight.nvim",
lazy = false,
priority = 1000,
opts = {},
config = function()
-- vim.cmd([[colorscheme tokyonight]])
end
},
{
"cpea2506/one_monokai.nvim",
config = function()
require("one_monokai").setup({})
vim.cmd([[colorscheme one_monokai]])
end
},
{
"folke/which-key.nvim",
event = "VeryLazy",
init = function()
vim.o.timout = true
vim.o.timoutlen = 300
end,
config = function()
require("which-key").register({
e = {
name = "Edit",
e = { "<cmd>e " .. vim.fn.stdpath("config") .. "/init.lua<cr>", "Edit init.lua" },
n = { "<cmd>enew<cr>", "New File" },
},
l = { "<cmd>Lazy<cr>", "Lazy" },
v = { "<C-v>", "Vertical select" },
d = {
name = "Diagnostic",
o = { "<cmd>lua vim.diagnostic.open_float()<cr>", "Show diagnostic window" },
q = { "<cmd>lua vim.diagnostic.setloclist()<cr>", "Add buffer diagnostics to the location list" },
n = { "<cmd>lua vim.diagnostic.goto_next()<cr>", "Next diagnostic" },
p = { "<cmd>lua vim.diagnostic.goto_next()<cr>", "Previous diagnostic" },
},
o = { "<cmd>ClangdSwitchSourceHeader<cr>", "Switch C/C++ Source/Header file" },
}, { prefix = "<leader>" })
require("which-key").register({
K = { "<cmd>lua vim.lsp.buf.hover()<cr>", "lsp-handler for the method textDocument/hover" },
["<C-k>"] = { "<cmd>lua vim.lsp.buf.signature_help()<cr>", "lsp-handler for the method textDocument/signatureHelp" },
g = {
name = "Goto",
d = { "<cmd>lua vim.lsp.buf.definition()<cr>", "Goto definition" },
D = { "<cmd>lua vim.lsp.buf.declaration()<cr>", "Goto declaration" },
t = { "<cmd>lua vim.lsp.buf.type_definition()<cr>", "Goto type of symbol definition" },
i = { "<cmd>lua vim.lsp.buf.implementation()<cr>", "List all implementations" },
r = { "<cmd>lua vim.lsp.buf.references()<cr>", "List all references" },
},
["<leader>"] = {
w = {
name = "Workspace",
l = { "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<cr>", "List workspace folders" },
a = { "<cmd>lua vim.lsp.buf.add_workspace_folder()<cr>", "Add workspace folder" },
r = { "<cmd>lua vim.lsp.buf.remove_workspace_folder()<cr>", "Remove workspace folder" },
},
c = {
name = "Code",
a = { "<cmd>CodeActionMenu<cr>", "Code Action Menu" },
r = { "<cmd>lua vim.lsp.buf.rename()<cr>", "Rename symbol" },
f = { "<cmd>lua vim.lsp.buf.formatting()<cr>", "Formating" },
},
},
}, { buffer = bufnr })
end,
opts = {},
},
"folke/neodev.nvim",
{
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
config = function()
require('nvim-treesitter.install').prefer_git = false
require('nvim-treesitter.install').compilers = { "zig", "clang", "gcc" }
require('nvim-treesitter.configs').setup({
ensure_installed = { },
sync_install = false,
auto_install = false,
ignore_install = { },
highlight = {
enable = true,
disable = { "c", "rust" },
-- disable = function(lang, buf)
-- local max_filesize = 100 * 1024 -- 100 KB
-- local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
-- if ok and stats and stats.size > max_filesize then
-- return true
-- end
-- end,
additional_vim_regex_highlighting = false,
},
})
vim.cmd([[
set foldmethod=expr
set foldexpr=nvim_treesitter#foldexpr()
set foldlevel=99
set nofoldenable
]])
end
},
{
"folke/noice.nvim",
event = "VeryLazy",
opts = {},
dependencies = {
-- if you lazy-load any plugin below, make sure to add proper `module="..."` entries
"MunifTanjim/nui.nvim",
-- OPTIONAL:
-- `nvim-notify` is only needed, if you want to use the notification view.
-- If not available, we use `mini` as the fallback
"rcarriga/nvim-notify",
},
config = function()
require("noice").setup({
lsp = {
-- override markdown rendering so that **cmp** and other plugins use **Treesitter**
override = {
["vim.lsp.util.convert_input_to_markdown_lines"] = true,
["vim.lsp.util.stylize_markdown"] = true,
["cmp.entry.get_documentation"] = true,
},
},
-- you can enable a preset for easier configuration
presets = {
bottom_search = true, -- use a classic bottom cmdline for search
command_palette = true, -- position the cmdline and popupmenu together
long_message_to_split = true, -- long messages will be sent to a split
inc_rename = false, -- enables an input dialog for inc-rename.nvim
lsp_doc_border = true, -- add a border to hover docs and signature help
},
})
end
},
-- lsp installer
{
"williamboman/mason.nvim",
build = ":MasonUpdate", -- :MasonUpdate updates registry contents
config = true,
},
{
"williamboman/mason-lspconfig.nvim",
opts = {},
config = true,
},
-- complete
{
"folke/neoconf.nvim",
config = true,
},
{
"neovim/nvim-lspconfig",
dependencies = {
"folke/neoconf.nvim",
}
},
{
"hrsh7th/nvim-cmp",
dependencies = {
"neovim/nvim-lspconfig",
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
"hrsh7th/cmp-cmdline",
"hrsh7th/cmp-vsnip",
"hrsh7th/vim-vsnip",
"onsails/lspkind-nvim",
},
},
-- telescope
{
'nvim-telescope/telescope.nvim', tag = '0.1.1',
dependencies = {
"folke/which-key.nvim",
"nvim-lua/plenary.nvim",
},
config = function()
require("which-key").register({
["<leader>f"] = {
name = "Telescope",
f = { "<cmd>Telescope find_files<cr>", "Find File" },
o = { "<cmd>Telescope oldfiles<cr>", "Open Recent File" },
b = { "<cmd>Telescope buffers<cr>", "Find Buffers" },
g = { "<cmd>Telescope live_grep<cr>", "Live Grep" },
h = { "<cmd>Telescope help_tags<cr>", "Help tags" },
}
})
end
},
-- terminal
{
'akinsho/toggleterm.nvim',
version = "*",
dependencies = {
"folke/which-key.nvim",
},
config = function()
require("toggleterm").setup()
-- toggle terminal
require("which-key").register({
["<leader>t"] = { "<cmd>ToggleTerm<cr>", "Toggle Terminal" },
})
-- terminal key map
vim.api.nvim_set_keymap("t", "<esc>", [[<C-\><C-N>]], { noremap = true })
vim.api.nvim_set_keymap("t", "<leader>t", "<cmd>ToggleTerm<cr>", { noremap = true })
end
},
-- github copilot
"github/copilot.vim",
-- code outline
{
"stevearc/aerial.nvim",
opts = {},
-- Optional dependencies
dependencies = {
"nvim-treesitter/nvim-treesitter",
"nvim-tree/nvim-web-devicons"
},
},
-- browse file system
{
"nvim-tree/nvim-tree.lua",
version = "*",
dependencies = {
"nvim-tree/nvim-web-devicons",
},
config = function()
require("nvim-tree").setup({})
require("which-key").register({
["<leader>x"] = { "<cmd>NvimTreeToggle<cr>", "Toggle File Explorer" },
})
end,
},
-- lualine
{
"nvim-lualine/lualine.nvim",
dependencies = {
"nvim-tree/nvim-web-devicons",
},
config = true,
},
-- rust-tools
{
"simrat39/rust-tools.nvim",
opts = {
server = {
on_attach = function(_, bufnr)
local rt = require("rust-tools")
-- Hover actions
vim.keymap.set("n", "<C-space>", rt.hover_actions.hover_actions, { buffer = bufnr })
-- Code action groups
vim.keymap.set("n", "<Leader>a", rt.code_action_group.code_action_group, { buffer = bufnr })
end,
},
}
},
-- dap
{
"mfussenegger/nvim-dap",
},
-- comment plugin
{
"numToStr/Comment.nvim",
config = true,
},
})
--------------------------------------------------------------
-- nvim-cmp
local cmp = require'cmp'
cmp.setup({
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif vim.fn["vsnip#available"](1) == 1 then
feedkey("<Plug>(vsnip-expand-or-jump)", "")
elseif has_words_before() then
cmp.complete()
else
fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function()
if cmp.visible() then
cmp.select_prev_item()
elseif vim.fn["vsnip#jumpable"](-1) == 1 then
feedkey("<Plug>(vsnip-jump-prev)", "")
end
end, { "i", "s" }),
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'vsnip' },
}, {
{ name = 'buffer' },
}),
formatting = {
format = require("lspkind").cmp_format({
mode = "symbol_text",
menu = ({
buffer = "[Buffer]",
nvim_lsp = "[LSP]",
luasnip = "[LuaSnip]",
nvim_lua = "[Lua]",
latex_symbols = "[Latex]",
})
}),
},
})
-- Set configuration for specific filetype.
cmp.setup.filetype('gitcommit', {
sources = cmp.config.sources({
{ name = 'git' }, -- You can specify the `git` source if [you were installed it](https://github.com/petertriho/cmp-git).
}, {
{ name = 'buffer' },
})
})
-- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline({ '/', '?' }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'buffer' }
}
})
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = 'path' }
}, {
{ name = 'cmdline' }
})
})
-- dap
local dap = require('dap')
dap.adapters.codelldb = {
type = 'server',
host = '127.0.0.1',
port = 13000 -- 💀 Use the port printed out or specified with `--port`
}
dap.adapters.codelldb = {
type = 'server',
port = "${port}",
executable = {
-- CHANGE THIS to your path!
command = 'codelldb',
args = {"--port", "${port}"},
-- On windows you may have to uncomment this:
detached = false,
}
}
-- Set up lspconfig.
local capabilities = require('cmp_nvim_lsp').default_capabilities()
-- Replace <YOUR_LSP_SERVER> with each lsp server you've enabled.
require('lspconfig').clangd.setup {
capabilities = capabilities
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment