Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Environment/Utility/
GlobPattern.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Glob pattern extraction helpers for `WorkspaceProvider::FindFiles` calls.
4//!
5//! VS Code passes glob patterns in several shapes depending on the caller:
6//! - Bare string: `"**/*.rs"`
7//! - `RelativePattern` object: `{ base, pattern }` or `{ baseUri, pattern }`
8//! - Legacy serialised form: `{ value: "**/*.rs" }`
9//!
10//! These helpers normalise all shapes to a `String` and extract the optional
11//! `base` directory for bounded walks.
12
13use serde_json::Value;
14
15/// Extract a glob string from any shape the caller can supply:
16/// - Bare string → returned as-is.
17/// - Object with `pattern` field (VS Code `RelativePattern`).
18/// - Object with `value` field (legacy serialised form).
19/// - Object with `Pattern` field (PascalCase variant).
20pub fn ExtractGlobPattern(Pattern:&Value) -> Option<String> {
21	if let Some(S) = Pattern.as_str() {
22		return Some(S.to_string());
23	}
24	if let Some(Obj) = Pattern.as_object() {
25		if let Some(P) = Obj.get("pattern").and_then(Value::as_str) {
26			return Some(P.to_string());
27		}
28		if let Some(P) = Obj.get("value").and_then(Value::as_str) {
29			return Some(P.to_string());
30		}
31		if let Some(P) = Obj.get("Pattern").and_then(Value::as_str) {
32			return Some(P.to_string());
33		}
34	}
35	None
36}
37
38/// Extract a `base` directory from a `RelativePattern`-shaped value.
39/// VS Code's `RelativePattern` carries `{ base, pattern }` or
40/// `{ baseUri, pattern }`. When present, the file walk is restricted to
41/// `base`. Returns `None` for plain glob strings.
42pub fn ExtractRelativeBase(Pattern:&Value) -> Option<String> {
43	let Obj = Pattern.as_object()?;
44
45	if let Some(B) = Obj.get("base").and_then(Value::as_str) {
46		return Some(B.to_string());
47	}
48
49	if let Some(B) = Obj.get("baseUri") {
50		if let Some(S) = B.as_str() {
51			if let Some(Stripped) = S.strip_prefix("file://") {
52				return Some(Stripped.to_string());
53			}
54			return Some(S.to_string());
55		}
56		if let Some(P) = B.as_object().and_then(|O| O.get("path")).and_then(Value::as_str) {
57			return Some(P.to_string());
58		}
59		if let Some(P) = B.as_object().and_then(|O| O.get("fsPath")).and_then(Value::as_str) {
60			return Some(P.to_string());
61		}
62	}
63
64	None
65}