Skip to main content

Mountain/RPC/CocoonService/Provider/
ProvideReferences.rs

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