Skip to main content

Mountain/RPC/CocoonService/Workspace/
ApplyEdit.rs

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