Skip to content

Commit

Permalink
chore: split list_workspaces, renamed filetree, refactored fetch_user…
Browse files Browse the repository at this point in the history
…s and fetch_buffers
  • Loading branch information
zaaarf committed Oct 15, 2024
1 parent 6f04c38 commit 8ce369e
Show file tree
Hide file tree
Showing 21 changed files with 239 additions and 183 deletions.
21 changes: 15 additions & 6 deletions dist/java/src/mp/code/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,26 @@ public void inviteToWorkspace(String workspaceId, String user) throws Connection
invite_to_workspace(this.ptr, workspaceId, user);
}

private static native String[] list_workspaces(long self, boolean owned, boolean invited) throws ConnectionRemoteException;
private static native String[] fetch_owned_workspaces(long self) throws ConnectionRemoteException;

/**
* Lists available workspaces according to certain filters.
* @param owned if owned workspaces should be included
* @param invited if workspaces the user is invited to should be included
* Lists workspaces owned by the current user.
* @return an array of workspace IDs
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public String[] listWorkspaces(boolean owned, boolean invited) throws ConnectionRemoteException {
return list_workspaces(this.ptr, owned, invited);
public String[] fetchOwnedWorkspaces() throws ConnectionRemoteException {
return fetch_owned_workspaces(this.ptr);
}

private static native String[] fetch_joined_workspaces(long self) throws ConnectionRemoteException;

/**
* Lists workspaces the current user has joined.
* @return an array of workspace IDs
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public String[] fetchJoinedWorkspaces() throws ConnectionRemoteException {
return fetch_joined_workspaces(this.ptr);
}

private static native String[] active_workspaces(long self);
Expand Down
41 changes: 21 additions & 20 deletions dist/java/src/mp/code/Workspace.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package mp.code;

import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;

import lombok.Getter;
import mp.code.data.User;
import mp.code.exceptions.ConnectionException;
import mp.code.exceptions.ConnectionRemoteException;
import mp.code.exceptions.ControllerException;
Expand Down Expand Up @@ -57,17 +57,16 @@ public Optional<BufferController> getBuffer(String path) {
return Optional.ofNullable(get_buffer(this.ptr, path));
}

private static native String[] filetree(long self, String filter, boolean strict);
private static native String[] search_buffers(long self, String filter);

/**
* Gets the file tree for this workspace, optionally filtering it.
* @param filter applies an optional filter to the outputs
* @param strict whether it should be a strict match (equals) or not (startsWith)
* Searches for buffers matching the filter in this workspace.
* @param filter the filter to apply
* @return an array containing file tree as flat paths
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public String[] filetree(Optional<String> filter, boolean strict) {
return filetree(this.ptr, filter.orElse(null), strict);
public String[] searchBuffers(Optional<String> filter) {
return search_buffers(this.ptr, filter.orElse(null));
}

private static native String[] active_buffers(long self);
Expand Down Expand Up @@ -125,37 +124,39 @@ public boolean detachBuffer(String path) {
return detach_buffer(this.ptr, path);
}

private static native void fetch_buffers(long self) throws ConnectionRemoteException;
private static native String[] fetch_buffers(long self) throws ConnectionRemoteException;

/**
* Updates the local list of buffers.
* Updates and fetches the local list of buffers.
* @return the updated list
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public void fetchBuffers() throws ConnectionRemoteException {
fetch_buffers(this.ptr);
public String[] fetchBuffers() throws ConnectionRemoteException {
return fetch_buffers(this.ptr);
}

private static native void fetch_users(long self) throws ConnectionRemoteException;
private static native User[] fetch_users(long self) throws ConnectionRemoteException;

/**
* Updates the local list of users.
* Updates and fetches the local list of users.
* @return the updated list
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public void fetchUsers() throws ConnectionRemoteException {
fetch_buffers(this.ptr);
public User[] fetchUsers() throws ConnectionRemoteException {
return fetch_users(this.ptr);
}

private static native UUID[] list_buffer_users(long self, String path) throws ConnectionRemoteException;
private static native User[] fetch_buffer_users(long self, String path) throws ConnectionRemoteException;

/**
* Lists the user attached to a certain buffer.
* Fetches the users attached to a certain buffer.
* The user must be attached to the buffer to perform this operation.
* @param path the path of the buffer to search
* @return an array of user {@link UUID UUIDs}
* @return an array of {@link User}s
* @throws ConnectionRemoteException if an error occurs in communicating with the server, or the user wasn't attached
*/
public UUID[] listBufferUsers(String path) throws ConnectionRemoteException {
return list_buffer_users(this.ptr, path);
public User[] fetchBufferUsers(String path) throws ConnectionRemoteException {
return fetch_buffer_users(this.ptr, path);
}

private static native void delete_buffer(long self, String path) throws ConnectionRemoteException;
Expand Down
33 changes: 27 additions & 6 deletions dist/lua/annotations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ function MaybeBufferUpdatePromise:cancel() end
---invoke callback asynchronously as soon as promise is ready
function MaybeBufferUpdatePromise:and_then(cb) end

---@class (exact) UserListPromise : Promise
local UserListPromise = {}
--- block until promise is ready and return value
--- @return User[]
function UserListPromise:await() end
--- cancel promise execution
function UserListPromise:cancel() end
---@param cb fun(x: User[]) callback to invoke
---invoke callback asynchronously as soon as promise is ready
function UserListPromise:and_then(cb) end

-- [[ END ASYNC STUFF ]]


Expand Down Expand Up @@ -212,13 +223,17 @@ function Client:delete_workspace(ws) end
---grant user acccess to workspace
function Client:invite_to_workspace(ws, user) end

---@param owned boolean? list owned workspaces, default true
---@param invited boolean? list invited workspaces, default true
---@return StringArrayPromise
---@async
---@nodiscard
---grant user acccess to workspace
function Client:list_workspaces(owned, invited) end
---fetch and list owned workspaces
function Client:fetch_owned_workspaces() end

---@return StringArrayPromise
---@async
---@nodiscard
---fetch and list joined workspaces
function Client:fetch_joined_workspaces() end

---@param ws string workspace id to get
---@return Workspace?
Expand Down Expand Up @@ -281,10 +296,9 @@ function Workspace:attach_buffer(path) end
function Workspace:detach_buffer(path) end

---@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, strict) end
function Workspace:search_buffers(filter) end

