feat: strict filetree()

This commit is contained in:
zaaarf 2024-09-13 20:02:42 +02:00
parent c0b8906043
commit 92d181246e
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
6 changed files with 23 additions and 15 deletions

View file

@ -30,9 +30,9 @@ public class Workspace {
return Optional.ofNullable(get_buffer(this.ptr, path)); return Optional.ofNullable(get_buffer(this.ptr, path));
} }
private static native String[] get_file_tree(long self, String filter); private static native String[] get_file_tree(long self, String filter, boolean strict);
public String[] getFileTree(Optional<String> filter) { public String[] getFileTree(Optional<String> filter, boolean strict) {
return get_file_tree(this.ptr, filter.orElse(null)); return get_file_tree(this.ptr, filter.orElse(null), strict);
} }
private static native void create_buffer(String path) throws ConnectionRemoteException; private static native void create_buffer(String path) throws ConnectionRemoteException;

View file

@ -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 crate::Workspace;
use super::{JExceptable, JObjectify, RT}; use super::{JExceptable, JObjectify, RT};
@ -69,7 +69,8 @@ pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree(
mut env: JNIEnv, mut env: JNIEnv,
_class: JClass, _class: JClass,
self_ptr: jlong, self_ptr: jlong,
filter: JString filter: JString,
strict: jboolean
) -> jobjectArray { ) -> jobjectArray {
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) }; let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
let filter: Option<String> = if filter.is_null() { let filter: Option<String> = 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") env.find_class("java/lang/String")
.and_then(|class| env.new_object_array(file_tree.len() as i32, class, JObject::null())) .and_then(|class| env.new_object_array(file_tree.len() as i32, class, JObject::null()))
.inspect(|arr| { .inspect(|arr| {

View file

@ -12,8 +12,8 @@ impl Workspace {
} }
#[napi(js_name = "filetree")] #[napi(js_name = "filetree")]
pub fn js_filetree(&self, filter: Option<&str>) -> Vec<String> { pub fn js_filetree(&self, filter: Option<&str>, strict: bool) -> Vec<String> {
self.filetree(filter) self.filetree(filter, strict)
} }
#[napi(js_name = "cursor")] #[napi(js_name = "cursor")]

View file

@ -294,8 +294,8 @@ impl LuaUserData for CodempWorkspace {
// Ok(()) // Ok(())
// }); // });
methods.add_method("filetree", |_, this, (filter,):(Option<String>,)| methods.add_method("filetree", |_, this, (filter, strict,):(Option<String>, bool,)|
Ok(this.filetree(filter.as_deref())) Ok(this.filetree(filter.as_deref(), strict))
); );
} }

View file

@ -82,8 +82,8 @@ impl Workspace {
} }
#[pyo3(name = "filetree")] #[pyo3(name = "filetree")]
#[pyo3(signature = (filter=None))] #[pyo3(signature = (filter=None, strict=false))]
fn pyfiletree(&self, filter: Option<&str>) -> Vec<String> { fn pyfiletree(&self, filter: Option<&str>, strict: bool) -> Vec<String> {
self.filetree(filter) self.filetree(filter, strict)
} }
} }

View file

@ -279,10 +279,17 @@ impl Workspace {
} }
/// Get the filetree as it is currently cached. /// 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 // #[cfg_attr(feature = "js", napi)] // https://github.com/napi-rs/napi-rs/issues/1120
pub fn filetree(&self, filter: Option<&str>) -> Vec<String> { pub fn filetree(&self, filter: Option<&str>, strict: bool) -> Vec<String> {
self.0.filetree.iter() 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()) .map(|f| f.clone())
.collect() .collect()
} }