From 7219822bfb022d9d6e46856a461205349b84a31f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Fri, 19 Jul 2024 08:28:27 +0200 Subject: [PATCH] refactor: cleanup --- lsp/src/discord.rs | 69 +++++++++++++++++++++++ lsp/src/main.rs | 135 ++++++++++++++++++++++++--------------------- 2 files changed, 140 insertions(+), 64 deletions(-) create mode 100644 lsp/src/discord.rs diff --git a/lsp/src/discord.rs b/lsp/src/discord.rs new file mode 100644 index 0000000..74881dd --- /dev/null +++ b/lsp/src/discord.rs @@ -0,0 +1,69 @@ +use std::{ + sync::{Mutex, MutexGuard}, + time::{Duration, SystemTime, UNIX_EPOCH}, +}; + +use discord_rich_presence::{ + activity::{self, Assets, Timestamps}, + DiscordIpc, DiscordIpcClient, +}; + +#[derive(Debug)] +pub struct Discord { + client: Mutex, + start_timestamp: Duration, +} + +impl Discord { + pub fn new() -> Self { + let discord_client = DiscordIpcClient::new("1263505205522337886") + .expect("Failed to initialize Discord Ipc Client"); + let start_timestamp = SystemTime::now(); + let since_epoch = start_timestamp + .duration_since(UNIX_EPOCH) + .expect("Failed to get duration since UNIX_EPOCH"); + + Self { + client: Mutex::new(discord_client), + start_timestamp: since_epoch, + } + } + + pub fn connect(&self) { + let mut client = self.get_client(); + let result = client.connect(); + result.unwrap(); + } + + pub fn change_file(&self, filename: &str, workspace: &str) { + self.change_activity( + format!("Working on {}", filename), + format!("In {}", workspace), + ) + } + + pub fn get_client(&self) -> MutexGuard { + return self.client.lock().expect("Failed to lock discord client"); + } + + fn change_activity(&self, state: String, details: String) { + let mut client = self.get_client(); + let timestamp: i64 = self.start_timestamp.as_millis() as i64; + + client + .set_activity( + activity::Activity::new() + .assets(Assets::new().large_image("logo")) + .state(state.as_str()) + .details(details.as_str()) + .timestamps(Timestamps::new().start(timestamp)), + ) + .expect( + format!( + "Failed to set activity with state {} and details {}", + state, details + ) + .as_str(), + ); + } +} diff --git a/lsp/src/main.rs b/lsp/src/main.rs index 338d8da..6e6e663 100644 --- a/lsp/src/main.rs +++ b/lsp/src/main.rs @@ -1,32 +1,24 @@ use std::fmt::Debug; use std::path::{Path, PathBuf}; use std::sync::{Mutex, MutexGuard}; -use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use discord::Discord; use tower_lsp::jsonrpc::Result; use tower_lsp::lsp_types::*; use tower_lsp::{Client, LanguageServer, LspService, Server}; -use discord_rich_presence::{ - activity::{self, Assets, Timestamps}, - DiscordIpc, DiscordIpcClient, -}; +mod discord; #[derive(Debug)] -struct Discord { - client: Mutex, - start_timestamp: Duration, +struct Document { + path: PathBuf, } #[derive(Debug)] struct Backend { - discord: Discord, + discord: discord::Discord, client: Client, - root_uri: Mutex, -} - -struct Document { - path: PathBuf, + workspace_file_name: Mutex, } impl Document { @@ -46,29 +38,13 @@ impl Document { impl Backend { fn new(client: Client) -> Self { - let discord_client = DiscordIpcClient::new("1263505205522337886") - .expect("Failed to initialize Discord Ipc Client"); - let start_timestamp = SystemTime::now(); - let since_epoch = start_timestamp - .duration_since(UNIX_EPOCH) - .expect("Failed to get duration since UNIX_EPOCH"); - Self { client, - discord: Discord { - client: Mutex::new(discord_client), - start_timestamp: since_epoch, - }, - root_uri: Mutex::new(PathBuf::new()), + discord: Discord::new(), + workspace_file_name: Mutex::new(String::new()), } } - fn start_client(&self) { - let mut client = self.get_discord_client(); - let result = client.connect(); - result.unwrap(); - } - async fn on_change(&self, doc: Document) { self.client .log_message( @@ -76,52 +52,42 @@ impl Backend { format!( "Changing to {} in {}", doc.get_filename(), - self.get_workspace() + self.get_workspace_file_name() ), ) .await; - let mut client = self.get_discord_client(); - let timestamp: i64 = self.discord.start_timestamp.as_millis() as i64; - - let _ = client.set_activity( - activity::Activity::new() - .assets(Assets::new().large_image("logo")) - .state(format!("Working on {}", doc.get_filename()).as_str()) - .details(format!("In {}", self.get_workspace()).as_str()) - .timestamps(Timestamps::new().start(timestamp)), - ); + self.discord + .change_file(doc.get_filename(), self.get_workspace_file_name().as_str()) } - fn get_discord_client(&self) -> MutexGuard { + fn get_workspace_file_name(&self) -> MutexGuard<'_, String> { return self - .discord - .client + .workspace_file_name .lock() - .unwrap_or_else(|e| e.into_inner()); - } - - fn get_workspace(&self) -> String { - return String::from( - self.root_uri - .lock() - .unwrap() - .file_name() - .unwrap() - .to_str() - .unwrap(), - ); + .expect("Failed to lock workspace file name"); } } #[tower_lsp::async_trait] impl LanguageServer for Backend { async fn initialize(&self, params: InitializeParams) -> Result { - self.start_client(); - self.root_uri + // Connect discord client + self.discord.connect(); + + // Set workspace name + let root_uri = params.root_uri.expect("Failed to get root uri"); + let workspace_path = Path::new(root_uri.path()); + self.workspace_file_name .lock() - .unwrap() - .push(params.root_uri.unwrap().path()); + .expect("Failed to lock workspace file name") + .push_str( + workspace_path + .file_name() + .expect("Failed to get workspace file name") + .to_str() + .expect("Failed to convert workspace file name &OsStr to &str"), + ); Ok(InitializeResult { server_info: Some(ServerInfo { @@ -159,6 +125,47 @@ impl LanguageServer for Backend { self.on_change(Document::new(params.text_document.uri)) .await; } + + async fn did_save(&self, params: DidSaveTextDocumentParams) { + self.on_change(Document::new(params.text_document.uri)) + .await; + } + + async fn hover(&self, params: HoverParams) -> Result> { + self.on_change(Document::new( + params.text_document_position_params.text_document.uri, + )) + .await; + + return Ok(None); + } + + async fn folding_range(&self, params: FoldingRangeParams) -> Result>> { + self.on_change(Document::new(params.text_document.uri)) + .await; + + return Ok(Some(vec![])); + } + + async fn semantic_tokens_full( + &self, + params: SemanticTokensParams, + ) -> Result> { + self.on_change(Document::new(params.text_document.uri)) + .await; + + return Ok(None); + } + + async fn semantic_tokens_full_delta( + &self, + params: SemanticTokensDeltaParams, + ) -> Result> { + self.on_change(Document::new(params.text_document.uri)) + .await; + + return Ok(None); + } } #[tokio::main] @@ -166,7 +173,7 @@ async fn main() { let stdin = tokio::io::stdin(); let stdout = tokio::io::stdout(); - let (service, socket) = LspService::new(|client| Backend::new(client)); + let (service, socket) = LspService::new(Backend::new); Server::new(stdin, stdout, socket).serve(service).await; }