Skip to content

Commit 4564d70

Browse files
committed
fix lsp
1 parent bc15b5b commit 4564d70

File tree

8 files changed

+278
-210
lines changed

8 files changed

+278
-210
lines changed

init.lua

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,92 @@
1+
-- ~/.config/nvim/init.lua
12
require 'core.options' -- Load general options
23
require 'core.keymaps' -- Load general keymaps
34
require 'core.snippets' -- Custom code snippets
45

5-
-- Install package manager
6-
local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
6+
-- Install package manager (lazy.nvim)
7+
local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim'
78
if not vim.loop.fs_stat(lazypath) then
8-
vim.fn.system {
9-
'git',
10-
'clone',
11-
'--filter=blob:none',
9+
vim.fn.system({
10+
'git', 'clone', '--filter=blob:none',
1211
'https://github.com/folke/lazy.nvim.git',
13-
'--branch=stable', -- latest stable release
14-
lazypath,
15-
}
12+
'--branch=stable', lazypath,
13+
})
1614
end
1715
vim.opt.rtp:prepend(lazypath)
16+
17+
-- Filetypes
1818
vim.filetype.add({
19-
extension = {
20-
templ = "templ",
21-
}
19+
extension = { templ = 'templ' },
2220
})
2321

24-
-- Import color theme based on environment variable NVIM_THEME
22+
-- Theme selection (robust against unknown NVIM_THEME)
2523
local default_color_scheme = 'quantum'
26-
local env_var_nvim_theme = os.getenv 'NVIM_THEME' or default_color_scheme
27-
28-
-- Define a table of theme modules
24+
local env_var_nvim_theme = os.getenv('NVIM_THEME') or default_color_scheme
2925
local themes = {
3026
quantum = 'plugins.themes.quantum',
31-
nord = 'plugins.themes.nord',
27+
nord = 'plugins.themes.nord',
3228
onedark = 'plugins.themes.onedark',
3329
}
30+
local theme_module = themes[env_var_nvim_theme] or themes[default_color_scheme]
3431

35-
-- Setup plugins
32+
-- Plugins
3633
require('lazy').setup({
37-
require(themes[env_var_nvim_theme]),
34+
require(theme_module),
3835
require 'core.ui',
3936

40-
require 'plugins.aaa', -- Mason setup
41-
require 'plugins.aerial',
42-
require 'plugins.flash',
43-
require 'plugins.autocompletion',
44-
require 'plugins.bufferline',
45-
require 'plugins.comment',
46-
require 'plugins.conform',
47-
require 'plugins.database',
48-
require 'plugins.debug',
49-
require 'plugins.gitsigns',
50-
require 'plugins.harpoon',
51-
require 'plugins.lazygit',
52-
require 'plugins.lsp',
37+
-- Load mason early so tools are ready for LSP configs
38+
require 'plugins.mason',
39+
40+
-- Core dev UX
41+
require 'plugins.treesitter',
42+
require 'plugins.telescope',
5343
require 'plugins.lualine',
54-
require 'plugins.none-ls',
44+
require 'plugins.bufferline',
5545
require 'plugins.indent-blankline',
5646
require 'plugins.neo-tree',
57-
require 'plugins.misc',
58-
require 'plugins.snack',
59-
require 'plugins.telescope',
6047
require 'plugins.toggleterm',
61-
require 'plugins.treesitter',
6248
require 'plugins.vim-tmux-navigator',
6349
require 'plugins.zellij',
50+
require 'plugins.flash',
51+
require 'plugins.comment',
52+
require 'plugins.harpoon',
53+
require 'plugins.gitsigns',
54+
require 'plugins.lazygit',
55+
require 'plugins.aerial',
56+
require 'plugins.misc',
6457

58+
-- LSP & companions
59+
require 'plugins.autocompletion',
60+
require 'plugins.lsp',
61+
require 'plugins.none-ls', -- none-ls/null-ls sources & setup
62+
require 'plugins.autoformat', -- your autoformat-on-save/idle logic
63+
64+
-- Optional: pick one formatter stack. If you keep Conform,
65+
-- ensure it doesn't also format Go on save to avoid double-format.
66+
require 'plugins.conform',
67+
68+
-- Debugging / DB (as you had)
69+
require 'plugins.debug',
70+
require 'plugins.database',
6571
}, {
6672
ui = {
67-
-- If you have a Nerd Font, set icons to an empty table which will use the
68-
-- default lazy.nvim defined Nerd Font icons otherwise define a unicode icons table
6973
icons = vim.g.have_nerd_font and {} or {
70-
cmd = '',
71-
config = '🛠',
72-
event = '📅',
73-
ft = '📂',
74-
init = '',
75-
keys = '🗝',
76-
plugin = '🔌',
77-
runtime = '💻',
78-
require = '🌙',
79-
source = '📄',
80-
start = '🚀',
81-
task = '📌',
82-
lazy = '💤 ',
74+
cmd = '', config = '🛠', event = '📅', ft = '📂', init = '',
75+
keys = '🗝', plugin = '🔌', runtime = '💻', require = '🌙',
76+
source = '📄', start = '🚀', task = '📌', lazy = '💤 ',
8377
},
8478
},
8579
})
8680

