( view: EditorView, )
| 286 | } |
| 287 | |
| 288 | export async function fetchCodeActions( |
| 289 | view: EditorView, |
| 290 | ): Promise<CodeActionItem[]> { |
| 291 | const plugin = LSPPlugin.get(view); |
| 292 | if (!plugin) return []; |
| 293 | |
| 294 | const capabilities = plugin.client.serverCapabilities; |
| 295 | if (!capabilities?.codeActionProvider) return []; |
| 296 | |
| 297 | const { from, to } = view.state.selection.main; |
| 298 | const range: LspRange = { |
| 299 | start: plugin.toPosition(from), |
| 300 | end: plugin.toPosition(to), |
| 301 | }; |
| 302 | |
| 303 | plugin.client.sync(); |
| 304 | |
| 305 | try { |
| 306 | const response = await requestCodeActions(plugin, range); |
| 307 | if (!response?.length) return []; |
| 308 | |
| 309 | const items: CodeActionItem[] = response.map((item) => { |
| 310 | if (isCommand(item)) { |
| 311 | return { title: item.title, icon: "terminal", action: item }; |
| 312 | } |
| 313 | return { |
| 314 | title: item.title, |
| 315 | kind: item.kind, |
| 316 | icon: getCodeActionIcon(item.kind), |
| 317 | isPreferred: item.isPreferred, |
| 318 | disabled: !!item.disabled, |
| 319 | disabledReason: item.disabled?.reason, |
| 320 | action: item, |
| 321 | }; |
| 322 | }); |
| 323 | |
| 324 | // Sort: preferred first, then quickfixes, then alphabetically |
| 325 | items.sort((a, b) => { |
| 326 | if (a.isPreferred && !b.isPreferred) return -1; |
| 327 | if (!a.isPreferred && b.isPreferred) return 1; |
| 328 | if (a.kind?.startsWith("quickfix") && !b.kind?.startsWith("quickfix")) |
| 329 | return -1; |
| 330 | if (!a.kind?.startsWith("quickfix") && b.kind?.startsWith("quickfix")) |
| 331 | return 1; |
| 332 | return a.title.localeCompare(b.title); |
| 333 | }); |
| 334 | |
| 335 | return items; |
| 336 | } catch (error) { |
| 337 | addLspLogFor(plugin, "error", "Code action fetch failed", error); |
| 338 | console.error("[LSP:CodeAction] Failed to fetch:", error); |
| 339 | return []; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | export async function executeCodeAction( |
| 344 | view: EditorView, |
no test coverage detected