Add restart-lsp keybinding

Keith Simmons created

Change summary

crates/editor/src/editor.rs       | 16 +++++++++++
crates/language/src/language.rs   |  2 +
crates/lsp/src/lsp.rs             | 21 ++++++++++++--
crates/project/src/project.rs     | 46 +++++++++++++++++++++++++++++++++
crates/workspace/src/workspace.rs |  6 ++++
5 files changed, 88 insertions(+), 3 deletions(-)

Detailed changes

crates/editor/src/editor.rs 🔗

@@ -141,6 +141,7 @@ action!(ToggleCodeActions, bool);
 action!(ConfirmCompletion, Option<usize>);
 action!(ConfirmCodeAction, Option<usize>);
 action!(OpenExcerpts);
+action!(RestartLanguageServer);
 
 enum DocumentHighlightRead {}
 enum DocumentHighlightWrite {}
@@ -302,6 +303,7 @@ pub fn init(cx: &mut MutableAppContext) {
         Binding::new("ctrl-space", ShowCompletions, Some("Editor")),
         Binding::new("cmd-.", ToggleCodeActions(false), Some("Editor")),
         Binding::new("alt-enter", OpenExcerpts, Some("Editor")),
+        Binding::new("cmd-f10", RestartLanguageServer, Some("Editor")),
     ]);
 
     cx.add_action(Editor::open_new);
