structured data editor
git clone https://git.lucas.co/cce-data-editor.git
Support property and child node span resolution in find_kdl_span_in_doc
src/main.rs | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/src/main.rs b/src/main.rs
index 268c896..51c9173 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -101,6 +101,18 @@ fn find_kdl_span_in_doc(doc: &kdl::KdlDocument, tokens: &[PathToken]) -> Option<
let span = node.span();
return Some((span.offset(), span.offset() + span.len()));
}
+ } else if tokens.len() == 2 {
+ if let PathToken::Key(prop_key) = &tokens[1] {
+ if let Some(entry) = node.entries().iter().find(|e| e.name().map(|id| id.value()) == Some(prop_key)) {
+ let span = entry.span();
+ return Some((span.offset(), span.offset() + span.len()));
+ }
+ }
+ if let Some(children) = node.children() {
+ if let Some(span) = find_kdl_span_in_doc(children, &tokens[1..]) {
+ return Some(span);
+ }
+ }
} else if let Some(children) = node.children() {
return find_kdl_span_in_doc(children, &tokens[1..]);
}
@@ -1907,4 +1919,18 @@ mod tests {
Err(e) => panic!("Failed to parse generated KDL: {}", e),
}
}
+
+ #[test]
+ fn test_kdl_span_lookup() {
+ let content = std::fs::read_to_string("/home/lsgalante/.config/cce/config.kdl").unwrap();
+ let val = cce_ui::config::parse_kdl_to_json(&content);
+ let mut flat = Vec::new();
+ flatten_json(&val, "", &mut flat);
+
+ for (k, _) in &flat {
+ let tokens = parse_path(k);
+ let span = find_kdl_span(&content, &tokens);
+ assert!(span.is_some(), "Should find span for path: {} with tokens: {:?}", k, tokens);
+ }
+ }
}