Skip to main content

Mountain/RPC/CocoonService/Workspace/
ApplyEdit.rs

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