Skip to main content

Mountain/RPC/CocoonService/Provider/
ProvideOnTypeFormatting.rs

1#![allow(non_snake_case)]
2
3//! Forward an on-type-formatting request to the registered provider.
4
5use 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	Request:ProvideOnTypeFormattingRequest,
22) -> Result<Response<ProvideOnTypeFormattingResponse>, Status> {
23	dev_log!("cocoon", "[CocoonService] Providing on-type formatting");
24	let URI = Request.uri.as_ref().map(|U| U.value.as_str()).unwrap_or("");
25	let DocumentURI = Url::parse(URI).map_err(|E| Status::invalid_argument(format!("Invalid URI: {}", E)))?;
26	let Position_ = Request.position.as_ref();
27	let PositionDTO_ = PositionDTO {
28		LineNumber:Position_.map(|P| P.line).unwrap_or(0),
29		Column:Position_.map(|P| P.character).unwrap_or(0),
30	};
31	let OptionsDTO = json!({ "tabSize": 4, "insertSpaces": true });
32	match Service
33		.environment
34		.ProvideOnTypeFormattingEdits(DocumentURI, PositionDTO_, Request.character, OptionsDTO)
35		.await
36	{
37		Ok(_) => Ok(Response::new(ProvideOnTypeFormattingResponse::default())),
38		Err(Error) => Err(Status::internal(format!("On-type formatting failed: {}", Error))),
39	}
40}