Skip to main content

Mountain/RPC/CocoonService/Workspace/
ApplyEdit.rs

1#![allow(non_snake_case)]
2
3//! Apply a sequence of text edits to a document via
4//! `sky://editor/applyEdits`. Each edit carries a `range` (start/end
5//! position) plus the replacement `newText`.
6
7use serde_json::json;
8use tauri::Emitter;
9use tonic::{Response, Status};
10
11use crate::{
12	RPC::CocoonService::CocoonServiceImpl,
13	Vine::Generated::{ApplyEditRequest, ApplyEditResponse},
14	dev_log,
15};
16
17pub async fn Fn(Service:&CocoonServiceImpl, Request:ApplyEditRequest) -> Result<Response<ApplyEditResponse>, Status> {
18	let URI = Request.uri.as_ref().map(|U| U.value.clone()).unwrap_or_default();
19
20	dev_log!(
21		"cocoon",
22		"[CocoonService] apply_edit: uri={} edits={}",
23		URI,
24		Request.edits.len()
25	);
26
27	let EditsJSON:Vec<serde_json::Value> = Request
28		.edits
29		.iter()
30		.map(|E| {
31			json!({
32				"range": {
33					"start": E.range.as_ref().and_then(|R| R.start.as_ref()).map(|P| {
34						json!({ "line": P.line, "character": P.character })
35					}),
36					"end": E.range.as_ref().and_then(|R| R.end.as_ref()).map(|P| {
37						json!({ "line": P.line, "character": P.character })
38					}),
39				},
40				"newText": E.new_text,
41			})
42		})
43		.collect();
44
45	let _ = Service
46		.environment
47		.ApplicationHandle
48		.emit("sky://editor/applyEdits", json!({ "uri": URI, "edits": EditsJSON }));
49
50	Ok(Response::new(ApplyEditResponse { success:true }))
51}