Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
SCM.rs

1use std::sync::Arc;
2
3use CommonLibrary::{
4	Environment::Requires::Requires,
5	SourceControlManagement::SourceControlManagementProvider::SourceControlManagementProvider,
6};
7use serde_json::{Value, json};
8use tauri::Runtime;
9
10use crate::{
11	Track::Effect::{
12		CreateEffectForRequest::Utilities::Params::{i64_at, val_at},
13		MappedEffectType::MappedEffect,
14	},
15	dev_log,
16};
17
18pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
19	dev_log!("scm", "[SCM] CreateEffect method={}", MethodName);
20
21	match MethodName {
22		"$scm:createSourceControl" => {
23			crate::effect!(run_time, {
24				let provider:Arc<dyn SourceControlManagementProvider> = run_time.Environment.Require();
25
26				let resource = val_at(&Parameters, 0);
27
28				provider
29					.CreateSourceControl(resource)
30					.await
31					.map(|handle| json!(handle))
32					.map_err(|e| e.to_string())
33			})
34		},
35
36		"$scm:updateSourceControl" => {
37			crate::effect!(run_time, {
38				let provider:Arc<dyn SourceControlManagementProvider> = run_time.Environment.Require();
39
40				let handle = i64_at(&Parameters, 0) as u32;
41
42				let update = val_at(&Parameters, 1);
43
44				provider
45					.UpdateSourceControl(handle, update)
46					.await
47					.map(|_| json!(null))
48					.map_err(|e| e.to_string())
49			})
50		},
51
52		"$scm:updateGroup" => {
53			crate::effect!(run_time, {
54				let provider:Arc<dyn SourceControlManagementProvider> = run_time.Environment.Require();
55
56				let handle = i64_at(&Parameters, 0) as u32;
57
58				let group_data = val_at(&Parameters, 1);
59
60				provider
61					.UpdateSourceControlGroup(handle, group_data)
62					.await
63					.map(|_| json!(null))
64					.map_err(|e| e.to_string())
65			})
66		},
67
68		"$scm:registerInputBox" => {
69			crate::effect!(run_time, {
70				let provider:Arc<dyn SourceControlManagementProvider> = run_time.Environment.Require();
71
72				let handle = i64_at(&Parameters, 0) as u32;
73
74				let options = val_at(&Parameters, 1);
75
76				provider
77					.RegisterInputBox(handle, options)
78					.await
79					.map(|_| json!(null))
80					.map_err(|e| e.to_string())
81			})
82		},
83
84		// `vscode.diff` is the canonical command the vscode.git extension
85		// calls when the user clicks a staged or unstaged file in the SCM
86		// sidebar. It receives three positional args:
87		//   [0] leftUri   - the "before" URI  (e.g. git://…?HEAD)
88		//   [1] rightUri  - the "after"  URI  (e.g. the working-tree file)
89		//   [2] title     - string label shown in the editor tab
90		//
91		// Without this arm the command falls through to the Unknown-method
92		// error branch, Mountain logs a warn, the extension's awaited
93		// promise rejects, and the diff editor never opens.
94		//
95		// We forward the full parameter array to Sky on
96		// `sky://editor/diff` as a round-trip request so the extension's
97		// `await commands.executeCommand("vscode.diff", …)` resolves when
98		// the workbench has actually opened the diff editor.
99		//
100		// `$scm:openDiff` is an older alias emitted by some extension
101		// versions; we handle it identically.
102		//
103		// `git.openChange` / `git.openFile` are the actual command IDs
104		// the built-in vscode.git extension uses as the `.command` field
105		// on its resource state entries - clicking a changed file in the
106		// SCM sidebar dispatches one of these. We alias them onto the
107		// same diff-forward path so the diff editor opens.
108		"vscode.diff" | "$scm:openDiff" | "git.openChange" | "git.openFile" => {
109			crate::effect!(run_time, {
110				dev_log!("scm", "[SCM] diff forwarding to sky://editor/diff params={:?}", Parameters);
111
112				match crate::Environment::UserInterfaceProvider::SendUserInterfaceRequest(
113					&run_time.Environment,
114					"sky://editor/diff",
115					Parameters,
116				)
117				.await
118				{
119					Ok(Result) => Ok(Result),
120					Err(Error) => {
121						dev_log!(
122							"scm",
123							"warn: [SCM] vscode.diff sky://editor/diff did not answer ({:?}); returning null",
124							Error
125						);
126
127						Ok(json!(null))
128					},
129				}
130			})
131		},
132
133		_ => None,
134	}
135}