cimoxide-view 0.7.0

A CGMES or NC model set as views: its grid at substation level, laid out, and its objects as a tree, with validation findings on each; and its files as text: where each object is written, and edits across them
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
//! Edits across a model set, as text edits for whoever holds the files to
//! apply: renaming an mRID, deleting objects and adding a field a finding
//! says is missing; and the objects a reference can name, the likeliest
//! first; adding an object in a container. They change the files as
//! written, keeping everything else of them, and reach every file of the
//! set: an object's EQ, SSH and SV elements and every reference to it.
//!
//! The caller gives the files as they are now, each named as it likes (a
//! URL, a file name), with its [`Index`]; the edits come back by that name.

use std::collections::{HashMap, HashSet};

use cimmodel::base::AttrKind;
use cimmodel::registry::type_registry;

use crate::index::{IdKind, Index, local};
use crate::text::{LineIndex, Position, Range, TextEdit, contains};
use crate::tree::{CONTAINED, Tree};

/// One file of the set, as it is now.
pub struct File<'a> {
    /// Whatever names it for the caller.
    pub name: &'a str,
    pub text: &'a str,
    pub lines: &'a LineIndex,
    pub index: &'a Index,
}

/// Edits by file, in the order the files were given; only files that change.
pub type Edits<'a> = Vec<(&'a str, Vec<TextEdit>)>;

impl File<'_> {
    /// The last `len` bytes of `r`: an mRID at the end of the value written
    /// (after `#`, or the whole of `_x` or `urn:uuid:x`).
    fn suffix(&self, r: Range, len: usize) -> Range {
        let end = self.lines.offset(self.text, r.end);
        Range::new(self.lines.position(self.text, end.saturating_sub(len)), r.end)
    }

    /// `r` widened to whole lines when nothing else is written on them.
    pub(crate) fn lines_of(&self, r: Range) -> Range {
        let (text, start, end) = (self.text, self.lines.offset(self.text, r.start), self.lines.offset(self.text, r.end));
        let line_start = text[..start].rfind('\n').map_or(0, |i| i + 1);
        let line_end = text[end..].find('\n').map_or(text.len(), |i| end + i + 1);
        if text[line_start..start].trim().is_empty() && text[end..line_end].trim().is_empty() {
            self.lines.range(text, line_start, line_end)
        } else {
            r
        }
    }

    /// The text of a field written `<q>text</q>`, and its range.
    fn field_text(&self, head: Range, full: Range, qname: &str) -> Option<(&str, Range)> {
        let start = self.lines.offset(self.text, head.end);
        let end = self.lines.offset(self.text, full.end).checked_sub(qname.len() + 3)?;
        let value = self.text.get(start..end)?;
        (!value.contains('<')).then(|| (value, self.lines.range(self.text, start, end)))
    }
}

/// The mRID at `pos` in the file `name` — an element's `rdf:ID`/`rdf:about`,
/// or an `rdf:resource` — and the range of it as written; anywhere else in an
/// element's start tag, that element's.
pub fn prepare_rename(files: &[File], name: &str, pos: Position) -> Option<(Range, String)> {
    let file = files.iter().find(|f| f.name == name)?;
    let el = file.index.element_at(pos)?;
    if let Some((mrid, r)) = el.fields.iter().filter_map(|f| f.resource.as_ref()).find(|(_, r)| contains(*r, pos)) {
        return Some((file.suffix(*r, mrid.len()), mrid.clone()));
    }
    let id = el.id_range?;
    (contains(el.head, pos) && !el.mrid.is_empty()).then(|| (file.suffix(id, el.mrid.len()), el.mrid.clone()))
}

/// Rename mRID `old` to `new` in every file: each element's `rdf:ID` or
/// `rdf:about`, each `rdf:resource` to it, and an `IdentifiedObject.mRID`
/// (or a class's own `….mRID`) that writes it (without its leading `_`, as
/// CGMES 3 does, if so).
pub fn rename<'a>(files: &[File<'a>], old: &str, new: &str) -> Result<Edits<'a>, String> {
    let new = new.trim();
    if new.is_empty() || new.contains(|c: char| c.is_whitespace() || "\"'<>&#".contains(c)) {
        return Err(format!("`{new}` cannot be an mRID: no spaces, quotes, `<`, `>`, `&` or `#`"));
    }
    let defined = files.iter().flat_map(|f| &f.index.elements).any(|e| e.mrid == old && e.id_kind == Some(IdKind::Id));
    let ncname = new.starts_with(|c: char| c.is_alphabetic() || c == '_') && !new.contains(':');
    if defined && !ncname {
        return Err(format!("`{new}` cannot be an `rdf:ID`: it must start with a letter or `_` and hold no `:`"));
    }
    if files.iter().flat_map(|f| &f.index.elements).any(|e| e.mrid == new) {
        return Err(format!("the model set already has an object `{new}`"));
    }
    let mut changes = Vec::new();
    for file in files {
        let mut edits = Vec::new();
        for el in &file.index.elements {
            if el.mrid == old
                && let Some(id) = el.id_range
            {
                edits.push(TextEdit::new(file.suffix(id, old.len()), new.into()));
                for f in el.fields.iter().filter(|f| f.key().ends_with(".mRID")) {
                    let Some((text, r)) = file.field_text(f.head, f.full, &f.qname) else { continue };
                    let bare = |s: &str| s.strip_prefix('_').unwrap_or(s).to_string();
                    if text.trim() == old {
                        edits.push(TextEdit::new(r, new.into()));
                    } else if text.trim() == bare(old) {
                        edits.push(TextEdit::new(r, bare(new)));
                    }
                }
            }
            for (mrid, r) in el.fields.iter().filter_map(|f| f.resource.as_ref()) {
                if mrid == old {
                    edits.push(TextEdit::new(file.suffix(*r, old.len()), new.into()));
                }
            }
        }
        if !edits.is_empty() {
            changes.push((file.name, edits));
        }
    }
    Ok(changes)
}

/// What deleting objects changes: the elements writing them, in every file,
/// and the fields referring to them from what stays. Whole lines go, so a
/// client should show it before applying it.
pub struct Deletion<'a> {
    pub edits: Edits<'a>,
    pub objects: usize,
    pub references: usize,
}

/// Delete objects `ids`.
pub fn delete<'a>(files: &[File<'a>], ids: &HashSet<&str>) -> Deletion<'a> {
    let mut changes = Vec::new();
    let (mut objects, mut references) = (HashSet::new(), 0);
    for file in files {
        let mut edits: Vec<TextEdit> = Vec::new();
        let mut edit = |r: Range| edits.push(TextEdit::new(file.lines_of(r), String::new()));
        for el in &file.index.elements {
            if ids.contains(el.mrid.as_str()) {
                objects.insert(el.mrid.as_str());
                edit(el.full);
                continue;
            }
            for f in &el.fields {
                if f.resource.as_ref().is_some_and(|(m, _)| ids.contains(m.as_str())) {
                    references += 1;
                    edit(f.full);
                }
            }
        }
        if !edits.is_empty() {
            changes.push((file.name, edits));
        }
    }
    Deletion { edits: changes, objects: objects.len(), references }
}

/// A field to add: where, and where its value is once added.
pub struct Addition {
    /// The file it goes in, by its name.
    pub file: String,
    pub edit: TextEdit,
    /// The inserted value (or where it goes), to select after the edit.
    pub value: Range,
    /// A reference, inserted empty: `rdf:resource="#"`.
    pub reference: bool,
}

impl Addition {
    /// The edit with the reference to `c` filled in.
    pub fn naming(&self, c: &Candidate) -> TextEdit {
        TextEdit::new(self.edit.range, self.edit.new_text.replacen("rdf:resource=\"#\"", &format!("rdf:resource=\"{}\"", c.reference()), 1))
    }
}

/// The profile codes `file`'s header declares (`md:Model.profile`,
/// `dcterms:conformsTo`), by the schemas' profile tables.
pub(crate) fn profiles(file: &File) -> Vec<&'static str> {
    let reg = type_registry();
    let tables: Vec<&[(&str, &str)]> = ["cgmes", "nc"].iter().filter_map(|f| reg.schema(f)).map(|s| s.profiles).collect();
    let Some(header) = file.index.elements.iter().find(|e| matches!(local(&e.qname), "FullModel" | "Dataset" | "DifferenceModel")) else { return Vec::new() };
    header
        .fields
        .iter()
        .filter(|f| matches!(f.key(), "Model.profile" | "conformsTo"))
        .filter_map(|f| match &f.resource {
            Some((uri, _)) => Some(uri.as_str()),
            None => file.field_text(f.head, f.full, &f.qname).map(|(t, _)| t.trim()),
        })
        .filter_map(|uri| tables.iter().flat_map(|t| t.iter()).find(|(_, u)| *u == uri).map(|(code, _)| *code))
        .collect()
}

