diff --git a/dist/java/src/mp/code/Workspace.java b/dist/java/src/mp/code/Workspace.java index ddc971a..3b5da95 100644 --- a/dist/java/src/mp/code/Workspace.java +++ b/dist/java/src/mp/code/Workspace.java @@ -30,9 +30,9 @@ public class Workspace { return Optional.ofNullable(get_buffer(this.ptr, path)); } - private static native String[] get_file_tree(long self, String filter); - public String[] getFileTree(Optional filter) { - return get_file_tree(this.ptr, filter.orElse(null)); + private static native String[] get_file_tree(long self, String filter, boolean strict); + public String[] getFileTree(Optional filter, boolean strict) { + return get_file_tree(this.ptr, filter.orElse(null), strict); } private static native void create_buffer(String path) throws ConnectionRemoteException; diff --git a/dist/lua/annotations.lua b/dist/lua/annotations.lua index 0f5e822..8e19337 100644 --- a/dist/lua/annotations.lua +++ b/dist/lua/annotations.lua @@ -147,10 +147,11 @@ function Workspace:attach_buffer(path) end ---detach from an active buffer, closing all streams. returns false if buffer was no longer active function Workspace:detach_buffer(path) end ----@param filter? string only return elements starting with given filter +---@param filter? string apply a filter to the return elements +---@param strict boolean whether to strictly match or just check whether it starts with it ---@return string[] ---return the list of available buffers in this workspace, as relative paths from workspace root -function Workspace:filetree(filter) end +function Workspace:filetree(filter, strict) end ---@return NilPromise ---@async diff --git a/dist/py/src/codemp/codemp.pyi b/dist/py/src/codemp/codemp.pyi index 6e064e8..dae26f7 100644 --- a/dist/py/src/codemp/codemp.pyi +++ b/dist/py/src/codemp/codemp.pyi @@ -66,7 +66,7 @@ class Workspace: def cursor(self) -> CursorController: ... def buffer_by_name(self, path: str) -> Optional[BufferController]: ... def buffer_list(self) -> list[str]: ... - def filetree(self, filter: Optional[str]) -> list[str]: ... + def filetree(self, filter: Optional[str], strict: bool) -> list[str]: ... class TextChange: """ diff --git a/src/ffi/java/workspace.rs b/src/ffi/java/workspace.rs index 2a21649..f024eda 100644 --- a/src/ffi/java/workspace.rs +++ b/src/ffi/java/workspace.rs @@ -1,4 +1,4 @@ -use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jlong, jobject, jobjectArray, jstring}, JNIEnv}; +use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jboolean, jlong, jobject, jobjectArray, jstring}, JNIEnv}; use crate::Workspace; use super::{JExceptable, JObjectify, RT}; @@ -69,7 +69,8 @@ pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree( mut env: JNIEnv, _class: JClass, self_ptr: jlong, - filter: JString + filter: JString, + strict: jboolean ) -> jobjectArray { let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) }; let filter: Option = if filter.is_null() { @@ -82,7 +83,7 @@ pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree( ) }; - let file_tree = workspace.filetree(filter.as_deref()); + let file_tree = workspace.filetree(filter.as_deref(), strict != 0); env.find_class("java/lang/String") .and_then(|class| env.new_object_array(file_tree.len() as i32, class, JObject::null())) .inspect(|arr| { diff --git a/src/ffi/js/workspace.rs b/src/ffi/js/workspace.rs index 0711beb..a470c2e 100644 --- a/src/ffi/js/workspace.rs +++ b/src/ffi/js/workspace.rs @@ -12,8 +12,8 @@ impl Workspace { } #[napi(js_name = "filetree")] - pub fn js_filetree(&self, filter: Option<&str>) -> Vec { - self.filetree(filter) + pub fn js_filetree(&self, filter: Option<&str>, strict: bool) -> Vec { + self.filetree(filter, strict) } #[napi(js_name = "cursor")] diff --git a/src/ffi/lua.rs b/src/ffi/lua.rs index 756e45a..4a80bab 100644 --- a/src/ffi/lua.rs +++ b/src/ffi/lua.rs @@ -294,8 +294,8 @@ impl LuaUserData for CodempWorkspace { // Ok(()) // }); - methods.add_method("filetree", |_, this, (filter,):(Option,)| - Ok(this.filetree(filter.as_deref())) + methods.add_method("filetree", |_, this, (filter, strict,):(Option, bool,)| + Ok(this.filetree(filter.as_deref(), strict)) ); } diff --git a/src/ffi/python/workspace.rs b/src/ffi/python/workspace.rs index c9b12e8..1a10f4d 100644 --- a/src/ffi/python/workspace.rs +++ b/src/ffi/python/workspace.rs @@ -82,8 +82,8 @@ impl Workspace { } #[pyo3(name = "filetree")] - #[pyo3(signature = (filter=None))] - fn pyfiletree(&self, filter: Option<&str>) -> Vec { - self.filetree(filter) + #[pyo3(signature = (filter=None, strict=false))] + fn pyfiletree(&self, filter: Option<&str>, strict: bool) -> Vec { + self.filetree(filter, strict) } } diff --git a/src/workspace.rs b/src/workspace.rs index c8ed85c..a59af0d 100644 --- a/src/workspace.rs +++ b/src/workspace.rs @@ -279,10 +279,17 @@ impl Workspace { } /// Get the filetree as it is currently cached. + /// A filter may be applied, and it may be strict (equality check) or not (starts_with check). // #[cfg_attr(feature = "js", napi)] // https://github.com/napi-rs/napi-rs/issues/1120 - pub fn filetree(&self, filter: Option<&str>) -> Vec { + pub fn filetree(&self, filter: Option<&str>, strict: bool) -> Vec { self.0.filetree.iter() - .filter(|f| filter.map_or(true, |flt| f.starts_with(flt))) + .filter(|f| filter.map_or(true, |flt| { + if strict { + f.eq(flt) + } else { + f.starts_with(flt) + } + })) .map(|f| f.clone()) .collect() }