Add restart-lsp keybinding

This commit is contained in:
Keith Simmons 2022-03-29 17:24:23 -07:00
parent cc9843c90e
commit 6d91fd078c
5 changed files with 88 additions and 3 deletions

View file

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

View file

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

View file

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

View file

@ -1308,6 +1308,7 @@ impl Project {
.or_insert_with(|| { .or_insert_with(|| {
let server_id = post_inc(&mut self.next_language_server_id); let server_id = post_inc(&mut self.next_language_server_id);
let language_server = self.languages.start_language_server( let language_server = self.languages.start_language_server(
server_id,
language.clone(), language.clone(),
worktree_path, worktree_path,
self.client.http_client(), 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( fn on_lsp_event(
&mut self, &mut self,
language_server_id: usize, language_server_id: usize,

View file

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