/// Add field `key` to object `id`, which writes it nowhere: in the file
/// (`prefer` first) whose profile carries it and that writes the object,
/// after its last field, with a value to type over — the object's mRID for
/// `IdentifiedObject.mRID`, `false`, `0`, an enumeration's first value, or
/// none. `None` if a file writes it already, its class does not declare
/// it, or no file fits.
pub fn add_missing(files: &[File], prefer: &str, id: &str, key: &str) -> Option<Addition> {
    addition(files, prefer, id, key, None)
}

/// [`add_missing`], with `given` as the value written (as [`set_field`] takes it) rather than
/// one to type over.
fn addition(files: &[File], prefer: &str, id: &str, key: &str, given: Option<&str>) -> Option<Addition> {
    let reg = type_registry();
    let sites: Vec<(&File, &crate::index::Element)> =
        files.iter().flat_map(|f| f.index.elements.iter().filter(|e| e.mrid == id).map(move |e| (f, e))).collect();
    if sites.iter().any(|(_, e)| e.fields.iter().any(|f| f.key() == key)) {
        return None;
    }
    let (def_file, def) = sites.iter().max_by_key(|(_, e)| e.id_kind == Some(IdKind::Id))?;
    let class = def_file.index.class_of(&def.qname)?;
    let attr = reg.attr(class, key).filter(|a| a.used)?;
    let fits = |f: &File| {
        let declared = profiles(f);
        declared.is_empty() || attr.origins.is_empty() || declared.iter().any(|p| attr.origins.contains(p))
    };
    let (file, el) = sites
        .iter()
        .filter(|(f, _)| fits(f))
        .min_by_key(|(f, e)| (f.name != prefer, e.id_kind != Some(IdKind::Id)))?;
    let tag = format!("{}:{}", file.index.prefix_of(attr.ns)?, attr.id);
    let (open, value, close) = match attr.kind {
        AttrKind::Literal => {
            let value = match (attr.id, attr.xsd) {
                _ if given.is_some() => escape(given.unwrap_or_default()),
                ("IdentifiedObject.mRID", _) => id.strip_prefix('_').unwrap_or(id).to_string(),
                (_, "boolean") => "false".into(),
                (_, "double" | "float" | "decimal" | "integer" | "int" | "long" | "short") => "0".into(),
                _ => String::new(),
            };
            (format!("<{tag}>"), value, format!("</{tag}>"))
        }
        AttrKind::Association => match given {
            Some(target) => (format!("<{tag} rdf:resource=\""), reference(target), "\" />".into()),
            None => (format!("<{tag} rdf:resource=\"#"), String::new(), "\" />".into()),
        },
        AttrKind::Enum => {
            let first = reg.family_of(class.qualified).and_then(|f| reg.schema(f)).and_then(|s| s.enumeration(attr.range)).and_then(|e| e.values.first());
            let value = given.map_or_else(|| first.map_or(String::new(), |v| v.id.to_string()), |v| enum_value(attr.range, v).to_string());
            (format!("<{tag} rdf:resource=\"{}", attr.value_ns), value, "\" />".into())
        }
    };
    // On a line of its own after the last field, indented as it is; else
    // inside the element, a step in from it.
    let (text, lines) = (file.text, file.lines);
    let indent_of = |r: Range| {
        let start = lines.offset(text, Position::new(r.start.line, 0));
        let at = lines.offset(text, r.start);
        text[start..at].chars().take_while(|c| c.is_whitespace()).collect::<String>()
    };
    let (at, indent) = match el.fields.last() {
        Some(f) => (f.full.end, indent_of(f.head)),
        None => {
            let end = lines.offset(text, el.full.end);
            let closing = end.checked_sub(el.qname.len() + 3).filter(|&c| text.get(c..end) == Some(&format!("</{}>", el.qname)))?;
            (lines.position(text, closing), format!("{}  ", indent_of(el.head)))
        }
    };
    // Lines end as the file's do.
    let eol = if text.contains("\r\n") { "\r\n" } else { "\n" };
    let new_text = if el.fields.is_empty() {
        format!("  {open}{value}{close}{eol}{}", indent_of(el.head))
    } else {
        format!("{eol}{indent}{open}{value}{close}")
    };
    let utf16 = |s: &str| s.encode_utf16().count() as u32;
    let start = if el.fields.is_empty() { Position::new(at.line, at.character + 2 + utf16(&open)) } else { Position::new(at.line + 1, utf16(&indent) + utf16(&open)) };
    let value_range = Range::new(start, Position::new(start.line, start.character + utf16(&value)));
    let reference = attr.kind == AttrKind::Association && given.is_none();
    Some(Addition { file: file.name.to_string(), edit: TextEdit::new(Range::new(at, at), new_text), value: value_range, reference })
}

/// `text` as XML character data.
fn escape(text: &str) -> String {
    text.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;")
}

/// A reference to object `target` as written: `#_id`, or an IRI as it is.
fn reference(target: &str) -> String {
    if target.contains(':') { target.to_string() } else { format!("#{target}") }
}

/// An enumeration's value as its IRI ends: `WindingConnection.D`, from `D` or the whole name.
fn enum_value<'v>(range: &str, value: &'v str) -> std::borrow::Cow<'v, str> {
    let value = value.rsplit_once('#').map_or(value, |(_, v)| v);
    if value.contains('.') { value.into() } else { format!("{range}.{value}").into() }
}

/// Set field `key` of object `id` to `value`: a literal's text (escaped here), a reference's
/// object (its mRID, or an IRI), or an enumeration's value (`D`, or `WindingConnection.D`).
/// Wherever a file writes the field, its value is replaced; where none does, it is added as
/// [`add_missing`] adds it, `prefer` first. An error if the object's class does not declare it.
pub fn set_field<'a>(files: &[File<'a>], prefer: &str, id: &str, key: &str, value: &str) -> Result<Edits<'a>, String> {
    let reg = type_registry();
    let (def_file, def) = files
        .iter()
        .flat_map(|f| f.index.elements.iter().filter(|e| e.mrid == id).map(move |e| (f, e)))
        .max_by_key(|(_, e)| e.id_kind == Some(IdKind::Id))
        .ok_or_else(|| format!("no file writes `{id}`"))?;
    let class = def_file.index.class_of(&def.qname).ok_or_else(|| format!("`{}` is no class of the tables", def.qname))?;
    let attr = reg.attr(class, key).ok_or_else(|| format!("{} has no field {key}", class.local))?;
    let mut changes: Edits<'a> = Vec::new();
    for file in files {
        let mut edits = Vec::new();
        for el in file.index.elements.iter().filter(|e| e.mrid == id) {
            for f in el.fields.iter().filter(|f| f.key() == key) {
                let edit = match (attr.kind, &f.resource) {
                    (AttrKind::Literal, None) => match file.field_text(f.head, f.full, &f.qname) {
                        Some((_, r)) => TextEdit::new(r, escape(value)),
                        // `<x/>`: written anew.
                        None => TextEdit::new(f.full, format!("<{q}>{}</{q}>", escape(value), q = f.qname)),
                    },
                    (AttrKind::Association, Some((_, r))) => TextEdit::new(*r, reference(value)),
                    (AttrKind::Enum, Some((_, r))) => {
                        let start = file.lines.offset(file.text, r.start);
                        let end = file.lines.offset(file.text, r.end);
                        let written = &file.text[start..end];
                        // Its namespace as written, else the attribute's.
                        let ns = written.rsplit_once('#').map_or(attr.value_ns.to_string(), |(ns, _)| format!("{ns}#"));
                        TextEdit::new(*r, format!("{ns}{}", enum_value(attr.range, value)))
                    }
                    _ => return Err(format!("{key} is written in {} as it cannot be set", file.name)),
                };
                edits.push(edit);
            }
        }
        if !edits.is_empty() {
            changes.push((file.name, edits));
        }
    }
    if changes.is_empty() {
        let added = addition(files, prefer, id, key, Some(value)).ok_or_else(|| format!("no file of the set takes {key} for `{id}`"))?;
        let name = files.iter().find(|f| f.name == added.file).map(|f| f.name).expect("one of the files");
        changes.push((name, vec![added.edit]));
    }
    Ok(changes)
}

/// Remove field `key` from object `id` in every file writing it, its lines with it.
pub fn remove_field<'a>(files: &[File<'a>], id: &str, key: &str) -> Edits<'a> {
    let mut changes = Vec::new();
    for file in files {
        let edits: Vec<TextEdit> = file
            .index
            .elements
            .iter()
            .filter(|e| e.mrid == id)
            .flat_map(|e| e.fields.iter().filter(|f| f.key() == key))
            .map(|f| TextEdit::new(file.lines_of(f.full), String::new()))
            .collect();
        if !edits.is_empty() {
            changes.push((file.name, edits));
        }
    }
    changes
}

