Do not detach subscriptions

This commit is contained in:
Kirill Bulatov 2023-10-17 15:41:27 +03:00
parent 33296802fb
commit 5a4161d293

View file

@ -1,4 +1,4 @@
use collections::HashMap; use collections::{HashMap, VecDeque};
use editor::Editor; use editor::Editor;
use futures::{channel::mpsc, StreamExt}; use futures::{channel::mpsc, StreamExt};
use gpui::{ use gpui::{
@ -36,7 +36,7 @@ struct ProjectState {
} }
struct LanguageServerState { struct LanguageServerState {
log_storage: Vec<String>, log_storage: VecDeque<String>,
rpc_state: Option<LanguageServerRpcState>, rpc_state: Option<LanguageServerRpcState>,
_io_logs_subscription: Option<lsp::Subscription>, _io_logs_subscription: Option<lsp::Subscription>,
_lsp_logs_subscription: Option<lsp::Subscription>, _lsp_logs_subscription: Option<lsp::Subscription>,
@ -49,6 +49,7 @@ struct LanguageServerRpcState {
pub struct LspLogView { pub struct LspLogView {
pub(crate) editor: ViewHandle<Editor>, pub(crate) editor: ViewHandle<Editor>,
_editor_subscription: Subscription,
log_store: ModelHandle<LogStore>, log_store: ModelHandle<LogStore>,
current_server_id: Option<LanguageServerId>, current_server_id: Option<LanguageServerId>,
is_showing_rpc_trace: bool, is_showing_rpc_trace: bool,
@ -168,14 +169,14 @@ impl LogStore {
project: &ModelHandle<Project>, project: &ModelHandle<Project>,
id: LanguageServerId, id: LanguageServerId,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Option<&mut Vec<String>> { ) -> Option<&mut LanguageServerState> {
let project_state = self.projects.get_mut(&project.downgrade())?; let project_state = self.projects.get_mut(&project.downgrade())?;
let server_state = project_state.servers.entry(id).or_insert_with(|| { let server_state = project_state.servers.entry(id).or_insert_with(|| {
cx.notify(); cx.notify();
LanguageServerState { LanguageServerState {
rpc_state: None, rpc_state: None,
// TODO kb move this to settings? // TODO kb move this to settings?
log_storage: Vec::with_capacity(10_000), log_storage: VecDeque::with_capacity(10_000),
_io_logs_subscription: None, _io_logs_subscription: None,
_lsp_logs_subscription: None, _lsp_logs_subscription: None,
} }
@ -185,7 +186,7 @@ impl LogStore {
if let Some(server) = server.as_deref() { if let Some(server) = server.as_deref() {
if server.has_notification_handler::<lsp::notification::LogMessage>() { if server.has_notification_handler::<lsp::notification::LogMessage>() {
// Another event wants to re-add the server that was already added and subscribed to, avoid doing it again. // Another event wants to re-add the server that was already added and subscribed to, avoid doing it again.
return Some(&mut server_state.log_storage); return Some(server_state);
} }
} }
@ -214,7 +215,7 @@ impl LogStore {
} }
}) })
}); });
Some(&mut server_state.log_storage) Some(server_state)
} }
fn add_language_server_log( fn add_language_server_log(
@ -224,22 +225,24 @@ impl LogStore {
message: &str, message: &str,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Option<()> { ) -> Option<()> {
let log_lines = match self let language_server_state = match self
.projects .projects
.get_mut(&project.downgrade())? .get_mut(&project.downgrade())?
.servers .servers
.get_mut(&id) .get_mut(&id)
.map(|state| &mut state.log_storage)
{ {
Some(existing_buffer) => existing_buffer, Some(existing_state) => existing_state,
None => self.add_language_server(&project, id, cx)?, None => self.add_language_server(&project, id, cx)?,
}; };
// TODO kb something better VecDequeue? let log_lines = &mut language_server_state.log_storage;
if log_lines.capacity() == log_lines.len() { if log_lines.capacity() == log_lines.len() {
log_lines.drain(..log_lines.len() / 2); log_lines.pop_front();
} }
log_lines.push(message.trim().to_string()); log_lines.push_back(message.trim().to_string());
//// TODO kb refresh editor too
//need LspLogView.
cx.notify(); cx.notify();
Some(()) Some(())
@ -261,7 +264,7 @@ impl LogStore {
&self, &self,
project: &ModelHandle<Project>, project: &ModelHandle<Project>,
server_id: LanguageServerId, server_id: LanguageServerId,
) -> Option<&[String]> { ) -> Option<&VecDeque<String>> {
let weak_project = project.downgrade(); let weak_project = project.downgrade();
let project_state = self.projects.get(&weak_project)?; let project_state = self.projects.get(&weak_project)?;
let server_state = project_state.servers.get(&server_id)?; let server_state = project_state.servers.get(&server_id)?;
@ -408,8 +411,10 @@ impl LspLogView {
cx.notify(); cx.notify();
}); });
let (editor, _editor_subscription) = Self::editor_for_buffer(project.clone(), buffer, cx);
let mut this = Self { let mut this = Self {
editor: Self::editor_for_buffer(project.clone(), buffer, cx), editor,
_editor_subscription,
project, project,
log_store, log_store,
current_server_id: None, current_server_id: None,
@ -426,16 +431,15 @@ impl LspLogView {
project: ModelHandle<Project>, project: ModelHandle<Project>,
buffer: ModelHandle<Buffer>, buffer: ModelHandle<Buffer>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> ViewHandle<Editor> { ) -> (ViewHandle<Editor>, Subscription) {
let editor = cx.add_view(|cx| { let editor = cx.add_view(|cx| {
let mut editor = Editor::for_buffer(buffer, Some(project), cx); let mut editor = Editor::for_buffer(buffer, Some(project), cx);
editor.set_read_only(true); editor.set_read_only(true);
editor.move_to_end(&Default::default(), cx); editor.move_to_end(&Default::default(), cx);
editor editor
}); });
cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone())) let subscription = cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone()));
.detach(); (editor, subscription)
editor
} }
pub(crate) fn menu_items<'a>(&'a self, cx: &'a AppContext) -> Option<Vec<LogMenuItem>> { pub(crate) fn menu_items<'a>(&'a self, cx: &'a AppContext) -> Option<Vec<LogMenuItem>> {
@ -488,19 +492,27 @@ impl LspLogView {
.log_store .log_store
.read(cx) .read(cx)
.server_logs(&self.project, server_id) .server_logs(&self.project, server_id)
.map(|lines| lines.join("\n")); .map(|lines| {
let (a, b) = lines.as_slices();
let log_contents = a.join("\n");
if b.is_empty() {
log_contents
} else {
log_contents + "\n" + &b.join("\n")
}
});
if let Some(log_contents) = log_contents { if let Some(log_contents) = log_contents {
self.current_server_id = Some(server_id); self.current_server_id = Some(server_id);
self.is_showing_rpc_trace = false; self.is_showing_rpc_trace = false;
let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, log_contents));
let editor = cx.add_view(|cx| { let editor = cx.add_view(|cx| {
let mut editor = Editor::for_buffer(buffer, Some(self.project.clone()), cx); let mut editor = Editor::multi_line(None, cx);
editor.set_read_only(true); editor.set_read_only(true);
editor.move_to_end(&Default::default(), cx); editor.move_to_end(&Default::default(), cx);
editor.set_text(log_contents, cx);
editor editor
}); });
cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone())) self._editor_subscription =
.detach(); cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone()));
self.editor = editor; self.editor = editor;
cx.notify(); cx.notify();
} }
@ -518,7 +530,10 @@ impl LspLogView {
if let Some(buffer) = buffer { if let Some(buffer) = buffer {
self.current_server_id = Some(server_id); self.current_server_id = Some(server_id);
self.is_showing_rpc_trace = true; self.is_showing_rpc_trace = true;
self.editor = Self::editor_for_buffer(self.project.clone(), buffer, cx); let (editor, _editor_subscription) =
Self::editor_for_buffer(self.project.clone(), buffer, cx);
self.editor = editor;
self._editor_subscription = _editor_subscription;
cx.notify(); cx.notify();
} }
} }