---@return User[]
---return all names of users currently in this workspace
Expand All @@ -302,6 +316,13 @@ function Workspace:fetch_buffers(path) end
---force refresh users list from workspace
function Workspace:fetch_users(path) end

---@param path string the buffer to look in
---@return UserListPromise
---@async
---@nodiscard
---fetch the list of users in the given buffer
function Workspace:fetch_buffer_users(path) end

---@class (exact) WorkspaceEvent
---@field type string
---@field value string
Expand Down
11 changes: 6 additions & 5 deletions dist/py/src/codemp/codemp.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class Client:
def create_workspace(self, workspace: str) -> Promise[None]: ...
def delete_workspace(self, workspace: str) -> Promise[None]: ...
def invite_to_workspace(self, workspace: str, username: str) -> Promise[None]: ...
def list_workspaces(self, owned: bool, invited: bool) -> Promise[list[str]]: ...
def fetch_owned_workspaces(self) -> Promise[list[str]]: ...
def fetch_joined_workspaces(self) -> Promise[list[str]]: ...
def leave_workspace(self, workspace: str) -> bool: ...
def get_workspace(self, id: str) -> Workspace: ...
def active_workspaces(self) -> list[str]: ...
Expand All @@ -69,16 +70,16 @@ class Workspace:
def create_buffer(self, path: str) -> Promise[None]: ...
def attach_buffer(self, path: str) -> Promise[BufferController]: ...
def detach_buffer(self, path: str) -> bool: ...
def fetch_buffers(self) -> Promise[None]: ...
def fetch_users(self) -> Promise[None]: ...
def list_buffer_users(self, path: str) -> Promise[list[str]]: ...
def fetch_buffers(self) -> Promise[list[str]]: ...
def fetch_users(self) -> Promise[list[User]]: ...
def fetch_buffer_users(self, path: str) -> Promise[list[User]]: ...
def delete_buffer(self, path: str) -> Promise[None]: ...
def id(self) -> str: ...
def cursor(self) -> CursorController: ...
def get_buffer(self, path: str) -> Optional[BufferController]: ...
def user_list(self) -> list[User]: ...
def active_buffers(self) -> list[str]: ...
def filetree(self, filter: Optional[str], strict: bool) -> list[str]: ...
def search_buffers(self, filter: Optional[str]) -> list[str]: ...
def recv(self) -> Promise[Event]: ...
def try_recv(self) -> Promise[Optional[Event]]: ...
def poll(self) -> Promise[None]: ...
Expand Down
8 changes: 3 additions & 5 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ pub mod event;
/// data structure for remote users
pub mod user;