/// A field of `class` written as an element of `file`, its value as [`set_field`] takes it.
pub(crate) fn field_element(file: &File, class: &'static cimmodel::base::ClassDef, key: &str, value: &str) -> Result<String, String> {
    let attr = type_registry().attr(class, key).ok_or_else(|| format!("{} has no field {key}", class.local))?;
    let prefix = file.index.prefix_of(attr.ns).ok_or_else(|| format!("{} binds no prefix to {}", file.name, attr.ns))?;
    let tag = format!("{prefix}:{}", attr.id);
    Ok(match attr.kind {
        AttrKind::Literal => format!("<{tag}>{}</{tag}>", escape(value)),
        AttrKind::Association => format!("<{tag} rdf:resource=\"{}\" />", reference(value)),
        AttrKind::Enum => format!("<{tag} rdf:resource=\"{}{}\" />", attr.value_ns, enum_value(attr.range, value)),
    })
}

/// `lines` (an element, unindented) inserted before the end tag of `file`'s root, indented as its
/// elements are, in its line endings.
pub(crate) fn at_end(file: &File, lines: &[String]) -> Result<TextEdit, String> {
    let text = file.text;
    let eol = if text.contains("\r\n") { "\r\n" } else { "\n" };
    let root_end = text.rfind("</").ok_or("the file has no root end tag")?;
    let line_start = text[..root_end].rfind('\n').map_or(0, |i| i + 1);
    let indent = file.index.elements.last().map_or("  ".to_string(), |el| {
        let head = file.lines.offset(text, el.head.start);
        text[text[..head].rfind('\n').map_or(0, |i| i + 1)..head].to_string()
    });
    let at = file.lines.position(text, line_start);
    let body: String = lines.iter().map(|l| format!("{indent}{l}{eol}")).collect();
    Ok(TextEdit::new(Range::new(at, at), body))
}

/// A fresh mRID, `_` and a UUID from `random`, that the set does not have yet nor `taken`.
pub(crate) fn fresh(files: &[File], taken: &HashSet<String>, random: &mut dyn FnMut() -> [u8; 16]) -> String {
    loop {
        let id = format!("_{}", uuid(random()));
        if !taken.contains(&id) && !files.iter().any(|f| f.index.elements.iter().any(|e| e.mrid == id)) {
            return id;
        }
    }
}

/// A new object of `class`, with `values` (field, value as [`set_field`] takes it) and its
/// mRID as text where the class has a field for it (`IdentifiedObject.mRID`, or its own), written at the end of the file `into`, with an
/// `rdf:ID` whose UUID is drawn from `random`. For objects in no container, which [`add`] cannot
/// place: a response, a dependency.
pub fn create<'a>(
    files: &[File<'a>],
    into: &str,
    class: &str,
    values: &[(&str, &str)],
    random: &mut dyn FnMut() -> [u8; 16],
) -> Result<Added<'a>, String> {
    let reg = type_registry();
    let file = files.iter().find(|f| f.name == into).ok_or_else(|| format!("no file {into}"))?;
    // The class by the namespaces the file binds: CGMES's or NC's, whichever has it.
    let found = file
        .index
        .namespaces
        .iter()
        .filter_map(|(_, ns)| reg.ns_table(ns)?.get(class).copied())
        .next()
        .ok_or_else(|| format!("{into} binds no namespace with a class {class}"))?;
    let prefix = file.index.prefix_of(found.ns).ok_or("no prefix")?;
    let id = fresh(files, &HashSet::new(), random);
    let mut lines = vec![format!("<{prefix}:{} rdf:ID=\"{id}\">", found.local)];
    // Its mRID as text: `IdentifiedObject.mRID`, or a class's own (`…Response.mRID`).
    let mrid_key = reg.attrs(found).map(|a| a.id).find(|k| k.ends_with(".mRID"));
    if let Some(key) = mrid_key.filter(|k| !values.iter().any(|(v, _)| v == k)) {
        lines.push(format!("  {}", field_element(file, found, key, &id[1..])?));
    }
    for (key, value) in values {
        lines.push(format!("  {}", field_element(file, found, key, value)?));
    }
    lines.push(format!("</{prefix}:{}>", found.local));
    let edit = at_end(file, &lines)?;
    let first = edit.range.start.line;
    let select = Range::new(Position::new(first, 0), Position::new(first, 0));
    Ok(Added { edits: vec![(file.name, vec![edit])], file: file.name.to_string(), select, id })
}

/// The text of a new file of profile `code` (`DL`) beside `like`: its
/// prolog and root, with their namespaces, and a FullModel header under the
/// model id `urn:uuid:{id}` dependent on `like`'s model, with its times,
/// version and modelling authority set.
pub fn companion(like: &File, code: &str, id: &str) -> Result<String, String> {
    let profile = type_registry()
        .schema("cgmes")
        .and_then(|s| s.profiles.iter().find(|(c, _)| *c == code))
        .map(|(_, uri)| *uri)
        .ok_or_else(|| format!("no profile {code}"))?;
    let text = like.text;
    let root_end = text
        .find("<rdf:RDF")
        .and_then(|s| text[s..].find('>').map(|e| s + e + 1))
        .ok_or_else(|| format!("{} has no rdf:RDF root", like.name))?;
    let header = like
        .index
        .elements
        .iter()
        .find(|e| local(&e.qname) == "FullModel")
        .ok_or_else(|| format!("{} has no FullModel header", like.name))?;
    let eol = if text.contains("\r\n") { "\r\n" } else { "\n" };
    let start = like.lines.offset(text, header.full.start);
    let indent = text[..start].rsplit_once('\n').map_or("", |(_, i)| i);
    let prefix = header.qname.split_once(':').map_or("md", |(p, _)| p);
    let model = if header.mrid.contains(':') { header.mrid.clone() } else { format!("#{}", header.mrid) };
    let mut lines = vec![format!("<{prefix}:FullModel rdf:about=\"urn:uuid:{id}\">")];
    for f in &header.fields {
        if matches!(f.key(), "Model.created" | "Model.scenarioTime" | "Model.version" | "Model.modelingAuthoritySet") {
            lines.push(format!("  {}", &text[like.lines.offset(text, f.full.start)..like.lines.offset(text, f.full.end)]));
        }
    }
    lines.push(format!("  <{prefix}:Model.profile>{profile}</{prefix}:Model.profile>"));
    lines.push(format!("  <{prefix}:Model.DependentOn rdf:resource=\"{model}\" />"));
    lines.push(format!("</{prefix}:FullModel>"));
    let body: String = lines.iter().map(|l| format!("{indent}{l}{eol}")).collect();
    Ok(format!("{}{eol}{body}</rdf:RDF>{eol}", &text[..root_end]))
}

/// An object a diagram draws: the mRID it shows, its name, and its points.
pub type Drawn<'a> = (&'a str, &'a str, Vec<(f64, f64)>);

/// A DL diagram written into `into`: a new `Diagram` named `name` (y
/// downwards), or in `existing` (in the file defining it, whatever `into`
/// says), its drawing of what `objects` show replaced and the rest kept. Each of `objects` (the mRID it shows, its name,
/// its points) a `DiagramObject` with its `DiagramObjectPoint`s, numbered
/// from 1. UUIDs from `random`. The diagram's mRID is the result's `id`.
pub fn diagram<'a>(
    files: &[File<'a>],
    into: &str,
    existing: Option<&str>,
    name: &str,
    objects: &[Drawn],
    random: &mut dyn FnMut() -> [u8; 16],
) -> Result<Added<'a>, String> {
    let defines = |f: &&File, id: &str| f.index.elements.iter().any(|e| e.mrid == id && e.id_kind == Some(IdKind::Id));
    let file = match existing {
        Some(d) => files.iter().find(|f| defines(f, d)).ok_or_else(|| format!("no file defines the diagram {d}"))?,
        None => files.iter().find(|f| f.name == into).ok_or_else(|| format!("no file {into}"))?,
    };
    let reg = type_registry();
    let class = |local: &str| {
        file.index
            .namespaces
            .iter()
            .filter_map(|(_, ns)| reg.ns_table(ns)?.get(local).copied())
            .next()
            .ok_or_else(|| format!("{} binds no namespace with a class {local}", file.name))
    };
    let (diagram_class, object_class, point_class) = (class("Diagram")?, class("DiagramObject")?, class("DiagramObjectPoint")?);
    let mut taken = HashSet::new();
    let mut fresh_id = |random: &mut dyn FnMut() -> [u8; 16]| {
        let id = fresh(files, &taken, random);
        taken.insert(id.clone());
        id
    };
    let mut edits: Edits<'a> = Vec::new();
    let mut lines = Vec::new();
    let diagram = match existing {
        Some(d) => {
            // Its objects of what is drawn anew go, with their points; the
            // diagram stays, with its name and the rest.
            let anew: HashSet<&str> = objects.iter().map(|(m, _, _)| *m).collect();
            let shows = |e: &crate::index::Element| {
                e.fields.iter().any(|f| f.key() == "DiagramObject.IdentifiedObject" && f.resource.as_ref().is_some_and(|(m, _)| anew.contains(m.as_str())))
            };
            let shown: HashSet<&str> = file
                .index
                .elements
                .iter()
                .filter(|e| local(&e.qname) == "DiagramObject")
                .filter(|e| e.fields.iter().any(|f| f.key() == "DiagramObject.Diagram" && f.resource.as_ref().is_some_and(|(m, _)| m == d)))
                .filter(|e| shows(e))
                .map(|e| e.mrid.as_str())
                .collect();
            let mut gone: HashSet<&str> = shown.clone();
            for e in files.iter().flat_map(|f| &f.index.elements) {
                if local(&e.qname) == "DiagramObjectPoint"
                    && e.fields.iter().any(|f| f.resource.as_ref().is_some_and(|(m, _)| shown.contains(m.as_str())))
                {
                    gone.insert(e.mrid.as_str());
                }
            }
            edits = delete(files, &gone).edits;
            d.to_string()
        }
        None => {
            let id = fresh_id(random);
            lines.extend(element(file, diagram_class, &id, &[("IdentifiedObject.name", name), ("Diagram.orientation", "negative")])?);
            id
        }
    };
    for (shown, label, points) in objects {
        let id = fresh_id(random);
        let mut values = vec![("DiagramObject.Diagram", diagram.as_str()), ("DiagramObject.IdentifiedObject", *shown), ("DiagramObject.rotation", "0")];
        if !label.is_empty() {
            values.insert(0, ("IdentifiedObject.name", *label));
        }
        lines.extend(element(file, object_class, &id, &values)?);
        for (k, (x, y)) in points.iter().enumerate() {
            let point = fresh_id(random);
            let (k, x, y) = ((k + 1).to_string(), number(*x), number(*y));
            lines.extend(element(
                file,
                point_class,
                &point,
                &[("DiagramObjectPoint.sequenceNumber", &k), ("DiagramObjectPoint.xPosition", &x), ("DiagramObjectPoint.yPosition", &y), ("DiagramObjectPoint.DiagramObject", &id)],
            )?);
        }
    }
    let edit = at_end(file, &lines)?;
    let first = edit.range.start.line;
    match edits.iter_mut().find(|(n, _)| *n == file.name) {
        Some((_, es)) => es.push(edit),
        None => edits.push((file.name, vec![edit])),
    }
    let select = Range::new(Position::new(first, 0), Position::new(first, 0));
    Ok(Added { edits, file: file.name.to_string(), select, id: diagram })
}