@@ -377,6 +379,7 @@ pub fn init(cx: &mut MutableAppContext) {
     cx.add_action(Editor::show_completions);
     cx.add_action(Editor::toggle_code_actions);
     cx.add_action(Editor::open_excerpts);
+    cx.add_action(Editor::restart_language_server);
     cx.add_async_action(Editor::confirm_completion);
     cx.add_async_action(Editor::confirm_code_action);
     cx.add_async_action(Editor::rename);
@@ -4867,6 +4870,19 @@ impl Editor {
         self.pending_rename.as_ref()
     }
 
+    fn restart_language_server(&mut self, _: &RestartLanguageServer, cx: &mut ViewContext<Self>) {
+        let project = self.project.clone();
+        if let Some(project) = project {
+            self.buffer.update(cx, |multi_buffer, cx| {
+                for buffer in multi_buffer.all_buffers() {
+                    project.update(cx, |project, cx| {
+                        project.restart_language_server_for_buffer(&buffer, cx);
+                    });
+                }
+            })
+        }
+    }
+
     fn refresh_active_diagnostics(&mut self, cx: &mut ViewContext<Editor>) {
         if let Some(active_diagnostics) = self.active_diagnostics.as_mut() {
             let buffer = self.buffer.read(cx).snapshot(cx);

crates/language/src/language.rs 🔗

@@ -245,6 +245,7 @@ impl LanguageRegistry {
 
     pub fn start_language_server(
         &self,
+        server_id: usize,
         language: Arc<Language>,
         root_path: Arc<Path>,
         http_client: Arc<dyn HttpClient>,
@@ -324,6 +325,7 @@ impl LanguageRegistry {
             let server_binary_path = server_binary_path.await?;
             let server_args = adapter.server_args();
             let server = lsp::LanguageServer::new(
+                server_id,
                 &server_binary_path,
                 server_args,
                 &root_path,

crates/lsp/src/lsp.rs 🔗

@@ -34,6 +34,7 @@ type NotificationHandler =
 type ResponseHandler = Box<dyn Send + FnOnce(Result<&str, Error>)>;
 
 pub struct LanguageServer {
+    server_id: usize,
     next_id: AtomicUsize,
     outbound_tx: channel::Sender<Vec<u8>>,
     name: String,
@@ -113,6 +114,7 @@ struct Error {
 
 impl LanguageServer {
     pub fn new(
+        server_id: usize,
         binary_path: &Path,
         args: &[&str],
         root_path: &Path,
@@ -133,7 +135,8 @@ impl LanguageServer {
             .spawn()?;
         let stdin = server.stdin.take().unwrap();
         let stdout = server.stdout.take().unwrap();
-        let mut server = Self::new_internal(stdin, stdout, root_path, options, background);
+        let mut server =
+            Self::new_internal(server_id, stdin, stdout, root_path, options, background);
         if let Some(name) = binary_path.file_name() {
             server.name = name.to_string_lossy().to_string();
         }
@@ -141,6 +144,7 @@ impl LanguageServer {
     }
 
     fn new_internal<Stdin, Stdout>(
+        server_id: usize,
         stdin: Stdin,
         stdout: Stdout,
         root_path: &Path,
@@ -240,6 +244,7 @@ impl LanguageServer {
         });
 
         Self {
+            server_id,
             notification_handlers,
             response_handlers,
             name: Default::default(),
@@ -446,6 +451,10 @@ impl LanguageServer {
         &self.capabilities
     }
 
+    pub fn server_id(&self) -> usize {
+        self.server_id
+    }
+
     pub fn request<T: request::Request>(
         self: &Arc<Self>,
         params: T::Params,
@@ -606,8 +615,14 @@ impl LanguageServer {
         });
 
         let executor = cx.background().clone();
-        let server =
-            Self::new_internal(stdin_writer, stdout_reader, Path::new("/"), None, executor);
+        let server = Self::new_internal(
+            0,
+            stdin_writer,
+            stdout_reader,
+            Path::new("/"),
+            None,
+            executor,
+        );
         (server, fake)
     }
 }

crates/project/src/project.rs 🔗

@@ -1308,6 +1308,7 @@ impl Project {
             .or_insert_with(|| {
                 let server_id = post_inc(&mut self.next_language_server_id);
                 let language_server = self.languages.start_language_server(
+                    server_id,
                     language.clone(),
                     worktree_path,
                     self.client.http_client(),
@@ -1507,6 +1508,51 @@ impl Project {
             });
     }
 
+    pub fn restart_language_server_for_buffer(
+        &mut self,
+        buffer: &ModelHandle<Buffer>,
+        cx: &mut ModelContext<Self>,
+    ) -> Option<()> {
+        let file = File::from_dyn(buffer.read(cx).file())?;
+        let worktree = file.worktree.read(cx).as_local()?;
+        let worktree_id = worktree.id();
+        let worktree_abs_path = worktree.abs_path().clone();
+        let full_path = buffer.read(cx).file()?.full_path(cx);
+        let language = self.languages.select_language(&full_path)?;
+        self.restart_language_server(worktree_id, worktree_abs_path, language, cx);
+
+        None
+    }
+
+    fn restart_language_server(
+        &mut self,
+        worktree_id: WorktreeId,
+        worktree_path: Arc<Path>,
+        language: Arc<Language>,
+        cx: &mut ModelContext<Self>,
+    ) {
+        let key = (worktree_id, language.name());
+        let server_to_shutdown = self.language_servers.remove(&key);
+        self.started_language_servers.remove(&key);
+        server_to_shutdown
+            .as_ref()
+            .map(|server| self.language_server_statuses.remove(&server.server_id()));
+        cx.spawn_weak(|this, mut cx| async move {
+            if let Some(this) = this.upgrade(&cx) {
+                if let Some(server_to_shutdown) = server_to_shutdown {
+                    if let Some(shutdown_task) = server_to_shutdown.shutdown() {
+                        shutdown_task.await;
+                    }
+                }
+
+                this.update(&mut cx, |this, cx| {
+                    this.start_language_server(worktree_id, worktree_path, language, cx);
+                });
+            }
+        })
+        .detach();
+    }
+
     fn on_lsp_event(
         &mut self,
         language_server_id: usize,

crates/workspace/src/workspace.rs 🔗

@@ -80,6 +80,7 @@ action!(Save);
 action!(DebugElements);
 action!(ActivatePreviousPane);
 action!(ActivateNextPane);
+action!(RestartLanguageServer);
 
 pub fn init(client: &Arc<Client>, cx: &mut MutableAppContext) {
     pane::init(cx);
@@ -119,6 +120,9 @@ pub fn init(client: &Arc<Client>, cx: &mut MutableAppContext) {
     cx.add_action(|workspace: &mut Workspace, _: &ActivateNextPane, cx| {
         workspace.activate_next_pane(cx)
     });
+    cx.add_action(|workspace: &mut Workspace, _: &RestartLanguageServer, cx| {
+        workspace.restart_language_server(cx)
+    });
     cx.add_bindings(vec![
         Binding::new("ctrl-alt-cmd-f", FollowNextCollaborator, None),
         Binding::new("cmd-s", Save, None),
@@ -1419,6 +1423,8 @@ impl Workspace {
         None
     }
 
+    fn restart_language_server(&mut self, cx: &mut ViewContext<Self>) {}
+
     fn render_connection_status(&self, cx: &mut RenderContext<Self>) -> Option<ElementBox> {
         let theme = &cx.global::<Settings>().theme;
         match &*self.client.status().borrow() {