Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/Navigation/
LabelGetURI.rs

1//! Resolve a human-readable display label for a URI. Two modes:
2//!
3//! - `Relative=false`: strip the `file://` scheme and return the raw absolute
4//!   path.
5//! - `Relative=true`: same, then trim the workspace-folder prefix so the user
6//!   sees `Source/main.rs` instead of `/Volumes/.../Mountain/Source/main.rs`.
7
8use std::sync::Arc;
9
10use serde_json::Value;
11
12use crate::{
13	IPC::WindServiceHandlers::Utilities::JsonValueHelpers::arg_bool,
14	RunTime::ApplicationRunTime::ApplicationRunTime,
15};
16
17pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
18	let Uri = Arguments
19		.first()
20		.and_then(|V| V.as_str())
21		.ok_or("label:getUri requires uri".to_string())?
22		.to_owned();
23
24	let Relative = arg_bool(&Arguments, 1);
25
26	if !Relative {
27		let Label = if let Some(stripped) = Uri.strip_prefix("file://") {
28			stripped.to_owned()
29		} else {
30			Uri.clone()
31		};
32
33		return Ok(Value::String(Label));
34	}
35
36	let WorkspaceRoot = RunTime
37		.Environment
38		.ApplicationState
39		.Workspace
40		.GetWorkspaceFolders()
41		.into_iter()
42		.next()
43		.map(|F| F.URI.to_string())
44		.unwrap_or_default();
45
46	let RawPath = if let Some(stripped) = Uri.strip_prefix("file://") {
47		stripped.to_owned()
48	} else {
49		Uri.clone()
50	};
51
52	let RootPath = if let Some(stripped) = WorkspaceRoot.strip_prefix("file://") {
53		stripped.to_owned()
54	} else {
55		WorkspaceRoot
56	};
57
58	let Label = if !RootPath.is_empty() && RawPath.starts_with(&RootPath) {
59		RawPath[RootPath.len()..].trim_start_matches('/').to_owned()
60	} else {
61		RawPath
62	};
63
64	Ok(Value::String(Label))
65}