Skip to main content

Mountain/RPC/CocoonService/Provider/
ProvideReferences.rs

1
2//! Resolve "find references" via the registered provider, mapping each
3//! result into the gRPC `Location` shape.
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::{Location, Position, ProvideReferencesRequest, ProvideReferencesResponse, Range, Uri},
16	dev_log,
17};
18
19pub async fn Fn(
20	Service:&CocoonServiceImpl,
21
22	Request:ProvideReferencesRequest,
23) -> Result<Response<ProvideReferencesResponse>, Status> {
24	dev_log!(
25		"cocoon",
26		"[CocoonService] Providing references for provider {}",
27		Request.provider_handle
28	);
29
30	let URI = Request.uri.as_ref().map(|U| U.value.as_str()).unwrap_or("");
31
32	let DocumentURI = Url::parse(URI).map_err(|E| Status::invalid_argument(format!("Invalid URI: {}", E)))?;
33
34	let Position_ = Request.position.as_ref();
35
36	let PositionDTO_ = PositionDTO {
37		LineNumber:Position_.map(|P| P.line).unwrap_or(0),
38
39		Column:Position_.map(|P| P.character).unwrap_or(0),
40	};
41
42	let ContextDTO = json!({ "includeDeclaration": true });
43
44	match Service
45		.environment
46		.ProvideReferences(DocumentURI, PositionDTO_, ContextDTO)
47		.await
48	{
49		Ok(Some(Locations)) => {
50			let Mapped = Locations
51				.iter()
52				.map(|Loc| {
53					Location {
54						uri:Some(Uri { value:Loc.Uri.to_string() }),
55						range:Some(Range {
56							start:Some(Position { line:Loc.Range.StartLineNumber, character:Loc.Range.StartColumn }),
57							end:Some(Position { line:Loc.Range.EndLineNumber, character:Loc.Range.EndColumn }),
58						}),
59					}
60				})
61				.collect();
62
63			Ok(Response::new(ProvideReferencesResponse { locations:Mapped }))
64		},
65
66		Ok(None) => Ok(Response::new(ProvideReferencesResponse { locations:Vec::new() })),
67
68		Err(Error) => Err(Status::internal(format!("References failed: {}", Error))),
69	}
70}