/// A coordinate as DL writes it: to a hundredth, without trailing zeros.
fn number(x: f64) -> String {
    let s = format!("{:.2}", (x * 100.0).round() / 100.0);
    let s = s.trim_end_matches('0').trim_end_matches('.');
    if s == "-0" { "0".into() } else { s.into() }
}

/// An object's element, unindented: its `rdf:ID`, its mRID as text where the class has a field for
/// it, and `values` as [`set_field`] takes them.
pub(crate) fn element(file: &File, class: &'static cimmodel::base::ClassDef, id: &str, values: &[(&str, &str)]) -> Result<Vec<String>, String> {
    let prefix = file.index.prefix_of(class.ns).ok_or("no prefix")?;
    let mut lines = vec![format!("<{prefix}:{} rdf:ID=\"{id}\">", class.local)];
    let mrid_key = type_registry().attrs(class).map(|a| a.id).find(|k| k.ends_with(".mRID"));
    if let Some(key) = mrid_key.filter(|k| !values.iter().any(|(v, _)| v == k)) {
        lines.push(format!("  {}", field_element(file, class, key, &id[1..])?));
    }
    for (key, value) in values {
        lines.push(format!("  {}", field_element(file, class, key, value)?));
    }
    lines.push(format!("</{prefix}:{}>", class.local));
    Ok(lines)
}

/// Objects copied: the edits, and each object's new mRID.
pub struct Copied<'a> {
    pub edits: Edits<'a>,
    /// (original, copy), in the order given.
    pub ids: Vec<(String, String)>,
}

/// Copy objects `ids` with new mRIDs (`_` and a UUID from `random`): every element writing each
/// of them, in every file or only in `from`, its `rdf:ID` or `rdf:about`, its mRID field, and its
/// references to the objects copied, rewritten to the copies; references to anything else stay.
/// Each copy goes after its original, or with `into`, at the end of that file, which must bind
/// the prefixes the copies use as their files do. `from` matters where two files define the same
/// objects (a proposal and what was agreed of it): their copies would be one object written twice.
pub fn copy<'a>(
    files: &[File<'a>],
    ids: &[&str],
    from: Option<&str>,
    into: Option<&str>,
    random: &mut dyn FnMut() -> [u8; 16],
) -> Result<Copied<'a>, String> {
    let sources = |f: &&File| from.is_none_or(|n| f.name == n);
    let target = match into {
        Some(name) => Some(files.iter().find(|f| f.name == name).ok_or_else(|| format!("no file {name}"))?),
        None => None,
    };
    let mut taken = HashSet::new();
    let mut renamed: Vec<(String, String)> = Vec::new();
    for id in ids {
        if !files.iter().filter(sources).any(|f| f.index.elements.iter().any(|e| e.mrid == *id)) {
            return Err(format!("no file {}writes `{id}`", from.map_or(String::new(), |n| format!("but {n} "))));
        }
        let new = fresh(files, &taken, random);
        taken.insert(new.clone());
        renamed.push((id.to_string(), new));
    }
    let to: HashMap<&str, &str> = renamed.iter().map(|(o, n)| (o.as_str(), n.as_str())).collect();
    let mut changes: Edits<'a> = Vec::new();
    let mut moved: Vec<String> = Vec::new();
    for file in files.iter().filter(sources) {
        let text = file.text;
        let mut edits = Vec::new();
        for el in file.index.elements.iter().filter(|e| to.contains_key(e.mrid.as_str())) {
            if let Some(t) = target.filter(|t| t.name != file.name) {
                // The copy's prefixes, bound as they are here.
                let qnames = std::iter::once(&el.qname).chain(el.fields.iter().map(|f| &f.qname));
                for q in qnames {
                    let p = crate::index::prefix(q);
                    if t.index.namespace(p) != file.index.namespace(p) {
                        return Err(format!("{} binds `{p}` otherwise than {}", t.name, file.name));
                    }
                }
            }
            let block = file.lines_of(el.full);
            let (start, end) = (file.lines.offset(text, block.start), file.lines.offset(text, block.end));
            // Replacements within the block, by offset.
            let mut swaps: Vec<(usize, usize, String)> = Vec::new();
            // An mRID at the end of the value written at `r`.
            let suffix = |r: Range, old: &str, new: &str| {
                let at = file.lines.offset(text, r.end);
                (at - old.len(), at, new.to_string())
            };
            let old = el.mrid.as_str();
            let new = to[old];
            if let Some(r) = el.id_range {
                swaps.push(suffix(r, old, new));
            }
            for f in &el.fields {
                if let Some((target_id, r)) = &f.resource
                    && let Some(n) = to.get(target_id.as_str())
                {
                    swaps.push(suffix(*r, target_id, n));
                }
                if f.key().ends_with(".mRID")
                    && let Some((value, r)) = file.field_text(f.head, f.full, &f.qname)
                {
                    let bare = |s: &str| s.strip_prefix('_').unwrap_or(s).to_string();
                    let (vs, ve) = (file.lines.offset(text, r.start), file.lines.offset(text, r.end));
                    if value.trim() == old {
                        swaps.push((vs, ve, new.to_string()));
                    } else if value.trim() == bare(old) {
                        swaps.push((vs, ve, bare(new)));
                    }
                }
            }
            swaps.sort_by_key(|s| std::cmp::Reverse(s.0));
            let mut copied = text[start..end].to_string();
            for (s, e, with) in swaps {
                copied.replace_range(s - start..e - start, &with);
            }
            if !copied.ends_with('\n') {
                copied.push_str(if text.contains("\r\n") { "\r\n" } else { "\n" });
            }
            match target {
                Some(_) => moved.push(copied),
                None => edits.push(TextEdit::new(Range::new(block.end, block.end), copied)),
            }
        }
        if !edits.is_empty() {
            changes.push((file.name, edits));
        }
    }
    if let Some(t) = target {
        let root_end = t.text.rfind("</").ok_or("the file has no root end tag")?;
        let line_start = t.text[..root_end].rfind('\n').map_or(0, |i| i + 1);
        let at = t.lines.position(t.text, line_start);
        let edit = TextEdit::new(Range::new(at, at), moved.concat());
        match changes.iter_mut().find(|(n, _)| *n == t.name) {
            Some((_, e)) => e.push(edit),
            None => changes.push((t.name, vec![edit])),
        }
    }
    Ok(Copied { edits: changes, ids: renamed })
}

/// An object a reference can name.
pub struct Candidate {
    pub id: String,
    pub label: String,
    pub class: &'static str,
    /// Its containers' labels, as the outline shows them.
    pub place: String,
}

impl Candidate {
    /// The reference to it as written: `#_id`, or an IRI as it is.
    pub fn reference(&self) -> String {
        if self.id.contains(':') { self.id.clone() } else { format!("#{}", self.id) }
    }
}

