Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/UI/
QuickInputShowQuickPick.rs

1//! Wire method: `quickInput:showQuickPick`.
2
3use std::sync::Arc;
4
5use serde_json::{Value, json};
6
7use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
8
9pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
10	use CommonLibrary::UserInterface::{
11		DTO::{QuickPickItemDTO::QuickPickItemDTO, QuickPickOptionsDTO::QuickPickOptionsDTO},
12		UserInterfaceProvider::UserInterfaceProvider,
13	};
14
15	let Items:Vec<QuickPickItemDTO> = Arguments
16		.first()
17		.and_then(|V| V.as_array())
18		.map(|Arr| {
19			Arr.iter()
20				.filter_map(|Item| {
21					let Label = Item.get("label").and_then(|L| L.as_str()).unwrap_or("").to_string();
22					let Description = Item.get("description").and_then(|D| D.as_str()).map(|S| S.to_string());
23					let Detail = Item.get("detail").and_then(|D| D.as_str()).map(|S| S.to_string());
24					let Picked = Item.get("picked").and_then(|P| P.as_bool()).unwrap_or(false);
25					Some(QuickPickItemDTO { Label, Description, Detail, Picked:Some(Picked), AlwaysShow:Some(false) })
26				})
27				.collect()
28		})
29		.unwrap_or_default();
30
31	let Options = QuickPickOptionsDTO {
32		PlaceHolder:Arguments
33			.get(1)
34			.and_then(|V| V.get("placeholder"))
35			.and_then(|P| P.as_str())
36			.map(|S| S.to_string()),
37
38		CanPickMany:Some(
39			Arguments
40				.get(1)
41				.and_then(|V| V.get("canPickMany"))
42				.and_then(|B| B.as_bool())
43				.unwrap_or(false),
44		),
45
46		Title:Arguments
47			.get(1)
48			.and_then(|V| V.get("title"))
49			.and_then(|T| T.as_str())
50			.map(|S| S.to_string()),
51		..Default::default()
52	};
53
54	// Extract before move into ShowQuickPick.
55	let CanPickMany = Options.CanPickMany == Some(true);
56
57	let Result = RunTime
58		.Environment
59		.ShowQuickPick(Items, Some(Options))
60		.await
61		.map_err(|Error| format!("quickInput:showQuickPick failed: {}", Error))?;
62
63	match Result {
64		// When canPickMany is true, VS Code expects an array; otherwise a
65		// single string. .next() was always returning only the first item
66		// even for multi-select, silently discarding all other selections.
67		Some(Labels) => {
68			if CanPickMany {
69				Ok(json!(Labels))
70			} else {
71				Ok(Labels.into_iter().next().map(|S| json!(S)).unwrap_or(Value::Null))
72			}
73		},
74
75		None => Ok(Value::Null),
76	}
77}