feat: implemented mostly everything but it's broken

This commit is contained in:
əlemi 2023-08-18 04:20:02 +02:00
parent 96370a4b6c
commit 9b83666a36

View file

@ -27,16 +27,28 @@ fn multiline_hl(buf: &mut Buffer, namespace: u32, hl: &str, start: RowCol, end:
Ok(()) Ok(())
} }
fn byte2rowcol(buf: &Buffer, index: usize) -> RowCol { fn byte2rowcol(buf: &Buffer, index: usize) -> oxi::Result<RowCol> {
buf.get_offset(index) // TODO lmao this impl
let mut row = 0;
oxi::api:: loop {
let offset = buf.get_offset(row+1)?;
if offset > index { // found line
let base = buf.get_offset(row)?;
return Ok(RowCol { row: row as i32, col: (index - base) as i32 });
}
row += 1;
}
} }
fn multiline_set_text(buf: &mut Buffer, change: CodempTextChange) -> oxi::Result<()> { fn multiline_set_text(buf: &mut Buffer, change: CodempTextChange) -> oxi::Result<()> {
for i in change.span let start = byte2rowcol(buf, change.span.start)?;
let end = byte2rowcol(buf, change.span.end)?;
buf.set_text(
start.row as usize ..= end.row as usize,
start.col as usize,
end.col as usize,
change.content.split('\n')
)?;
Ok(()) Ok(())
} }
@ -56,7 +68,7 @@ fn buffer_content(buf: &Buffer) -> oxi::Result<String> {
out.push_str(&line.to_string_lossy()); out.push_str(&line.to_string_lossy());
out.push('\n'); out.push('\n');
} }
Ok(out) Ok(out.trim().to_string())
} }
impl CursorStorage { impl CursorStorage {
@ -79,11 +91,11 @@ impl CursorStorage {
} }
#[oxi::module] #[oxi::module]
fn api() -> oxi::Result<()> { fn codemp_nvim() -> oxi::Result<()> {
oxi::api::create_user_command( oxi::api::create_user_command(
"Connect", "Connect",
|args: CommandArgs| { |args: CommandArgs| {
let addr = args.args.unwrap_or("http://alemi.dev:50051".into()); let addr = args.args.unwrap_or("http://127.0.0.1:50051".into());
RUNTIME.block_on(CODEMP_INSTANCE.connect(&addr)) RUNTIME.block_on(CODEMP_INSTANCE.connect(&addr))
.map_err(|e| nvim_oxi::api::Error::Other(format!("xx could not connect: {}", e)))?; .map_err(|e| nvim_oxi::api::Error::Other(format!("xx could not connect: {}", e)))?;
@ -100,7 +112,7 @@ fn api() -> oxi::Result<()> {
oxi::api::create_user_command( oxi::api::create_user_command(
"Join", "Join",
|args: CommandArgs| { |args: CommandArgs| {
let workspace = args.args.expect("one arg required but not provided"); let workspace = args.args.unwrap_or("default".into());
let controller = RUNTIME.block_on(CODEMP_INSTANCE.join(&workspace)) let controller = RUNTIME.block_on(CODEMP_INSTANCE.join(&workspace))
.map_err(|e| nvim_oxi::api::Error::Other(format!("xx could not join: {}", e)))?; .map_err(|e| nvim_oxi::api::Error::Other(format!("xx could not join: {}", e)))?;
@ -115,7 +127,7 @@ fn api() -> oxi::Result<()> {
Ok::<_, oxi::Error>(()) Ok::<_, oxi::Error>(())
}).map_err(|e| oxi::api::Error::Other(format!("xx could not create handle: {}", e)))?; }).map_err(|e| oxi::api::Error::Other(format!("xx could not create handle: {}", e)))?;
controller.clone().callback(move |x| { controller.clone().callback(&RUNTIME, move |x| {
tx.send(x).unwrap_or_warn("could not enqueue callback"); tx.send(x).unwrap_or_warn("could not enqueue callback");
handle.send().unwrap_or_warn("could not wake async handle"); handle.send().unwrap_or_warn("could not wake async handle");
}); });
@ -139,7 +151,7 @@ fn api() -> oxi::Result<()> {
}, },
&CreateCommandOpts::builder() &CreateCommandOpts::builder()
.desc("join a codemp workspace and start processing cursors") .desc("join a codemp workspace and start processing cursors")
.nargs(CommandNArgs::One) .nargs(CommandNArgs::ZeroOrOne) // TODO wtf if I put "One" I cannot require codemp_nvim ('invalid nargs')
.build(), .build(),
)?; )?;
@ -152,17 +164,18 @@ fn api() -> oxi::Result<()> {
.map_err(|e| nvim_oxi::api::Error::Other(format!("xx could not attach: {}", e)))?; .map_err(|e| nvim_oxi::api::Error::Other(format!("xx could not attach: {}", e)))?;
let buf = oxi::api::get_current_buf(); let buf = oxi::api::get_current_buf();
let mut buf_m = buf.clone();
let (tx, mut rx) = mpsc::unbounded_channel::<CodempTextChange>(); let (tx, mut rx) = mpsc::unbounded_channel::<CodempTextChange>();
// let mut container = CursorStorage::default(); // let mut container = CursorStorage::default();
let handle = AsyncHandle::new(move || { let handle = AsyncHandle::new(move || {
while let Ok(x) = rx.try_recv() { // TODO do this inside oxi::schedule() to not block vim while let Ok(change) = rx.try_recv() { // TODO do this inside oxi::schedule() to not block vim
buf.set_text(line_range, start_col, end_col, replacement) multiline_set_text(&mut buf_m, change)?;
} }
Ok::<_, oxi::Error>(()) Ok::<_, oxi::Error>(())
}).map_err(|e| oxi::api::Error::Other(format!("xx could not create handle: {}", e)))?; }).map_err(|e| oxi::api::Error::Other(format!("xx could not create handle: {}", e)))?;
controller.clone().callback(move |x| { controller.clone().callback(&RUNTIME, move |x| {
tx.send(x).unwrap_or_warn("could not enqueue callback"); tx.send(x).unwrap_or_warn("could not enqueue callback");
handle.send().unwrap_or_warn("could not wake async handle"); handle.send().unwrap_or_warn("could not wake async handle");
}); });
@ -190,8 +203,25 @@ fn api() -> oxi::Result<()> {
Ok(()) Ok(())
}, },
&CreateCommandOpts::builder() &CreateCommandOpts::builder()
.desc("join a codemp workspace and start processing cursors") .desc("attach to buffer, sending and receiving changes")
.nargs(CommandNArgs::One) .nargs(CommandNArgs::ZeroOrOne) // TODO wtf if I put "One" I cannot require codemp_nvim ('invalid nargs')
.build(),
)?;
oxi::api::create_user_command(
"Create",
|args: CommandArgs| {
let path = args.args.expect("one arg required but not provided");
RUNTIME.block_on(CODEMP_INSTANCE.create(&path, None))
.map_err(|e| nvim_oxi::api::Error::Other(format!("xx could not attach: {}", e)))?;
oxi::print!("++ created buffer '{}'", path);
Ok(())
},
&CreateCommandOpts::builder()
.desc("create a new buffer")
.nargs(CommandNArgs::ZeroOrOne) // TODO wtf if I put "One" I cannot require codemp_nvim ('invalid nargs')
.build(), .build(),
)?; )?;