/// The objects field `key` of object `id` can name — of the class it ranges
/// over, or below — at most `limit`, the likeliest first: those linked to
/// the object, naming what it names with its other fields or named by it
/// (a terminal's connectivity node, of its topological node; equipment's
/// base voltage, its voltage level's); then those that share the most
/// containers with it, by where they are or by where the objects are that
/// name them with the same field (a line's base voltage is the one the
/// equipment of its voltage level names); then the most named.
/// Empty if `key` is not a reference of the object's class.
pub fn candidates(indexes: &[&Index], tree: &Tree, id: &str, key: &str, limit: usize) -> Vec<Candidate> {
    let reg = type_registry();
    let class = indexes
        .iter()
        .flat_map(|ix| ix.elements.iter().filter(|e| e.mrid == id).map(move |e| (ix, e)))
        .max_by_key(|(_, e)| e.id_kind == Some(IdKind::Id))
        .and_then(|(ix, e)| ix.class_of(&e.qname));
    let Some(attr) = class.and_then(|c| reg.attr(c, key)).filter(|a| a.kind == AttrKind::Association) else { return Vec::new() };
    // What the object names with its other fields.
    let own: HashSet<&str> = indexes
        .iter()
        .flat_map(|ix| ix.elements.iter().filter(|e| e.mrid == id))
        .flat_map(|e| e.fields.iter().filter(|f| f.key() != key).filter_map(|f| f.resource.as_ref().map(|(t, _)| t.as_str())))
        .filter(|t| !t.is_empty())
        .collect();
    let mut linked: HashSet<&str> = HashSet::new();
    let mut found: HashMap<&str, (Option<&str>, &'static str)> = HashMap::new();
    let mut named: HashMap<&str, Vec<&str>> = HashMap::new();
    for ix in indexes {
        for el in &ix.elements {
            let targets = || el.fields.iter().filter_map(|f| f.resource.as_ref().map(|(t, _)| t.as_str()));
            if own.contains(el.mrid.as_str()) {
                linked.extend(targets());
            } else if targets().any(|t| own.contains(t)) {
                linked.insert(el.mrid.as_str());
            }
            for f in el.fields.iter().filter(|f| f.key() == key && el.mrid != id) {
                if let Some((target, _)) = &f.resource {
                    named.entry(target.as_str()).or_default().push(el.mrid.as_str());
                }
            }
            if el.mrid.is_empty() || el.mrid == id {
                continue;
            }
            let Some(c) = ix.class_of(&el.qname).filter(|c| reg.chain(c).iter().any(|a| a.local == attr.range)) else { continue };
            let entry = found.entry(el.mrid.as_str()).or_insert((None, c.local));
            if entry.0.is_none() {
                entry.0 = el.name.as_deref().filter(|n| !n.is_empty());
            }
        }
    }
    let here = {
        let mut chain = tree.chain(id);
        chain.pop();
        chain
    };
    let shared = |chain: &[u32]| here.iter().zip(chain).take_while(|(a, b)| a == b).count();
    let mut ranked: Vec<(bool, usize, usize, String, &str, &'static str)> = found
        .into_iter()
        .map(|(c, (name, class))| {
            let by = named.get(c).map_or(&[][..], Vec::as_slice);
            let near = by.iter().map(|r| {
                let mut chain = tree.chain(r);
                chain.pop();
                shared(&chain)
            });
            let score = near.chain(std::iter::once(shared(&tree.chain(c)))).max().unwrap_or(0);
            (linked.contains(c), score, by.len(), name.unwrap_or(c).to_string(), c, class)
        })
        .collect();
    ranked.sort_by(|a, b| b.0.cmp(&a.0).then(b.1.cmp(&a.1)).then(b.2.cmp(&a.2)).then_with(|| a.3.to_lowercase().cmp(&b.3.to_lowercase())));
    ranked
        .into_iter()
        .take(limit)
        .map(|(_, _, _, label, c, class)| Candidate { id: c.to_string(), label, class, place: tree.containers(c).join(" › ") })
        .collect()
}

/// A class that can be added in an object, and the field naming it there.
pub struct Addable {
    pub class: &'static cimmodel::base::ClassDef,
    pub key: &'static str,
}

/// The class of object `id` as the file defining it writes it.
fn class_in(files: &[File], id: &str) -> Option<&'static cimmodel::base::ClassDef> {
    let (file, el) = files
        .iter()
        .flat_map(|f| f.index.elements.iter().filter(|e| e.mrid == id).map(move |e| (f, e)))
        .max_by_key(|(_, e)| e.id_kind == Some(IdKind::Id))?;
    file.index.class_of(&el.qname)
}

/// The concrete classes of `parent`'s family whose objects the containment
/// puts in it: the first of [`CONTAINED`] they declare that
/// ranges over its class. Only classes of the profiles the set's files
/// declare (all, if none declares one).
pub fn addable(files: &[File], parent: &str) -> Vec<Addable> {
    let reg = type_registry();
    let Some(class) = class_in(files, parent) else { return Vec::new() };
    let Some(schema) = reg.family_of(class.qualified).and_then(|f| reg.schema(f)) else { return Vec::new() };
    let is = |range: &str| reg.chain(class).iter().any(|c| c.local == range);
    let declared: HashSet<&str> = files.iter().flat_map(profiles).collect();
    let mut out: Vec<Addable> = schema
        .classes
        .iter()
        .filter(|c| c.concrete && (declared.is_empty() || c.origins.iter().any(|o| declared.contains(o))))
        .filter_map(|c| {
            let key = CONTAINED.iter().find_map(|k| reg.attr(c, k).filter(|a| a.used && a.kind == AttrKind::Association))?;
            is(key.range).then_some(Addable { class: c, key: key.id })
        })
        .collect();
    out.sort_by_key(|a| a.class.local);
    out
}

/// A version 4 UUID from 16 random bytes, which the caller draws: the standard
/// library has no randomness on every target this runs on.
pub fn uuid(mut b: [u8; 16]) -> String {
    b[6] = (b[6] & 0x0f) | 0x40;
    b[8] = (b[8] & 0x3f) | 0x80;
    let hex: String = b.iter().map(|x| format!("{x:02x}")).collect();
    format!("{}-{}-{}-{}-{}", &hex[..8], &hex[8..12], &hex[12..16], &hex[16..20], &hex[20..])
}

/// An object added: the edits, and where its name (else its start tag) is.
pub struct Added<'a> {
    pub edits: Edits<'a>,
    /// The file its element is in, by its name.
    pub file: String,
    pub select: Range,
    pub id: String,
}

/// Add an object of `class` in `parent`, named `name` if given: an
/// `rdf:ID`, its mRID, name and the field naming `parent`, in the file
/// whose profile carries the class (the parent's own first), after the last
/// of the parent's objects there, else after the parent, else at the end;
/// and where the set has an SSH file and the class SSH attributes, an empty
/// `rdf:about` there for them. What else it needs, validation asks for. Its
/// mRID is `_` and a UUID from `random` ([`uuid`]), drawn again while the set
/// has it.
pub fn add<'a>(
    files: &[File<'a>],
    tree: &Tree,
    parent: &str,
    class: &str,
    name: Option<&str>,
    random: &mut dyn FnMut() -> [u8; 16],
) -> Result<Added<'a>, String> {
    let reg = type_registry();
    let found = addable(files, parent);
    let Addable { class, key } = found.into_iter().find(|a| a.class.local == class).ok_or_else(|| format!("a {class} cannot be added there"))?;
    let fits = |f: &File| {
        let declared = profiles(f);
        declared.is_empty() || declared.iter().any(|p| class.origins.contains(p))
    };
    let defines = |f: &File, id: &str| f.index.elements.iter().any(|e| e.mrid == id && e.id_kind == Some(IdKind::Id));
    let main = files
        .iter()
        .filter(|f| fits(f) && !profiles(f).contains(&"SSH"))
        .max_by_key(|f| defines(f, parent))
        .ok_or_else(|| format!("no file of the model set has a profile for {}", class.local))?;
    let mut id = format!("_{}", uuid(random()));
    while files.iter().any(|f| f.index.elements.iter().any(|e| e.mrid == id)) {
        id = format!("_{}", uuid(random()));
    }

    let prefix = |f: &File, ns: &str| f.index.prefix_of(ns).map(str::to_string);
    let tag = format!("{}:{}", prefix(main, class.ns).ok_or("the file binds no prefix to the class's namespace")?, class.local);
    let field = |f: &File, key: &str| reg.attr(class, key).and_then(|a| Some((a, format!("{}:{}", prefix(f, a.ns)?, a.id))));
    let carries = |f: &File, key: &str| {
        let declared = profiles(f);
        reg.attr(class, key).is_some_and(|a| declared.is_empty() || a.origins.iter().any(|o| declared.contains(o)))
    };
    let mut lines = vec![format!("<{tag} rdf:ID=\"{id}\">")];
    let mut select_line = None;
    if carries(main, "IdentifiedObject.mRID")
        && let Some((_, t)) = field(main, "IdentifiedObject.mRID")
    {
        lines.push(format!("  <{t}>{}</{t}>", &id[1..]));
    }
    if let Some(name) = name.map(str::trim).filter(|n| !n.is_empty())
        && let Some((_, t)) = field(main, "IdentifiedObject.name")
    {
        select_line = Some((lines.len(), t.len() + 4, name.encode_utf16().count()));
        let escaped = name.replace('&', "&amp;").replace('<', "&lt;");
        lines.push(format!("  <{t}>{escaped}</{t}>"));
    }
    let (_, container) = field(main, key).ok_or("the file binds no prefix to the field's namespace")?;
    let target = if parent.contains(':') { parent.to_string() } else { format!("#{parent}") };
    lines.push(format!("  <{container} rdf:resource=\"{target}\" />"));
    lines.push(format!("</{tag}>"));

    let mut changes: Edits<'a> = Vec::new();
    let (at, line_no) = insertion(main, tree, parent, &lines)?;
    let select = match select_line {
        Some((l, col, len)) => {
            let start = Position::new(line_no + l as u32, at.1 + col as u32);
            Range::new(start, Position::new(start.line, start.character + len as u32))
        }
        None => Range::new(Position::new(line_no, at.1), Position::new(line_no, at.1)),
    };
    changes.push((main.name, vec![at.0]));
    // Its steady-state part.
    if class.origins.contains(&"SSH")
        && let Some(ssh) = files.iter().find(|f| profiles(f).contains(&"SSH"))
        && let Some(p) = prefix(ssh, class.ns)
    {
        let tag = format!("{p}:{}", class.local);
        let ssh_lines = vec![format!("<{tag} rdf:about=\"#{id}\">"), format!("</{tag}>")];
        let (edit, _) = insertion(ssh, tree, parent, &ssh_lines)?;
        changes.push((ssh.name, vec![edit.0]));
    }
    Ok(Added { edits: changes, file: main.name.to_string(), select, id })
}

