Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Environment/Utility/
GlobPattern.rs

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