mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-29 14:43:55 +03:00
Cap every language server logs (#3134)
* on opening a language server's logs, a new editor for server logs is now created from `\n`-joined `VecDeque` elements instead of a buffer, as before * every `VecDeque` entry is a log line we receiver out of stderr or LSP server, and their general amount is capped with `let MAX_STORED_LOG_ENTRIES: usize = 2000;` * currently opened editor with logs (`Editor::multi_line`) keeps getting log lines appended and may get over this cap, but only last stored 2000 entries will be restored on reopen * similarly, cap rpc message logs Release Notes: - Improved memory usage by storing less language LSP server and rpc logs
This commit is contained in:
commit
c380d437c6
@ -1,5 +1,5 @@
|
||||
use collections::HashMap;
|
||||
use editor::Editor;
|
||||
use collections::{HashMap, VecDeque};
|
||||
use editor::{Editor, MoveToEnd};
|
||||
use futures::{channel::mpsc, StreamExt};
|
||||
use gpui::{
|
||||
actions,
|
||||
@ -11,7 +11,7 @@ use gpui::{
|
||||
AnyElement, AppContext, Element, Entity, ModelContext, ModelHandle, Subscription, View,
|
||||
ViewContext, ViewHandle, WeakModelHandle,
|
||||
};
|
||||
use language::{Buffer, LanguageServerId, LanguageServerName};
|
||||
use language::{LanguageServerId, LanguageServerName};
|
||||
use lsp::IoKind;
|
||||
use project::{search::SearchQuery, Project};
|
||||
use std::{borrow::Cow, sync::Arc};
|
||||
@ -22,8 +22,9 @@ use workspace::{
|
||||
ToolbarItemLocation, ToolbarItemView, Workspace, WorkspaceCreated,
|
||||
};
|
||||
|
||||
const SEND_LINE: &str = "// Send:\n";
|
||||
const RECEIVE_LINE: &str = "// Receive:\n";
|
||||
const SEND_LINE: &str = "// Send:";
|
||||
const RECEIVE_LINE: &str = "// Receive:";
|
||||
const MAX_STORED_LOG_ENTRIES: usize = 2000;
|
||||
|
||||
pub struct LogStore {
|
||||
projects: HashMap<WeakModelHandle<Project>, ProjectState>,
|
||||
@ -36,24 +37,25 @@ struct ProjectState {
|
||||
}
|
||||
|
||||
struct LanguageServerState {
|
||||
log_buffer: ModelHandle<Buffer>,
|
||||
log_messages: VecDeque<String>,
|
||||
rpc_state: Option<LanguageServerRpcState>,
|
||||
_io_logs_subscription: Option<lsp::Subscription>,
|
||||
_lsp_logs_subscription: Option<lsp::Subscription>,
|
||||
}
|
||||
|
||||
struct LanguageServerRpcState {
|
||||
buffer: ModelHandle<Buffer>,
|
||||
rpc_messages: VecDeque<String>,
|
||||
last_message_kind: Option<MessageKind>,
|
||||
}
|
||||
|
||||
pub struct LspLogView {
|
||||
pub(crate) editor: ViewHandle<Editor>,
|
||||
editor_subscription: Subscription,
|
||||
log_store: ModelHandle<LogStore>,
|
||||
current_server_id: Option<LanguageServerId>,
|
||||
is_showing_rpc_trace: bool,
|
||||
project: ModelHandle<Project>,
|
||||
_log_store_subscription: Subscription,
|
||||
_log_store_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
|
||||
pub struct LspLogToolbarItemView {
|
||||
@ -122,10 +124,9 @@ impl LogStore {
|
||||
io_tx,
|
||||
};
|
||||
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) {
|
||||
this.update(&mut cx, |this, cx| {
|
||||
message.push('\n');
|
||||
this.on_io(project, server_id, io_kind, &message, cx);
|
||||
});
|
||||
}
|
||||
@ -168,15 +169,13 @@ impl LogStore {
|
||||
project: &ModelHandle<Project>,
|
||||
id: LanguageServerId,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Option<ModelHandle<Buffer>> {
|
||||
) -> Option<&mut LanguageServerState> {
|
||||
let project_state = self.projects.get_mut(&project.downgrade())?;
|
||||
let server_state = project_state.servers.entry(id).or_insert_with(|| {
|
||||
cx.notify();
|
||||
LanguageServerState {
|
||||
rpc_state: None,
|
||||
log_buffer: cx
|
||||
.add_model(|cx| Buffer::new(0, cx.model_id() as u64, ""))
|
||||
.clone(),
|
||||
log_messages: VecDeque::with_capacity(MAX_STORED_LOG_ENTRIES),
|
||||
_io_logs_subscription: None,
|
||||
_lsp_logs_subscription: None,
|
||||
}
|
||||
@ -186,7 +185,7 @@ impl LogStore {
|
||||
if let Some(server) = server.as_deref() {
|
||||
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.
|
||||
return Some(server_state.log_buffer.clone());
|
||||
return Some(server_state);
|
||||
}
|
||||
}
|
||||
|
||||
@ -215,7 +214,7 @@ impl LogStore {
|
||||
}
|
||||
})
|
||||
});
|
||||
Some(server_state.log_buffer.clone())
|
||||
Some(server_state)
|
||||
}
|
||||
|
||||
fn add_language_server_log(
|
||||
@ -225,24 +224,26 @@ impl LogStore {
|
||||
message: &str,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Option<()> {
|
||||
let buffer = match self
|
||||
let language_server_state = match self
|
||||
.projects
|
||||
.get_mut(&project.downgrade())?
|
||||
.servers
|
||||
.get(&id)
|
||||
.map(|state| state.log_buffer.clone())
|
||||
.get_mut(&id)
|
||||
{
|
||||
Some(existing_buffer) => existing_buffer,
|
||||
Some(existing_state) => existing_state,
|
||||
None => self.add_language_server(&project, id, cx)?,
|
||||
};
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
let len = buffer.len();
|
||||
let has_newline = message.ends_with("\n");
|
||||
buffer.edit([(len..len, message)], None, cx);
|
||||
if !has_newline {
|
||||
let len = buffer.len();
|
||||
buffer.edit([(len..len, "\n")], None, cx);
|
||||
}
|
||||
|
||||
let log_lines = &mut language_server_state.log_messages;
|
||||
while log_lines.len() >= MAX_STORED_LOG_ENTRIES {
|
||||
log_lines.pop_front();
|
||||
}
|
||||
let message = message.trim();
|
||||
log_lines.push_back(message.to_string());
|
||||
cx.emit(Event::NewServerLogEntry {
|
||||
id,
|
||||
entry: message.to_string(),
|
||||
is_rpc: false,
|
||||
});
|
||||
cx.notify();
|
||||
Some(())
|
||||
@ -260,46 +261,32 @@ impl LogStore {
|
||||
Some(())
|
||||
}
|
||||
|
||||
pub fn log_buffer_for_server(
|
||||
fn server_logs(
|
||||
&self,
|
||||
project: &ModelHandle<Project>,
|
||||
server_id: LanguageServerId,
|
||||
) -> Option<ModelHandle<Buffer>> {
|
||||
) -> Option<&VecDeque<String>> {
|
||||
let weak_project = project.downgrade();
|
||||
let project_state = self.projects.get(&weak_project)?;
|
||||
let server_state = project_state.servers.get(&server_id)?;
|
||||
Some(server_state.log_buffer.clone())
|
||||
Some(&server_state.log_messages)
|
||||
}
|
||||
|
||||
fn enable_rpc_trace_for_language_server(
|
||||
&mut self,
|
||||
project: &ModelHandle<Project>,
|
||||
server_id: LanguageServerId,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Option<ModelHandle<Buffer>> {
|
||||
) -> Option<&mut LanguageServerRpcState> {
|
||||
let weak_project = project.downgrade();
|
||||
let project_state = self.projects.get_mut(&weak_project)?;
|
||||
let server_state = project_state.servers.get_mut(&server_id)?;
|
||||
let rpc_state = server_state.rpc_state.get_or_insert_with(|| {
|
||||
let language = project.read(cx).languages().language_for_name("JSON");
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, ""));
|
||||
cx.spawn_weak({
|
||||
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,
|
||||
let rpc_state = server_state
|
||||
.rpc_state
|
||||
.get_or_insert_with(|| LanguageServerRpcState {
|
||||
rpc_messages: VecDeque::with_capacity(MAX_STORED_LOG_ENTRIES),
|
||||
last_message_kind: None,
|
||||
}
|
||||
});
|
||||
Some(rpc_state.buffer.clone())
|
||||
});
|
||||
Some(rpc_state)
|
||||
}
|
||||
|
||||
pub fn disable_rpc_trace_for_language_server(
|
||||
@ -328,7 +315,7 @@ impl LogStore {
|
||||
IoKind::StdIn => false,
|
||||
IoKind::StdErr => {
|
||||
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);
|
||||
return Some(());
|
||||
}
|
||||
@ -341,24 +328,37 @@ impl LogStore {
|
||||
.get_mut(&language_server_id)?
|
||||
.rpc_state
|
||||
.as_mut()?;
|
||||
state.buffer.update(cx, |buffer, cx| {
|
||||
let kind = if is_received {
|
||||
MessageKind::Receive
|
||||
} else {
|
||||
MessageKind::Send
|
||||
let kind = if is_received {
|
||||
MessageKind::Receive
|
||||
} else {
|
||||
MessageKind::Send
|
||||
};
|
||||
|
||||
let rpc_log_lines = &mut state.rpc_messages;
|
||||
if state.last_message_kind != Some(kind) {
|
||||
let line_before_message = match kind {
|
||||
MessageKind::Send => SEND_LINE,
|
||||
MessageKind::Receive => RECEIVE_LINE,
|
||||
};
|
||||
if state.last_message_kind != Some(kind) {
|
||||
let len = buffer.len();
|
||||
let line = match kind {
|
||||
MessageKind::Send => SEND_LINE,
|
||||
MessageKind::Receive => RECEIVE_LINE,
|
||||
};
|
||||
buffer.edit([(len..len, line)], None, cx);
|
||||
state.last_message_kind = Some(kind);
|
||||
}
|
||||
let len = buffer.len();
|
||||
buffer.edit([(len..len, message)], None, cx);
|
||||
rpc_log_lines.push_back(line_before_message.to_string());
|
||||
cx.emit(Event::NewServerLogEntry {
|
||||
id: language_server_id,
|
||||
entry: line_before_message.to_string(),
|
||||
is_rpc: true,
|
||||
});
|
||||
}
|
||||
|
||||
while 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(())
|
||||
}
|
||||
}
|
||||
@ -374,8 +374,7 @@ impl LspLogView {
|
||||
.projects
|
||||
.get(&project.downgrade())
|
||||
.and_then(|project| project.servers.keys().copied().next());
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, ""));
|
||||
let _log_store_subscription = cx.observe(&log_store, |this, store, cx| {
|
||||
let model_changes_subscription = cx.observe(&log_store, |this, store, cx| {
|
||||
(|| -> Option<()> {
|
||||
let project_state = store.read(cx).projects.get(&this.project.downgrade())?;
|
||||
if let Some(current_lsp) = this.current_server_id {
|
||||
@ -411,13 +410,31 @@ impl LspLogView {
|
||||
|
||||
cx.notify();
|
||||
});
|
||||
let events_subscriptions = cx.subscribe(&log_store, |log_view, _, e, cx| match e {
|
||||
Event::NewServerLogEntry { id, entry, is_rpc } => {
|
||||
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| {
|
||||
editor.set_read_only(false);
|
||||
editor.handle_input(entry.trim(), cx);
|
||||
editor.handle_input("\n", cx);
|
||||
editor.set_read_only(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
let (editor, editor_subscription) = Self::editor_for_logs(String::new(), cx);
|
||||
let mut this = Self {
|
||||
editor: Self::editor_for_buffer(project.clone(), buffer, cx),
|
||||
editor,
|
||||
editor_subscription,
|
||||
project,
|
||||
log_store,
|
||||
current_server_id: None,
|
||||
is_showing_rpc_trace: false,
|
||||
_log_store_subscription,
|
||||
_log_store_subscriptions: vec![model_changes_subscription, events_subscriptions],
|
||||
};
|
||||
if let Some(server_id) = server_id {
|
||||
this.show_logs_for_server(server_id, cx);
|
||||
@ -425,20 +442,19 @@ impl LspLogView {
|
||||
this
|
||||
}
|
||||
|
||||
fn editor_for_buffer(
|
||||
project: ModelHandle<Project>,
|
||||
buffer: ModelHandle<Buffer>,
|
||||
fn editor_for_logs(
|
||||
log_contents: String,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> ViewHandle<Editor> {
|
||||
) -> (ViewHandle<Editor>, Subscription) {
|
||||
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.move_to_end(&Default::default(), cx);
|
||||
editor
|
||||
});
|
||||
cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone()))
|
||||
.detach();
|
||||
editor
|
||||
let editor_subscription = cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone()));
|
||||
(editor, editor_subscription)
|
||||
}
|
||||
|
||||
pub(crate) fn menu_items<'a>(&'a self, cx: &'a AppContext) -> Option<Vec<LogMenuItem>> {
|
||||
@ -487,14 +503,17 @@ impl LspLogView {
|
||||
}
|
||||
|
||||
fn show_logs_for_server(&mut self, server_id: LanguageServerId, cx: &mut ViewContext<Self>) {
|
||||
let buffer = self
|
||||
let log_contents = self
|
||||
.log_store
|
||||
.read(cx)
|
||||
.log_buffer_for_server(&self.project, server_id);
|
||||
if let Some(buffer) = buffer {
|
||||
.server_logs(&self.project, server_id)
|
||||
.map(log_contents);
|
||||
if let Some(log_contents) = log_contents {
|
||||
self.current_server_id = Some(server_id);
|
||||
self.is_showing_rpc_trace = false;
|
||||
self.editor = Self::editor_for_buffer(self.project.clone(), buffer, cx);
|
||||
let (editor, editor_subscription) = Self::editor_for_logs(log_contents, cx);
|
||||
self.editor = editor;
|
||||
self.editor_subscription = editor_subscription;
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
@ -504,13 +523,37 @@ impl LspLogView {
|
||||
server_id: LanguageServerId,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
let buffer = self.log_store.update(cx, |log_set, cx| {
|
||||
log_set.enable_rpc_trace_for_language_server(&self.project, server_id, cx)
|
||||
let rpc_log = self.log_store.update(cx, |log_store, _| {
|
||||
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.is_showing_rpc_trace = true;
|
||||
self.editor = Self::editor_for_buffer(self.project.clone(), buffer, cx);
|
||||
let (editor, editor_subscription) = Self::editor_for_logs(rpc_log, 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_subscription = editor_subscription;
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
@ -523,7 +566,7 @@ impl LspLogView {
|
||||
) {
|
||||
self.log_store.update(cx, |log_store, cx| {
|
||||
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 {
|
||||
log_store.disable_rpc_trace_for_language_server(&self.project, server_id, cx);
|
||||
}
|
||||
@ -535,6 +578,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 {
|
||||
fn ui_name() -> &'static str {
|
||||
"LspLogView"
|
||||
@ -949,8 +1002,16 @@ impl LspLogToolbarItemView {
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Event {
|
||||
NewServerLogEntry {
|
||||
id: LanguageServerId,
|
||||
entry: String,
|
||||
is_rpc: bool,
|
||||
},
|
||||
}
|
||||
|
||||
impl Entity for LogStore {
|
||||
type Event = ();
|
||||
type Event = Event;
|
||||
}
|
||||
|
||||
impl Entity for LspLogView {
|
||||
|
Loading…
Reference in New Issue
Block a user