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};
11use ::Vine::Generated::{Location, Position, ProvideReferencesRequest, ProvideReferencesResponse, Range, Uri};
12
13use crate::{RPC::CocoonService::CocoonServiceImpl, dev_log};
14
15pub async fn Fn(
16	Service:&CocoonServiceImpl,
17
18	Request:ProvideReferencesRequest,
19) -> Result<Response<ProvideReferencesResponse>, Status> {
20	dev_log!(
21		"cocoon",
22		"[CocoonService] Providing references for provider {}",
23		Request.provider_handle
24	);
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 ContextDTO = json!({ "includeDeclaration": true });
39
40	match Service
41		.environment
42		.ProvideReferences(DocumentURI, PositionDTO_, ContextDTO)
43		.await
44	{
45		Ok(Some(Locations)) => {
46			let Mapped = Locations
47				.iter()
48				.map(|Loc| {
49					Location {
50						uri:Some(Uri { value:Loc.Uri.to_string() }),
51						range:Some(Range {
52							start:Some(Position { line:Loc.Range.StartLineNumber, character:Loc.Range.StartColumn }),
53							end:Some(Position { line:Loc.Range.EndLineNumber, character:Loc.Range.EndColumn }),
54						}),
55					}
56				})
57				.collect();
58
59			Ok(Response::new(ProvideReferencesResponse { locations:Mapped }))
60		},
61
62		Ok(None) => Ok(Response::new(ProvideReferencesResponse { locations:Vec::new() })),
63
64		Err(Error) => Err(Status::internal(format!("References failed: {}", Error))),
65	}
66}