feat: golang lsp

This commit is contained in:
RahatMelsov 2025-08-03 13:42:58 +05:00
parent dd80e39dff
commit a94e3eb11d
3 changed files with 75 additions and 85 deletions

View File

@ -380,6 +380,7 @@ require('lazy').setup({
vim.keymap.set('n', '<leader>wl', '<C-w>l', { desc = '[W]indow Right' }) vim.keymap.set('n', '<leader>wl', '<C-w>l', { desc = '[W]indow Right' })
vim.keymap.set('n', '<leader>wq', '<C-w>q', { desc = '[W]indow Quit' }) vim.keymap.set('n', '<leader>wq', '<C-w>q', { desc = '[W]indow Quit' })
vim.keymap.set('n', '<leader>w=', '<C-w>=', { desc = '[W]indow Equalize' }) vim.keymap.set('n', '<leader>w=', '<C-w>=', { desc = '[W]indow Equalize' })
vim.keymap.set('n', '<leader>e', ':Neotree toggle<CR>', { desc = 'Toggle Neotree' })
-- Slightly advanced example of overriding default behavior and theme -- Slightly advanced example of overriding default behavior and theme
vim.keymap.set('n', '<leader>/', function() vim.keymap.set('n', '<leader>/', function()
@ -423,57 +424,18 @@ require('lazy').setup({
-- Main LSP Configuration -- Main LSP Configuration
'neovim/nvim-lspconfig', 'neovim/nvim-lspconfig',
dependencies = { dependencies = {
-- Automatically install LSPs and related tools to stdpath for Neovim
-- Mason must be loaded before its dependents so we need to set it up here.
-- NOTE: `opts = {}` is the same as calling `require('mason').setup({})`
{ 'mason-org/mason.nvim', opts = {} }, { 'mason-org/mason.nvim', opts = {} },
'mason-org/mason-lspconfig.nvim', 'mason-org/mason-lspconfig.nvim',
'WhoIsSethDaniel/mason-tool-installer.nvim', 'WhoIsSethDaniel/mason-tool-installer.nvim',
-- Useful status updates for LSP.
{ 'j-hui/fidget.nvim', opts = {} }, { 'j-hui/fidget.nvim', opts = {} },
-- Allows extra capabilities provided by blink.cmp
'saghen/blink.cmp', 'saghen/blink.cmp',
}, },
config = function() config = function()
-- Brief aside: **What is LSP?**
--
-- LSP is an initialism you've probably heard, but might not understand what it is.
--
-- LSP stands for Language Server Protocol. It's a protocol that helps editors
-- and language tooling communicate in a standardized fashion.
--
-- In general, you have a "server" which is some tool built to understand a particular
-- language (such as `gopls`, `lua_ls`, `rust_analyzer`, etc.). These Language Servers
-- (sometimes called LSP servers, but that's kind of like ATM Machine) are standalone
-- processes that communicate with some "client" - in this case, Neovim!
--
-- LSP provides Neovim with features like:
-- - Go to definition
-- - Find references
-- - Autocompletion
-- - Symbol Search
-- - and more!
--
-- Thus, Language Servers are external tools that must be installed separately from
-- Neovim. This is where `mason` and related plugins come into play.
--
-- If you're wondering about lsp vs treesitter, you can check out the wonderfully
-- and elegantly composed help section, `:help lsp-vs-treesitter`
-- This function gets run when an LSP attaches to a particular buffer.
-- That is to say, every time a new file is opened that is associated with
-- an lsp (for example, opening `main.rs` is associated with `rust_analyzer`) this
-- function will be executed to configure the current buffer
vim.api.nvim_create_autocmd('LspAttach', { vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('kickstart-lsp-attach', { clear = true }), group = vim.api.nvim_create_augroup('kickstart-lsp-attach', { clear = true }),
callback = function(event) callback = function(event)
-- NOTE: Remember that Lua is a real programming language, and as such it is possible
-- to define small helper and utility functions so you don't have to repeat yourself.
--
-- In this case, we create a function that lets us more easily define mappings specific
-- for LSP related items. It sets the mode, buffer and description for us each time.
local map = function(keys, func, desc, mode) local map = function(keys, func, desc, mode)
mode = mode or 'n' mode = mode or 'n'
vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = 'LSP: ' .. desc }) vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = 'LSP: ' .. desc })
@ -543,12 +505,24 @@ require('lazy').setup({
callback = vim.lsp.buf.document_highlight, callback = vim.lsp.buf.document_highlight,
}) })
vim.api.nvim_create_autocmd('BufWritePre', {
pattern = '*.go',
callback = function()
vim.cmd 'silent! !gofmt -w %'
end,
})
vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, { vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
buffer = event.buf, buffer = event.buf,
group = highlight_augroup, group = highlight_augroup,
callback = vim.lsp.buf.clear_references, callback = vim.lsp.buf.clear_references,
}) })
vim.api.nvim_create_autocmd('CursorHold', {
callback = function()
vim.diagnostic.open_float(nil, { focusable = false })
end,
})
vim.api.nvim_create_autocmd('LspDetach', { vim.api.nvim_create_autocmd('LspDetach', {
group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }), group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }),
callback = function(event2) callback = function(event2)
@ -616,18 +590,6 @@ require('lazy').setup({
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/ -- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
local servers = { local servers = {
-- clangd = {}, -- clangd = {},
-- gopls = {},
-- pyright = {},
-- rust_analyzer = {},
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
--
-- Some languages (like typescript) have entire language plugins that can be useful:
-- https://github.com/pmizio/typescript-tools.nvim
--
-- But for many setups, the LSP (`ts_ls`) will work just fine
-- ts_ls = {},
--
lua_ls = { lua_ls = {
-- cmd = { ... }, -- cmd = { ... },
-- filetypes = { ... }, -- filetypes = { ... },
@ -828,11 +790,11 @@ require('lazy').setup({
-- change the command in the config to whatever the name of that colorscheme is. -- change the command in the config to whatever the name of that colorscheme is.
-- --
-- If you want to see what colorschemes are already installed, you can use `:Telescope colorscheme`. -- If you want to see what colorschemes are already installed, you can use `:Telescope colorscheme`.
'folke/tokyonight.nvim', 'thesimonho/kanagawa-paper.nvim',
priority = 1000, -- Make sure to load this before all the other start plugins. priority = 1000, -- Make sure to load this before all the other start plugins.
config = function() config = function()
---@diagnostic disable-next-line: missing-fields ---@diagnostic disable-next-line: missing-fields
require('tokyonight').setup { require('kanagawa-paper').setup {
styles = { styles = {
comments = { italic = false }, -- Disable italics in comments comments = { italic = false }, -- Disable italics in comments
}, },
@ -841,7 +803,14 @@ require('lazy').setup({
-- Load the colorscheme here. -- Load the colorscheme here.
-- Like many other themes, this one has different styles, and you could load -- Like many other themes, this one has different styles, and you could load
-- any other, such as 'tokyonight-storm', 'tokyonight-moon', or 'tokyonight-day'. -- any other, such as 'tokyonight-storm', 'tokyonight-moon', or 'tokyonight-day'.
vim.cmd.colorscheme 'tokyonight-night' vim.cmd.colorscheme 'kanagawa-paper'
vim.api.nvim_set_hl(0, 'Normal', { bg = 'none' })
vim.api.nvim_set_hl(0, 'NormalNC', { bg = 'none' })
vim.api.nvim_set_hl(0, 'NormalFloat', { bg = 'none' })
vim.api.nvim_set_hl(0, 'FloatBorder', { bg = 'none' })
vim.api.nvim_set_hl(0, 'SignColumn', { bg = 'none' })
vim.api.nvim_set_hl(0, 'VertSplit', { bg = 'none' })
end, end,
}, },

View File

@ -5,38 +5,46 @@
-- Bootstrap lazy.nvim -- Bootstrap lazy.nvim
return { return {
{ {
{ 'nvim-neo-tree/neo-tree.nvim',
'ray-x/go.nvim', branch = 'v3.x',
dependencies = { dependencies = {
'ray-x/guihua.lua', -- required UI library 'nvim-lua/plenary.nvim',
'nvim-treesitter/nvim-treesitter', 'MunifTanjim/nui.nvim',
'neovim/nvim-lspconfig', 'nvim-tree/nvim-web-devicons', -- optional, but recommended
},
ft = { 'go', 'gomod' },
config = function()
require('go').setup {
lsp_cfg = true, -- auto-setup gopls
lsp_on_attach = function(client, bufnr)
-- Auto format on save
if client.server_capabilities.documentFormattingProvider then
vim.api.nvim_create_autocmd('BufWritePre', {
group = vim.api.nvim_create_augroup('GoFormat', { clear = true }),
buffer = bufnr,
callback = function()
vim.lsp.buf.format { async = false }
end,
})
end
end,
}
end,
}, },
{ lazy = false, -- neo-tree will lazily load itself
'windwp/nvim-autopairs', },
event = 'InsertEnter', {
config = true, 'ray-x/go.nvim',
-- use opts = {} for passing setup options dependencies = {
-- this is equivalent to setup({}) function 'ray-x/guihua.lua', -- required UI library
'nvim-treesitter/nvim-treesitter',
'neovim/nvim-lspconfig',
}, },
ft = { 'go', 'gomod' },
config = function()
require('go').setup {
lsp_cfg = true, -- auto-setup gopls
lsp_on_attach = function(client, bufnr)
-- Auto format on save
if client.server_capabilities.documentFormattingProvider then
vim.api.nvim_create_autocmd('BufWritePre', {
group = vim.api.nvim_create_augroup('GoFormat', { clear = true }),
buffer = bufnr,
callback = function()
vim.lsp.buf.format { async = false }
end,
})
end
end,
}
end,
},
{
'windwp/nvim-autopairs',
event = 'InsertEnter',
config = true,
-- use opts = {} for passing setup options
-- this is equivalent to setup({}) function
}, },
} }

View File

@ -0,0 +1,13 @@
lspconfig.gopls.setup {
capabilities = capabilities,
on_attach = on_attach,
settings = {
gopls = {
completeUnimported = true,
usePlaceholders = true,
analyses = {
unusedparams = true,
},
},
},
}