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