pub(super) fn qualified_key(text: &str, offset: usize) -> Option<String> {
let document: toml_edit::Document<String> = text.parse().ok()?;
let mut path = Vec::new();
named_at(document.as_table(), offset, &mut path).then(|| path.join("."))
}
fn segment(key: &toml_edit::Key) -> String {
key.default_repr()
.as_raw()
.as_str()
.map_or_else(|| key.get().to_owned(), ToOwned::to_owned)
}
fn named_at(table: &toml_edit::Table, offset: usize, path: &mut Vec<String>) -> bool {
for (name, item) in table {
let Some(key) = table.key(name) else { continue };
path.push(segment(key));
if key.span().is_some_and(|span| span.contains(&offset))
|| named_in_item(item, offset, path)
{
return true;
}
path.pop();
}
false
}
fn named_in_inline(inline: &toml_edit::InlineTable, offset: usize, path: &mut Vec<String>) -> bool {
for (name, value) in inline {
let Some(key) = inline.key(name) else {
continue;
};
path.push(segment(key));
let found = key.span().is_some_and(|span| span.contains(&offset))
|| value
.as_inline_table()
.is_some_and(|nested| named_in_inline(nested, offset, path))
|| value.as_array().is_some_and(|array| {
array.iter().any(|element| {
element
.as_inline_table()
.is_some_and(|nested| named_in_inline(nested, offset, path))
})
});
if found {
return true;
}
path.pop();
}
false
}
fn named_in_item(item: &toml_edit::Item, offset: usize, path: &mut Vec<String>) -> bool {
if let Some(child) = item.as_table() {
return named_at(child, offset, path);
}
if let Some(inline) = item.as_inline_table() {
return named_in_inline(inline, offset, path);
}
if let Some(array) = item.as_array_of_tables() {
return array.iter().any(|child| named_at(child, offset, path));
}
if let Some(array) = item.as_array() {
return array.iter().any(|value| {
value
.as_inline_table()
.is_some_and(|nested| named_in_inline(nested, offset, path))
});
}
false
}
#[cfg(test)]
mod tests {
use super::qualified_key;
#[test]
fn offsets_resolve_to_the_path_toml_resolved() {
let cases: &[(&str, usize, &str, Option<&str>)] = &[
("a = 1\nnope = 2\n", 6, "nope", Some("nope")),
("a = 1\nnope = 2\n", 8, "pe", Some("nope")),
("[a.b]\nnope = 1\n", 6, "nope", Some("a.b.nope")),
("a.b.nope = 1\n", 4, "nope", Some("a.b.nope")),
("a.b.nope = 1\n", 0, "a", Some("a")),
(
"[[x]]\nnope = 1\n\n[[x]]\nnope = 2\n",
6,
"nope",
Some("x.nope"),
),
(
"[[x]]\nnope = 1\n\n[[x]]\nnope = 2\n",
22,
"nope",
Some("x.nope"),
),
("t = { a = 1, nope = 2 }\n", 13, "nope", Some("t.nope")),
("t = { u = { nope = 1 } }\n", 12, "nope", Some("t.u.nope")),
("t = [{ nope = 1 }]\n", 7, "nope", Some("t.nope")),
("t = { u = [{ nope = 1 }] }\n", 13, "nope", Some("t.u.nope")),
(
"[t.\"a.b\"]\nnope = 1\n",
10,
"nope",
Some("t.\"a.b\".nope"),
),
(
"t = { \"a.b\" = { nope = 1 } }\n",
16,
"nope",
Some("t.\"a.b\".nope"),
),
(
"[t.\"@s/p\"]\nnope = 1\n",
11,
"nope",
Some("t.\"@s/p\".nope"),
),
("[t.\"a\"]\nnope = 1\n", 8, "nope", Some("t.a.nope")),
("[t]\nnope = 1\n", 1, "t", Some("t")),
("a = 1\n", 4, "1", None),
("t = { a = 1 }\n", 10, "1", None),
("a = 1\n", 6, "", None),
("a = 1\n", 100, "", None),
("a = \n", 0, "a", None),
];
let mut mismatches = Vec::new();
for (document, offset, under, expected) in cases {
assert!(
document.get(*offset..).unwrap_or("").starts_with(under),
"offset {offset} of {document:?} is not on {under:?}"
);
let actual = qualified_key(document, *offset);
if actual.as_deref() != *expected {
mismatches.push(format!(
"offset {offset} ({under:?}) of {document:?}: got {actual:?}, expected {expected:?}"
));
}
}
assert!(mismatches.is_empty(), "{}", mismatches.join("\n"));
}
}