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!("commands", "[Language Feature] Providing completions for: {} at {:?}", uri, position);
29
30	validate_language_feature_request("completions", &uri, &position)?;
31
32	let document_uri = Url::parse(&uri).map_err(|error| error.to_string())?;
33
34	let position_dto:PositionDTO =
35		serde_json::from_value(position.clone()).map_err(|error| format!("Failed to parse position: {}", error))?;
36
37	let context_dto:CompletionContextDTO =
38		serde_json::from_value(context.clone()).map_err(|error| format!("Failed to parse context: {}", error))?;
39
40	invoke_provider(application_handle, |provider| {
41		async move {
42			// Cancellation token currently not used, pass None
43			let result = provider
44				.ProvideCompletions(document_uri, position_dto, context_dto, None)
45				.await?;
46			Ok(serde_json::to_value(result)?)
47		}
48	})
49	.await
50}