Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/Terminal/
AttachToProcess.rs

1//! `localPty:attachToProcess` - reconnect the workbench to an existing
2//! Mountain-owned PTY after a window reload.
3//!
4//! VS Code calls this when the workbench learns that a terminal ID from the
5//! previous session is already live (via `localPty:getTerminalLayoutInfo` or
6//! `localPty:reviveTerminalProcesses`). Instead of spawning a new PTY it
7//! attaches to the one that Mountain kept running.
8//!
9//! Wire shape: `Arguments[0]` = id (u64)
10//!
11//! Returns `{ id, pid }` on success or `null` when the id is unknown.
12
13use std::sync::Arc;
14
15use CommonLibrary::{Environment::Requires::Requires, Terminal::TerminalProvider::TerminalProvider};
16use serde_json::{Value, json};
17
18use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
19
20pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
21	let TerminalId = match Arguments.first() {
22		Some(Value::Number(N)) => N.as_u64().unwrap_or(0),
23
24		Some(Value::Object(Obj)) => Obj.get("id").and_then(Value::as_u64).unwrap_or(0),
25
26		_ => 0,
27	};
28
29	if TerminalId == 0 {
30		dev_log!("terminal", "warn: [AttachToProcess] called with id=0, ignoring");
31
32		return Ok(Value::Null);
33	}
34
35	let Provider:Arc<dyn TerminalProvider> = RunTime.Environment.Require();
36
37	// Fetch the PID so the workbench can bind its tooltip + debug adapter.
38	// If the terminal no longer exists the provider returns None.
39	match Provider.GetTerminalProcessId(TerminalId).await {
40		Ok(Some(Pid)) => {
41			dev_log!("terminal", "[AttachToProcess] attached id={} pid={}", TerminalId, Pid);
42
43			Ok(json!({ "id": TerminalId, "pid": Pid }))
44		},
45
46		Ok(None) => {
47			dev_log!(
48				"terminal",
49				"warn: [AttachToProcess] id={} not found in active terminals",
50				TerminalId
51			);
52
53			Ok(Value::Null)
54		},
55
56		Err(Error) => {
57			dev_log!("terminal", "warn: [AttachToProcess] id={} error: {}", TerminalId, Error);
58
59			Ok(Value::Null)
60		},
61	}
62}