/// Where to insert `lines` (an element, unindented) in `file`: after the
/// last element there of an object `parent` holds, else after `parent`'s,
/// else before the root's end tag; indented as the elements are, in the
/// file's line endings. The edit with the indent's width, and the line the
/// element starts on.
pub(crate) fn insertion(file: &File, tree: &Tree, parent: &str, lines: &[String]) -> Result<((TextEdit, u32), u32), String> {
    let (text, li) = (file.text, file.lines);
    let eol = if text.contains("\r\n") { "\r\n" } else { "\n" };
    let siblings: HashSet<&str> = tree.children(parent).into_iter().collect();
    let anchor = file.index.elements.iter().rev().find(|e| siblings.contains(e.mrid.as_str()))
        .or_else(|| file.index.elements.iter().rev().find(|e| e.mrid == parent));
    let (offset, indent) = match anchor {
        Some(el) => {
            let end = li.offset(text, el.full.end);
            let line_end = text[end..].find('\n').map_or(text.len(), |i| end + i + 1);
            let head = li.offset(text, el.head.start);
            let line_start = text[..head].rfind('\n').map_or(0, |i| i + 1);
            (line_end, text[line_start..head].to_string())
        }
        None => {
            let root_end = text.rfind("</").ok_or("the file has no root end tag")?;
            let line_start = text[..root_end].rfind('\n').map_or(0, |i| i + 1);
            let indent = file.index.elements.last().map_or("  ".to_string(), |el| {
                let head = li.offset(text, el.head.start);
                text[text[..head].rfind('\n').map_or(0, |i| i + 1)..head].to_string()
            });
            (line_start, indent)
        }
    };
    // An anchor ending the file without a newline: start a line first.
    let lead = if offset == text.len() && !text.ends_with('\n') { eol } else { "" };
    let body: String = lines.iter().map(|l| format!("{indent}{l}{eol}")).collect();
    let at = li.position(text, offset);
    let first = if lead.is_empty() { at.line } else { at.line + 1 };
    Ok(((TextEdit::new(Range::new(at, at), format!("{lead}{body}")), indent.encode_utf16().count() as u32), first))
}

/// A fix for a reference to nothing (`rdf:resource="#"`) or to an object the
/// set lacks: name one of the likeliest objects instead.
pub struct ReferenceFix {
    pub key: String,
    /// The file it is in, by its name.
    pub file: String,
    pub edit: TextEdit,
    pub title: String,
    /// It names nothing; else something the set does not have.
    pub empty: bool,
}

/// The fixes for object `id`'s references to nothing or to objects the set
/// lacks, in every file that writes it, or only the field at `at`: each the
/// five likeliest objects ([`candidates`]); `at` names a file and a place in
/// it. An enumeration value is not a reference to an object, so it has none.
pub fn reference_fixes(files: &[File], tree: &Tree, id: &str, at: Option<(&str, Position)>) -> Vec<ReferenceFix> {
    let sites: Vec<(&File, &crate::index::Field, &str, Range)> = files
        .iter()
        .filter(|f| at.is_none_or(|(n, _)| f.name == n))
        .flat_map(|f| f.index.elements.iter().filter(|e| e.mrid == id).map(move |e| (f, e)))
        .flat_map(|(f, e)| e.fields.iter().map(move |x| (f, x)))
        .filter(|(_, x)| at.is_none_or(|(_, pos)| contains(x.full, pos)))
        .filter_map(|(f, x)| x.resource.as_ref().map(|(t, r)| (f, x, t.as_str(), *r)))
        .collect();
    if sites.is_empty() {
        return Vec::new();
    }
    let known: HashSet<&str> = files.iter().flat_map(|f| f.index.elements.iter().map(|e| e.mrid.as_str())).collect();
    let indexes: Vec<&Index> = files.iter().map(|f| f.index).collect();
    let mut out = Vec::new();
    for (file, field, target, value) in sites {
        if !target.is_empty() && known.contains(target) {
            continue;
        }
        for c in candidates(&indexes, tree, id, field.key(), 5) {
            let place = if c.place.is_empty() { String::new() } else { format!(" in {}", c.place) };
            out.push(ReferenceFix {
                key: field.key().to_string(),
                file: file.name.to_string(),
                edit: TextEdit::new(value, c.reference()),
                title: format!("Set {} to {} {}{place}", field.key(), c.class, c.label),
                empty: target.is_empty(),
            });
        }
    }
    out
}

/// The objects that refer to object `id`, and by which field, as the files
/// write them: (referrer, field), each once.
pub fn referrers<'a>(files: &[File<'a>], id: &str) -> Vec<(&'a str, &'a str)> {
    let mut out: Vec<(&str, &str)> = files
        .iter()
        .flat_map(|f| f.index.elements.iter())
        .flat_map(|e| e.fields.iter().filter(|x| x.resource.as_ref().is_some_and(|(t, _)| t == id)).map(move |x| (e.mrid.as_str(), x.key())))
        .filter(|(r, _)| !r.is_empty() && *r != id)
        .collect();
    out.sort_unstable();
    out.dedup();
    out
}

