Mountain/Command/
LanguageFeature.rs

1//! # Language Feature Commands
2//!
3//! Defines the specific Tauri command handlers for language feature requests
4//! that originate from the `Sky` frontend UI (e.g., Monaco Editor).
5
6#![allow(non_snake_case, non_camel_case_types)]
7
8use std::sync::Arc;
9
10use Common::{
11	Environment::Requires::Requires,
12	Error::CommonError::CommonError,
13	LanguageFeature::{
14		DTO::{CompletionContextDTO::CompletionContextDTO, PositionDTO::PositionDTO},
15		LanguageFeatureProviderRegistry::LanguageFeatureProviderRegistry,
16	},
17};
18use serde_json::Value;
19use tauri::{AppHandle, Manager, Wry, command};
20use url::Url;
21
22use crate::RunTime::ApplicationRunTime::ApplicationRunTime as MountainRunTime;
23
24/// A generic helper to reduce boilerplate in language feature command handlers.
25async fn InvokeProvider<F, T>(ApplicationHandle:AppHandle<Wry>, Handler:F) -> Result<Value, String>
26where
27	F: FnOnce(Arc<dyn LanguageFeatureProviderRegistry>) -> T,
28	T: std::future::Future<Output = Result<Value, CommonError>>, {
29	let RunTime = ApplicationHandle.state::<Arc<MountainRunTime>>().inner().clone();
30
31	let Provider:Arc<dyn LanguageFeatureProviderRegistry> = RunTime.Environment.Require();
32
33	let Result = Handler(Provider).await.map_err(|Error| Error.to_string())?;
34
35	serde_json::to_value(Result).map_err(|Error| Error.to_string())
36}
37
38#[command]
39pub async fn MountainProvideHover(
40	ApplicationHandle:AppHandle<Wry>,
41
42	URI:String,
43
44	Position:Value,
45) -> Result<Value, String> {
46	let DocumentURI = Url::parse(&URI).map_err(|Error| Error.to_string())?;
47
48	let PositionDTO:PositionDTO = serde_json::from_value(Position).map_err(|Error| Error.to_string())?;
49
50	InvokeProvider(ApplicationHandle, |Provider| {
51		async move {
52			let Result = Provider.ProvideHover(DocumentURI, PositionDTO).await?;
53
54			Ok(serde_json::to_value(Result)?)
55		}
56	})
57	.await
58}
59
60#[command]
61pub async fn MountainProvideCompletions(
62	ApplicationHandle:AppHandle<Wry>,
63
64	URI:String,
65
66	Position:Value,
67
68	Context:Value,
69) -> Result<Value, String> {
70	let DocumentURI = Url::parse(&URI).map_err(|Error| Error.to_string())?;
71
72	let PositionDTO:PositionDTO = serde_json::from_value(Position).map_err(|Error| Error.to_string())?;
73
74	let ContextDTO:CompletionContextDTO = serde_json::from_value(Context).map_err(|Error| Error.to_string())?;
75
76	InvokeProvider(ApplicationHandle, |Provider| {
77		async move {
78			let Result = Provider.ProvideCompletions(DocumentURI, PositionDTO, ContextDTO, None).await?;
79
80			Ok(serde_json::to_value(Result)?)
81		}
82	})
83	.await
84}
85
86#[command]
87pub async fn MountainProvideDefinition(
88	ApplicationHandle:AppHandle<Wry>,
89
90	URI:String,
91
92	Position:Value,
93) -> Result<Value, String> {
94	let DocumentURI = Url::parse(&URI).map_err(|Error| Error.to_string())?;
95
96	let PositionDTO:PositionDTO = serde_json::from_value(Position).map_err(|Error| Error.to_string())?;
97
98	InvokeProvider(ApplicationHandle, |Provider| {
99		async move {
100			let Result = Provider.ProvideDefinition(DocumentURI, PositionDTO).await?;
101
102			Ok(serde_json::to_value(Result)?)
103		}
104	})
105	.await
106}
107
108#[command]
109pub async fn MountainProvideReferences(
110	ApplicationHandle:AppHandle<Wry>,
111
112	URI:String,
113
114	Position:Value,
115
116	Context:Value,
117) -> Result<Value, String> {
118	let DocumentURI = Url::parse(&URI).map_err(|Error| Error.to_string())?;
119
120	let PositionDTO:PositionDTO = serde_json::from_value(Position).map_err(|Error| Error.to_string())?;
121
122	InvokeProvider(ApplicationHandle, |Provider| {
123		async move {
124			let Result = Provider.ProvideReferences(DocumentURI, PositionDTO, Context).await?;
125
126			Ok(serde_json::to_value(Result)?)
127		}
128	})
129	.await
130}