Skip to main content

Mountain/IPC/WindServiceHandlers/Navigation/
LabelGetURI.rs

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