From 33c17d48ff7bf7c3db92fc1e6e0e16effddc5319 Mon Sep 17 00:00:00 2001
From: Michael Davis <mcarsondavis@gmail.com>
Date: Sun, 16 Mar 2025 13:50:56 -0400
Subject: [PATCH] minor: Move 'execute_lsp_command' helper into helix-view

This is a minor move that will make future refactors of code actions
simpler. We should be able to move nearly all code action functionality
into `helix-view`, save UI stuff like the `menu::Item` implementation
and dealings with the compositor.
---
 helix-term/src/commands/lsp.rs   | 31 ++-----------------------------
 helix-term/src/commands/typed.rs |  7 +++----
 helix-view/src/handlers/lsp.rs   | 18 ++++++++++++++++++
 3 files changed, 23 insertions(+), 33 deletions(-)

diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs
index c5442924..f9603166 100644
--- a/helix-term/src/commands/lsp.rs
+++ b/helix-term/src/commands/lsp.rs
@@ -772,7 +772,7 @@ pub fn code_action(cx: &mut Context) {
                 match &action.lsp_item {
                     lsp::CodeActionOrCommand::Command(command) => {
                         log::debug!("code action command: {:?}", command);
-                        execute_lsp_command(editor, action.language_server_id, command.clone());
+                        editor.execute_lsp_command(command.clone(), action.language_server_id);
                     }
                     lsp::CodeActionOrCommand::CodeAction(code_action) => {
                         log::debug!("code action: {:?}", code_action);
@@ -801,7 +801,7 @@ pub fn code_action(cx: &mut Context) {
                         // if code action provides both edit and command first the edit
                         // should be applied and then the command
                         if let Some(command) = &code_action.command {
-                            execute_lsp_command(editor, action.language_server_id, command.clone());
+                            editor.execute_lsp_command(command.clone(), action.language_server_id);
                         }
                     }
                 }
@@ -817,33 +817,6 @@ pub fn code_action(cx: &mut Context) {
     });
 }
 
-pub fn execute_lsp_command(
-    editor: &mut Editor,
-    language_server_id: LanguageServerId,
-    cmd: lsp::Command,
-) {
-    // the command is executed on the server and communicated back
-    // to the client asynchronously using workspace edits
-    let future = match editor
-        .language_server_by_id(language_server_id)
-        .and_then(|language_server| language_server.command(cmd))
-    {
-        Some(future) => future,
-        None => {
-            editor.set_error("Language server does not support executing commands");
-            return;
-        }
-    };
-
-    tokio::spawn(async move {
-        let res = future.await;
-
-        if let Err(e) = res {
-            log::error!("execute LSP command: {}", e);
-        }
-    });
-}
-
 #[derive(Debug)]
 pub struct ApplyEditError {
     pub kind: ApplyEditErrorKind,
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index bcb6e543..fd741bf9 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -1456,7 +1456,7 @@ fn lsp_workspace_command(
                         commands,
                         (),
                         move |cx, (ls_id, command), _action| {
-                            execute_lsp_command(cx.editor, *ls_id, command.clone());
+                            cx.editor.execute_lsp_command(command.clone(), *ls_id);
                         },
                     );
                     compositor.push(Box::new(overlaid(picker)))
@@ -1484,14 +1484,13 @@ fn lsp_workspace_command(
                     .transpose()?
                     .filter(|args| !args.is_empty());
 
-                execute_lsp_command(
-                    cx.editor,
-                    *ls_id,
+                cx.editor.execute_lsp_command(
                     helix_lsp::lsp::Command {
                         title: command.clone(),
                         arguments,
                         command,
                     },
+                    *ls_id,
                 );
             }
             [] => {
diff --git a/helix-view/src/handlers/lsp.rs b/helix-view/src/handlers/lsp.rs
index dc2e1755..bf541929 100644
--- a/helix-view/src/handlers/lsp.rs
+++ b/helix-view/src/handlers/lsp.rs
@@ -361,4 +361,22 @@ impl Editor {
             helix_event::dispatch(DiagnosticsDidChange { editor: self, doc });
         }
     }
+
+    pub fn execute_lsp_command(&mut self, command: lsp::Command, server_id: LanguageServerId) {
+        // the command is executed on the server and communicated back
+        // to the client asynchronously using workspace edits
+        let Some(future) = self
+            .language_server_by_id(server_id)
+            .and_then(|server| server.command(command))
+        else {
+            self.set_error("Language server does not support executing commands");
+            return;
+        };
+
+        tokio::spawn(async move {
+            if let Err(err) = future.await {
+                log::error!("Error executing LSP command: {err}");
+            }
+        });
+    }
 }