Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Command/LanguageFeature/
Completions.rs

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