wsb 0.181.4020

Workspace - A tool suite for file operations, version management, and development workflow automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
use crate::prelude::*;
pub mod runtime;

use regex::Regex;
use serde_json::Value;
use std::fs::File;
use std::io::{Cursor, Read, Seek};
use std::path::{Component, Path, PathBuf};
use zip::ZipArchive;

use self::runtime::{execute_commonjs_command, inspect_commonjs_bundle, CommandExecution};

/// Parse JSONC (JSON with Comments) — handles // comments, /* */ blocks, and trailing commas.
/// VS Code extension files (themes, grammars, language configs, snippets) use JSONC, not strict JSON.
fn parse_jsonc<T: serde::de::DeserializeOwned>(text: &str) -> Result<T> {
    jsonc_parser::parse_to_serde_value(text, &Default::default())
        .map_err(|e| anyhow::anyhow!("{e}"))?
        .ok_or_else(|| anyhow::anyhow!("empty JSONC document"))
        .and_then(|v| serde_json::from_value(v).map_err(|e| anyhow::anyhow!("{e}")))
}

#[derive(Debug, Clone)]
pub struct ExtensionHost {
    workspace_root: PathBuf,
}

impl ExtensionHost {
    pub fn new(workspace_root: impl Into<PathBuf>) -> Self {
        Self {
            workspace_root: workspace_root.into(),
        }
    }

    /// Discover all installed extensions.  Bad VSIX files are skipped and
    /// their errors are returned as warnings so the rest of the app keeps working.
    pub fn discover(&self) -> Result<(Vec<InstalledExtension>, Vec<String>)> {
        let mut packages = Vec::new();
        let mut warnings = Vec::new();

        let extensions_dir = self.workspace_root.join(".wsb").join("extensions");
        if extensions_dir.is_dir() {
            for entry in std::fs::read_dir(&extensions_dir)
                .with_context(|| format!("failed to read {}", extensions_dir.display()))?
            {
                let entry = entry?;
                let path = entry.path();
                if !path.is_file()
                    || path.extension().and_then(|ext| ext.to_str()) != Some("vsix")
                {
                    continue;
                }
                match InstalledExtension::from_vsix(&path) {
                    Ok((extension, extension_warnings)) => {
                        packages.push(extension);
                        warnings.extend(extension_warnings);
                    }
                    Err(e) => {
                        let msg = format!("{}: {e:#}", path.display());
                        tracing::warn!("skipping bad extension {msg}");
                        warnings.push(msg);
                    }
                }
            }
        }

        packages.sort_by(|left, right| left.id.cmp(&right.id));
        Ok((packages, warnings))
    }

    pub fn workbench_model(&self) -> Result<(VsCodeWorkbenchModel, Vec<String>)> {
        let (all, mut warnings) = self.discover()?;
        let disabled = self.disabled_ids();
        let enabled: Vec<InstalledExtension> = all
            .into_iter()
            .filter(|ext| !disabled.contains(&ext.id))
            .collect();
        let (model, model_warnings) = VsCodeWorkbenchModel::from_extensions(enabled)?;
        warnings.extend(model_warnings);
        Ok((model, warnings))
    }

    /// Return the set of disabled extension IDs from .wsb/extensions/disabled.json.
    pub fn disabled_ids(&self) -> std::collections::HashSet<String> {
        let path = self.disabled_file_path();
        let bytes = match std::fs::read(&path) {
            Ok(b) => b,
            Err(_) => return std::collections::HashSet::new(),
        };
        serde_json::from_slice::<Vec<String>>(&bytes)
            .unwrap_or_default()
            .into_iter()
            .collect()
    }

    /// Write the disabled extension IDs to .wsb/extensions/disabled.json.
    pub fn set_disabled_ids(&self, ids: &std::collections::HashSet<String>) -> Result<()> {
        let path = self.disabled_file_path();
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let mut sorted: Vec<&String> = ids.iter().collect();
        sorted.sort();
        let json = serde_json::to_string_pretty(&sorted)?;
        std::fs::write(&path, json)?;
        Ok(())
    }

    /// Disable an extension by ID. Returns error if extension is not installed.
    pub fn disable_extension(&self, extension_id: &str) -> Result<()> {
        let (extensions, _) = self.discover()?;
        if !extensions.iter().any(|ext| ext.id == extension_id) {
            anyhow::bail!("extension '{}' is not installed", extension_id);
        }
        let mut disabled = self.disabled_ids();
        disabled.insert(extension_id.to_string());
        self.set_disabled_ids(&disabled)
    }

    /// Enable a previously disabled extension. Returns error if extension is not installed.
    pub fn enable_extension(&self, extension_id: &str) -> Result<()> {
        let (extensions, _) = self.discover()?;
        if !extensions.iter().any(|ext| ext.id == extension_id) {
            anyhow::bail!("extension '{}' is not installed", extension_id);
        }
        let mut disabled = self.disabled_ids();
        disabled.remove(extension_id);
        self.set_disabled_ids(&disabled)
    }

    fn disabled_file_path(&self) -> PathBuf {
        self.workspace_root.join(".wsb").join("extensions").join("disabled.json")
    }

    pub fn execute_command(
        &self,
        command_id: &str,
        server_base_url: &str,
        context: WorkbenchExecutionContext,
    ) -> Result<CommandExecution> {
        let extension = self.extension_for_command(command_id)?;
        extension.execute_command(command_id, server_base_url, context)
    }

    pub fn read_asset(&self, extension_id: &str, asset_path: &str) -> Result<ExtensionAsset> {
        let extension = self
            .discover()?.0
            .into_iter()
            .find(|extension| extension.id == extension_id)
            .with_context(|| format!("unknown extension id '{}'", extension_id))?;
        extension.read_asset(asset_path)
    }

    pub fn read_readme(&self, extension_id: &str) -> Result<String> {
        let extension = self
            .discover()?.0
            .into_iter()
            .find(|extension| extension.id == extension_id)
            .with_context(|| format!("unknown extension id '{}'", extension_id))?;
        extension.read_readme()
    }

