Skip to content

Commit

Permalink
feat: allow filtering workspace filetree
Browse files Browse the repository at this point in the history
  • Loading branch information
alemidev committed Aug 21, 2024
1 parent 3b45c4d commit a99eee1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/ffi/js/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>| {
Expand Down Expand Up @@ -41,4 +41,4 @@ impl BufferController {
pub async fn js_send(&self, op: TextChange) -> napi::Result<()> {
Ok(self.send(op).await?)
}
}
}
4 changes: 2 additions & 2 deletions src/ffi/js/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>| {
Expand Down
6 changes: 3 additions & 3 deletions src/ffi/js/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -41,4 +41,4 @@ impl Workspace {
Ok(self.delete(&path).await?)
}

}
}
5 changes: 4 additions & 1 deletion src/ffi/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions src/ffi/python/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/workspace/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down

0 comments on commit a99eee1

Please sign in to comment.