Do not create buffers for rpc logs

This commit is contained in:
Kirill Bulatov 2023-10-17 21:40:25 +03:00
parent c872c86c4a
commit 08af830fd7

View file

@ -1,5 +1,5 @@
use collections::{HashMap, VecDeque}; use collections::{HashMap, VecDeque};
use editor::Editor; use editor::{Editor, MoveToEnd};
use futures::{channel::mpsc, StreamExt}; use futures::{channel::mpsc, StreamExt};
use gpui::{ use gpui::{
actions, actions,
@ -11,7 +11,7 @@ use gpui::{
AnyElement, AppContext, Element, Entity, ModelContext, ModelHandle, Subscription, View, AnyElement, AppContext, Element, Entity, ModelContext, ModelHandle, Subscription, View,
ViewContext, ViewHandle, WeakModelHandle, ViewContext, ViewHandle, WeakModelHandle,
}; };
use language::{Buffer, LanguageServerId, LanguageServerName}; use language::{LanguageServerId, LanguageServerName};
use lsp::IoKind; use lsp::IoKind;
use project::{search::SearchQuery, Project}; use project::{search::SearchQuery, Project};
use std::{borrow::Cow, sync::Arc}; use std::{borrow::Cow, sync::Arc};
@ -22,8 +22,8 @@ use workspace::{
ToolbarItemLocation, ToolbarItemView, Workspace, WorkspaceCreated, ToolbarItemLocation, ToolbarItemView, Workspace, WorkspaceCreated,
}; };
const SEND_LINE: &str = "// Send:\n"; const SEND_LINE: &str = "// Send:";
const RECEIVE_LINE: &str = "// Receive:\n"; const RECEIVE_LINE: &str = "// Receive:";
const MAX_STORED_LOG_ENTRIES: usize = 5000; const MAX_STORED_LOG_ENTRIES: usize = 5000;
pub struct LogStore { pub struct LogStore {
@ -37,20 +37,20 @@ struct ProjectState {
} }
struct LanguageServerState { struct LanguageServerState {
log_storage: VecDeque<String>, log_messages: 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>,
} }
struct LanguageServerRpcState { struct LanguageServerRpcState {
buffer: ModelHandle<Buffer>, rpc_messages: VecDeque<String>,
last_message_kind: Option<MessageKind>, last_message_kind: Option<MessageKind>,
} }
pub struct LspLogView { pub struct LspLogView {
pub(crate) editor: ViewHandle<Editor>, pub(crate) editor: ViewHandle<Editor>,
_editor_subscription: Subscription, 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,
@ -124,10 +124,9 @@ impl LogStore {
io_tx, io_tx,
}; };
cx.spawn_weak(|this, mut cx| async move { cx.spawn_weak(|this, mut cx| async move {
while let Some((project, server_id, io_kind, mut message)) = io_rx.next().await { while let Some((project, server_id, io_kind, message)) = io_rx.next().await {
if let Some(this) = this.upgrade(&cx) { if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
message.push('\n');
this.on_io(project, server_id, io_kind, &message, cx); this.on_io(project, server_id, io_kind, &message, cx);
}); });
} }
@ -176,7 +175,7 @@ impl LogStore {
cx.notify(); cx.notify();
LanguageServerState { LanguageServerState {
rpc_state: None, rpc_state: None,
log_storage: VecDeque::with_capacity(MAX_STORED_LOG_ENTRIES), log_messages: VecDeque::with_capacity(MAX_STORED_LOG_ENTRIES),
_io_logs_subscription: None, _io_logs_subscription: None,
_lsp_logs_subscription: None, _lsp_logs_subscription: None,
} }
@ -235,16 +234,16 @@ impl LogStore {
None => self.add_language_server(&project, id, cx)?, None => self.add_language_server(&project, id, cx)?,
}; };
let log_lines = &mut language_server_state.log_storage; let log_lines = &mut language_server_state.log_messages;
if log_lines.len() == MAX_STORED_LOG_ENTRIES { if log_lines.len() == MAX_STORED_LOG_ENTRIES {
log_lines.pop_front(); log_lines.pop_front();
} }
let message = message.trim(); let message = message.trim();
log_lines.push_back(message.to_string()); log_lines.push_back(message.to_string());
cx.emit(Event::NewServerLogEntry { cx.emit(Event::NewServerLogEntry {
id, id,
entry: message.to_string(), entry: message.to_string(),
is_rpc: false,
}); });
cx.notify(); cx.notify();
Some(()) Some(())
@ -270,38 +269,24 @@ impl LogStore {
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)?;
Some(&server_state.log_storage) Some(&server_state.log_messages)
} }
fn enable_rpc_trace_for_language_server( fn enable_rpc_trace_for_language_server(
&mut self, &mut self,
project: &ModelHandle<Project>, project: &ModelHandle<Project>,
server_id: LanguageServerId, server_id: LanguageServerId,
cx: &mut ModelContext<Self>, ) -> Option<&mut LanguageServerRpcState> {
) -> Option<ModelHandle<Buffer>> {
let weak_project = project.downgrade(); let weak_project = project.downgrade();
let project_state = self.projects.get_mut(&weak_project)?; let project_state = self.projects.get_mut(&weak_project)?;
let server_state = project_state.servers.get_mut(&server_id)?; let server_state = project_state.servers.get_mut(&server_id)?;
let rpc_state = server_state.rpc_state.get_or_insert_with(|| { let rpc_state = server_state
let language = project.read(cx).languages().language_for_name("JSON"); .rpc_state
let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, "")); .get_or_insert_with(|| LanguageServerRpcState {
cx.spawn_weak({ rpc_messages: VecDeque::with_capacity(MAX_STORED_LOG_ENTRIES),
let buffer = buffer.clone();
|_, mut cx| async move {
let language = language.await.ok();
buffer.update(&mut cx, |buffer, cx| {
buffer.set_language(language, cx);
});
}
})
.detach();
LanguageServerRpcState {
buffer,
last_message_kind: None, last_message_kind: None,
}
}); });
Some(rpc_state.buffer.clone()) Some(rpc_state)
} }
pub fn disable_rpc_trace_for_language_server( pub fn disable_rpc_trace_for_language_server(
@ -330,7 +315,7 @@ impl LogStore {
IoKind::StdIn => false, IoKind::StdIn => false,
IoKind::StdErr => { IoKind::StdErr => {
let project = project.upgrade(cx)?; let project = project.upgrade(cx)?;
let message = format!("stderr: {}\n", message.trim()); let message = format!("stderr: {}", message.trim());
self.add_language_server_log(&project, language_server_id, &message, cx); self.add_language_server_log(&project, language_server_id, &message, cx);
return Some(()); return Some(());
} }
@ -343,24 +328,40 @@ impl LogStore {
.get_mut(&language_server_id)? .get_mut(&language_server_id)?
.rpc_state .rpc_state
.as_mut()?; .as_mut()?;
state.buffer.update(cx, |buffer, cx| {
let kind = if is_received { let kind = if is_received {
MessageKind::Receive MessageKind::Receive
} else { } else {
MessageKind::Send MessageKind::Send
}; };
let rpc_log_lines = &mut state.rpc_messages;
if rpc_log_lines.len() == MAX_STORED_LOG_ENTRIES {
rpc_log_lines.pop_front();
}
if state.last_message_kind != Some(kind) { if state.last_message_kind != Some(kind) {
let len = buffer.len(); let line_before_message = match kind {
let line = match kind {
MessageKind::Send => SEND_LINE, MessageKind::Send => SEND_LINE,
MessageKind::Receive => RECEIVE_LINE, MessageKind::Receive => RECEIVE_LINE,
}; };
buffer.edit([(len..len, line)], None, cx); rpc_log_lines.push_back(line_before_message.to_string());
state.last_message_kind = Some(kind); cx.emit(Event::NewServerLogEntry {
} id: language_server_id,
let len = buffer.len(); entry: line_before_message.to_string(),
buffer.edit([(len..len, message)], None, cx); is_rpc: true,
}); });
}
if rpc_log_lines.len() == MAX_STORED_LOG_ENTRIES {
rpc_log_lines.pop_front();
}
let message = message.trim();
rpc_log_lines.push_back(message.to_string());
cx.emit(Event::NewServerLogEntry {
id: language_server_id,
entry: message.to_string(),
is_rpc: true,
});
cx.notify();
Some(()) Some(())
} }
} }
@ -413,28 +414,25 @@ impl LspLogView {
cx.notify(); cx.notify();
}); });
let events_subscriptions = cx.subscribe(&log_store, |log_view, _, e, cx| match e { let events_subscriptions = cx.subscribe(&log_store, |log_view, _, e, cx| match e {
Event::NewServerLogEntry { id, entry } => { Event::NewServerLogEntry { id, entry, is_rpc } => {
if log_view.current_server_id == Some(*id) { if log_view.current_server_id == Some(*id) {
if (*is_rpc && log_view.is_showing_rpc_trace)
|| (!*is_rpc && !log_view.is_showing_rpc_trace)
{
log_view.editor.update(cx, |editor, cx| { log_view.editor.update(cx, |editor, cx| {
editor.set_read_only(false); editor.set_read_only(false);
editor.handle_input(entry, cx); editor.handle_input(entry.trim(), cx);
editor.handle_input("\n", cx); editor.handle_input("\n", cx);
editor.set_read_only(true); editor.set_read_only(true);
}) });
}
} }
} }
}); });
// TODO kb deduplicate let (editor, editor_subscription) = Self::editor_for_logs(String::new(), cx);
let editor = cx.add_view(|cx| {
let mut editor = Editor::multi_line(None, cx);
editor.set_read_only(true);
editor.move_to_end(&Default::default(), cx);
editor
});
let _editor_subscription = cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone()));
let mut this = Self { let mut this = Self {
editor, editor,
_editor_subscription, editor_subscription,
project, project,
log_store, log_store,
current_server_id: None, current_server_id: None,
@ -447,19 +445,19 @@ impl LspLogView {
this this
} }
fn editor_for_buffer( fn editor_for_logs(
project: ModelHandle<Project>, log_contents: String,
buffer: ModelHandle<Buffer>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> (ViewHandle<Editor>, Subscription) { ) -> (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::multi_line(None, cx);
editor.set_text(log_contents, cx);
editor.move_to_end(&MoveToEnd, cx);
editor.set_read_only(true); editor.set_read_only(true);
editor.move_to_end(&Default::default(), cx);
editor editor
}); });
let subscription = cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone())); let editor_subscription = cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone()));
(editor, subscription) (editor, editor_subscription)
} }
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>> {
@ -512,28 +510,13 @@ impl LspLogView {
.log_store .log_store
.read(cx) .read(cx)
.server_logs(&self.project, server_id) .server_logs(&self.project, server_id)
.map(|lines| { .map(log_contents);
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 editor = cx.add_view(|cx| { let (editor, editor_subscription) = Self::editor_for_logs(log_contents, cx);
let mut editor = Editor::multi_line(None, cx);
editor.set_read_only(true);
editor.move_to_end(&Default::default(), cx);
editor.set_text(log_contents, cx);
editor
});
self._editor_subscription =
cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone()));
self.editor = editor; self.editor = editor;
self.editor_subscription = editor_subscription;
cx.notify(); cx.notify();
} }
} }
@ -543,16 +526,37 @@ impl LspLogView {
server_id: LanguageServerId, server_id: LanguageServerId,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
let buffer = self.log_store.update(cx, |log_set, cx| { let rpc_log = self.log_store.update(cx, |log_store, _| {
log_set.enable_rpc_trace_for_language_server(&self.project, server_id, cx) log_store
.enable_rpc_trace_for_language_server(&self.project, server_id)
.map(|state| log_contents(&state.rpc_messages))
}); });
if let Some(buffer) = buffer { if let Some(rpc_log) = rpc_log {
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;
let (editor, _editor_subscription) = let (editor, editor_subscription) = Self::editor_for_logs(rpc_log, cx);
Self::editor_for_buffer(self.project.clone(), buffer, cx); let language = self.project.read(cx).languages().language_for_name("JSON");
editor
.read(cx)
.buffer()
.read(cx)
.as_singleton()
.expect("log buffer should be a singleton")
.update(cx, |_, cx| {
cx.spawn_weak({
let buffer = cx.handle();
|_, mut cx| async move {
let language = language.await.ok();
buffer.update(&mut cx, |buffer, cx| {
buffer.set_language(language, cx);
});
}
})
.detach();
});
self.editor = editor; self.editor = editor;
self._editor_subscription = _editor_subscription; self.editor_subscription = editor_subscription;
cx.notify(); cx.notify();
} }
} }
@ -565,7 +569,7 @@ impl LspLogView {
) { ) {
self.log_store.update(cx, |log_store, cx| { self.log_store.update(cx, |log_store, cx| {
if enabled { if enabled {
log_store.enable_rpc_trace_for_language_server(&self.project, server_id, cx); log_store.enable_rpc_trace_for_language_server(&self.project, server_id);
} else { } else {
log_store.disable_rpc_trace_for_language_server(&self.project, server_id, cx); log_store.disable_rpc_trace_for_language_server(&self.project, server_id, cx);
} }
@ -577,6 +581,16 @@ impl LspLogView {
} }
} }
fn log_contents(lines: &VecDeque<String>) -> String {
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")
}
}
impl View for LspLogView { impl View for LspLogView {
fn ui_name() -> &'static str { fn ui_name() -> &'static str {
"LspLogView" "LspLogView"
@ -992,7 +1006,11 @@ impl LspLogToolbarItemView {
} }
pub enum Event { pub enum Event {
NewServerLogEntry { id: LanguageServerId, entry: String }, NewServerLogEntry {
id: LanguageServerId,
entry: String,
is_rpc: bool,
},
} }
impl Entity for LogStore { impl Entity for LogStore {