Skip to main content

Mountain/Command/LanguageFeature/
Completions.rs

1//! # LanguageFeature - Completions
2//!
3//! Provides code completion suggestions
4
5#[allow(unused_imports)]
6use CommonLibrary::{
7	Error::CommonError::CommonError,
8	LanguageFeature::{
9		DTO::{CompletionContextDTO::CompletionContextDTO, PositionDTO::PositionDTO},
10		LanguageFeatureProviderRegistry::LanguageFeatureProviderRegistry,
11	},
12};
13use serde_json::Value;
14use tauri::{AppHandle, Wry};
15use url::Url;
16
17use super::{InvokeProvider::invoke_provider, Validation::validate_language_feature_request};
18use crate::dev_log;
19
20/// Implementation of completions command - called by the command wrapper in the
21/// parent module.
22pub(super) async fn provide_completions_impl(
23	application_handle:AppHandle<Wry>,
24	uri:String,
25	position:Value,
26	context:Value,
27) -> Result<Value, String> {
28	dev_log!(
29		"commands",
30		"[Language Feature] Providing completions for: {} at {:?}",
31		uri,
32		position
33	);
34
35	validate_language_feature_request("completions", &uri, &position)?;
36
37	let document_uri = Url::parse(&uri).map_err(|error| error.to_string())?;
38
39	let position_dto:PositionDTO =
40		serde_json::from_value(position.clone()).map_err(|error| format!("Failed to parse position: {}", error))?;
41
42	let context_dto:CompletionContextDTO =
43		serde_json::from_value(context.clone()).map_err(|error| format!("Failed to parse context: {}", error))?;
44
45	invoke_provider(application_handle, |provider| {
46		async move {
47			// Cancellation token currently not used, pass None
48			let result = provider
49				.ProvideCompletions(document_uri, position_dto, context_dto, None)
50				.await?;
51			Ok(serde_json::to_value(result)?)
52		}
53	})
54	.await
55}