|
| 1 | +use crate::capabilities::SupportedCommand; |
| 2 | +use crate::server; |
| 3 | +use crate::server::api::LSPResult; |
| 4 | +use crate::server::api::RequestHandler; |
| 5 | +use crate::server::api::traits::SyncRequestHandler; |
| 6 | +use crate::session::Session; |
| 7 | +use crate::session::client::Client; |
| 8 | +use lsp_server::ErrorCode; |
| 9 | +use lsp_types::{self as types, request as req}; |
| 10 | +use std::fmt::Write; |
| 11 | +use std::str::FromStr; |
| 12 | +use ty_project::Db; |
| 13 | + |
| 14 | +pub(crate) struct ExecuteCommand; |
| 15 | + |
| 16 | +impl RequestHandler for ExecuteCommand { |
| 17 | + type RequestType = req::ExecuteCommand; |
| 18 | +} |
| 19 | + |
| 20 | +impl SyncRequestHandler for ExecuteCommand { |
| 21 | + fn run( |
| 22 | + session: &mut Session, |
| 23 | + _client: &Client, |
| 24 | + params: types::ExecuteCommandParams, |
| 25 | + ) -> server::Result<Option<serde_json::Value>> { |
| 26 | + let command = SupportedCommand::from_str(¶ms.command) |
| 27 | + .with_failure_code(ErrorCode::InvalidParams)?; |
| 28 | + |
| 29 | + match command { |
| 30 | + SupportedCommand::Debug => Ok(Some(serde_json::Value::String( |
| 31 | + debug_information(session).with_failure_code(ErrorCode::InternalError)?, |
| 32 | + ))), |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Returns a string with detailed memory usage. |
| 38 | +fn debug_information(session: &Session) -> crate::Result<String> { |
| 39 | + let mut buffer = String::new(); |
| 40 | + |
| 41 | + writeln!( |
| 42 | + buffer, |
| 43 | + "Client capabilities: {:#?}", |
| 44 | + session.client_capabilities() |
| 45 | + )?; |
| 46 | + writeln!( |
| 47 | + buffer, |
| 48 | + "Position encoding: {:#?}", |
| 49 | + session.position_encoding() |
| 50 | + )?; |
| 51 | + writeln!(buffer, "Global settings: {:#?}", session.global_settings())?; |
| 52 | + writeln!( |
| 53 | + buffer, |
| 54 | + "Open text documents: {}", |
| 55 | + session.text_document_keys().count() |
| 56 | + )?; |
| 57 | + writeln!(buffer)?; |
| 58 | + |
| 59 | + for (root, workspace) in session.workspaces() { |
| 60 | + writeln!(buffer, "Workspace {root} ({})", workspace.url())?; |
| 61 | + writeln!(buffer, "Settings: {:#?}", workspace.settings())?; |
| 62 | + writeln!(buffer)?; |
| 63 | + } |
| 64 | + |
| 65 | + for db in session.project_dbs() { |
| 66 | + writeln!(buffer, "Project at {}", db.project().root(db))?; |
| 67 | + writeln!(buffer, "Settings: {:#?}", db.project().settings(db))?; |
| 68 | + writeln!(buffer)?; |
| 69 | + writeln!( |
| 70 | + buffer, |
| 71 | + "Memory report:\n{}", |
| 72 | + db.salsa_memory_dump().display_full() |
| 73 | + )?; |
| 74 | + } |
| 75 | + Ok(buffer) |
| 76 | +} |
0 commit comments