From 2f6557f9719f2ce66f0e31b4d77966b54a89e0ad Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 21 Sep 2024 12:05:09 +0200 Subject: [PATCH 1/4] feat: add user_list, allow config::new with &str --- src/api/config.rs | 6 +++--- src/workspace.rs | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/api/config.rs b/src/api/config.rs index 8a70a10..f22c183 100644 --- a/src/api/config.rs +++ b/src/api/config.rs @@ -27,10 +27,10 @@ pub struct Config { impl Config { /// construct a new Config object, with given username and password - pub fn new(username: String, password: String) -> Self { + pub fn new(username: impl ToString, password: impl ToString) -> Self { Self { - username, - password, + username: username.to_string(), + password: password.to_string(), host: None, port: None, tls: None, diff --git a/src/workspace.rs b/src/workspace.rs index b474121..a6d0cee 100644 --- a/src/workspace.rs +++ b/src/workspace.rs @@ -278,6 +278,15 @@ impl Workspace { .collect() } + /// Get all names of users currently in this workspace + pub fn user_list(&self) -> Vec { + self.0 + .users + .iter() + .map(|elem| elem.value().name.clone()) + .collect() + } + /// 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 From db77fce3ab78bb17d376658692dc9511b91cfbff Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 21 Sep 2024 13:18:38 +0200 Subject: [PATCH 2/4] chore: implemented user_list for lua, js, py --- src/ffi/js/workspace.rs | 5 +++++ src/ffi/lua/workspace.rs | 4 ++++ src/ffi/python/workspace.rs | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/src/ffi/js/workspace.rs b/src/ffi/js/workspace.rs index a470c2e..8437a82 100644 --- a/src/ffi/js/workspace.rs +++ b/src/ffi/js/workspace.rs @@ -16,6 +16,11 @@ impl Workspace { self.filetree(filter, strict) } + #[napi(js_name = "user_list")] + pub fn js_user_list(&self) -> Vec { + self.user_list() + } + #[napi(js_name = "cursor")] pub fn js_cursor(&self) -> CursorController { self.cursor() diff --git a/src/ffi/lua/workspace.rs b/src/ffi/lua/workspace.rs index 5ce4bd2..caed60b 100644 --- a/src/ffi/lua/workspace.rs +++ b/src/ffi/lua/workspace.rs @@ -41,6 +41,10 @@ impl LuaUserData for CodempWorkspace { methods.add_method("filetree", |_, this, (filter, strict,):(Option, Option,)| Ok(this.filetree(filter.as_deref(), strict.unwrap_or(false))) ); + + methods.add_method("user_list", |_, this, ()| + Ok(this.user_list()) + ); } fn add_fields>(fields: &mut F) { diff --git a/src/ffi/python/workspace.rs b/src/ffi/python/workspace.rs index 1a10f4d..79574b8 100644 --- a/src/ffi/python/workspace.rs +++ b/src/ffi/python/workspace.rs @@ -86,4 +86,9 @@ impl Workspace { fn pyfiletree(&self, filter: Option<&str>, strict: bool) -> Vec { self.filetree(filter, strict) } + + #[pyo3(name = "user_list")] + fn pyuser_list(&self) -> Vec { + self.user_list() + } } From ba1a53c3a971e73e42e47f761de177e8e0634556 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 21 Sep 2024 13:18:53 +0200 Subject: [PATCH 3/4] chore: draft of user_list for java but its broken --- dist/java/src/mp/code/Workspace.java | 10 ++++++++++ src/ffi/java/workspace.rs | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/dist/java/src/mp/code/Workspace.java b/dist/java/src/mp/code/Workspace.java index 33f2de3..38cb016 100644 --- a/dist/java/src/mp/code/Workspace.java +++ b/dist/java/src/mp/code/Workspace.java @@ -69,6 +69,16 @@ public final class Workspace { return get_file_tree(this.ptr, filter.orElse(null), strict); } + private static native String[] user_list(long self); + + /** + * Get names of all users currently in this workspace + * @return an array containing user display names + */ + public String[] userList() { + return user_list(this.ptr); + } + private static native String[] active_buffers(long self); /** diff --git a/src/ffi/java/workspace.rs b/src/ffi/java/workspace.rs index 27a9702..341991c 100644 --- a/src/ffi/java/workspace.rs +++ b/src/ffi/java/workspace.rs @@ -76,6 +76,27 @@ pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree( }).jexcept(&mut env).as_raw() } +/// Get the user list. +#[no_mangle] +pub extern "system" fn Java_mp_code_Workspace_user_1list( + mut env: JNIEnv, + _class: JClass, + self_ptr: jlong, +) -> jobjectArray { + let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) }; + + let user_list = workspace.user_list(); + env.find_class("java/lang/String") + .and_then(|class| env.new_object_array(user_list.len() as i32, class, JObject::null())) + .inspect(|arr| { + for (idx, path) in user_list.iter().enumerate() { + env.new_string(path) + .and_then(|path| env.set_object_array_element(arr, idx as i32, path)) + .jexcept(&mut env) + } + }).jexcept(&mut env).as_raw() +} + /// Gets a list of the active buffers. #[no_mangle] pub extern "system" fn Java_mp_code_Workspace_active_1buffers( From 9d1b919b3159675be0f67938ffe5ae84a89eb7e5 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 24 Sep 2024 19:57:03 +0200 Subject: [PATCH 4/4] Revert "chore: draft of user_list for java but its broken" This reverts commit ba1a53c3a971e73e42e47f761de177e8e0634556. --- dist/java/src/mp/code/Workspace.java | 10 ---------- src/ffi/java/workspace.rs | 21 --------------------- 2 files changed, 31 deletions(-) diff --git a/dist/java/src/mp/code/Workspace.java b/dist/java/src/mp/code/Workspace.java index 38cb016..33f2de3 100644 --- a/dist/java/src/mp/code/Workspace.java +++ b/dist/java/src/mp/code/Workspace.java @@ -69,16 +69,6 @@ public final class Workspace { return get_file_tree(this.ptr, filter.orElse(null), strict); } - private static native String[] user_list(long self); - - /** - * Get names of all users currently in this workspace - * @return an array containing user display names - */ - public String[] userList() { - return user_list(this.ptr); - } - private static native String[] active_buffers(long self); /** diff --git a/src/ffi/java/workspace.rs b/src/ffi/java/workspace.rs index 341991c..27a9702 100644 --- a/src/ffi/java/workspace.rs +++ b/src/ffi/java/workspace.rs @@ -76,27 +76,6 @@ pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree( }).jexcept(&mut env).as_raw() } -/// Get the user list. -#[no_mangle] -pub extern "system" fn Java_mp_code_Workspace_user_1list( - mut env: JNIEnv, - _class: JClass, - self_ptr: jlong, -) -> jobjectArray { - let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) }; - - let user_list = workspace.user_list(); - env.find_class("java/lang/String") - .and_then(|class| env.new_object_array(user_list.len() as i32, class, JObject::null())) - .inspect(|arr| { - for (idx, path) in user_list.iter().enumerate() { - env.new_string(path) - .and_then(|path| env.set_object_array_element(arr, idx as i32, path)) - .jexcept(&mut env) - } - }).jexcept(&mut env).as_raw() -} - /// Gets a list of the active buffers. #[no_mangle] pub extern "system" fn Java_mp_code_Workspace_active_1buffers(