feat: allow filtering workspace filetree

This commit is contained in:
əlemi 2024-08-21 14:57:07 +02:00
parent 3b45c4ddb6
commit a99eee170d
Signed by: alemi
GPG key ID: A4895B84D311642C
6 changed files with 20 additions and 14 deletions

View file

@ -7,8 +7,8 @@ use crate::buffer::controller::BufferController;
#[napi]
impl BufferController {
#[napi(ts_args_type = "fun: (event: TextChange) => void")]
pub fn callback(&self, fun: napi::JsFunction) -> napi::Result<()>{
#[napi(js_name = "callback", ts_args_type = "fun: (event: TextChange) => void")]
pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{
let tsfn : ThreadsafeFunction<crate::api::TextChange, Fatal> =
fun.create_threadsafe_function(0,
|ctx : ThreadSafeCallContext<crate::api::TextChange>| {
@ -41,4 +41,4 @@ impl BufferController {
pub async fn js_send(&self, op: TextChange) -> napi::Result<()> {
Ok(self.send(op).await?)
}
}
}

View file

@ -42,8 +42,8 @@ impl From<crate::api::Cursor> for JsCursor {
#[napi]
impl CursorController {
#[napi(ts_args_type = "fun: (event: Cursor) => void")]
pub fn callback(&self, fun: napi::JsFunction) -> napi::Result<()>{
#[napi(js_name = "callback", ts_args_type = "fun: (event: Cursor) => void")]
pub fn jscallback(&self, fun: napi::JsFunction) -> napi::Result<()>{
let tsfn : ThreadsafeFunction<JsCursor, ErrorStrategy::Fatal> =
fun.create_threadsafe_function(0,
|ctx : ThreadSafeCallContext<JsCursor>| {

View file

@ -12,8 +12,8 @@ impl Workspace {
}
#[napi(js_name = "filetree")]
pub fn js_filetree(&self) -> Vec<String> {
self.filetree()
pub fn js_filetree(&self, filter: Option<&str>) -> Vec<String> {
self.filetree(filter)
}
#[napi(js_name = "cursor")]
@ -41,4 +41,4 @@ impl Workspace {
Ok(self.delete(&path).await?)
}
}
}

View file

@ -166,12 +166,15 @@ impl LuaUserData for CodempWorkspace {
});
Ok(())
});
methods.add_method("filetree", |_, this, (filter,):(Option<String>,)|
Ok(this.filetree(filter.as_deref()))
);
}
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("id", |_, this| Ok(this.id()));
fields.add_field_method_get("cursor", |_, this| Ok(this.cursor()));
fields.add_field_method_get("filetree", |_, this| Ok(this.filetree()));
fields.add_field_method_get("active_buffers", |_, this| Ok(this.buffer_list()));
// fields.add_field_method_get("users", |_, this| Ok(this.0.users())); // TODO
}

View file

@ -72,7 +72,7 @@ impl Workspace {
.list_buffer_users(path.as_str())
.await?
.into_iter()
.map(|e| e.id)
.map(|e| e.id.to_string())
.collect();
Ok(usrlist)
@ -118,8 +118,8 @@ impl Workspace {
}
#[pyo3(name = "filetree")]
fn pyfiletree(&self) -> Vec<String> {
self.filetree()
fn pyfiletree(&self, filter: Option<&str>) -> Vec<String> {
self.filetree(filter)
}
}

View file

@ -332,8 +332,11 @@ impl Workspace {
/// get the currently cached "filetree"
// #[cfg_attr(feature = "js", napi)] // https://github.com/napi-rs/napi-rs/issues/1120
pub fn filetree(&self) -> Vec<String> {
self.0.filetree.iter().map(|f| f.clone()).collect()
pub fn filetree(&self, filter: Option<&str>) -> Vec<String> {
self.0.filetree.iter()
.filter(|f| filter.map_or(true, |flt| f.starts_with(flt)))
.map(|f| f.clone())
.collect()
}
}