Skip to main content

Mountain/Vine/Server/Notification/
RegisterLanguageProvider.rs

1#![allow(unused_variables, dead_code, unused_imports)]
2
3//! Handles all `register_*` / `register_*_provider` gRPC notifications from
4//! the Cocoon extension host. Each such notification wires a language-feature
5//! provider into Mountain's `ProviderRegistration` keyed on `Handle`; the
6//! language-feature RPC path (e.g. `GetHoverAtPosition`) then proxies back to
7//! Cocoon with the original `$providerXxx` method.
8//!
9//! Wire-method naming uses snake_case with two trailing shapes:
10//! - plain verbs:     `register_rename`, `register_debug_adapter`
11//! - `_provider` suffix: `register_hover_provider`,
12//!   `register_code_lens_provider`
13//!
14//! Both forms are normalised by stripping `register_` prefix and optional
15//! `_provider` suffix before the enum lookup.
16
17use CommonLibrary::LanguageFeature::DTO::ProviderType::ProviderType as PT;
18use serde_json::{Value, json};
19
20use crate::{
21	ApplicationState::DTO::ProviderRegistrationDTO::ProviderRegistrationDTO,
22	Vine::Server::MountainVinegRPCService::MountainVinegRPCService,
23	dev_log,
24};
25
26/// Dispatch a `register_*` notification. Returns `true` if the method was
27/// recognised and a `ProviderRegistrationDTO` was inserted.
28pub async fn RegisterLanguageProvider(Service:&MountainVinegRPCService, MethodName:&str, Parameter:&Value) -> bool {
29	let Handle = Parameter.get("handle").and_then(|h| h.as_u64()).unwrap_or(0) as u32;
30
31	// Accept camelCase (current Cocoon shape) with snake_case fallback for
32	// partial rebuild compatibility.
33	let Selector = Parameter
34		.get("languageSelector")
35		.or_else(|| Parameter.get("language_selector"))
36		.and_then(|s| s.as_str())
37		.unwrap_or("*");
38
39	let ExtId = Parameter
40		.get("extensionId")
41		.or_else(|| Parameter.get("extension_id"))
42		.and_then(|e| e.as_str())
43		.unwrap_or("");
44
45	let Scheme = Parameter.get("scheme").and_then(|s| s.as_str()).unwrap_or("");
46
47	let ProviderTypeName = MethodName
48		.strip_prefix("register_")
49		.map(|Stripped| Stripped.strip_suffix("_provider").unwrap_or(Stripped))
50		.unwrap_or("");
51
52	dev_log!(
53		"grpc-verbose",
54		"[MountainVinegRPCService] Cocoon registered {} provider: handle={}, lang={}",
55		ProviderTypeName,
56		Handle,
57		Selector
58	);
59
60	dev_log!(
61		"provider-register",
62		"[ProviderRegister] accepted method={} type={} handle={} lang={} scheme={} ext={}",
63		MethodName,
64		ProviderTypeName,
65		Handle,
66		Selector,
67		Scheme,
68		ExtId
69	);
70
71	let ProvType:Option<PT> = match ProviderTypeName {
72		"authentication" => Some(PT::Authentication),
73
74		"call_hierarchy" => Some(PT::CallHierarchy),
75
76		"code_actions" => Some(PT::CodeAction),
77
78		"code_lens" => Some(PT::CodeLens),
79
80		"color" => Some(PT::Color),
81
82		"completion_item" => Some(PT::Completion),
83
84		"debug_adapter" => Some(PT::DebugAdapter),
85
86		"debug_configuration" => Some(PT::DebugConfiguration),
87
88		"declaration" => Some(PT::Declaration),
89
90		"definition" => Some(PT::Definition),
91
92		"document_drop_edit" => Some(PT::DocumentDropEdit),
93
94		"document_formatting" => Some(PT::DocumentFormatting),
95
96		"document_highlight" => Some(PT::DocumentHighlight),
97
98		"document_link" => Some(PT::DocumentLink),
99
100		"document_paste_edit" => Some(PT::DocumentPasteEdit),
101
102		"document_range_formatting" => Some(PT::DocumentRangeFormatting),
103
104		"document_symbol" => Some(PT::DocumentSymbol),
105
106		"evaluatable_expression" => Some(PT::EvaluatableExpression),
107
108		"external_uri_opener" => Some(PT::ExternalUriOpener),
109
110		"file_decoration" => Some(PT::FileDecoration),
111
112		"file_system" => Some(PT::FileSystem),
113
114		"folding_range" => Some(PT::FoldingRange),
115
116		"hover" => Some(PT::Hover),
117
118		"implementation" => Some(PT::Implementation),
119
120		"inlay_hints" => Some(PT::InlayHint),
121
122		"inline_completion_item" => Some(PT::InlineCompletion),
123
124		"inline_edit" => Some(PT::InlineEdit),
125
126		"inline_values" => Some(PT::InlineValues),
127
128		"linked_editing_range" => Some(PT::LinkedEditingRange),
129
130		"mapped_edits" => Some(PT::MappedEdits),
131
132		"multi_document_highlight" => Some(PT::MultiDocumentHighlight),
133
134		"notebook_content" => Some(PT::NotebookContent),
135
136		"notebook_serializer" => Some(PT::NotebookSerializer),
137
138		"on_type_formatting" => Some(PT::OnTypeFormatting),
139
140		"reference" => Some(PT::References),
141
142		"remote_authority_resolver" => Some(PT::RemoteAuthorityResolver),
143
144		"rename" => Some(PT::Rename),
145
146		"resource_label_formatter" => Some(PT::ResourceLabelFormatter),
147
148		"scm" => Some(PT::SourceControl),
149
150		"scm_resource_group" => Some(PT::ScmResourceGroup),
151
152		"selection_range" => Some(PT::SelectionRange),
153
154		"semantic_tokens" => Some(PT::SemanticTokens),
155
156		"signature_help" => Some(PT::SignatureHelp),
157
158		"task" => Some(PT::Task),
159
160		"terminal_link" => Some(PT::TerminalLink),
161
162		"terminal_profile" => Some(PT::TerminalProfile),
163
164		"text_document_content" => Some(PT::TextDocumentContent),
165
166		"type_definition" => Some(PT::TypeDefinition),
167
168		"type_hierarchy" => Some(PT::TypeHierarchy),
169
170		"uri_handler" => Some(PT::UriHandler),
171
172		"workspace_symbol" => Some(PT::WorkspaceSymbol),
173
174		_ => None,
175	};
176
177	let Some(ProviderType) = ProvType else { return false };
178
179	// Scheme-bound providers carry their scheme in the selector so Mountain's
180	// resolver (FileSystem router, URI handler dispatch, …) can match on it.
181	let SelectorValue = if !Scheme.is_empty() {
182		json!([{ "scheme": Scheme, "language": Selector }])
183	} else {
184		json!([{ "language": Selector }])
185	};
186
187	let Dto = ProviderRegistrationDTO {
188		Handle,
189
190		ProviderType,
191
192		Selector:SelectorValue,
193
194		SideCarIdentifier:"cocoon-main".to_string(),
195
196		ExtensionIdentifier:json!(ExtId),
197
198		Options:Parameter.get("options").cloned(),
199	};
200
201	Service
202		.RunTime()
203		.Environment
204		.ApplicationState
205		.Extension
206		.ProviderRegistration
207		.RegisterProvider(Handle, Dto);
208
209	true
210}