    fn extension_for_command(&self, command_id: &str) -> Result<InstalledExtension> {
        self.discover()?.0
            .into_iter()
            .find(|extension| {
                extension
                    .contributes
                    .commands
                    .iter()
                    .any(|command| command.command == command_id)
            })
            .with_context(|| format!("unknown extension command '{}'", command_id))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExtensionAsset {
    pub path: String,
    pub bytes: Vec<u8>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TextDocumentContext {
    pub path: String,
    pub uri: String,
    pub language_id: String,
    pub content: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkbenchTabContext {
    pub id: String,
    pub label: String,
    pub input_kind: String,
    pub file_path: Option<String>,
    pub language_id: Option<String>,
    pub is_active: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkbenchGroupContext {
    pub id: String,
    pub tabs: Vec<WorkbenchTabContext>,
    pub active_tab_id: Option<String>,
    pub is_active: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkbenchExecutionContext {
    pub groups: Vec<WorkbenchGroupContext>,
    pub active_group_id: Option<String>,
    pub document: Option<TextDocumentContext>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ShowDocumentRequest {
    pub uri: String,
    pub view_column: i32,
    pub preserve_focus: bool,
    pub selection_start_line: Option<u32>,
    pub selection_start_char: Option<u32>,
    pub selection_end_line: Option<u32>,
    pub selection_end_char: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ExtensionMessage {
    pub level: String,
    pub message: String,
    pub items: Vec<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct VsCodeWorkbenchModel {
    pub extensions: Vec<WorkbenchExtension>,
    pub commands: Vec<WorkbenchCommand>,
    pub languages: Vec<WorkbenchLanguage>,
    pub grammars: Vec<WorkbenchGrammar>,
    pub menus: HashMap<String, Vec<WorkbenchMenuItem>>,
    pub configuration_properties: Vec<WorkbenchConfigurationProperty>,
    pub views_containers: HashMap<String, Vec<WorkbenchViewContainer>>,
    pub views: HashMap<String, Vec<WorkbenchView>>,
    pub themes: Vec<WorkbenchTheme>,
    pub keybindings: Vec<WorkbenchKeybinding>,
    pub snippets: Vec<WorkbenchSnippet>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct WorkbenchViewContainer {
    pub extension_id: String,
    pub id: String,
    pub title: String,
    pub icon: String,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct WorkbenchView {
    pub extension_id: String,
    pub id: String,
    pub name: String,
    pub when: Option<String>,
}

impl VsCodeWorkbenchModel {
    pub fn from_extensions(extensions: Vec<InstalledExtension>) -> Result<(Self, Vec<String>)> {
        let extension_records = extensions
            .iter()
            .map(WorkbenchExtension::from_installed)
            .collect::<Vec<_>>();
        let mut warnings = Vec::new();
        let mut commands = Vec::new();
        let mut languages = Vec::new();
        let mut grammars = Vec::new();
        let mut menus: HashMap<String, Vec<WorkbenchMenuItem>> = HashMap::new();
        let mut configuration_properties = Vec::new();
        let mut views_containers: HashMap<String, Vec<WorkbenchViewContainer>> = HashMap::new();
        let mut views: HashMap<String, Vec<WorkbenchView>> = HashMap::new();
        let mut themes = Vec::new();
        let mut keybindings = Vec::new();
        let mut snippets = Vec::new();
        let mut seen_commands: HashMap<String, String> = HashMap::new();
        let mut seen_languages: HashMap<String, String> = HashMap::new();
        let mut seen_grammars: HashMap<String, String> = HashMap::new();
        let mut seen_configuration_properties: HashMap<String, String> = HashMap::new();
        let mut seen_view_containers: HashMap<String, String> = HashMap::new();
        let mut seen_views: HashMap<String, String> = HashMap::new();

        for extension in &extensions {
            for command in &extension.contributes.commands {
                if let Some(previous) =
                    seen_commands.insert(command.command.clone(), extension.id.clone())
                {
                    warnings.push(format!(
                        "command '{}' is contributed by both '{}' and '{}' — skipping duplicate",
                        command.command, previous, extension.id
                    ));
                    continue;
                }
                commands.push(WorkbenchCommand {
                    extension_id: extension.id.clone(),
                    title: command.title.clone(),
                    command: command.command.clone(),
                    enable_command_uris: command.enable_command_uris,
                    icon: command.icon.clone(),
                    activation_events: extension.activation_events.clone(),
                });
            }

            for language in &extension.contributes.languages {
                if let Some(previous) =
                    seen_languages.insert(language.id.clone(), extension.id.clone())
                {
                    warnings.push(format!(
                        "language '{}' is contributed by both '{}' and '{}' — skipping duplicate",
                        language.id, previous, extension.id
                    ));
                    continue;
                }

                languages.push(WorkbenchLanguage {
                    extension_id: extension.id.clone(),
                    id: language.id.clone(),
                    aliases: language.aliases.clone(),
                    extensions: language.extensions.clone(),
                    configuration_path: language.configuration_path.clone(),
                    configuration: language.configuration.clone(),
                });
            }

            for grammar in &extension.contributes.grammars {
                if let Some(previous) =
                    seen_grammars.insert(grammar.scope_name.clone(), extension.id.clone())
                {
                    warnings.push(format!(
                        "grammar for scope '{}' is contributed by both '{}' and '{}' — skipping duplicate",
                        grammar.scope_name, previous, extension.id
                    ));
                    continue;
                }

                grammars.push(WorkbenchGrammar {
                    extension_id: extension.id.clone(),
                    language: grammar.language.clone(),
                    scope_name: grammar.scope_name.clone(),
                    path: grammar.path.clone(),
                    grammar: grammar.grammar.clone(),
                });
            }

            for (menu_id, menu_items) in &extension.contributes.menus {
                let bucket = menus.entry(menu_id.clone()).or_default();
                for item in menu_items {
                    bucket.push(WorkbenchMenuItem {
                        extension_id: extension.id.clone(),
                        command: item.command.clone(),
                        when: item.when.clone(),
                        group: item.group.clone(),
                    });
                }
            }

            if let Some(configuration) = &extension.contributes.configuration {
                match configuration.properties.as_object() {
                    Some(properties) => {
                        for (property_key, schema) in properties {
                            if let Some(previous) = seen_configuration_properties
                                .insert(property_key.clone(), extension.id.clone())
                            {
                                warnings.push(format!(
                                    "configuration property '{}' is contributed by both '{}' and '{}' — skipping duplicate",
                                    property_key, previous, extension.id
                                ));
                                continue;
                            }
                            configuration_properties.push(WorkbenchConfigurationProperty {
                                extension_id: extension.id.clone(),
                                key: property_key.clone(),
                                schema: schema.clone(),
                            });
                        }
                    }
                    None => {
                        warnings.push(format!(
                            "configuration properties for '{}' is not a JSON object — skipping",
                            extension.id
                        ));
                    }
                }
            }

            for (location, containers) in &extension.contributes.views_containers {
                let bucket = views_containers.entry(location.clone()).or_default();
                for container in containers {
                    if let Some(previous) =
                        seen_view_containers.insert(container.id.clone(), extension.id.clone())
                    {
                        warnings.push(format!(
                            "view container '{}' is contributed by both '{}' and '{}' — skipping duplicate",
                            container.id, previous, extension.id
                        ));
                        continue;
                    }
                    bucket.push(WorkbenchViewContainer {
                        extension_id: extension.id.clone(),
                        id: container.id.clone(),
                        title: container.title.clone(),
                        icon: container.icon.clone().unwrap_or_default(),
                    });
                }
            }

            for (container_id, view_list) in &extension.contributes.views {
                let bucket = views.entry(container_id.clone()).or_default();
                for view in view_list {
                    if let Some(previous) =
                        seen_views.insert(view.id.clone(), extension.id.clone())
                    {
                        warnings.push(format!(
                            "view '{}' is contributed by both '{}' and '{}' — skipping duplicate",
                            view.id, previous, extension.id
                        ));
                        continue;
                    }
                    bucket.push(WorkbenchView {
                        extension_id: extension.id.clone(),
                        id: view.id.clone(),
                        name: view.name.clone(),
                        when: view.when.clone(),
                    });
                }
            }

            for theme in &extension.contributes.themes {
                themes.push(WorkbenchTheme {
                    extension_id: extension.id.clone(),
                    label: theme.label.clone(),
                    ui_theme: theme.ui_theme.clone(),
                    colors: theme.colors.clone(),
                    token_colors: theme.token_colors.clone(),
                });
            }

            for keybinding in &extension.contributes.keybindings {
                keybindings.push(WorkbenchKeybinding {
                    extension_id: extension.id.clone(),
                    command: keybinding.command.clone(),
                    key: keybinding.key.clone(),
                    mac: keybinding.mac.clone(),
                    linux: keybinding.linux.clone(),
                    when: keybinding.when.clone(),
                });
            }

            for snippet in &extension.contributes.snippets {
                snippets.push(WorkbenchSnippet {
                    extension_id: extension.id.clone(),
                    language: snippet.language.clone(),
                    body: snippet.body.clone(),
                });
            }
        }

        // Prepend built-in themes
        let mut all_themes = builtin_themes();
        all_themes.extend(themes);
        let themes = all_themes;

        commands.sort_by(|left, right| left.command.cmp(&right.command));
        languages.sort_by(|left, right| left.id.cmp(&right.id));
        grammars.sort_by(|left, right| left.scope_name.cmp(&right.scope_name));
        configuration_properties.sort_by(|left, right| left.key.cmp(&right.key));
        for menu_items in menus.values_mut() {
            menu_items.sort_by(|left, right| left.command.cmp(&right.command));
        }

        Ok((Self {
            extensions: extension_records,
            commands,
            languages,
            grammars,
            menus,
            configuration_properties,
            views_containers,
            views,
            themes,
            keybindings,
            snippets,
        }, warnings))
    }
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct WorkbenchExtension {
    pub id: String,
    pub publisher: String,
    pub name: String,
    pub display_name: String,
    pub version: String,
    pub description: Option<String>,
    pub icon: Option<String>,
    pub package_path: String,
    pub main: Option<String>,
    pub runtime: Option<ExtensionRuntime>,
    pub activation_events: Vec<String>,
}

impl WorkbenchExtension {
    pub fn from_installed(extension: &InstalledExtension) -> Self {
        Self {
            id: extension.id.clone(),
            publisher: extension.publisher.clone(),
            name: extension.name.clone(),
            display_name: extension.display_name.clone(),
            version: extension.version.clone(),
            description: extension.description.clone(),
            icon: extension.icon.clone(),
            package_path: extension.package_path.clone(),
            main: extension.main.clone(),
            runtime: extension.runtime.clone(),
            activation_events: extension.activation_events.clone(),
        }
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct WorkbenchCommand {
    pub extension_id: String,
    pub title: String,
    pub command: String,
    pub enable_command_uris: bool,
    pub icon: Option<CommandIcon>,
    pub activation_events: Vec<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct WorkbenchLanguage {
    pub extension_id: String,
    pub id: String,
    pub aliases: Vec<String>,
    pub extensions: Vec<String>,
    pub configuration_path: Option<String>,
    pub configuration: Option<Value>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct WorkbenchGrammar {
    pub extension_id: String,
    pub language: Option<String>,
    pub scope_name: String,
    pub path: String,
    pub grammar: Value,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct WorkbenchMenuItem {
    pub extension_id: String,
    pub command: String,
    pub when: Option<String>,
    pub group: Option<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct WorkbenchConfigurationProperty {
    pub extension_id: String,
    pub key: String,
    pub schema: Value,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct WorkbenchTheme {
    pub extension_id: String,
    pub label: String,
    pub ui_theme: String,
    pub colors: Value,
    pub token_colors: Value,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct WorkbenchKeybinding {
    pub extension_id: String,
    pub command: String,
    pub key: String,
    pub mac: Option<String>,
    pub linux: Option<String>,
    pub when: Option<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct WorkbenchSnippet {
    pub extension_id: String,
    pub language: String,
    pub body: Value,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct InstalledExtension {
    pub id: String,
    pub publisher: String,
    pub name: String,
    pub display_name: String,
    pub version: String,
    pub description: Option<String>,
    pub icon: Option<String>,
    pub package_path: String,
    pub main: Option<String>,
    pub runtime: Option<ExtensionRuntime>,
    pub activation_events: Vec<String>,
    pub contributes: ExtensionContributions,
}

impl InstalledExtension {
    pub fn from_vsix(path: &Path) -> Result<(Self, Vec<String>)> {
        let file = File::open(path)
            .with_context(|| format!("failed to open VSIX package {}", path.display()))?;
        let mut archive = ZipArchive::new(file)
            .with_context(|| format!("failed to read ZIP archive {}", path.display()))?;
        Self::from_archive(&path.display().to_string(), &mut archive)
    }

    pub fn from_vsix_bytes(package_path: &str, bytes: Vec<u8>) -> Result<(Self, Vec<String>)> {
        let cursor = Cursor::new(bytes);
        let mut archive = ZipArchive::new(cursor)
            .with_context(|| format!("failed to read ZIP archive {package_path}"))?;
        Self::from_archive(package_path, &mut archive)
    }

    fn from_archive<R: Read + Seek>(
        package_path: &str,
        archive: &mut ZipArchive<R>,
    ) -> Result<(Self, Vec<String>)> {
        let mut warnings = Vec::new();

        let manifest_text = read_zip_string(archive, "extension/package.json")?;
        let manifest: VsixManifest = parse_jsonc(&manifest_text)
            .with_context(|| format!("failed to parse extension/package.json in {package_path}"))?;

        let contributes = ExtensionContributions::from_manifest(&manifest, archive)?;
        let display_name = manifest
            .display_name
            .clone()
            .unwrap_or_else(|| manifest.name.clone());
        let icon = manifest
            .icon
            .as_deref()
            .map(normalize_runtime_asset_path)
            .transpose()
            .with_context(|| format!("invalid icon path in {package_path}"))?;

        let extension_id = format!("{}.{}", manifest.publisher, manifest.name);
        let runtime = match manifest.main.as_deref() {
            Some(main) => match ExtensionRuntime::from_main(main, archive) {
                Ok(runtime) => Some(runtime),
                Err(error) => {
                    warnings.push(format!(
                        "extension '{}' runtime inspection failed: {error:#}",
                        extension_id
                    ));
                    None
                }
            },
            None => None,
        };

        Ok((Self {
            id: format!("{}.{}", manifest.publisher, manifest.name),
            publisher: manifest.publisher,
            name: manifest.name,
            display_name,
            version: manifest.version,
            description: manifest.description,
            icon,
            package_path: package_path.to_string(),
            main: manifest.main,
            runtime,
            activation_events: manifest.activation_events.unwrap_or_default(),
            contributes,
        }, warnings))
    }

    pub fn execute_command(
        &self,
        command_id: &str,
        server_base_url: &str,
        context: WorkbenchExecutionContext,
    ) -> Result<CommandExecution> {
        let main = self
            .main
            .as_deref()
            .with_context(|| format!("extension '{}' has no runtime entrypoint", self.id))?;
        let file = File::open(&self.package_path)
            .with_context(|| format!("failed to open VSIX package {}", self.package_path))?;
        let mut archive = ZipArchive::new(file)
            .with_context(|| format!("failed to read ZIP archive {}", self.package_path))?;
        let resolved = resolve_extension_path(main)?;
        let source = read_zip_string(&mut archive, &resolved)?;
        let asset_files = read_zip_assets(&mut archive)?;
        execute_commonjs_command(
            &source,
            Path::new(main),
            &self.id,
            command_id,
            server_base_url,
            &asset_files,
            &context,
        )
        .with_context(|| format!("failed to execute command '{}' for extension '{}'", command_id, self.id))
    }

    pub fn read_asset(&self, asset_path: &str) -> Result<ExtensionAsset> {
        let file = File::open(&self.package_path)
            .with_context(|| format!("failed to open VSIX package {}", self.package_path))?;
        let mut archive = ZipArchive::new(file)
            .with_context(|| format!("failed to read ZIP archive {}", self.package_path))?;
        let normalized = normalize_runtime_asset_path(asset_path)?;
        let bytes = read_zip_bytes(&mut archive, &runtime_asset_to_archive_entry(&normalized))?;
        Ok(ExtensionAsset {
            path: normalized,
            bytes,
        })
    }

    pub fn read_readme(&self) -> Result<String> {
        let file = File::open(&self.package_path)
            .with_context(|| format!("failed to open VSIX package {}", self.package_path))?;
        let mut archive = ZipArchive::new(file)
            .with_context(|| format!("failed to read ZIP archive {}", self.package_path))?;
        let entry_name = find_extension_readme_entry(&mut archive)
            .with_context(|| format!("extension '{}' does not contain a supported README", self.id))?;
        read_zip_string(&mut archive, &entry_name)
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ExtensionRuntime {
    pub entrypoint: String,
    pub builtin_modules: Vec<String>,
    pub server_entrypoints: Vec<String>,
    pub export_names: Vec<String>,
    pub registered_commands: Vec<String>,
    pub output_channels: Vec<String>,
    pub workspace_listeners: Vec<String>,
    pub subscription_count: usize,
}

impl ExtensionRuntime {
    fn from_main<R: Read + Seek>(main: &str, archive: &mut ZipArchive<R>) -> Result<Self> {
        let resolved = resolve_extension_path(main)?;
        let source = read_zip_string(archive, &resolved)?;

        let require_pattern =
            Regex::new(r#"require\("([^"]+)"\)|require\('([^']+)'\)"#).unwrap();
        let mut builtin_modules = require_pattern
            .captures_iter(&source)
            .filter_map(|capture| capture.get(1).or_else(|| capture.get(2)))
            .map(|value| value.as_str().to_string())
            .filter(|value| !value.starts_with('.'))
            .collect::<Vec<_>>();
        builtin_modules.sort();
        builtin_modules.dedup();

        let server_entrypoints = detect_server_entrypoints(&source);

        let inspection = inspect_commonjs_bundle(&source, Path::new(main))
            .with_context(|| format!("failed to inspect bundled runtime entrypoint {}", main))?;

        Ok(Self {
            entrypoint: main.to_string(),
            builtin_modules,
            server_entrypoints,
            export_names: inspection.export_names,
            registered_commands: inspection.registered_commands,
            output_channels: inspection.output_channels,
            workspace_listeners: inspection.workspace_listeners,
            subscription_count: inspection.subscription_count,
        })
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Default)]
pub struct ExtensionContributions {
    pub commands: Vec<ExtensionCommand>,
    pub languages: Vec<ExtensionLanguage>,
    pub grammars: Vec<ExtensionGrammar>,
    pub configuration: Option<ExtensionConfiguration>,
    pub menus: HashMap<String, Vec<ExtensionMenuItem>>,
    pub views_containers: HashMap<String, Vec<ManifestViewContainer>>,
    pub views: HashMap<String, Vec<ManifestView>>,
    pub themes: Vec<ExtensionTheme>,
    pub keybindings: Vec<ExtensionKeybinding>,
    pub snippets: Vec<ExtensionSnippet>,
}

impl ExtensionContributions {
    fn from_manifest<R: Read + Seek>(manifest: &VsixManifest, archive: &mut ZipArchive<R>) -> Result<Self> {
        let Some(contributes) = manifest.contributes.as_ref() else {
            return Ok(Self::default());
        };

        let commands = contributes
            .commands
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(ExtensionCommand::from_manifest)
            .collect();

        let languages = contributes
            .languages
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(|language| ExtensionLanguage::from_manifest(language, archive))
            .collect::<Result<Vec<_>>>()?;

        let grammars = contributes
            .grammars
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(|grammar| ExtensionGrammar::from_manifest(grammar, archive))
            .collect::<Result<Vec<_>>>()?;

        let configuration = contributes
            .configuration
            .clone()
            .map(ExtensionConfiguration::from_manifest);

        let themes = contributes
            .themes
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(|theme| ExtensionTheme::from_manifest(theme, archive))
            .collect::<Result<Vec<_>>>()?;

        let keybindings = contributes
            .keybindings
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(ExtensionKeybinding::from_manifest)
            .collect();

        let snippets = contributes
            .snippets
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(|snippet| ExtensionSnippet::from_manifest(snippet, archive))
            .collect::<Result<Vec<_>>>()?;

        Ok(Self {
            commands,
            languages,
            grammars,
            configuration,
            menus: contributes.menus.clone().unwrap_or_default(),
            views_containers: contributes.views_containers.clone().unwrap_or_default(),
            views: contributes.views.clone().unwrap_or_default(),
            themes,
            keybindings,
            snippets,
        })
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ExtensionCommand {
    pub command: String,
    pub title: String,
    pub enable_command_uris: bool,
    pub icon: Option<CommandIcon>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum CommandIcon {
    ThemePath {
        light: String,
        dark: String,
    },
    ThemeIcon(String),
}

impl ExtensionCommand {
    fn from_manifest(command: ManifestCommand) -> Self {
        Self {
            command: command.command,
            title: command.title,
            enable_command_uris: command.enable_command_uris.unwrap_or(false),
            icon: command.icon,
        }
    }
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct ExtensionLanguage {
    pub id: String,
    pub aliases: Vec<String>,
    pub extensions: Vec<String>,
    pub configuration_path: Option<String>,
    pub configuration: Option<Value>,
}

impl ExtensionLanguage {
    fn from_manifest<R: Read + Seek>(language: ManifestLanguage, archive: &mut ZipArchive<R>) -> Result<Self> {
        let configuration_path = language.configuration.clone();
        let configuration = if let Some(path) = configuration_path.as_deref() {
            let resolved = resolve_extension_path(path)?;
            let text = read_zip_string(archive, &resolved)?;
            Some(
                parse_jsonc(&text)
                    .with_context(|| format!("failed to parse language configuration {}", resolved))?,
            )
        } else {
            None
        };

        Ok(Self {
            id: language.id,
            aliases: language.aliases.unwrap_or_default(),
            extensions: language.extensions.unwrap_or_default(),
            configuration_path,
            configuration,
        })
    }
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct ExtensionGrammar {
    pub language: Option<String>,
    pub scope_name: String,
    pub path: String,
    pub grammar: Value,
}

impl ExtensionGrammar {
    fn from_manifest<R: Read + Seek>(grammar: ManifestGrammar, archive: &mut ZipArchive<R>) -> Result<Self> {
        let resolved = resolve_extension_path(&grammar.path)?;
        let text = read_zip_string(archive, &resolved)?;
        let value = parse_jsonc(&text)
            .with_context(|| format!("failed to parse grammar {}", resolved))?;

        Ok(Self {
            language: grammar.language,
            scope_name: grammar.scope_name,
            path: grammar.path,
            grammar: value,
        })
    }
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct ExtensionConfiguration {
    pub title: Option<String>,
    pub schema_type: Option<String>,
    pub properties: Value,
}

impl ExtensionConfiguration {
    fn from_manifest(configuration: ManifestConfiguration) -> Self {
        Self {
            title: configuration.title,
            schema_type: configuration.schema_type,
            properties: configuration.properties.unwrap_or(Value::Object(Default::default())),
        }
    }
}

impl ExtensionTheme {
    fn from_manifest<R: Read + Seek>(theme: ManifestTheme, archive: &mut ZipArchive<R>) -> Result<Self> {
        let resolved = resolve_extension_path(&theme.path)?;
        let text = read_zip_string(archive, &resolved)
            .with_context(|| format!("failed to read theme file '{}' from VSIX", theme.path))?;
        let parsed: Value = parse_jsonc(&text)
            .with_context(|| format!("failed to parse theme JSON '{}'", theme.path))?;
        let colors = parsed.get("colors").cloned().unwrap_or(Value::Object(Default::default()));
        let token_colors = parsed.get("tokenColors").cloned().unwrap_or(Value::Array(Default::default()));
        Ok(Self {
            label: theme.label,
            ui_theme: theme.ui_theme,
            path: theme.path,
            colors,
            token_colors,
        })
    }
}

impl ExtensionKeybinding {
    fn from_manifest(keybinding: ManifestKeybinding) -> Self {
        Self {
            command: keybinding.command,
            key: keybinding.key,
            mac: keybinding.mac,
            linux: keybinding.linux,
            when: keybinding.when,
        }
    }
}

impl ExtensionSnippet {
    fn from_manifest<R: Read + Seek>(snippet: ManifestSnippet, archive: &mut ZipArchive<R>) -> Result<Self> {
        let resolved = resolve_extension_path(&snippet.path)?;
        let text = read_zip_string(archive, &resolved)
            .with_context(|| format!("failed to read snippet file '{}' from VSIX", snippet.path))?;
        let body: Value = parse_jsonc(&text)
            .with_context(|| format!("failed to parse snippet JSON '{}'", snippet.path))?;
        Ok(Self {
            language: snippet.language,
            path: snippet.path,
            body,
        })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ExtensionMenuItem {
    pub command: String,
    #[serde(default)]
    pub when: Option<String>,
    #[serde(default)]
    pub group: Option<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct ExtensionTheme {
    pub label: String,
    pub ui_theme: String,
    pub path: String,
    pub colors: Value,
    pub token_colors: Value,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ExtensionKeybinding {
    pub command: String,
    pub key: String,
    pub mac: Option<String>,
    pub linux: Option<String>,
    pub when: Option<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct ExtensionSnippet {
    pub language: String,
    pub path: String,
    pub body: Value,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct VsixManifest {
    name: String,
    publisher: String,
    version: String,
    #[serde(default)]
    display_name: Option<String>,
    #[serde(default)]
    description: Option<String>,
    #[serde(default)]
    icon: Option<String>,
    #[serde(default)]
    main: Option<String>,
    #[serde(default)]
    activation_events: Option<Vec<String>>,
    #[serde(default)]
    contributes: Option<ManifestContributes>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ManifestContributes {
    #[serde(default)]
    commands: Option<Vec<ManifestCommand>>,
    #[serde(default)]
    languages: Option<Vec<ManifestLanguage>>,
    #[serde(default)]
    grammars: Option<Vec<ManifestGrammar>>,
    #[serde(default)]
    configuration: Option<ManifestConfiguration>,
    #[serde(default)]
    menus: Option<HashMap<String, Vec<ExtensionMenuItem>>>,
    #[serde(default)]
    views_containers: Option<HashMap<String, Vec<ManifestViewContainer>>>,
    #[serde(default)]
    views: Option<HashMap<String, Vec<ManifestView>>>,
    #[serde(default)]
    themes: Option<Vec<ManifestTheme>>,
    #[serde(default)]
    keybindings: Option<Vec<ManifestKeybinding>>,
    #[serde(default)]
    snippets: Option<Vec<ManifestSnippet>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ManifestViewContainer {
    pub id: String,
    pub title: String,
    #[serde(default)]
    pub icon: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ManifestView {
    pub id: String,
    pub name: String,
    #[serde(default)]
    pub when: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ManifestCommand {
    command: String,
    title: String,
    #[serde(default)]
    enable_command_uris: Option<bool>,
    #[serde(default)]
    icon: Option<CommandIcon>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ManifestLanguage {
    id: String,
    #[serde(default)]
    aliases: Option<Vec<String>>,
    #[serde(default)]
    extensions: Option<Vec<String>>,
    #[serde(default)]
    configuration: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ManifestGrammar {
    #[serde(default)]
    language: Option<String>,
    path: String,
    scope_name: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ManifestConfiguration {
    #[serde(default)]
    title: Option<String>,
    #[serde(rename = "type", default)]
    schema_type: Option<String>,
    #[serde(default)]
    properties: Option<Value>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ManifestTheme {
    label: String,
    ui_theme: String,
    path: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ManifestKeybinding {
    command: String,
    key: String,
    #[serde(default)]
    mac: Option<String>,
    #[serde(default)]
    linux: Option<String>,
    #[serde(default)]
    when: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ManifestSnippet {
    language: String,
    path: String,
}

fn resolve_extension_path(raw_path: &str) -> Result<String> {
    let trimmed = raw_path.trim();
    if trimmed.is_empty() {
        anyhow::bail!("extension asset path must not be empty");
    }

    let mut relative = PathBuf::new();
    for component in Path::new(trimmed).components() {
        match component {
            Component::CurDir => {}
            Component::Normal(part) => relative.push(part),
            Component::ParentDir => {
                anyhow::bail!("extension asset path '{}' must not traverse outside the package root", raw_path)
            }
            Component::RootDir | Component::Prefix(_) => {
                anyhow::bail!("extension asset path '{}' must be relative to the package root", raw_path)
            }
        }
    }

    Ok(format!("extension/{}", relative.to_string_lossy()))
}

fn read_zip_string<R: Read + Seek>(archive: &mut ZipArchive<R>, entry_name: &str) -> Result<String> {
    let mut file = archive
        .by_name(entry_name)
        .with_context(|| format!("missing '{}' in VSIX package", entry_name))?;
    let mut buffer = String::new();
    file.read_to_string(&mut buffer)
        .with_context(|| format!("failed to read '{}' from VSIX package", entry_name))?;
    Ok(buffer)
}

fn read_zip_bytes<R: Read + Seek>(archive: &mut ZipArchive<R>, entry_name: &str) -> Result<Vec<u8>> {
    let mut file = archive
        .by_name(entry_name)
        .with_context(|| format!("missing '{}' in VSIX package", entry_name))?;
    let mut buffer = Vec::new();
    file.read_to_end(&mut buffer)
        .with_context(|| format!("failed to read '{}' from VSIX package", entry_name))?;
    Ok(buffer)
}

fn read_zip_assets<R: Read + Seek>(archive: &mut ZipArchive<R>) -> Result<HashMap<String, Vec<u8>>> {
    let mut assets = HashMap::new();
    let len = archive.len();
    for index in 0..len {
        let mut file = archive.by_index(index)?;
        if file.name().ends_with('/') {
            continue;
        }
        let name = file.name().to_string();
        if !name.starts_with("extension/") {
            continue;
        }
        let runtime_path = format!("/{}", name.trim_start_matches("extension/"));
        let mut bytes = Vec::new();
        file.read_to_end(&mut bytes)
            .with_context(|| format!("failed to read '{}' from VSIX package", name))?;
        assets.insert(runtime_path, bytes);
    }
    Ok(assets)
}

fn find_extension_readme_entry<R: Read + Seek>(archive: &mut ZipArchive<R>) -> Option<String> {
    const CANDIDATES: &[&str] = &[
        "extension/readme.md",
        "extension/readme.markdown",
        "extension/readme",
    ];

    let mut names = Vec::new();
    for index in 0..archive.len() {
        let file = archive.by_index(index).ok()?;
        if file.name().ends_with('/') {
            continue;
        }
        names.push(file.name().to_string());
    }

    CANDIDATES.iter().find_map(|candidate| {
        names
            .iter()
            .find(|name| name.eq_ignore_ascii_case(candidate))
            .cloned()
    })
}

fn normalize_runtime_asset_path(raw_path: &str) -> Result<String> {
    let trimmed = raw_path.trim();
    if trimmed.is_empty() {
        anyhow::bail!("extension runtime asset path must not be empty");
    }
    let mut normalized = PathBuf::new();
    for component in Path::new(trimmed).components() {
        match component {
            Component::RootDir | Component::CurDir => {}
            Component::Normal(part) => normalized.push(part),
            Component::ParentDir => {
                anyhow::bail!(
                    "extension runtime asset path '{}' must not traverse outside the package root",
                    raw_path
                )
            }
            Component::Prefix(_) => {
                anyhow::bail!(
                    "extension runtime asset path '{}' must not use a path prefix",
                    raw_path
                )
            }
        }
    }
    let normalized = format!("/{}", normalized.to_string_lossy());
    if normalized == "/" {
        anyhow::bail!("extension runtime asset path '{}' must address a file", raw_path);
    }
    Ok(normalized)
}

fn runtime_asset_to_archive_entry(asset_path: &str) -> String {
    format!("extension/{}", asset_path.trim_start_matches('/'))
}

fn detect_server_entrypoints(source: &str) -> Vec<String> {
    let pattern = Regex::new(
        r#"join\("([^"]+)"\s*,\s*"([^"]+)"\s*,\s*"([^"]+)"\)|join\('([^']+)'\s*,\s*'([^']+)'\s*,\s*'([^']+)'\)"#,
    )
    .unwrap();

    let mut results = pattern
        .captures_iter(source)
        .filter_map(|capture| {
            let values = (1..=6)
                .filter_map(|index| capture.get(index).map(|value| value.as_str()))
                .collect::<Vec<_>>();
            if values.len() != 3 {
                return None;
            }
            let joined = values.join("/");
            if joined.ends_with(".js") {
                Some(format!("./{}", joined))
            } else {
                None
            }
        })
        .collect::<Vec<_>>();
    results.sort();
    results.dedup();
    results
}

fn builtin_themes() -> Vec<WorkbenchTheme> {
    let light_colors = serde_json::json!({
        "editor.background": "#f5f6f8",
        "sideBar.background": "#ffffff",
        "list.hoverBackground": "#f0f1f4",
        "input.background": "#ecedf2",
        "tab.activeBackground": "#ffffff",
        "panel.border": "#d8dae2",
        "widget.border": "#e4e6ec",
        "editor.foreground": "#181b24",
        "foreground": "#555b6e",
        "disabledForeground": "#878ea0",
        "focusBorder": "#2563eb",
        "errorForeground": "#dc2626",
        "testing.iconPassed": "#059669",
        "editorWarning.foreground": "#d97706",
        "textPreformat.foreground": "#7c3aed",
        "widget.shadow": "rgba(0,0,0,0.3)"
    });

    let dark_colors = serde_json::json!({
        "editor.background": "#0d0d12",
        "sideBar.background": "#16161e",
        "list.hoverBackground": "#1f1f2a",
        "input.background": "#111116",
        "tab.activeBackground": "#16161e",
        "panel.border": "#2b2b38",
        "widget.border": "#232330",
        "editor.foreground": "#e4e6eb",
        "foreground": "#9ea5b4",
        "disabledForeground": "#6c7385",
        "focusBorder": "#60a5fa",
        "errorForeground": "#f87171",
        "testing.iconPassed": "#34d399",
        "editorWarning.foreground": "#fbbf24",
        "textPreformat.foreground": "#a78bfa",
        "widget.shadow": "rgba(0,0,0,0.6)"
    });

    vec![
        WorkbenchTheme {
            extension_id: "builtin".to_string(),
            label: "WSB Light".to_string(),
            ui_theme: "vs".to_string(),
            colors: light_colors,
            token_colors: Value::Array(vec![]),
        },
        WorkbenchTheme {
            extension_id: "builtin".to_string(),
            label: "WSB Dark".to_string(),
            ui_theme: "vs-dark".to_string(),
            colors: dark_colors,
            token_colors: Value::Array(vec![]),
        },
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::tempdir;
    use zip::write::SimpleFileOptions;

    #[test]
    fn resolve_extension_path_rejects_parent_traversal() {
        let error = resolve_extension_path("../dist/extension.js")
            .expect_err("path traversal must fail");
        assert!(error
            .to_string()
            .contains("must not traverse outside the package root"));
    }

    #[test]
    fn builtin_themes_are_prepended_to_workbench_model() {
        let (model, warnings) = VsCodeWorkbenchModel::from_extensions(vec![])
            .expect("empty extensions should produce a valid model");
        assert!(warnings.is_empty());
        assert_eq!(model.themes.len(), 2);
        assert_eq!(model.themes[0].label, "WSB Light");
        assert_eq!(model.themes[0].ui_theme, "vs");
        assert_eq!(model.themes[0].extension_id, "builtin");
        assert_eq!(model.themes[1].label, "WSB Dark");
        assert_eq!(model.themes[1].ui_theme, "vs-dark");
        assert_eq!(model.themes[1].extension_id, "builtin");

        // Verify colors contain expected VS Code token keys
        let light_colors = model.themes[0].colors.as_object()
            .expect("light theme colors must be a JSON object");
        assert_eq!(light_colors.get("editor.background").and_then(|v| v.as_str()), Some("#f5f6f8"));
        assert_eq!(light_colors.get("focusBorder").and_then(|v| v.as_str()), Some("#2563eb"));

        let dark_colors = model.themes[1].colors.as_object()
            .expect("dark theme colors must be a JSON object");
        assert_eq!(dark_colors.get("editor.background").and_then(|v| v.as_str()), Some("#0d0d12"));
        assert_eq!(dark_colors.get("focusBorder").and_then(|v| v.as_str()), Some("#60a5fa"));
    }

    #[test]
    fn workbench_model_warns_on_duplicate_commands() {
        let extension_a = InstalledExtension {
            id: "pub.a".to_string(),
            publisher: "pub".to_string(),
            name: "a".to_string(),
            display_name: "A".to_string(),
            version: "1.0.0".to_string(),
            description: None,
            icon: None,
            package_path: "a.vsix".to_string(),
            main: None,
            runtime: None,
            activation_events: vec![],
            contributes: ExtensionContributions {
                commands: vec![ExtensionCommand {
                    command: "duplicate.command".to_string(),
                    title: "A".to_string(),
                    enable_command_uris: false,
                    icon: None,
                }],
                ..ExtensionContributions::default()
            },
        };
        let extension_b = InstalledExtension {
            id: "pub.b".to_string(),
            publisher: "pub".to_string(),
            name: "b".to_string(),
            display_name: "B".to_string(),
            version: "1.0.0".to_string(),
            description: None,
            icon: None,
            package_path: "b.vsix".to_string(),
            main: None,
            runtime: None,
            activation_events: vec![],
            contributes: ExtensionContributions {
                commands: vec![ExtensionCommand {
                    command: "duplicate.command".to_string(),
                    title: "B".to_string(),
                    enable_command_uris: false,
                    icon: None,
                }],
                ..ExtensionContributions::default()
            },
        };

        let (model, warnings) = VsCodeWorkbenchModel::from_extensions(vec![extension_a, extension_b])
            .expect("duplicate commands should produce a model with warnings");
        // The first extension's command is kept, the duplicate is skipped
        assert_eq!(model.commands.len(), 1);
        assert_eq!(model.commands[0].extension_id, "pub.a");
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].contains("command 'duplicate.command' is contributed by both"));
    }

    #[test]
    fn normalize_runtime_asset_path_accepts_relative_icon_path() {
        let normalized = normalize_runtime_asset_path("images/icon.png")
            .expect("relative icon path should normalize");
        assert_eq!(normalized, "/images/icon.png");
    }

    #[test]
    fn manifest_command_icon_accepts_theme_path_object() {
        let command: ManifestCommand = serde_json::from_str(
            r#"{
                "command": "example.open",
                "title": "Example: Open",
                "icon": {
                    "light": "resources/light.svg",
                    "dark": "resources/dark.svg"
                }
            }"#,
        )
        .expect("manifest command icon object must deserialize");

        assert_eq!(
            command.icon,
            Some(CommandIcon::ThemePath {
                light: "resources/light.svg".to_string(),
                dark: "resources/dark.svg".to_string(),
            })
        );
    }

    #[test]
    fn discover_keeps_extension_when_runtime_inspection_fails_and_returns_warning() {
        let workspace = tempdir().expect("temp workspace must be created");
        let extensions_dir = workspace.path().join(".wsb").join("extensions");
        std::fs::create_dir_all(&extensions_dir)
            .expect("extensions directory must be created");

        let vsix_path = extensions_dir.join("broken-runtime.vsix");
        let file = File::create(&vsix_path).expect("vsix file must be created");
        let mut zip = zip::ZipWriter::new(file);
        let options = SimpleFileOptions::default();

        zip.start_file("extension/package.json", options.clone())
            .expect("package.json entry must be created");
        write!(
            zip,
            r#"{{
  "name": "broken-runtime",
  "publisher": "test",
  "version": "1.0.0",
  "main": "./dist/extension.js",
  "contributes": {{
    "themes": [
      {{
        "label": "Broken Runtime Theme",
        "uiTheme": "vs-dark",
        "path": "./themes/theme.json"
      }}
    ]
  }}
}}"#
        )
        .expect("package.json must be written");

        zip.start_file("extension/dist/extension.js", options.clone())
            .expect("runtime entrypoint must be created");
        write!(
            zip,
            r#"const missing = require("definitely-unsupported");
exports.activate = () => missing;"#
        )
        .expect("runtime entrypoint must be written");

        zip.start_file("extension/themes/theme.json", options)
            .expect("theme entry must be created");
        write!(
            zip,
            r##"{{
  "colors": {{
    "editor.background": "#010203"
  }}
}}"##
        )
        .expect("theme file must be written");

        zip.finish().expect("vsix archive must be finalized");

        let host = ExtensionHost::new(workspace.path());
        let (extensions, warnings) = host.discover().expect("discovery must succeed");
        assert_eq!(extensions.len(), 1, "extension must still be discovered");
        assert_eq!(extensions[0].id, "test.broken-runtime");
        assert!(
            warnings.iter().any(|warning| warning.contains("runtime inspection failed")),
            "runtime inspection failure must surface as a warning for frontend notifications: {warnings:?}"
        );
        assert!(
            warnings
                .iter()
                .any(|warning| warning.contains("test.broken-runtime")),
            "warning must identify the affected extension: {warnings:?}"
        );

        let (model, model_warnings) = host.workbench_model()
            .expect("workbench model must still be created");
        assert!(
            model
                .themes
                .iter()
                .any(|theme| theme.label == "Broken Runtime Theme"),
            "non-runtime contributions must still be available despite runtime inspection failure"
        );
        assert_eq!(
            warnings, model_warnings,
            "workbench model must propagate discovery warnings unchanged"
        );
        assert!(
            extensions[0].runtime.is_none(),
            "runtime metadata must be omitted when inspection fails"
        );
    }

    #[test]
    fn read_readme_loads_root_readme_case_insensitively() {
        let workspace = tempdir().expect("temp workspace must be created");
        let extensions_dir = workspace.path().join(".wsb").join("extensions");
        std::fs::create_dir_all(&extensions_dir)
            .expect("extensions directory must be created");

        let vsix_path = extensions_dir.join("readme-extension.vsix");
        let file = File::create(&vsix_path).expect("vsix file must be created");
        let mut zip = zip::ZipWriter::new(file);
        let options = SimpleFileOptions::default();

        zip.start_file("extension/package.json", options.clone())
            .expect("package.json entry must be created");
        write!(
            zip,
            "{}",
            r#"{
  "name": "readme-extension",
  "publisher": "test",
  "version": "1.0.0"
}"#
        )
        .expect("package.json must be written");

        zip.start_file("extension/README.MD", options)
            .expect("readme entry must be created");
        write!(zip, "# Test Readme\n\nHello from the package.\n")
            .expect("readme must be written");

        zip.finish().expect("vsix archive must be finalized");

        let host = ExtensionHost::new(workspace.path());
        let readme = host
            .read_readme("test.readme-extension")
            .expect("root readme must be readable");

        assert!(readme.contains("# Test Readme"));
        assert!(readme.contains("Hello from the package."));
    }
}