/// How an object is named in a title: its class and name, else mRID.
pub fn describe(files: &[File], id: &str) -> String {
    let el = files.iter().flat_map(|f| &f.index.elements).filter(|e| e.mrid == id).max_by_key(|e| e.id_kind == Some(IdKind::Id));
    match el {
        Some(el) => format!("{} {}", local(&el.qname), el.name.as_deref().filter(|n| !n.is_empty()).unwrap_or(id)),
        None => id.to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn micro_grid() -> std::path::PathBuf {
        std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../CGMES-Test-Configurations/v3.0/MicroGrid/MicroGid-BaseCase/MicroGrid-BaseCase-Merged")
    }

    #[test]
    fn a_uuid_is_version_4() {
        let id = uuid([0xff; 16]);
        assert_eq!(id.len(), 36);
        assert_eq!(&id[14..15], "4");
        assert!(matches!(&id[19..20], "8" | "9" | "a" | "b"), "{id}");
    }

    const EQ: &str = r##"<rdf:RDF xmlns:cim="http://iec.ch/TC57/CIM100#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <cim:Breaker rdf:ID="_b1">
    <cim:IdentifiedObject.mRID>b1</cim:IdentifiedObject.mRID>
    <cim:IdentifiedObject.name>B1</cim:IdentifiedObject.name>
  </cim:Breaker>
  <cim:Terminal rdf:ID="_t1">
    <cim:Terminal.ConductingEquipment rdf:resource="#_b1" />
    <cim:IdentifiedObject.name>T1</cim:IdentifiedObject.name>
  </cim:Terminal>
</rdf:RDF>"##;
    const SSH: &str = r##"<rdf:RDF xmlns:cim="http://iec.ch/TC57/CIM100#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <cim:Breaker rdf:about="#_b1">
    <cim:Switch.open>false</cim:Switch.open>
  </cim:Breaker>
</rdf:RDF>"##;

    fn apply(text: &str, edits: Vec<TextEdit>) -> String {
        crate::text::apply(text, &edits)
    }

    /// The edits of the file `name`.
    fn of<'a>(edits: &Edits<'a>, name: &str) -> Vec<TextEdit> {
        edits.iter().find(|(n, _)| *n == name).map(|(_, e)| e.clone()).unwrap_or_default()
    }

    fn with_files<R>(f: impl FnOnce(&[File]) -> R) -> R {
        let names = ["EQ.xml", "SSH.xml"];
        let texts = [EQ, SSH];
        let lines: Vec<LineIndex> = texts.iter().map(|t| LineIndex::new(t)).collect();
        let indexes: Vec<Index> = texts.iter().map(|t| Index::build(t)).collect();
        let files: Vec<File> = (0..2).map(|i| File { name: names[i], text: texts[i], lines: &lines[i], index: &indexes[i] }).collect();
        f(&files)
    }

    #[test]
    fn renames_an_mrid_everywhere() {
        with_files(|files| {
            // On the reference, and on the start tag.
            let (r, old) = prepare_rename(files, "EQ.xml", Position::new(6, 55)).unwrap();
            assert_eq!((old.as_str(), r), ("_b1", Range::new(Position::new(6, 53), Position::new(6, 56))));
            assert_eq!(prepare_rename(files, "SSH.xml", Position::new(1, 4)).unwrap().1, "_b1");
            assert!(prepare_rename(files, "EQ.xml", Position::new(2, 10)).is_none());

            let changes = rename(files, "_b1", "_b2").unwrap();
            let eq = apply(EQ, of(&changes, "EQ.xml"));
            assert!(eq.contains(r#"rdf:ID="_b2""#) && eq.contains(">b2</") && eq.contains(r##"rdf:resource="#_b2""##), "{eq}");
            assert!(!eq.contains("b1"), "{eq}");
            assert_eq!(apply(SSH, of(&changes, "SSH.xml")), SSH.replace("#_b1", "#_b2"));

            assert!(rename(files, "_b1", "_t1").is_err());
            assert!(rename(files, "_b1", "a b").is_err());
            assert!(rename(files, "_b1", "1b").is_err());
        });
    }

    #[test]
    fn adds_a_missing_field_where_its_profile_is() {
        let header = |p: &str| format!(r#"  <md:FullModel rdf:about="urn:uuid:{p}">
    <md:Model.profile>http://iec.ch/TC57/ns/CIM/{p}/3.0</md:Model.profile>
  </md:FullModel>
"#);
        let with_header = |text: &str, p: &str| {
            text.replacen(r#"xmlns:rdf"#, r#"xmlns:md="http://iec.ch/TC57/61970-552/ModelDescription/1#" xmlns:rdf"#, 1)
                .replacen(">\n", &format!(">\n{}", header(p)), 1)
        };
        let texts = [with_header(EQ, "CoreEquipment-EU"), with_header(SSH, "SteadyStateHypothesis-EU")];
        let names = ["EQ.xml", "SSH.xml"];
        let lines: Vec<LineIndex> = texts.iter().map(|t| LineIndex::new(t)).collect();
        let indexes: Vec<Index> = texts.iter().map(|t| Index::build(t)).collect();
        let files: Vec<File> = (0..2).map(|i| File { name: names[i], text: &texts[i], lines: &lines[i], index: &indexes[i] }).collect();
        let added = |id: &str, key: &str| {
            let a = add_missing(&files, names[0], id, key)?;
            let i = names.iter().position(|n| *n == a.file).unwrap();
            let out = apply(&texts[i], vec![a.edit.clone()]);
            let selected = apply(&out, vec![TextEdit::new(a.value, "|".into())]);
            Some((i, out, selected))
        };

        // An SSH attribute goes to the SSH file, though asked from the EQ one.
        let (i, out, selected) = added("_b1", "Equipment.inService").unwrap();
        assert_eq!(i, 1);
        assert!(out.contains("<cim:Switch.open>false</cim:Switch.open>\n    <cim:Equipment.inService>false</cim:Equipment.inService>\n  </cim:Breaker>"), "{out}");
        assert!(selected.contains("<cim:Equipment.inService>|</cim:Equipment.inService>"), "{selected}");
        // An EQ one to the EQ file; a reference left to fill in, an enumeration its first value.
        let (i, _, selected) = added("_t1", "Terminal.ConnectivityNode").unwrap();
        assert_eq!(i, 0);
        assert!(selected.contains(r##"<cim:Terminal.ConnectivityNode rdf:resource="#|" />"##), "{selected}");
        let (_, out, _) = added("_t1", "Terminal.phases").unwrap();
        assert!(out.contains(r#"<cim:Terminal.phases rdf:resource="http://iec.ch/TC57/CIM100#PhaseCode."#), "{out}");
        // Written already, or not the class's.
        assert!(added("_b1", "Switch.open").is_none());
        assert!(added("_b1", "ACLineSegment.r").is_none());
    }

    #[test]
    fn sets_and_removes_a_field_wherever_it_is_written() {
        with_files(|files| {
            // A literal, escaped; written in the SSH file.
            let set = set_field(files, "EQ.xml", "_b1", "Switch.open", "true").unwrap();
            assert_eq!(apply(SSH, of(&set, "SSH.xml")), SSH.replace(">false<", ">true<"));
            let named = set_field(files, "EQ.xml", "_b1", "IdentifiedObject.name", "B & 1").unwrap();
            assert!(apply(EQ, of(&named, "EQ.xml")).contains("<cim:IdentifiedObject.name>B &amp; 1</cim:IdentifiedObject.name>"));
            // A reference, to an mRID.
            let to = set_field(files, "EQ.xml", "_t1", "Terminal.ConductingEquipment", "_b2").unwrap();
            assert!(apply(EQ, of(&to, "EQ.xml")).contains(r##"<cim:Terminal.ConductingEquipment rdf:resource="#_b2" />"##));
            // Written nowhere: added, with the value given.
            let added = set_field(files, "EQ.xml", "_t1", "Terminal.phases", "ABC").unwrap();
            assert!(apply(EQ, of(&added, "EQ.xml")).contains(r#"<cim:Terminal.phases rdf:resource="http://iec.ch/TC57/CIM100#PhaseCode.ABC" />"#));
            assert!(set_field(files, "EQ.xml", "_b1", "ACLineSegment.r", "1").is_err());

            let removed = remove_field(files, "_t1", "Terminal.ConductingEquipment");
            let eq = apply(EQ, of(&removed, "EQ.xml"));
            assert!(!eq.contains("ConductingEquipment") && eq.contains("<cim:Terminal rdf:ID=\"_t1\">\n    <cim:IdentifiedObject.name>T1"), "{eq}");
        });
    }

    fn counting() -> impl FnMut() -> [u8; 16] {
        let mut n = 0u8;
        move || {
            n += 1;
            [n; 16]
        }
    }

    #[test]
    fn copies_objects_with_new_mrids_and_their_references_among_them() {
        with_files(|files| {
            let mut random = counting();
            let copied = copy(files, &["_b1", "_t1"], None, None, &mut random).unwrap();
            let (b2, t2) = (format!("_{}", uuid([1; 16])), format!("_{}", uuid([2; 16])));
            assert_eq!(copied.ids, [("_b1".to_string(), b2.clone()), ("_t1".to_string(), t2.clone())]);
            let eq = apply(EQ, of(&copied.edits, "EQ.xml"));
            // The originals stay; the copies follow them, naming each other.
            assert!(eq.contains(r#"<cim:Breaker rdf:ID="_b1">"#) && eq.contains(&format!(r#"<cim:Breaker rdf:ID="{b2}">"#)), "{eq}");
            assert!(eq.contains(&format!("<cim:IdentifiedObject.mRID>{}</cim:IdentifiedObject.mRID>", &b2[1..])), "{eq}");
            assert!(eq.contains(&format!(r##"<cim:Terminal.ConductingEquipment rdf:resource="#{b2}" />"##)), "{eq}");
            assert_eq!(eq.matches(r##"rdf:resource="#_b1""##).count(), 1);
            let ssh = apply(SSH, of(&copied.edits, "SSH.xml"));
            assert!(ssh.contains(&format!(r##"<cim:Breaker rdf:about="#{b2}">"##)), "{ssh}");
            // Only the terminal: its reference to the breaker stays.
            let alone = copy(files, &["_t1"], None, None, &mut random).unwrap();
            assert_eq!(apply(EQ, of(&alone.edits, "EQ.xml")).matches(r##"rdf:resource="#_b1""##).count(), 2);
            // From one file, into another: the breaker's SSH part is not copied.
            let only = copy(files, &["_b1"], Some("EQ.xml"), None, &mut random).unwrap();
            assert_eq!(only.edits.len(), 1, "EQ only");
            let moved = copy(files, &["_t1"], None, Some("SSH.xml"), &mut random).unwrap();
            let ssh = apply(SSH, of(&moved.edits, "SSH.xml"));
            assert!(ssh.contains(&format!(r#"<cim:Terminal rdf:ID="{}">"#, moved.ids[0].1)) && ssh.ends_with("</rdf:RDF>"), "{ssh}");
            assert!(copy(files, &["_nothing"], None, None, &mut random).is_err());
            assert!(copy(files, &["_t1"], Some("SSH.xml"), None, &mut random).is_err());
        });
    }

    #[test]
    fn creates_an_object_of_any_class_with_its_values() {
        with_files(|files| {
            let created = create(files, "EQ.xml", "Breaker", &[("IdentifiedObject.name", "B9"), ("Switch.normalOpen", "true")], &mut counting()).unwrap();
            let eq = apply(EQ, of(&created.edits, "EQ.xml"));
            let id = &created.id;
            assert!(eq.contains(&format!("  <cim:Breaker rdf:ID=\"{id}\">\n    <cim:IdentifiedObject.mRID>{}</cim:IdentifiedObject.mRID>\n    <cim:IdentifiedObject.name>B9</cim:IdentifiedObject.name>\n    <cim:Switch.normalOpen>true</cim:Switch.normalOpen>\n  </cim:Breaker>\n</rdf:RDF>", &id[1..])), "{eq}");
            assert!(create(files, "EQ.xml", "NoSuchClass", &[], &mut counting()).is_err());
            assert!(create(files, "EQ.xml", "Breaker", &[("ACLineSegment.r", "1")], &mut counting()).is_err());
        });
    }

    #[test]
    fn lists_what_refers_to_an_object() {
        with_files(|files| {
            assert_eq!(referrers(files, "_b1"), [("_t1", "Terminal.ConductingEquipment")]);
            assert!(referrers(files, "_t1").is_empty());
        });
    }

    #[test]
    fn deletes_an_object_from_every_file() {
        with_files(|files| {
            let d = delete(files, &HashSet::from(["_b1"]));
            assert_eq!((d.objects, d.references), (1, 1));
            let eq = apply(EQ, of(&d.edits, "EQ.xml"));
            assert!(!eq.contains("Breaker") && !eq.contains("ConductingEquipment") && eq.contains("<cim:Terminal rdf:ID=\"_t1\">\n    <cim:IdentifiedObject.name>T1"), "{eq}");
            assert!(!apply(SSH, of(&d.edits, "SSH.xml")).contains("Breaker"));
        });
    }

    /// On MicroGrid, the object each reference names is among the
    /// candidates offered for it, mostly first.
    #[test]
    fn candidates_rank_what_the_data_names() {
        let read = crate::testing::collect(&micro_grid());
        let set = crate::testing::validate(&read);
        let built: Vec<Index> = read.iter().map(|(_, t)| Index::build(t)).collect();
        let indexes: Vec<&Index> = built.iter().collect();
        let mut stats: HashMap<&str, (usize, usize, usize)> = HashMap::new();
        for ix in &indexes {
            for el in &ix.elements {
                for f in &el.fields {
                    let key = f.key();
                    let Some((target, _)) = &f.resource else { continue };
                    // Not the containment's own fields: the object is where they put it.
                    if !["ConductingEquipment.BaseVoltage", "Terminal.ConnectivityNode", "Terminal.TopologicalNode", "RegulatingControl.Terminal"].contains(&key) {
                        continue;
                    }
                    let c = candidates(&indexes, &set.tree, &el.mrid, key, 5);
                    let s = stats.entry(key).or_default();
                    s.0 += 1;
                    if c.first().is_some_and(|c| c.id == *target) { s.1 += 1 }
                    if c.iter().any(|c| c.id == *target) { s.2 += 1 }
                }
            }
        }
        for (key, (n, first, top5)) in &stats {
            eprintln!("{key}: {n} references, first {first}, top 5 {top5}");
        }
        // Linked through the topological node: always first. A base voltage:
        // a line's, among its sub-region's, not always.
        assert_eq!(stats["Terminal.ConnectivityNode"], (73, 73, 73));
        assert_eq!(stats["Terminal.TopologicalNode"], (73, 73, 73));
        assert_eq!(stats["RegulatingControl.Terminal"], (7, 7, 7));
        assert_eq!(stats["ConductingEquipment.BaseVoltage"], (23, 20, 23));
    }

    /// On MicroGrid: what a voltage level and a transformer can hold; a breaker
    /// added in a voltage level, in EQ with an SSH part, which validates
    /// with findings for what it lacks and sits in the voltage level.
    #[test]
    fn adds_an_object_in_a_container() {
        let read = crate::testing::collect(&micro_grid());
        let set = crate::testing::validate(&read);
        let names: Vec<String> = read.iter().map(|(p, _)| p.to_string_lossy().into_owned()).collect();
        let lines: Vec<LineIndex> = read.iter().map(|(_, t)| LineIndex::new(t)).collect();
        let indexes: Vec<Index> = read.iter().map(|(_, t)| Index::build(t)).collect();
        let files: Vec<File> = (0..read.len()).map(|i| File { name: &names[i], text: &read[i].1, lines: &lines[i], index: &indexes[i] }).collect();
        let find = |class: &str, name: &str| indexes.iter().flat_map(|ix| &ix.elements)
            .find(|e| local(&e.qname) == class && e.name.as_deref() == Some(name)).unwrap().mrid.clone();
        let vl = find("VoltageLevel", "110.0");
        let classes: Vec<&str> = addable(&files, &vl).iter().map(|a| a.class.local).collect();
        for c in ["Breaker", "Bay", "ConnectivityNode", "LinearShuntCompensator"] {
            assert!(classes.contains(&c), "{classes:?}");
        }
        assert!(!classes.contains(&"Terminal"), "{classes:?}");
        let transformer = find("PowerTransformer", "BE-TR2_1");
        let classes: Vec<&str> = addable(&files, &transformer).iter().map(|a| a.class.local).collect();
        assert!(classes.contains(&"Terminal") && classes.contains(&"PowerTransformerEnd"), "{classes:?}");

        let mut draws = 0u8;
        let mut random = || {
            draws += 1;
            [draws; 16]
        };
        let added = add(&files, &set.tree, &vl, "Breaker", Some("B-new"), &mut random).unwrap();
        assert_eq!(added.edits.len(), 2, "EQ and SSH");
        assert_eq!(added.id, format!("_{}", uuid([1; 16])));
        let edited: Vec<(std::path::PathBuf, std::sync::Arc<str>)> = read.iter().enumerate().map(|(i, (p, t))| {
            let e = of(&added.edits, &names[i]);
            (p.clone(), std::sync::Arc::from(if e.is_empty() { t.to_string() } else { apply(t, e) }))
        }).collect();
        let main = names.iter().position(|n| *n == added.file).unwrap();
        assert!(edited[main].0.to_string_lossy().contains("_EQ_"));
        let text = &edited[main].1;
        let selected = apply(text, vec![TextEdit::new(added.select, "|".into())]);
        assert!(selected.contains("<cim:IdentifiedObject.name>|</cim:IdentifiedObject.name>"), "the name is selected");

        let set2 = crate::testing::validate(&edited);
        assert_eq!(set2.tree.containers(&added.id), ["PP_Brussels", "110.0"]);
        let ssh = edited.iter().find(|(p, _)| p.to_string_lossy().contains("_BE_SSH_")).unwrap();
        assert!(ssh.1.contains(&format!("rdf:about=\"#{}\"", added.id)));
    }

    /// A DL file beside MicroGrid's EQ: its namespaces, a header of its own
    /// dependent on EQ's, and room for a diagram, which validates.
    #[test]
    fn a_companion_file_takes_a_diagram() {
        let read = crate::testing::collect(&micro_grid());
        let names: Vec<String> = read.iter().map(|(p, _)| p.to_string_lossy().into_owned()).collect();
        let at = names.iter().position(|n| n.contains("_BE_EQ_")).unwrap();
        let (lines, index) = (LineIndex::new(&read[at].1), Index::build(&read[at].1));
        let eq = File { name: &names[at], text: &read[at].1, lines: &lines, index: &index };
        let text = companion(&eq, "DL", "0d1e2f30-0000-4000-8000-000000000000").unwrap();
        assert!(text.contains("<md:Model.profile>http://iec.ch/TC57/ns/CIM/DiagramLayout-EU/3.0</md:Model.profile>"), "{text}");
        assert!(text.contains(&format!("<md:Model.DependentOn rdf:resource=\"{}\" />", index.elements[0].mrid)), "{text}");
        assert!(!text.contains("CoreEquipment"), "{text}");
        let (dl_lines, dl_index) = (LineIndex::new(&text), Index::build(&text));
        let dl = File { name: "new_DL.xml", text: &text, lines: &dl_lines, index: &dl_index };
        let mut counter = 0u8;
        let mut random = || {
            counter += 1;
            [counter; 16]
        };
        let breaker = index.elements.iter().find(|e| local(&e.qname) == "Breaker").map(|e| e.mrid.clone());
        let shown = breaker.as_deref().unwrap_or("_x");
        let added = diagram(&[dl], "new_DL.xml", None, "One", &[(shown, "B", vec![(1.0, 2.5)])], &mut random).unwrap();
        let written = crate::text::apply(&text, &added.edits[0].1);
        assert!(written.contains("<cim:DiagramObjectPoint.xPosition>1</cim:DiagramObjectPoint.xPosition>"), "{written}");
        assert!(written.contains("<cim:DiagramObjectPoint.yPosition>2.5</cim:DiagramObjectPoint.yPosition>"), "{written}");
        assert!(written.contains("OrientationKind.negative"), "{written}");
        cimmodel::CimDataset::decode_str(&written).unwrap();
    }
}