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