Skip to main content

Mountain/RPC/CocoonService/Provider/
ProvideReferences.rs

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