Skip to content

Instantly share code, notes, and snippets.

@sefatanam
Last active October 11, 2024 18:25
Show Gist options
  • Save sefatanam/dcebfa8cf97fd112e8dd28503569c7e4 to your computer and use it in GitHub Desktop.
Save sefatanam/dcebfa8cf97fd112e8dd28503569c7e4 to your computer and use it in GitHub Desktop.
my neovim config ...
vim.g.base46_cache = vim.fn.stdpath "data" .. "/base46/"
vim.g.mapleader = " "
-- bootstrap lazy and all plugins
local lazypath = vim.fn.stdpath "data" .. "/lazy/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
local repo = "https://github.com/folke/lazy.nvim.git"
vim.fn.system { "git", "clone", "--filter=blob:none", repo, "--branch=stable", lazypath }
end
vim.opt.rtp:prepend(lazypath)
vim.opt.scrolloff=10;
vim.opt.relativenumber = true -- Enable relative number
vim.opt.number = true -- Also show the current line number
vim.opt.expandtab = true -- Convert tabs to spaces
vim.opt.smartindent = true -- Enable smart indentation
vim.opt.shiftwidth = 4 -- Number of spaces for each indentation level
vim.opt.tabstop = 4 -- Number of spaces a tab counts for
vim.opt.softtabstop = 4 -- Number of spaces a tab key inserts
vim.diagnostic.config({
virtual_text = true,
update_in_insert = true,
severity_sort = true,
})
local lazy_config = require "configs.lazy"
-- load plugins
require("lazy").setup({
{
"NvChad/NvChad",
lazy = false,
branch = "v2.5",
import = "nvchad.plugins",
},
{ import = "plugins" },
}, lazy_config)
-- load theme
dofile(vim.g.base46_cache .. "defaults")
dofile(vim.g.base46_cache .. "statusline")
require "options"
require "nvchad.autocmds"
vim.schedule(function()
require "mappings"
end)
local Terminal = require('toggleterm.terminal').Terminal
local lazygit = Terminal:new({
cmd = "lazygit",
hidden = true,
direction = "float",
float_opts = {
border = "curved", -- or "single", "double", etc.
},
on_open = function(term)
vim.cmd("startinsert!")
end,
on_close = function(term)
vim.cmd("startinsert!")
end,
})
function _lazygit_toggle()
lazygit:toggle()
end
vim.keymap.set('n', '<leader>gg', _lazygit_toggle, { noremap = true, silent = true, desc = "Open LazyGit" })
vim.keymap.set('n', '<C-p>', ":Telescope find_files<CR>", { noremap = true, silent = true, desc = "Find Files" })
-- vim.keymap.set({'i', 'v', 'n'}, '<S-t>', ":General Format file<CR>", { noremap = true, silent = false, desc = "Format File" });
vim.keymap.set({'i','n','v'}, '<S-f>', function()
vim.lsp.buf.format({ async = true })
end, { noremap = true, silent = true, desc = "Format file" })
-- Comment
vim.keymap.set({ "n", "i", "v" }, "<C-/>", "gcc", { desc = "Toggle Comment", remap = true })
vim.keymap.set({ "n", "i", "v" }, "<C-k><C-c>", "gcc", { desc = "Toggle Comment", remap = true })
vim.keymap.set({'i', 'v', 'n'}, "<C-f>", "<cmd>Telescope buffers<CR>", { desc = "telescope find buffers" })
-- vim.keymap.set({'i', 'v', 'n'}, '<C-f>', ":lua vim.lsp.buf.format()<CR>", { noremap = true, silent = true, desc = "Format File" })
vim.api.nvim_create_user_command("LazyUpdate", function()
require("lazy").sync() -- Syncs and updates plugins
end, { desc = "Update lazy.nvim and all plugins" })
vim.keymap.set('t', '<Esc>', [[<C-\><C-n>]], { noremap = true, silent = true, desc = "Exit terminal mode" })
-- Function to focus the terminal window if it's open
local function focus_terminal()
local current_win = vim.api.nvim_get_current_win()
local terminal_win = nil
-- Iterate through all open windows to find the terminal
for _, win in ipairs(vim.api.nvim_list_wins()) do
local buf = vim.api.nvim_win_get_buf(win)
local buf_type = vim.api.nvim_buf_get_option(buf, 'buftype')
if buf_type == 'terminal' then
terminal_win = win
break
end
end
-- If a terminal window is found, focus on it
if terminal_win then
vim.api.nvim_set_current_win(terminal_win)
else
print("No terminal window is open.")
end
end
-- Set up the key mapping for normal mode
vim.keymap.set('n', '<S-t>ft', focus_terminal, { noremap = true, silent = true, desc = "Focus terminal window" })
-- load defaults i.e lua_lsp
require("nvchad.configs.lspconfig").defaults()
local lspconfig = require "lspconfig"
local servers = { "html", "cssls", "clangd", "ts_ls", "gopls", "rust_analyzer" }
local nvlsp = require "nvchad.configs.lspconfig"
-- lsps with default config
for _, lsp in ipairs(servers) do
lspconfig[lsp].setup {
on_attach = nvlsp.on_attach,
on_init = nvlsp.on_init,
capabilities = nvlsp.capabilities,
}
end
-- Load Mason's registry
local ok, mason_registry = pcall(require, 'mason-registry')
if not ok then
vim.notify 'mason-registry could not be loaded'
return
end
-- Get Angular Language Server's install path from Mason
local angularls_path = mason_registry.get_package('angular-language-server'):get_install_path()
-- Define the command to start ngserver with correct paths
local cmd = {
'ngserver',
'--stdio',
'--tsProbeLocations',
table.concat({
angularls_path,
vim.uv.cwd(),
}, ','),
'--ngProbeLocations',
table.concat({
angularls_path .. '/node_modules/@angular/language-server',
vim.uv.cwd(),
}, ','),
}
-- LSP configuration for angularls
local config = {
cmd = cmd, -- The dynamically built command
on_new_config = function(new_config, new_root_dir)
-- Update the command with new configuration if needed
new_config.cmd = cmd
end,
root_dir = require('lspconfig').util.root_pattern('angular.json', '.git'), -- Detect Angular projects
filetypes = { 'typescript', 'html', 'typescriptreact' }, -- File types for Angular
capabilities = require('nvchad.configs.lspconfig').capabilities, -- Capabilities, like autocompletion
on_attach = require('nvchad.configs.lspconfig').on_attach, -- Attach custom LSP behaviors
}
-- Setup angularls with the dynamically generated configuration
require('lspconfig').angularls.setup(config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment