Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/Terminal/
AttachToProcess.rs

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