Skip to content

Instantly share code, notes, and snippets.

@GerardoHP
Last active March 3, 2025 03:30
Show Gist options
  • Save GerardoHP/e97af19b37c06971646bd7b4fceab7f1 to your computer and use it in GitHub Desktop.
Save GerardoHP/e97af19b37c06971646bd7b4fceab7f1 to your computer and use it in GitHub Desktop.
Lua configuration for neovim
-- Configuraciones básicas de Vim traducidas a Lua
vim.opt.number = true -- Habilita números de línea
vim.opt.relativenumber = true -- Habilita números de línea relativos
vim.opt.ruler = true -- Habilita la regla (muestra la posición del cursor)
vim.opt.autoindent = true -- Habilita la indentación automática
vim.opt.expandtab = true -- Convierte tabs en espacios
vim.opt.tabstop = 2 -- Establece el ancho de un tab en 2 espacios
vim.opt.shiftwidth = 2 -- Establece el ancho de la indentación en 2 espacios
-- Definir el <leader> como la coma
vim.g.mapleader = ";"
-- Instalar lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable", -- Usa la versión estable
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
-- Configurar lazy.nvim para cargar plugins
require("lazy").setup({
-- nvim-cmp (autocompletado)
{
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-nvim-lua",
"saadparwaiz1/cmp_luasnip",
"L3MON4D3/LuaSnip",
},
config = function()
local cmp = require("cmp")
cmp.setup({
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body) -- Usar LuaSnip para snippets
end,
},
mapping = {
["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
["<C-y>"] = cmp.config.disable, -- Deshabilitar la tecla `<C-y>` por defecto
["<C-e>"] = cmp.mapping({
i = cmp.mapping.abort(), -- Cancelar autocompletado en modo insertar
c = cmp.mapping.close(), -- Cerrar autocompletado en modo comando
}),
["<CR>"] = cmp.mapping.confirm({ select = true }), -- Confirmar selección
},
sources = cmp.config.sources({
{ name = "nvim_lsp" }, -- Fuente de autocompletado para LSP
{ name = "luasnip" }, -- Fuente de autocompletado para snippets
{ name = "buffer" }, -- Fuente de autocompletado para el buffer actual
{ name = "path" }, -- Fuente de autocompletado para rutas de archivos
}),
})
end,
},
-- add telescope-fzf-native
{
"telescope.nvim",
dependencies = {
"nvim-telescope/telescope-fzf-native.nvim",
build = "make",
config = function()
require("telescope").load_extension("fzf")
end,
},
},
-- dotnet
{
"GustavEikaas/easy-dotnet.nvim",
-- 'nvim-telescope/telescope.nvim' or 'ibhagwan/fzf-lua'
-- are highly recommended for a better experience
dependencies = { "nvim-lua/plenary.nvim", 'nvim-telescope/telescope.nvim', },
config = function()
local function get_secret_path(secret_guid)
local path = ""
local home_dir = vim.fn.expand('~')
if require("easy-dotnet.extensions").isWindows() then
local secret_path = home_dir ..
'\\AppData\\Roaming\\Microsoft\\UserSecrets\\' .. secret_guid .. "\\secrets.json"
path = secret_path
else
local secret_path = home_dir .. "/.microsoft/usersecrets/" .. secret_guid .. "/secrets.json"
path = secret_path
end
return path
end
local dotnet = require("easy-dotnet")
-- Options are not required
dotnet.setup({
--Optional function to return the path for the dotnet sdk (e.g C:/ProgramFiles/dotnet/sdk/8.0.0)
-- easy-dotnet will resolve the path automatically if this argument is omitted, for a performance improvement you can add a function that returns a hardcoded string
-- You should define this function to return a hardcoded path for a performance improvement 🚀
get_sdk_path = get_sdk_path,
---@type TestRunnerOptions
test_runner = {
---@type "split" | "float" | "buf"
viewmode = "float",
enable_buffer_test_execution = true, --Experimental, run tests directly from buffer
noBuild = true,
noRestore = true,
icons = {
passed = "",
skipped = "",
failed = "",
success = "",
reload = "",
test = "",
sln = "󰘐",
project = "󰘐",
dir = "",
package = "",
},
mappings = {
run_test_from_buffer = { lhs = "<leader>r", desc = "run test from buffer" },
filter_failed_tests = { lhs = "<leader>fe", desc = "filter failed tests" },
debug_test = { lhs = "<leader>d", desc = "debug test" },
go_to_file = { lhs = "g", desc = "got to file" },
run_all = { lhs = "<leader>R", desc = "run all tests" },
run = { lhs = "<leader>r", desc = "run test" },
peek_stacktrace = { lhs = "<leader>p", desc = "peek stacktrace of failed test" },
expand = { lhs = "o", desc = "expand" },
expand_node = { lhs = "E", desc = "expand node" },
expand_all = { lhs = "-", desc = "expand all" },
collapse_all = { lhs = "W", desc = "collapse all" },
close = { lhs = "q", desc = "close testrunner" },
refresh_testrunner = { lhs = "<C-r>", desc = "refresh testrunner" }
},
--- Optional table of extra args e.g "--blame crash"
additional_args = {}
},
---@param action "test" | "restore" | "build" | "run"
terminal = function(path, action, args)
local commands = {
run = function()
return string.format("dotnet run --project %s %s", path, args)
end,
test = function()
return string.format("dotnet test %s %s", path, args)
end,
restore = function()
return string.format("dotnet restore %s %s", path, args)
end,
build = function()
return string.format("dotnet build %s %s", path, args)
end
}
local command = commands[action]() .. "\r"
vim.cmd("vsplit")
vim.cmd("term " .. command)
end,
secrets = {
path = get_secret_path
},
csproj_mappings = true,
fsproj_mappings = true,
auto_bootstrap_namespace = {
--block_scoped, file_scoped
type = "block_scoped",
enabled = true
},
-- choose which picker to use with the plugin
-- possible values are "telescope" | "fzf" | "basic"
picker = "telescope"
})
-- Example command
vim.api.nvim_create_user_command('Secrets', function()
dotnet.secrets()
end, {})
-- Example keybinding
vim.keymap.set("n", "<C-p>", function()
dotnet.run_project()
end)
end
},
-- ######
-- Add C# to treesitter
{
"nvim-treesitter/nvim-treesitter",
opts = function(_, opts)
if type(opts.ensure_installed) == "table" then
util.list_insert_unique(opts.ensure_installed, "c_sharp")
end
end,
},
-- Correctly setup lspconfig for C# 🚀
{
"neovim/nvim-lspconfig",
opts = {
servers = {
-- Ensure mason installs the server
omnisharp = {},
},
-- configure omnisharp to fix the semantic tokens bug (really annoying)
setup = {
omnisharp = function(_, _)
require("lazyvim.util").on_attach(function(client, _)
if client.name == "omnisharp" then
---@type string[]
local tokenModifiers = client.server_capabilities.semanticTokensProvider.legend.tokenModifiers
for i, v in ipairs(tokenModifiers) do
tokenModifiers[i] = v:gsub(" ", "_")
end
---@type string[]
local tokenTypes = client.server_capabilities.semanticTokensProvider.legend.tokenTypes
for i, v in ipairs(tokenTypes) do
tokenTypes[i] = v:gsub(" ", "_")
end
end
end)
return false
end,
},
},
},
-- ######
})
-- Atajos para cambiar entre buffers
-- Cambiar al buffer anterior
vim.keymap.set('n', '<leader>bp', '<cmd>bprevious<cr>', { desc = 'Buffer anterior' })
-- Cambiar al buffer siguiente
vim.keymap.set('n', '<leader>bn', '<cmd>bnext<cr>', { desc = 'Buffer siguiente' })
-- Cerrar el buffer actual
vim.keymap.set('n', '<leader>bd', '<cmd>bdelete<cr>', { desc = 'Cerrar buffer' })
-- Atajos de teclado para telescope.nvim
vim.keymap.set('n', '<leader>ff', '<cmd>Telescope find_files<cr>', { desc = 'Buscar archivos' })
vim.keymap.set('n', '<leader>fg', '<cmd>Telescope live_grep<cr>', { desc = 'Buscar texto en archivos' })
vim.keymap.set('n', '<leader>fb', '<cmd>Telescope buffers<cr>', { desc = 'Buscar buffers abiertos' })
vim.keymap.set('n', '<leader>fh', '<cmd>Telescope help_tags<cr>', { desc = 'Buscar en la ayuda' })
-- Configuración de fzf-lua
-- local fzf_lua = require('fzf-lua')
-- Atajos de teclado para fzf-lua
-- vim.keymap.set('n', '<leader>pf', fzf_lua.files, { desc = 'Buscar archivos' })
-- vim.keymap.set('n', '<leader>pg', fzf_lua.live_grep, { desc = 'Buscar texto en archivos' })
-- vim.keymap.set('n', '<leader>pb', fzf_lua.buffers, { desc = 'Buscar buffers abiertos' })
-- vim.keymap.set('n', '<leader>ph', fzf_lua.help_tags, { desc = 'Buscar en la ayuda' })
-- Configuración de nvim-cmp (autocompletado)
local cmp = require('cmp')
cmp.setup({
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body) -- Usar LuaSnip para snippets
end,
},
mapping = {
['<C-b>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }),
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
['<C-y>'] = cmp.config.disable, -- Deshabilitar la tecla `<C-y>` por defecto
['<C-e>'] = cmp.mapping({
i = cmp.mapping.abort(), -- Cancelar autocompletado en modo insertar
c = cmp.mapping.close(), -- Cerrar autocompletado en modo comando
}),
['<CR>'] = cmp.mapping.confirm({ select = true }), -- Confirmar selección
},
sources = cmp.config.sources({
{ name = 'nvim_lsp' }, -- Fuente de autocompletado para LSP
{ name = 'luasnip' }, -- Fuente de autocompletado para snippets
{ name = 'buffer' }, -- Fuente de autocompletado para el buffer actual
{ name = 'path' }, -- Fuente de autocompletado para rutas de archivos
}),
})
-- Configuración de LSP (Language Server Protocol)
-- require('mason').setup()
-- require('mason-lspconfig').setup({
-- ensure_installed = { 'omnisharp' }, -- Instalar OmniSharp automáticamente
-- })
-- local lspconfig = require('lspconfig')
-- Configuración de OmniSharp
-- lspconfig.omnisharp.setup({
-- on_attach = function(client, bufnr)
-- -- Atajos de teclado específicos para OmniSharp
-- vim.keymap.set('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<cr>', { buffer = bufnr, desc = 'Ir a la definición' })
-- vim.keymap.set('n', 'gr', '<cmd>lua vim.lsp.buf.references()<cr>', { buffer = bufnr, desc = 'Ver referencias' })
-- vim.keymap.set('n', 'K', '<cmd>lua vim.lsp.buf.hover()<cr>', { buffer = bufnr, desc = 'Mostrar documentación' })
-- end,
-- cmd = { "omnisharp", "--languageserver", "--hostPID", tostring(vim.fn.getpid()) },
-- })
-- Configuración de LuaSnip (snippets)
require('luasnip').config.setup({})
-- Ejemplo de snippet personalizado
require('luasnip').add_snippets('all', {
require('luasnip').snippet(
'hola',
require('luasnip').text_node('Console.WriteLine("¡Hola, mundo!");')
),
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment