Skip to content

Commit 12336ff

Browse files
committed
feat(nvim): get project info
1 parent d256c49 commit 12336ff

File tree

6 files changed

+110
-6
lines changed

6 files changed

+110
-6
lines changed

lua/xcodebase/init.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ local address = vim.env.NVIM_LISTEN_ADDRESS
88
---@field ensure fun():boolean: When the a new server started it should return true
99
---@field is_running fun():boolean
1010
---@field register fun(pid: number, root: string):boolean
11-
M.daemon = lib.daemon
11+
M.daemon = {}
12+
13+
M.daemon.project_info = function(root)
14+
require("xcodebase.state").projects[root] = nil
15+
lib.daemon.project_info(pid, root)
16+
-- while require("xcodebase.state").projects[root] == nil do
17+
-- print "Wating"
18+
-- end
19+
end
1220

1321
---@class XcodeBaseCommand
1422
local command = lib.command
@@ -37,8 +45,8 @@ M.try_register = function(opts)
3745
local root = vim.loop.cwd()
3846

3947
if M.should_register(root, opts) then
40-
local _ = M.daemon.ensure()
41-
M.daemon.register(pid, root, address)
48+
local _ = lib.daemon.ensure()
49+
lib.daemon.register(pid, root, address)
4250
vim.cmd [[ autocmd VimLeavePre * lua require'xcodebase'.daemon.drop(vim.fn.getpid(), vim.loop.cwd())]]
4351
else
4452
return

lua/xcodebase/state.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
local M = {}
2+
3+
---@class XcodeTarget
4+
---@field type string
5+
---@field platform string
6+
---@field sources string[]
7+
8+
---@class XcodeProject
9+
---@field name string @Project name
10+
---@field targets string @Project name
11+
---@field root string @Project root
12+
13+
---Holds project informations
14+
---@type table<string, XcodeProject>
15+
M.projects = {}
16+
17+
return M

src/daemon.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub enum DaemonRequest {
4040
Run(Run),
4141
RenameFile(RenameFile),
4242
Register(Register),
43+
ProjectInfo(ProjectInfo),
4344
Drop(Drop),
4445
}
4546

@@ -53,6 +54,7 @@ impl DaemonRequest {
5354
DaemonRequest::RenameFile(c) => c.handle(state).await,
5455
DaemonRequest::Register(c) => c.handle(state).await,
5556
DaemonRequest::Drop(c) => c.handle(state).await,
57+
DaemonRequest::ProjectInfo(c) => c.handle(state).await,
5658
}
5759
}
5860

@@ -65,6 +67,7 @@ impl DaemonRequest {
6567
RenameFile::KEY => Self::RenameFile(RenameFile::parse(args)?),
6668
Register::KEY => Self::Register(Register::parse(args)?),
6769
Drop::KEY => Self::Drop(Drop::parse(args)?),
70+
ProjectInfo::KEY => Self::ProjectInfo(ProjectInfo::parse(args)?),
6871
cmd => anyhow::bail!("Unknown command messsage: {cmd}"),
6972
})
7073
}
@@ -79,6 +82,7 @@ impl Daemon {
7982
table.set("ensure", lua.create_function(Self::ensure)?)?;
8083
table.set("register", lua.create_function(Register::lua)?)?;
8184
table.set("drop", lua.create_function(Drop::lua)?)?;
85+
table.set("project_info", lua.create_function(ProjectInfo::lua)?)?;
8286
Ok(table)
8387
}
8488

src/daemon/requests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
mod build;
22
mod drop;
3+
mod project_info;
34
mod register;
45
mod rename_file;
56
mod run;
67

78
pub use build::Build;
89
pub use drop::Drop;
10+
pub use project_info::ProjectInfo;
911
pub use register::Register;
1012
pub use rename_file::RenameFile;
1113
pub use run::Run;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

src/daemon/state/project.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
#[cfg(feature = "daemon")]
2+
use serde::{Deserialize, Serialize};
3+
14
use std::collections::HashMap;
25
use std::path::PathBuf;
36

47
/// Represent Xcode Target
58
#[derive(Debug)]
69
#[allow(dead_code)]
7-
#[cfg_attr(feature = "serial", derive(serde::Deserialize))]
10+
#[cfg_attr(feature = "serial", derive(Deserialize, Serialize))]
811
pub struct Target {
912
r#type: String,
1013
platform: String,
1114
sources: Vec<PathBuf>,
1215
}
1316

1417
#[derive(Debug, Default)]
15-
#[cfg_attr(feature = "serial", derive(serde::Deserialize))]
18+
#[cfg_attr(feature = "serial", derive(Deserialize, Serialize))]
1619
pub struct LocalConfig {
1720
pub ignore: Vec<String>,
1821
}
@@ -21,7 +24,7 @@ pub type TargetMap = HashMap<String, Target>;
2124

2225
/// Represent XcodeGen Project
2326
#[derive(Debug)]
24-
#[cfg_attr(feature = "serial", derive(serde::Deserialize))]
27+
#[cfg_attr(feature = "serial", derive(Deserialize, Serialize))]
2528
pub struct Project {
2629
/// Project Name or rather xproj generated file name.
2730
name: String,

0 commit comments

Comments
 (0)