|
| 1 | +#![allow(non_snake_case, dead_code)] |
| 2 | + |
| 3 | +//! Shared helpers for emitting VS Code `UriComponents` payloads. |
| 4 | +//! |
| 5 | +//! VS Code's IPC reviver (`src/vs/base/common/uriIpc.ts` |
| 6 | +//! `_transformIncomingURIs`) walks every response object and only calls |
| 7 | +//! `URI.revive()` on nested objects tagged with the marshalling marker |
| 8 | +//! `$mid === MarshalledId.Uri` (= 1). An untagged `UriComponents` reaches |
| 9 | +//! callers as a plain bag - `uri.with is not a function`, `uri.fsPath` |
| 10 | +//! undefined - and the sidebar / icon loader / joinPath chain silently |
| 11 | +//! breaks. |
| 12 | +//! |
| 13 | +//! Every handler that returns a URI-shaped payload to the renderer has to |
| 14 | +//! stamp the marker. Keep construction centralized here so we can't lose a |
| 15 | +//! call site to inline `json!` builders. |
| 16 | +
|
| 17 | +use serde_json::{Value, json}; |
| 18 | + |
| 19 | +/// `MarshalledId.Uri` from VS Code's `src/vs/base/common/marshallingIds.ts`. |
| 20 | +/// The reviver keys off this exact value; changing VS Code's enum would |
| 21 | +/// require updating here in lockstep (search for `MID_URI` callers). |
| 22 | +pub const MID_URI:u64 = 1; |
| 23 | + |
| 24 | +/// Insert `$mid: 1` into a `UriComponents` object if it isn't already tagged. |
| 25 | +/// Non-object values pass through unchanged - that lets call sites pipe any |
| 26 | +/// `serde_json::Value` they already have through the helper without branching |
| 27 | +/// on the variant first. |
| 28 | +pub fn StampMidUri(Input:Value) -> Value { |
| 29 | + match Input { |
| 30 | + Value::Object(mut Map) => { |
| 31 | + Map.entry("$mid".to_string()).or_insert(json!(MID_URI)); |
| 32 | + Value::Object(Map) |
| 33 | + } |
| 34 | + Other => Other, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// Build a `file://` `UriComponents` from an absolute filesystem path |
| 39 | +/// string. Path is emitted verbatim - no percent-encoding, no normalisation - |
| 40 | +/// which mirrors what VS Code's own `URI.file(…)` / `URI.parse(…)` path |
| 41 | +/// readers expect (they undo any encoding themselves when reading `.fsPath`). |
| 42 | +pub fn FromFilePath<S:AsRef<str>>(Path:S) -> Value { |
| 43 | + StampMidUri(json!({ |
| 44 | + "scheme": "file", |
| 45 | + "authority": "", |
| 46 | + "path": Path.as_ref(), |
| 47 | + "query": "", |
| 48 | + "fragment": "", |
| 49 | + })) |
| 50 | +} |
| 51 | + |
| 52 | +/// Build a `UriComponents` from a fully-formed URL string. Handles `file://` |
| 53 | +/// (authority-optional) and any other scheme generically (`scheme:path` + |
| 54 | +/// optional `//authority`). Fragment / query are split off verbatim so |
| 55 | +/// downstream `URI.revive()` reconstructs the same URL. Strings that don't |
| 56 | +/// parse as URLs fall back to `{ scheme:"file", path:<input> }` - a defensive |
| 57 | +/// shape the workbench still tolerates for "unknown location" placeholders. |
| 58 | +pub fn FromUrl(Url:&str) -> Value { |
| 59 | + if let Some(Rest) = Url.strip_prefix("file://") { |
| 60 | + let (Authority, Path) = match Rest.find('/') { |
| 61 | + Some(0) => ("", Rest), |
| 62 | + Some(Index) => (&Rest[..Index], &Rest[Index..]), |
| 63 | + None => ("", ""), |
| 64 | + }; |
| 65 | + return StampMidUri(json!({ |
| 66 | + "scheme": "file", |
| 67 | + "authority": Authority, |
| 68 | + "path": Path, |
| 69 | + "query": "", |
| 70 | + "fragment": "", |
| 71 | + })); |
| 72 | + } |
| 73 | + if let Some((Scheme, PathPart)) = Url.split_once(':') { |
| 74 | + let Trimmed = PathPart.trim_start_matches("//"); |
| 75 | + let (Authority, Path) = match Trimmed.find('/') { |
| 76 | + Some(0) => ("", Trimmed), |
| 77 | + Some(Index) => (&Trimmed[..Index], &Trimmed[Index..]), |
| 78 | + None => ("", Trimmed), |
| 79 | + }; |
| 80 | + return StampMidUri(json!({ |
| 81 | + "scheme": Scheme, |
| 82 | + "authority": Authority, |
| 83 | + "path": Path, |
| 84 | + "query": "", |
| 85 | + "fragment": "", |
| 86 | + })); |
| 87 | + } |
| 88 | + StampMidUri(json!({ |
| 89 | + "scheme": "file", |
| 90 | + "authority": "", |
| 91 | + "path": Url, |
| 92 | + "query": "", |
| 93 | + "fragment": "", |
| 94 | + })) |
| 95 | +} |
| 96 | + |
| 97 | +/// Normalise an `extensionLocation` (or any similar) field that arrives as |
| 98 | +/// either a URL string, a pre-built UriComponents object (possibly |
| 99 | +/// already tagged), or is missing / null. The output is always an object |
| 100 | +/// with `$mid: 1` and the five URI fields. |
| 101 | +pub fn Normalize(Raw:Option<&Value>) -> Value { |
| 102 | + match Raw { |
| 103 | + Some(Value::Object(Map)) if Map.contains_key("scheme") => { |
| 104 | + StampMidUri(Value::Object(Map.clone())) |
| 105 | + } |
| 106 | + Some(Value::String(Url)) => FromUrl(Url), |
| 107 | + _ => FromFilePath("/extensions/unknown"), |
| 108 | + } |
| 109 | +} |
0 commit comments