Skip to content

Commit 4543d31

Browse files
committed
feat: unregister client on nvim exist + drop workspace
1 parent 3e80bb4 commit 4543d31

File tree

9 files changed

+108
-163
lines changed

9 files changed

+108
-163
lines changed

Cargo.lock

Lines changed: 0 additions & 139 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ wax = "0.4.0"
3232
regex = "1.5"
3333
lazy_static = "1.4.0"
3434
shell-words = "1.1.0"
35-
35+
# TODO: make libxcodebase lightweight
36+
# Use features to exclude stuff likce tracing and async-trait as well tokio
3637
# WARN: api totally changed on later versions.
3738
# It would make sense to create custom function
3839
# Breaks
@@ -47,5 +48,4 @@ members = [
4748
"daemon",
4849
"lsp",
4950
"nvim",
50-
"server"
5151
]

core/command.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ mod build;
66
mod register;
77
mod rename_file;
88
mod run;
9+
mod unregister;
910

1011
pub use build::Build;
1112
pub use register::Register;
1213
pub use rename_file::RenameFile;
1314
pub use run::Run;
15+
pub use unregister::UnRegister;
1416

1517
#[async_trait]
1618
pub trait DaemonCommand {
@@ -24,6 +26,7 @@ pub enum Command {
2426
Run(Run),
2527
RenameFile(RenameFile),
2628
Register(Register),
29+
UnRegister(UnRegister),
2730
}
2831

2932
impl Command {
@@ -33,6 +36,7 @@ impl Command {
3336
Command::Run(c) => c.handle(state).await,
3437
Command::RenameFile(c) => c.handle(state).await,
3538
Command::Register(c) => c.handle(state).await,
39+
Command::UnRegister(c) => c.handle(state).await,
3640
}
3741
}
3842

@@ -44,6 +48,7 @@ impl Command {
4448
"run" => Ok(Self::Run(Run::new(args)?)),
4549
"rename_file" => Ok(Self::RenameFile(RenameFile::new(args)?)),
4650
"register" => Ok(Self::Register(Register::new(args)?)),
51+
"unregister" => Ok(Self::UnRegister(UnRegister::new(args)?)),
4752
_ => bail!("Unknown command messsage: {cmd}"),
4853
}
4954
}

core/command/unregister.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::state::SharedState;
2+
use crate::{daemon, DaemonCommand};
3+
use anyhow::{bail, Result};
4+
use async_trait::async_trait;
5+
use tracing::trace;
6+
7+
/// Register new client with workspace
8+
#[derive(Debug)]
9+
pub struct UnRegister {
10+
pub pid: i32,
11+
pub root: String,
12+
}
13+
14+
impl UnRegister {
15+
pub fn new(args: Vec<&str>) -> Result<Self> {
16+
let pid = args.get(0);
17+
let root = args.get(1);
18+
19+
if pid.is_none() || root.is_none() {
20+
bail!("Missing arugments: [ pid: {:?}, root: {:?} ]", pid, root)
21+
}
22+
23+
Ok(Self {
24+
pid: pid.unwrap().parse::<i32>()?,
25+
root: root.unwrap().to_string(),
26+
})
27+
}
28+
29+
pub fn request(pid: i32, root: String) -> Result<()> {
30+
daemon::execute(&["unregister", pid.to_string().as_str(), root.as_str()])
31+
}
32+
}
33+
34+
#[async_trait]
35+
impl DaemonCommand for UnRegister {
36+
async fn handle(&self, state: SharedState) -> Result<()> {
37+
trace!("{:?}", self);
38+
state
39+
.lock()
40+
.await
41+
.remove_workspace(&self.root, self.pid)
42+
.await
43+
}
44+
}

core/state.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::workspace::Workspace;
2-
use anyhow::{Ok, Result};
2+
use anyhow::{bail, Ok, Result};
33
use libproc::libproc::proc_pid;
44
use std::collections::HashMap;
55
use std::sync::Arc;
@@ -49,15 +49,34 @@ impl State {
4949
match self.workspaces.get_mut(root) {
5050
Some(workspace) => workspace.add_client(pid),
5151
None => {
52-
self.workspaces.insert(
53-
root.to_string(),
54-
Workspace::new_with_client(&root, pid).await?,
55-
);
52+
let workspace = Workspace::new_with_client(&root, pid).await?;
53+
54+
tracing::info!("Managing [{}] {:?}", workspace.project.name(), root);
55+
56+
self.workspaces.insert(root.to_string(), workspace);
5657
}
5758
};
5859

5960
// Print New state
6061
trace!("{:#?}", self);
6162
Ok(())
6263
}
64+
65+
// Remove remove client from workspace and the workspace if it's this client was the last one.
66+
pub async fn remove_workspace(&mut self, root: &str, pid: i32) -> Result<()> {
67+
let mut name = None;
68+
if let Some(workspace) = self.workspaces.get_mut(root) {
69+
let clients_len = workspace.remove_client(pid);
70+
clients_len
71+
.eq(&0)
72+
.then(|| name = workspace.project.name().to_string().into());
73+
} else {
74+
bail!("workspace with '{root}' with given pid {pid} doesn't exist")
75+
}
76+
if let Some(name) = name {
77+
tracing::info!("Dropping [{}] {:?}", name, root);
78+
self.workspaces.remove(root);
79+
}
80+
Ok(())
81+
}
6382
}

core/workspace.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ impl Workspace {
3434
Project::new_from_project_yml(root.clone(), path).await?
3535
};
3636

37-
tracing::info!("Managing [{}] {:?}", project.name(), root);
38-
3937
Ok(Self {
4038
root,
4139
project,
@@ -53,7 +51,7 @@ impl Workspace {
5351
let name = self.project.name();
5452
self.clients.retain(|&pid| {
5553
if proc_pid::name(pid).is_err() {
56-
tracing::debug!("[{}]: Remove Client: {pid}", name);
54+
tracing::info!("[{}]: Remove Client: {pid}", name);
5755
false
5856
} else {
5957
true
@@ -66,10 +64,17 @@ impl Workspace {
6664
// Remove no longer active clients
6765
self.update_clients();
6866
// NOTE: Implicitly assuming that pid is indeed a valid pid
69-
tracing::debug!("[{}] Add Client: {pid}", self.name());
67+
tracing::info!("[{}] Add Client: {pid}", self.name());
7068
self.clients.push(pid)
7169
}
7270

71+
/// Remove client from workspace
72+
pub fn remove_client(&mut self, pid: i32) -> usize {
73+
tracing::info!("[{}] Remove Client: {pid}", self.name());
74+
self.clients.retain(|&p| p != pid);
75+
self.clients.iter().count()
76+
}
77+
7378
/// Wrapper around Project.name:
7479
/// Returns project name
7580
pub fn name(&self) -> &str {

0 commit comments

Comments
 (0)