87-
-- Function to check if a file exists
81+
-- (Optional) tiny helper if you ever want to source a session file
8882
local function file_exists(file)
8983
local f = io.open(file, 'r')
90-
if f then
91-
f:close()
92-
return true
93-
else
94-
return false
95-
end
84+
if f then f:close(); return true end
85+
return false
9686
end
9787

98-
-- Path to the session file
99-
local session_file = '.session.vim'
88+
-- local session_file = '.session.vim'
89+
-- if file_exists(session_file) then vim.cmd('source ' .. session_file) end
10090

101-
-- Check if the session file exists in the current directory
102-
-- if file_exists(session_file) then
103-
-- -- Source the session file
104-
-- vim.cmd('source ' .. session_file)
105-
-- end
106-
107-
-- The line beneath this is called `modeline`. See `:help modeline`
10891
-- vim: ts=2 sts=2 sw=2 et
92+

lua/plugins/aaa.lua

Lines changed: 0 additions & 19 deletions
This file was deleted.

lua/plugins/autoformat.lua

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
-- lua/plugins/autoformat.lua
2+
-- Automatically format Go code on save and when idle after changes
3+
4+
return {
5+
"neovim/nvim-lspconfig",
6+
event = { "BufReadPre", "BufNewFile" },
7+
config = function()
8+
---------------------------------------------------------------------------
9+
-- 🧹 Format on save
10+
---------------------------------------------------------------------------
11+
vim.api.nvim_create_autocmd("BufWritePre", {
12+
pattern = "*.go",
13+
callback = function()
14+
-- Runs both gopls and none-ls formatters in order
15+
vim.lsp.buf.format({ async = false })
16+
end,
17+
})
18+
19+
---------------------------------------------------------------------------
20+
-- ⚡ Auto-format when idle (after you stop typing)
21+
---------------------------------------------------------------------------
22+
local format_timer = vim.loop.new_timer()
23+
24+
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
25+
pattern = "*.go",
26+
callback = function()
27+
-- Cancel previous pending format
28+
format_timer:stop()
29+
30+
-- Wait 1.5 seconds after the last change before formatting
31+
format_timer:start(1500, 0, vim.schedule_wrap(function()
32+
-- Only format if the buffer still exists and is listed
33+
local bufnr = vim.api.nvim_get_current_buf()
34+
if vim.api.nvim_buf_is_valid(bufnr) and vim.bo[bufnr].modifiable then
35+
vim.lsp.buf.format({ async = true })
36+
end
37+
end))
38+
end,
39+
})
40+
41+
---------------------------------------------------------------------------
42+
-- 🧪 Optional: run `goimports` and quick test on save
43+
---------------------------------------------------------------------------
44+
vim.api.nvim_create_autocmd("BufWritePost", {
45+
pattern = "*.go",
46+
callback = function()
47+
-- Automatically fix imports using goimports if available
48+
vim.fn.jobstart({ "goimports", "-w", vim.fn.expand("%:p") }, {
49+
on_exit = function()
50+
-- Optionally, trigger a quick test run for feedback
51+
vim.fn.jobstart({ "go", "test", "./..." }, {
52+
cwd = vim.fn.getcwd(),
53+
stdout_buffered = true,
54+
stderr_buffered = true,
55+
on_stdout = function(_, data)
56+
if data then
57+
vim.notify(table.concat(data, "\n"), vim.log.levels.INFO, { title = "go test" })
58+
end
59+
end,
60+
on_stderr = function(_, data)
61+
if data then
62+
vim.notify(table.concat(data, "\n"), vim.log.levels.ERROR, { title = "go test" })
63+
end
64+
end,
65+
})
66+
end,
67+
})
68+
end,
69+
})
70+
end,
71+
}
72+

0 commit comments

Comments
 (0)