pub use change::BufferUpdate;
pub use change::TextChange;
pub use change::{BufferUpdate, TextChange};
pub use config::Config;
pub use controller::Controller;
pub use cursor::Cursor;
pub use cursor::Selection;
pub use controller::{AsyncReceiver, AsyncSender, Controller};
pub use cursor::{Cursor, Selection};
pub use event::Event;
pub use user::User;
11 changes: 8 additions & 3 deletions src/buffer/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ impl BufferWorker {

// in case we have a "replace" span
if change.is_delete() {
self.branch
.delete_without_content(&mut self.oplog, self.agent_id, clip_start..clip_end);
self.branch.delete_without_content(
&mut self.oplog,
self.agent_id,
clip_start..clip_end,
);
}

if change.is_insert() {
Expand Down Expand Up @@ -247,7 +250,9 @@ impl BufferWorker {
{
tracing::warn!(
"Insert span ({}, {}) differs from effective content len ({})",
dtop.start(), dtop.end(), dtop.content_as_str().unwrap_or_default().len()
dtop.start(),
dtop.end(),
dtop.content_as_str().unwrap_or_default().len()
);
}
crate::api::BufferUpdate {
Expand Down
31 changes: 19 additions & 12 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,37 @@ impl Client {
Ok(())
}

/// List all available workspaces, also filtering between those owned and those invited to.
pub async fn list_workspaces(&self, owned: bool, invited: bool) -> RemoteResult<Vec<String>> {
let mut workspaces = self
/// Fetch the names of all workspaces owned by the current user.
pub async fn fetch_owned_workspaces(&self) -> RemoteResult<Vec<String>> {
self.fetch_workspaces(true).await
}

/// Fetch the names of all workspaces the current user has joined.
pub async fn fetch_joined_workspaces(&self) -> RemoteResult<Vec<String>> {
self.fetch_workspaces(false).await
}

async fn fetch_workspaces(&self, owned: bool) -> RemoteResult<Vec<String>> {
let workspaces = self
.0
.session
.clone()
.list_workspaces(Empty {})
.await?
.into_inner();

let mut out = Vec::new();

if owned {
out.append(&mut workspaces.owned)
}
if invited {
out.append(&mut workspaces.invited)
Ok(workspaces.owned)
} else {
Ok(workspaces.invited)
}

Ok(out)
}

/// Join and return a [`Workspace`].
pub async fn attach_workspace(&self, workspace: impl AsRef<str>) -> ConnectionResult<Workspace> {
pub async fn attach_workspace(
&self,
workspace: impl AsRef<str>,
) -> ConnectionResult<Workspace> {
let token = self
.0
.session
Expand Down
5 changes: 1 addition & 4 deletions src/ffi/java/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use jni::{objects::JObject, JNIEnv};
use jni_toolbox::jni;

use crate::{
api::{
controller::{AsyncReceiver, AsyncSender},
BufferUpdate, TextChange,
},
api::{AsyncReceiver, AsyncSender, BufferUpdate, TextChange},
errors::ControllerError,
};

Expand Down
16 changes: 9 additions & 7 deletions src/ffi/java/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ fn invite_to_workspace(
super::tokio().block_on(client.invite_to_workspace(workspace, user))
}

/// List available workspaces.
/// List owned workspaces.
#[jni(package = "mp.code", class = "Client")]
fn list_workspaces(
client: &mut Client,
owned: bool,
invited: bool,
) -> Result<Vec<String>, RemoteError> {
super::tokio().block_on(client.list_workspaces(owned, invited))
fn fetch_owned_workspaces(client: &mut Client) -> Result<Vec<String>, RemoteError> {
super::tokio().block_on(client.fetch_owned_workspaces())
}

/// List joined workspaces.
#[jni(package = "mp.code", class = "Client")]
fn fetch_joined_workspaces(client: &mut Client) -> Result<Vec<String>, RemoteError> {
super::tokio().block_on(client.fetch_joined_workspaces())
}

/// List available workspaces.
Expand Down
5 changes: 1 addition & 4 deletions src/ffi/java/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::{
api::{
controller::{AsyncReceiver, AsyncSender},
Cursor, Selection,
},
api::{AsyncReceiver, AsyncSender, Cursor, Selection},
errors::ControllerError,
};
use jni::{objects::JObject, JNIEnv};
Expand Down
16 changes: 8 additions & 8 deletions src/ffi/java/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ fn get_buffer(workspace: &mut Workspace, path: String) -> Option<crate::buffer::
workspace.get_buffer(&path)
}

/// Get the filetree.
/// Searches for buffers matching the filter.
#[jni(package = "mp.code", class = "Workspace")]
fn filetree(workspace: &mut Workspace, filter: Option<String>, strict: bool) -> Vec<String> {
workspace.filetree(filter.as_deref(), strict)
fn search_buffers(workspace: &mut Workspace, filter: Option<String>) -> Vec<String> {
workspace.search_buffers(filter.as_deref())
}

/// Gets a list of the active buffers.
Expand Down Expand Up @@ -66,23 +66,23 @@ fn detach_buffer(workspace: &mut Workspace, path: String) -> bool {

/// Update the local buffer list.
#[jni(package = "mp.code", class = "Workspace")]
fn fetch_buffers(workspace: &mut Workspace) -> Result<(), RemoteError> {
fn fetch_buffers(workspace: &mut Workspace) -> Result<Vec<String>, RemoteError> {
super::tokio().block_on(workspace.fetch_buffers())
}

/// Update the local user list.
#[jni(package = "mp.code", class = "Workspace")]
fn fetch_users(workspace: &mut Workspace) -> Result<(), RemoteError> {
fn fetch_users(workspace: &mut Workspace) -> Result<Vec<User>, RemoteError> {
super::tokio().block_on(workspace.fetch_users())
}

/// List users attached to a buffer.
/// Fetch users attached to a buffer.
#[jni(package = "mp.code", class = "Workspace")]
fn list_buffer_users(
fn fetch_buffer_users(
workspace: &mut Workspace,
path: String,
) -> Result<Vec<crate::api::User>, RemoteError> {
super::tokio().block_on(workspace.list_buffer_users(&path))
super::tokio().block_on(workspace.fetch_buffer_users(&path))
}

/// Delete a buffer.
Expand Down
Loading

0 comments on commit 8ce369e

Please sign in to comment.