mirror of
https://github.com/xHyroM/zed-discord-presence.git
synced 2024-11-10 00:18:06 +01:00
refactor: cleanup
This commit is contained in:
parent
ed6b07ca4a
commit
7219822bfb
2 changed files with 140 additions and 64 deletions
69
lsp/src/discord.rs
Normal file
69
lsp/src/discord.rs
Normal file
|
@ -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<DiscordIpcClient>,
|
||||||
|
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<DiscordIpcClient> {
|
||||||
|
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(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
135
lsp/src/main.rs
135
lsp/src/main.rs
|
@ -1,32 +1,24 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::{Mutex, MutexGuard};
|
use std::sync::{Mutex, MutexGuard};
|
||||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
|
||||||
|
|
||||||
|
use discord::Discord;
|
||||||
use tower_lsp::jsonrpc::Result;
|
use tower_lsp::jsonrpc::Result;
|
||||||
use tower_lsp::lsp_types::*;
|
use tower_lsp::lsp_types::*;
|
||||||
use tower_lsp::{Client, LanguageServer, LspService, Server};
|
use tower_lsp::{Client, LanguageServer, LspService, Server};
|
||||||
|
|
||||||
use discord_rich_presence::{
|
mod discord;
|
||||||
activity::{self, Assets, Timestamps},
|
|
||||||
DiscordIpc, DiscordIpcClient,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Discord {
|
struct Document {
|
||||||
client: Mutex<DiscordIpcClient>,
|
path: PathBuf,
|
||||||
start_timestamp: Duration,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Backend {
|
struct Backend {
|
||||||
discord: Discord,
|
discord: discord::Discord,
|
||||||
client: Client,
|
client: Client,
|
||||||
root_uri: Mutex<PathBuf>,
|
workspace_file_name: Mutex<String>,
|
||||||
}
|
|
||||||
|
|
||||||
struct Document {
|
|
||||||
path: PathBuf,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Document {
|
impl Document {
|
||||||
|
@ -46,29 +38,13 @@ impl Document {
|
||||||
|
|
||||||
impl Backend {
|
impl Backend {
|
||||||
fn new(client: Client) -> Self {
|
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 {
|
Self {
|
||||||
client,
|
client,
|
||||||
discord: Discord {
|
discord: Discord::new(),
|
||||||
client: Mutex::new(discord_client),
|
workspace_file_name: Mutex::new(String::new()),
|
||||||
start_timestamp: since_epoch,
|
|
||||||
},
|
|
||||||
root_uri: Mutex::new(PathBuf::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) {
|
async fn on_change(&self, doc: Document) {
|
||||||
self.client
|
self.client
|
||||||
.log_message(
|
.log_message(
|
||||||
|
@ -76,52 +52,42 @@ impl Backend {
|
||||||
format!(
|
format!(
|
||||||
"Changing to {} in {}",
|
"Changing to {} in {}",
|
||||||
doc.get_filename(),
|
doc.get_filename(),
|
||||||
self.get_workspace()
|
self.get_workspace_file_name()
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let mut client = self.get_discord_client();
|
self.discord
|
||||||
let timestamp: i64 = self.discord.start_timestamp.as_millis() as i64;
|
.change_file(doc.get_filename(), self.get_workspace_file_name().as_str())
|
||||||
|
|
||||||
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)),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_discord_client(&self) -> MutexGuard<DiscordIpcClient> {
|
fn get_workspace_file_name(&self) -> MutexGuard<'_, String> {
|
||||||
return self
|
return self
|
||||||
.discord
|
.workspace_file_name
|
||||||
.client
|
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap_or_else(|e| e.into_inner());
|
.expect("Failed to lock workspace file name");
|
||||||
}
|
|
||||||
|
|
||||||
fn get_workspace(&self) -> String {
|
|
||||||
return String::from(
|
|
||||||
self.root_uri
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.file_name()
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tower_lsp::async_trait]
|
#[tower_lsp::async_trait]
|
||||||
impl LanguageServer for Backend {
|
impl LanguageServer for Backend {
|
||||||
async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
|
async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
|
||||||
self.start_client();
|
// Connect discord client
|
||||||
self.root_uri
|
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()
|
.lock()
|
||||||
.unwrap()
|
.expect("Failed to lock workspace file name")
|
||||||
.push(params.root_uri.unwrap().path());
|
.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 {
|
Ok(InitializeResult {
|
||||||
server_info: Some(ServerInfo {
|
server_info: Some(ServerInfo {
|
||||||
|
@ -159,6 +125,47 @@ impl LanguageServer for Backend {
|
||||||
self.on_change(Document::new(params.text_document.uri))
|
self.on_change(Document::new(params.text_document.uri))
|
||||||
.await;
|
.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<Option<Hover>> {
|
||||||
|
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<Option<Vec<FoldingRange>>> {
|
||||||
|
self.on_change(Document::new(params.text_document.uri))
|
||||||
|
.await;
|
||||||
|
|
||||||
|
return Ok(Some(vec![]));
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn semantic_tokens_full(
|
||||||
|
&self,
|
||||||
|
params: SemanticTokensParams,
|
||||||
|
) -> Result<Option<SemanticTokensResult>> {
|
||||||
|
self.on_change(Document::new(params.text_document.uri))
|
||||||
|
.await;
|
||||||
|
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn semantic_tokens_full_delta(
|
||||||
|
&self,
|
||||||
|
params: SemanticTokensDeltaParams,
|
||||||
|
) -> Result<Option<SemanticTokensFullDeltaResult>> {
|
||||||
|
self.on_change(Document::new(params.text_document.uri))
|
||||||
|
.await;
|
||||||
|
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -166,7 +173,7 @@ async fn main() {
|
||||||
let stdin = tokio::io::stdin();
|
let stdin = tokio::io::stdin();
|
||||||
let stdout = tokio::io::stdout();
|
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;
|
Server::new(stdin, stdout, socket).serve(service).await;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue