Line data Source code
1 : -- lua/yoda/commands/lsp.lua
2 : -- LSP utility commands
3 :
4 1 : local M = {}
5 :
6 : --- Show LSP clients attached to current buffer
7 : local function show_lsp_clients()
8 : local clients = vim.lsp.get_clients({ bufnr = 0 })
9 : if #clients == 0 then
10 : vim.notify("No LSP clients attached to current buffer", vim.log.levels.INFO)
11 : return
12 : end
13 :
14 : local lines = { "LSP clients attached to current buffer:" }
15 : for _, client in pairs(clients) do
16 : table.insert(lines, " - " .. client.name .. " (id: " .. client.id .. ")")
17 : end
18 : vim.notify(table.concat(lines, "\n"), vim.log.levels.INFO)
19 1 : end
20 :
21 : --- Debug Helm template detection and LSP setup
22 : local function debug_helm_setup()
23 : local filepath = vim.fn.expand("%:p")
24 : local current_ft = vim.bo.filetype
25 :
26 : local lines = {
27 : "=== Helm Debug Info ===",
28 : "File path: " .. filepath,
29 : "Current filetype: " .. current_ft,
30 : }
31 :
32 : -- Check if file matches Helm patterns
33 : local path_lower = filepath:lower()
34 : local is_templates_dir = path_lower:match("/templates/[^/]*%.ya?ml$") ~= nil
35 : local is_charts_templates = path_lower:match("/charts/.*/templates/") ~= nil
36 : local is_crds = path_lower:match("/crds/[^/]*%.ya?ml$") ~= nil
37 :
38 : table.insert(lines, "\nPattern matches:")
39 : table.insert(lines, " templates/ directory: " .. tostring(is_templates_dir))
40 : table.insert(lines, " charts/.../templates/: " .. tostring(is_charts_templates))
41 : table.insert(lines, " crds/ directory: " .. tostring(is_crds))
42 :
43 : -- Check LSP clients
44 : local clients = vim.lsp.get_clients({ bufnr = 0 })
45 : table.insert(lines, "\nAttached LSP clients:")
46 : if #clients == 0 then
47 : table.insert(lines, " None")
48 : else
49 : for _, client in pairs(clients) do
50 : table.insert(lines, " - " .. client.name)
51 : end
52 : end
53 :
54 : vim.notify(table.concat(lines, "\n"), vim.log.levels.INFO)
55 1 : end
56 :
57 : --- Show comprehensive LSP status for the current buffer and environment
58 : local function lsp_status()
59 : local clients = vim.lsp.get_clients()
60 : local bufnr = vim.api.nvim_get_current_buf()
61 : local filetype = vim.bo[bufnr].filetype
62 : local filename = vim.api.nvim_buf_get_name(bufnr)
63 :
64 : local lines = {
65 : "=== LSP Status Debug ===",
66 : "Current buffer: " .. bufnr,
67 : "Current filetype: " .. filetype,
68 : "Filename: " .. filename,
69 : "Total active LSP clients: " .. #clients,
70 : "",
71 : "--- Running LSP Servers ---",
72 : }
73 :
74 : if #clients == 0 then
75 : table.insert(lines, " No LSP clients are running")
76 : else
77 : for _, client in ipairs(clients) do
78 : local attached = client.attached_buffers[bufnr] ~= nil
79 : local root_dir = client.config and client.config.root_dir or "unknown"
80 : table.insert(lines, string.format(" %s (id:%d): attached=%s, root=%s", client.name, client.id, attached, root_dir))
81 : end
82 : end
83 : table.insert(lines, "")
84 :
85 : table.insert(lines, "--- Available LSP Servers ---")
86 : local available_servers = { "gopls", "lua_ls", "ts_ls", "basedpyright", "yamlls", "jdtls", "marksman" }
87 : for _, server in ipairs(available_servers) do
88 : local cmd_available = vim.fn.executable(server) == 1
89 : table.insert(lines, string.format(" %s: %s", server, cmd_available and "available" or "not found"))
90 : end
91 : table.insert(lines, "========================")
92 :
93 : vim.notify(table.concat(lines, "\n"), vim.log.levels.INFO)
94 1 : end
95 :
96 : --- Debug Python LSP (basedpyright) configuration
97 : local function python_lsp_debug()
98 : local bufnr = vim.api.nvim_get_current_buf()
99 : local clients = vim.lsp.get_clients({ bufnr = bufnr, name = "basedpyright" })
100 :
101 : local lines = {
102 : "=== Python LSP Debug ===",
103 : "Buffer filetype: " .. vim.bo[bufnr].filetype,
104 : "Python LSP clients: " .. #clients,
105 : }
106 :
107 : if #clients > 0 then
108 : local client = clients[1]
109 : table.insert(lines, "Client name: " .. client.name)
110 : table.insert(lines, "Client ID: " .. client.id)
111 : table.insert(lines, "Root dir: " .. (client.config and client.config.root_dir or "unknown"))
112 :
113 : if client.config and client.config.settings and client.config.settings.basedpyright then
114 : local settings = client.config.settings.basedpyright.analysis
115 : table.insert(lines, "Python path: " .. (settings.pythonPath or "default"))
116 : table.insert(lines, "Extra paths: " .. vim.inspect(settings.extraPaths or {}))
117 : table.insert(lines, "Auto search paths: " .. tostring(settings.autoSearchPaths or false))
118 : table.insert(lines, "Diagnostic mode: " .. (settings.diagnosticMode or "default"))
119 : end
120 : else
121 : table.insert(lines, "No Python LSP clients attached!")
122 :
123 : if vim.fn.executable("basedpyright-langserver") == 1 then
124 : table.insert(lines, "basedpyright-langserver is available")
125 : else
126 : table.insert(lines, "basedpyright-langserver not found in PATH")
127 : table.insert(lines, "Install with: npm install -g basedpyright")
128 : end
129 :
130 : local root = vim.fs.root(bufnr, { "pyproject.toml", "setup.py", "requirements.txt", ".git" })
131 : table.insert(lines, "Detected project root: " .. (root or "none"))
132 :
133 : local cwd = vim.fn.getcwd()
134 : local possible_venvs = {
135 : cwd .. "/.venv/bin/python",
136 : cwd .. "/venv/bin/python",
137 : cwd .. "/env/bin/python",
138 : }
139 :
140 : table.insert(lines, "Virtual environment check:")
141 : for _, path in ipairs(possible_venvs) do
142 : if vim.fn.executable(path) == 1 then
143 : table.insert(lines, " found: " .. path)
144 : else
145 : table.insert(lines, " missing: " .. path)
146 : end
147 : end
148 : end
149 : table.insert(lines, "========================")
150 :
151 : vim.notify(table.concat(lines, "\n"), vim.log.levels.INFO)
152 1 : end
153 :
154 : --- Debug Groovy/Java LSP (jdtls) configuration
155 : local function groovy_lsp_debug()
156 : local bufnr = vim.api.nvim_get_current_buf()
157 : local clients = vim.lsp.get_clients({ bufnr = bufnr, name = "jdtls" })
158 :
159 : local lines = {
160 : "=== Groovy/Java LSP Debug ===",
161 : "Buffer filetype: " .. vim.bo[bufnr].filetype,
162 : "JDTLS clients: " .. #clients,
163 : }
164 :
165 : if #clients > 0 then
166 : local client = clients[1]
167 : table.insert(lines, "Client name: " .. client.name)
168 : table.insert(lines, "Client ID: " .. client.id)
169 : table.insert(lines, "Root dir: " .. (client.config and client.config.root_dir or "unknown"))
170 : table.insert(lines, "Status: " .. (client:is_stopped() and "stopped" or "running"))
171 :
172 : if client.config and client.config.settings and client.config.settings.java then
173 : table.insert(lines, "Java settings configured: yes")
174 : end
175 :
176 : table.insert(lines, "Client attached buffers: " .. vim.tbl_count(client.attached_buffers))
177 : else
178 : table.insert(lines, "No JDTLS clients attached!")
179 :
180 : if vim.fn.executable("jdtls") == 1 then
181 : table.insert(lines, "jdtls is available")
182 : else
183 : table.insert(lines, "jdtls not found in PATH")
184 : table.insert(lines, "Install Eclipse JDT Language Server")
185 : end
186 :
187 : local root = vim.fs.root(bufnr, { "build.gradle", "build.gradle.kts", "pom.xml", "settings.gradle", "settings.gradle.kts", ".git" })
188 : table.insert(lines, "Detected project root: " .. (root or "none"))
189 :
190 : if vim.fn.executable("java") == 1 then
191 : table.insert(lines, "Java runtime available")
192 : else
193 : table.insert(lines, "Java runtime not found")
194 : end
195 : end
196 : table.insert(lines, "========================")
197 :
198 : vim.notify(table.concat(lines, "\n"), vim.log.levels.INFO)
199 1 : end
200 :
201 1 : function M.setup()
202 11 : vim.api.nvim_create_user_command("YodaLspInfo", show_lsp_clients, { desc = "Show LSP clients for current buffer" })
203 11 : vim.api.nvim_create_user_command("YodaHelmDebug", debug_helm_setup, { desc = "Debug Helm template detection and LSP" })
204 11 : vim.api.nvim_create_user_command("LSPStatus", lsp_status, { desc = "Show comprehensive LSP status" })
205 22 : vim.api.nvim_create_user_command("LSPRestart", function()
206 : vim.cmd("lsp restart *")
207 : vim.notify("LSP clients restarted", vim.log.levels.INFO)
208 11 : end, { desc = "Restart LSP clients" })
209 22 : vim.api.nvim_create_user_command("LSPInfo", function()
210 : vim.cmd("lsp")
211 11 : end, { desc = "Show LSP information" })
212 11 : vim.api.nvim_create_user_command("PythonLSPDebug", python_lsp_debug, { desc = "Debug Python LSP configuration" })
213 11 : vim.api.nvim_create_user_command("GroovyLSPDebug", groovy_lsp_debug, { desc = "Debug Groovy/Java LSP configuration" })
214 12 : end
215 :
216 1 : return M
|