11return {
22 -- Main LSP Configuration
3- ' neovim/nvim-lspconfig' ,
4- event = { ' BufReadPre' , ' BufNewFile' },
3+ " neovim/nvim-lspconfig" ,
4+ event = { " BufReadPre" , " BufNewFile" },
55 dependencies = {
66 -- Allows extra capabilities provided by nvim-cmp
7- ' hrsh7th/cmp-nvim-lsp' ,
7+ " hrsh7th/cmp-nvim-lsp" ,
88 -- Modify imports when a file has been renamed
9- { ' antosha417/nvim-lsp-file-operations' , config = true },
9+ { " antosha417/nvim-lsp-file-operations" , config = true },
1010 -- Improved LSP functionality when working with lua files
11- { ' folke/neodev.nvim' , opts = {} },
11+ { " folke/neodev.nvim" , opts = {} },
1212
1313 -- Automatically install LSPs and related tools to stdpath for Neovim
1414 -- { 'williamboman/mason.nvim', config = true }, -- NOTE: Must be loaded before dependants
1515 -- 'williamboman/mason-lspconfig.nvim',
1616
1717 -- Useful status updates for LSP.
1818 -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
19- { ' j-hui/fidget.nvim' , opts = {} },
19+ { " j-hui/fidget.nvim" , opts = {} },
2020 },
2121 config = function ()
2222 -- Brief aside: **What is LSP?**
@@ -49,12 +49,12 @@ return {
4949 -- an lsp (for example, opening `main.rs` is associated with `rust_analyzer`) this
5050 -- function will be executed to configure the current buffer
5151 -- import lspconfig plugin
52- local lspconfig = require (' lspconfig' )
53- local util = require (' lspconfig/util' )
52+ local lspconfig = require (" lspconfig" )
53+ local util = require (" lspconfig/util" )
5454 lspconfig .gopls .setup ({
55- cmd = { ' gopls' },
56- filetypes = { ' go ' , ' gomod' , ' gowork' , ' gotmpl' },
57- root_dir = util .root_pattern (' go.work' , ' go.mod' , ' .git' ),
55+ cmd = { " gopls" },
56+ filetypes = { " go " , " gomod" , " gowork" , " gotmpl" },
57+ root_dir = util .root_pattern (" go.work" , " go.mod" , " .git" ),
5858 settings = {
5959 gopls = {
6060 completeUnimported = true ,
@@ -67,64 +67,64 @@ return {
6767 })
6868
6969 -- import mason_lspconfig plugin
70- local mason_lspconfig = require (' mason-lspconfig' )
70+ local mason_lspconfig = require (" mason-lspconfig" )
7171
7272 -- import cmp-nvim-lsp plugin
73- local cmp_nvim_lsp = require (' cmp_nvim_lsp' )
73+ local cmp_nvim_lsp = require (" cmp_nvim_lsp" )
7474
7575 local keymap = vim .keymap
7676
77- vim .api .nvim_create_autocmd (' LspAttach' , {
78- group = vim .api .nvim_create_augroup (' UserLspConfig' , {}),
77+ vim .api .nvim_create_autocmd (" LspAttach" , {
78+ group = vim .api .nvim_create_augroup (" UserLspConfig" , {}),
7979 callback = function (event )
8080 -- Buffer local mappings
8181 -- See `:help vim.lsp.*` for documentation on ay of the below function
8282 local opts = { buffer = event .buf , silent = true }
8383 -- set keybinds
84- opts .desc = ' Show LSP references'
85- keymap .set (' n ' , ' gR ' , ' <cmd>Telescope lsp_references<CR>' , opts ) -- show definition, references
84+ opts .desc = " Show LSP references"
85+ keymap .set (" n " , " gR " , " <cmd>Telescope lsp_references<CR>" , opts ) -- show definition, references
8686
87- opts .desc = ' Go to declaration'
88- keymap .set (' n ' , ' gD ' , vim .lsp .buf .declaration , opts ) -- go to declaration
87+ opts .desc = " Go to declaration"
88+ keymap .set (" n " , " gD " , vim .lsp .buf .declaration , opts ) -- go to declaration
8989
90- opts .desc = ' Show LSP definitions'
91- keymap .set (' n ' , ' gd ' , ' <cmd>Telescope lsp_definitions<CR>' , opts ) -- show lsp definitions
90+ opts .desc = " Show LSP definitions"
91+ keymap .set (" n " , " gd " , " <cmd>Telescope lsp_definitions<CR>" , opts ) -- show lsp definitions
9292
93- opts .desc = ' Show LSP implementations'
94- keymap .set (' n ' , ' gi ' , ' <cmd>Telescope lsp_implementations<CR>' , opts ) -- show lsp implementations
93+ opts .desc = " Show LSP implementations"
94+ keymap .set (" n " , " gi " , " <cmd>Telescope lsp_implementations<CR>" , opts ) -- show lsp implementations
9595
96- opts .desc = ' Show LSP type definitions'
97- keymap .set (' n ' , ' gt ' , ' <cmd>Telescope lsp_type_definitions<CR>' , opts ) -- show lsp type definitions
96+ opts .desc = " Show LSP type definitions"
97+ keymap .set (" n " , " gt " , " <cmd>Telescope lsp_type_definitions<CR>" , opts ) -- show lsp type definitions
9898
99- opts .desc = ' See available code actions'
100- keymap .set ({ ' n ' , ' v ' }, ' <leader>ca' , vim .lsp .buf .code_action , opts ) -- see available code actions, in visual mode will apply to selection
99+ opts .desc = " See available code actions"
100+ keymap .set ({ " n " , " v " }, " <leader>ca" , vim .lsp .buf .code_action , opts ) -- see available code actions, in visual mode will apply to selection
101101
102- opts .desc = ' Smart rename'
103- keymap .set (' n ' , ' <leader>rn' , vim .lsp .buf .rename , opts ) -- smart rename
102+ opts .desc = " Smart rename"
103+ keymap .set (" n " , " <leader>rn" , vim .lsp .buf .rename , opts ) -- smart rename
104104
105- opts .desc = ' Show buffer diagnostics'
106- keymap .set (' n ' , ' <leader>D' , ' <cmd>Telescope diagnostics bufnr=0<CR>' , opts ) -- show diagnostics for file
105+ opts .desc = " Show buffer diagnostics"
106+ keymap .set (" n " , " <leader>D" , " <cmd>Telescope diagnostics bufnr=0<CR>" , opts ) -- show diagnostics for file
107107
108- opts .desc = ' Show line diagnostics'
109- keymap .set (' n ' , ' <leader>d' , vim .diagnostic .open_float , opts ) -- show diagnostics for line
108+ opts .desc = " Show line diagnostics"
109+ keymap .set (" n " , " <leader>d" , vim .diagnostic .open_float , opts ) -- show diagnostics for line
110110
111- opts .desc = ' Go to previous diagnostic'
112- keymap .set (' n ' , ' [d ' , vim .diagnostic .goto_prev , opts ) -- jump to previous diagnostic in buffer
111+ opts .desc = " Go to previous diagnostic"
112+ keymap .set (" n " , " [d " , vim .diagnostic .goto_prev , opts ) -- jump to previous diagnostic in buffer
113113
114- opts .desc = ' Go to next diagnostic'
115- keymap .set (' n ' , ' ]d ' , vim .diagnostic .goto_next , opts ) -- jump to next diagnostic in buffer
114+ opts .desc = " Go to next diagnostic"
115+ keymap .set (" n " , " ]d " , vim .diagnostic .goto_next , opts ) -- jump to next diagnostic in buffer
116116
117- opts .desc = ' Show documentation for what is under cursor'
118- keymap .set (' n ' , ' K ' , vim .lsp .buf .hover , opts ) -- show documentation for what is under cursor
117+ opts .desc = " Show documentation for what is under cursor"
118+ keymap .set (" n " , " K " , vim .lsp .buf .hover , opts ) -- show documentation for what is under cursor
119119
120- opts .desc = ' Restart LSP'
121- keymap .set (' n ' , ' <leader>rs' , ' :LspRestart<CR>' , opts ) -- mapping to restart lsp if necessary
120+ opts .desc = " Restart LSP"
121+ keymap .set (" n " , " <leader>rs" , " :LspRestart<CR>" , opts ) -- mapping to restart lsp if necessary
122122 end ,
123123 })
124124
125125 -- Configure Neovim tab settings for Go files [Go indendation]
126- vim .api .nvim_create_autocmd (' FileType' , {
127- pattern = ' go ' ,
126+ vim .api .nvim_create_autocmd (" FileType" , {
127+ pattern = " go " ,
128128 callback = function ()
129129 vim .bo .expandtab = true -- Use spaces instead of tabs
130130 vim .bo .tabstop = 4 -- Display each tab as 4 spaces
@@ -133,13 +133,37 @@ return {
133133 end ,
134134 })
135135
136+ -- Organize imports on save. Also helps remove unused imports on save.
137+ vim .api .nvim_create_autocmd (" BufWritePre" , {
138+ pattern = " *.go" ,
139+ callback = function ()
140+ local params = vim .lsp .util .make_range_params ()
141+ params .context = { only = { " source.organizeImports" } }
142+ -- buf_request_sync defaults to a 1000ms timeout. Depending on your
143+ -- machine and codebase, you may want longer. Add an additional
144+ -- argument after params if you find that you have to write the file
145+ -- twice for changes to be saved.
146+ -- E.g., vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 3000)
147+ local result = vim .lsp .buf_request_sync (0 , " textDocument/codeAction" , params )
148+ for cid , res in pairs (result or {}) do
149+ for _ , r in pairs (res .result or {}) do
150+ if r .edit then
151+ local enc = (vim .lsp .get_client_by_id (cid ) or {}).offset_encoding or " utf-16"
152+ vim .lsp .util .apply_workspace_edit (r .edit , enc )
153+ end
154+ end
155+ end
156+ vim .lsp .buf .format ({ async = false })
157+ end ,
158+ })
159+
136160 local capabilities = cmp_nvim_lsp .default_capabilities ()
137161 -- Change the Diagnostic symbols in the sign column (gutter)
138162 -- (not in youtube nvim video)
139- local signs = { Error = ' ' , Warn = ' ' , Hint = ' ' , Info = ' ' }
163+ local signs = { Error = " " , Warn = " " , Hint = " " , Info = " " }
140164 for type , icon in pairs (signs ) do
141- local hl = ' DiagnosticSign' .. type
142- vim .fn .sign_define (hl , { text = icon , texthl = hl , numhl = ' ' })
165+ local hl = " DiagnosticSign" .. type
166+ vim .fn .sign_define (hl , { text = icon , texthl = hl , numhl = " " })
143167 end
144168
145169 mason_lspconfig .setup_handlers ({
@@ -151,18 +175,18 @@ return {
151175 end ,
152176
153177 -- Language specific configuration for any individual language server. Lua in this case
154- [' lua_ls' ] = function ()
178+ [" lua_ls" ] = function ()
155179 -- configure lua server (with special settings)
156- lspconfig [' lua_ls' ].setup ({
180+ lspconfig [" lua_ls" ].setup ({
157181 capabilities = capabilities ,
158182 settings = {
159183 Lua = {
160184 -- make the language server recognize "vim" global
161185 diagnostics = {
162- globals = { ' vim' },
186+ globals = { " vim" },
163187 },
164188 completion = {
165- callSnippet = ' Replace' ,
189+ callSnippet = " Replace" ,
166190 },
167191 },
168192 },
0 commit comments