|
| 1 | +#[cfg(feature = "mlua")] |
| 2 | +use crate::daemon::Daemon; |
| 3 | + |
| 4 | +#[cfg(feature = "daemon")] |
| 5 | +use crate::daemon::{DaemonRequestHandler, DaemonState}; |
| 6 | + |
| 7 | +#[cfg(feature = "daemon")] |
| 8 | +use anyhow::{bail, Result}; |
| 9 | + |
| 10 | +/// Return curr working direcotry project info |
| 11 | +#[derive(Debug)] |
| 12 | +pub struct ProjectInfo { |
| 13 | + pub pid: i32, |
| 14 | + pub root: String, |
| 15 | +} |
| 16 | + |
| 17 | +impl ProjectInfo { |
| 18 | + pub const KEY: &'static str = "project_info"; |
| 19 | +} |
| 20 | + |
| 21 | +#[cfg(feature = "daemon")] |
| 22 | +#[async_trait::async_trait] |
| 23 | +impl DaemonRequestHandler<ProjectInfo> for ProjectInfo { |
| 24 | + fn parse(args: Vec<&str>) -> Result<Self> { |
| 25 | + if let (Some(pid), Some(root)) = (args.get(0), args.get(1)) { |
| 26 | + Ok(Self { |
| 27 | + pid: pid.parse::<i32>()?, |
| 28 | + root: root.to_string(), |
| 29 | + }) |
| 30 | + } else { |
| 31 | + anyhow::bail!("Missing arugments: {:?}", args) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + async fn handle(&self, state: DaemonState) -> Result<()> { |
| 36 | + tracing::info!("Getting info for {}", self.root); |
| 37 | + let state = state.lock().await; |
| 38 | + |
| 39 | + let workspace = match state.workspaces.get(&self.root) { |
| 40 | + Some(o) => o, |
| 41 | + None => bail!("No workspace for {}", self.root), |
| 42 | + }; |
| 43 | + |
| 44 | + let nvim = match workspace.clients.get(&self.pid) { |
| 45 | + Some(o) => o, |
| 46 | + None => bail!("No nvim instance for {}", self.pid), |
| 47 | + }; |
| 48 | + |
| 49 | + nvim.exec_lua( |
| 50 | + &format!( |
| 51 | + "require'xcodebase.state'.projects['{}'] = vim.json.decode([[{}]])", |
| 52 | + self.root, |
| 53 | + serde_json::to_string(&workspace.project)? |
| 54 | + ), |
| 55 | + vec![], |
| 56 | + ) |
| 57 | + .await?; |
| 58 | + |
| 59 | + Ok(()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[cfg(feature = "lua")] |
| 64 | +impl ProjectInfo { |
| 65 | + pub fn lua(lua: &mlua::Lua, (pid, root): (i32, String)) -> mlua::Result<()> { |
| 66 | + use crate::util::mlua::LuaExtension; |
| 67 | + lua.trace(&format!("grapping project info"))?; |
| 68 | + Daemon::execute(&[Self::KEY, pid.to_string().as_str(), root.as_str()]) |
| 69 | + } |
| 70 | +} |
0 commit comments