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	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}