Mountain/RPC/CocoonService/Provider/
ProvideOnTypeFormatting.rs1#![allow(non_snake_case)]
2
3use serde_json::json;
6use tonic::{Response, Status};
7use url::Url;
8use CommonLibrary::LanguageFeature::{
9 DTO::PositionDTO::PositionDTO,
10 LanguageFeatureProviderRegistry::LanguageFeatureProviderRegistry,
11};
12
13use crate::{
14 RPC::CocoonService::CocoonServiceImpl,
15 Vine::Generated::{ProvideOnTypeFormattingRequest, ProvideOnTypeFormattingResponse},
16 dev_log,
17};
18
19pub async fn Fn(
20 Service:&CocoonServiceImpl,
21
22 Request:ProvideOnTypeFormattingRequest,
23) -> Result<Response<ProvideOnTypeFormattingResponse>, Status> {
24 dev_log!("cocoon", "[CocoonService] Providing on-type formatting");
25
26 let URI = Request.uri.as_ref().map(|U| U.value.as_str()).unwrap_or("");
27
28 let DocumentURI = Url::parse(URI).map_err(|E| Status::invalid_argument(format!("Invalid URI: {}", E)))?;
29
30 let Position_ = Request.position.as_ref();
31
32 let PositionDTO_ = PositionDTO {
33 LineNumber:Position_.map(|P| P.line).unwrap_or(0),
34
35 Column:Position_.map(|P| P.character).unwrap_or(0),
36 };
37
38 let OptionsDTO = json!({ "tabSize": 4, "insertSpaces": true });
39
40 match Service
41 .environment
42 .ProvideOnTypeFormattingEdits(DocumentURI, PositionDTO_, Request.character, OptionsDTO)
43 .await
44 {
45 Ok(_) => Ok(Response::new(ProvideOnTypeFormattingResponse::default())),
46
47 Err(Error) => Err(Status::internal(format!("On-type formatting failed: {}", Error))),
48 }
49}