Mountain/RPC/CocoonService/Provider/
ProvideDocumentRangeFormatting.rs1
2use serde_json::json;
5use tonic::{Response, Status};
6use url::Url;
7use CommonLibrary::LanguageFeature::LanguageFeatureProviderRegistry::LanguageFeatureProviderRegistry;
8
9use crate::{
10 RPC::CocoonService::CocoonServiceImpl,
11 Vine::Generated::{ProvideDocumentRangeFormattingRequest, ProvideDocumentRangeFormattingResponse},
12 dev_log,
13};
14
15pub async fn Fn(
16 Service:&CocoonServiceImpl,
17
18 Request:ProvideDocumentRangeFormattingRequest,
19) -> Result<Response<ProvideDocumentRangeFormattingResponse>, Status> {
20 dev_log!("cocoon", "[CocoonService] Providing document range formatting");
21
22 let URI = Request.uri.as_ref().map(|U| U.value.as_str()).unwrap_or("");
23
24 let DocumentURI = Url::parse(URI).map_err(|E| Status::invalid_argument(format!("Invalid URI: {}", E)))?;
25
26 let R = Request.range.as_ref();
27
28 let RangeDTO = json!({
29 "StartLineNumber": R.and_then(|R| R.start.as_ref()).map(|P| P.line).unwrap_or(0),
30 "StartColumn": R.and_then(|R| R.start.as_ref()).map(|P| P.character).unwrap_or(0),
31 "EndLineNumber": R.and_then(|R| R.end.as_ref()).map(|P| P.line).unwrap_or(0),
32 "EndColumn": R.and_then(|R| R.end.as_ref()).map(|P| P.character).unwrap_or(0),
33 });
34
35 let OptionsDTO = json!({ "tabSize": 4, "insertSpaces": true });
36
37 match Service
38 .environment
39 .ProvideDocumentRangeFormattingEdits(DocumentURI, RangeDTO, OptionsDTO)
40 .await
41 {
42 Ok(_) => Ok(Response::new(ProvideDocumentRangeFormattingResponse::default())),
43
44 Err(Error) => Err(Status::internal(format!("Document range formatting failed: {}", Error))),
45 }
46}