package mp.code; import mp.code.data.TextChange; import mp.code.exceptions.ControllerException; import java.util.Optional; import java.util.function.Consumer; /** * Allows interaction with a CodeMP buffer, which in simple terms is a document * that multiple people can edit concurrently. *
* It is generally safer to avoid storing this directly, see the api notes for {@link Workspace}.
*/
public final class BufferController {
private final long ptr;
BufferController(long ptr) {
this.ptr = ptr;
Extensions.CLEANER.register(this, () -> free(ptr));
}
private static native String get_name(long self);
/**
* Gets the name (path) of the buffer.
* @return the path of the buffer
*/
public String getName() {
return get_name(this.ptr);
}
private static native String get_content(long self) throws ControllerException;
/**
* Gets the contents of the buffer as a flat string.
* This may return incomplete results if called immediately after attaching.
* @return the contents fo the buffer as a flat string
* @throws ControllerException if the controller was stopped
*/
public String getContent() throws ControllerException {
return get_content(this.ptr);
}
private static native TextChange try_recv(long self) throws ControllerException;
/**
* Tries to get a {@link TextChange} from the queue if any were present, and returns
* an empty optional otherwise.
* @return the first text change in queue, if any are present
* @throws ControllerException if the controller was stopped
*/
public Optional