use jsonx_lib::Value;
use crate::{
json::{to_value, write_str, write_u64},
position::Range,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[allow(missing_docs)] pub enum SymbolKind {
File = 1,
Module = 2,
Namespace = 3,
Package = 4,
Class = 5,
Method = 6,
Property = 7,
Field = 8,
Constructor = 9,
Enum = 10,
Interface = 11,
Function = 12,
Variable = 13,
Constant = 14,
String = 15,
Number = 16,
Boolean = 17,
Array = 18,
Object = 19,
Key = 20,
Null = 21,
EnumMember = 22,
Struct = 23,
Event = 24,
Operator = 25,
TypeParameter = 26,
}
impl SymbolKind {
#[must_use]
pub const fn as_u32(self) -> u32 {
self as u32
}
}
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DocumentSymbol {
pub name: String,
pub detail: Option<String>,
pub kind: SymbolKind,
pub range: Range,
pub selection_range: Range,
pub children: Vec<DocumentSymbol>,
}
impl DocumentSymbol {
#[must_use]
pub fn new(
name: impl Into<String>,
kind: SymbolKind,
range: Range,
selection_range: Range,
) -> Self {
Self {
name: name.into(),
detail: None,
kind,
range,
selection_range,
children: Vec::new(),
}
}
#[must_use]
pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
self.detail = Some(detail.into());
self
}
#[must_use]
pub fn with_child(mut self, child: DocumentSymbol) -> Self {
self.children.push(child);
self
}
#[must_use]
pub fn to_json(&self) -> Value<'static> {
let mut out = String::new();
write_symbols(&mut out, std::slice::from_ref(self));
match to_value(&out) {
Value::Array(mut items) if items.len() == 1 => items.pop().unwrap_or_default(),
other => other,
}
}
}
impl Clone for DocumentSymbol {
fn clone(&self) -> Self {
struct Frame<'a> {
source: &'a DocumentSymbol,
started: usize,
done: Vec<DocumentSymbol>,
}
fn frame(source: &DocumentSymbol) -> Frame<'_> {
Frame {
source,
started: 0,
done: Vec::with_capacity(source.children.len()),
}
}
let mut stack = vec![frame(self)];
loop {
let Some(top) = stack.last_mut() else {
return Self::new(
self.name.clone(),
self.kind,
self.range,
self.selection_range,
);
};
if let Some(child) = top.source.children.get(top.started) {
top.started += 1;
stack.push(frame(child));
continue;
}
let Some(finished) = stack.pop() else {
continue;
};
let mut copy = DocumentSymbol {
name: finished.source.name.clone(),
detail: finished.source.detail.clone(),
kind: finished.source.kind,
range: finished.source.range,
selection_range: finished.source.selection_range,
children: Vec::new(),
};
copy.children = finished.done;
match stack.last_mut() {
Some(parent) => parent.done.push(copy),
None => return copy,
}
}
}
}
impl Drop for DocumentSymbol {
fn drop(&mut self) {
let mut stack = std::mem::take(&mut self.children);
while let Some(mut symbol) = stack.pop() {
stack.append(&mut symbol.children);
}
}
}
fn contain(range: Range, selection: Range) -> Range {
let (low, high) = if range.start <= range.end {
(range.start, range.end)
} else {
(range.end, range.start)
};
let start = selection.start.clamp(low, high);
let end = selection.end.clamp(start, high);
Range::new(start, end)
}
pub(crate) fn write_symbols(out: &mut String, symbols: &[DocumentSymbol]) {
out.push('[');
let mut stack: Vec<(&[DocumentSymbol], usize)> = vec![(symbols, 0)];
while let Some((items, done)) = stack.last_mut() {
let Some(symbol) = items.get(*done) else {
out.push(']');
let _finished = stack.pop();
if !stack.is_empty() {
out.push('}');
}
continue;
};
if *done > 0 {
out.push(',');
}
*done += 1;
write_head(out, symbol);
if symbol.children.is_empty() {
out.push('}');
} else {
out.push_str(",\"children\":[");
stack.push((&symbol.children, 0));
}
}
}
fn write_head(out: &mut String, symbol: &DocumentSymbol) {
out.push_str("{\"name\":");
write_str(out, &symbol.name);
if let Some(detail) = &symbol.detail {
out.push_str(",\"detail\":");
write_str(out, detail);
}
out.push_str(",\"kind\":");
write_u64(out, u64::from(symbol.kind.as_u32()));
out.push_str(",\"range\":");
symbol.range.write(out);
out.push_str(",\"selectionRange\":");
contain(symbol.range, symbol.selection_range).write(out);
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;
use crate::position::Position;
fn at(line: u32, from: u32, to: u32) -> Range {
Range::new(Position::new(line, from), Position::new(line, to))
}
#[test]
fn test_symbols_write_nested_children_in_order() {
let tree = vec![
DocumentSymbol::new("a", SymbolKind::Module, at(0, 0, 9), at(0, 0, 1))
.with_detail("mod")
.with_child(
DocumentSymbol::new("b", SymbolKind::Function, at(1, 0, 5), at(1, 0, 1))
.with_child(DocumentSymbol::new(
"c",
SymbolKind::Variable,
at(2, 0, 3),
at(2, 0, 1),
)),
)
.with_child(DocumentSymbol::new(
"d",
SymbolKind::Constant,
at(3, 0, 3),
at(3, 0, 1),
)),
DocumentSymbol::new("e", SymbolKind::Class, at(4, 0, 3), at(4, 0, 1)),
];
let mut out = String::new();
write_symbols(&mut out, &tree);
let json = jsonx_lib::parse(&out).map_or(Value::Null, Value::into_owned);
assert_eq!(json[0]["name"], "a");
assert_eq!(json[0]["detail"], "mod");
assert_eq!(json[0]["children"][0]["children"][0]["name"], "c");
assert_eq!(json[0]["children"][1]["name"], "d");
assert_eq!(json[1]["name"], "e");
assert!(json[1].get("children").is_none());
assert_eq!(json[1]["kind"], 5);
}
#[test]
fn test_empty_list_is_an_empty_array() {
let mut out = String::new();
write_symbols(&mut out, &[]);
assert_eq!(out, "[]");
}
#[test]
fn test_selection_is_clamped_into_the_range() {
let wild = DocumentSymbol::new("x", SymbolKind::Field, at(2, 5, 9), at(0, 0, 99));
let json = wild.to_json();
assert_eq!(json["selectionRange"]["start"]["line"], 2);
assert_eq!(json["selectionRange"]["start"]["character"], 5);
assert_eq!(json["selectionRange"]["end"]["character"], 5);
let fine = DocumentSymbol::new("x", SymbolKind::Field, at(0, 0, 9), at(0, 2, 4)).to_json();
assert_eq!(fine["selectionRange"]["start"]["character"], 2);
assert_eq!(fine["selectionRange"]["end"]["character"], 4);
let reversed = Range::new(Position::new(0, 9), Position::new(0, 1));
let r = DocumentSymbol::new("x", SymbolKind::Field, reversed, at(0, 3, 4)).to_json();
assert_eq!(r["selectionRange"]["start"]["character"], 3);
}
#[test]
fn test_clone_copies_the_hierarchy() {
let tree = DocumentSymbol::new("a", SymbolKind::Module, at(0, 0, 9), at(0, 0, 1))
.with_detail("d")
.with_child(
DocumentSymbol::new("b", SymbolKind::Field, at(1, 0, 5), at(1, 0, 1)).with_child(
DocumentSymbol::new("c", SymbolKind::Field, at(2, 0, 5), at(2, 0, 1)),
),
)
.with_child(DocumentSymbol::new(
"d",
SymbolKind::Field,
at(3, 0, 5),
at(3, 0, 1),
));
assert_eq!(tree.clone(), tree);
}
#[test]
fn test_a_very_deep_outline_is_written_and_dropped_without_recursing() {
const DEPTH: usize = 200_000;
let handle = std::thread::Builder::new()
.stack_size(256 * 1024)
.spawn(|| {
let mut tree =
DocumentSymbol::new("leaf", SymbolKind::Null, at(0, 0, 1), at(0, 0, 1));
for _ in 0..DEPTH {
tree = DocumentSymbol::new("n", SymbolKind::Null, at(0, 0, 1), at(0, 0, 1))
.with_child(tree);
}
let copy = tree.clone();
let mut out = String::new();
write_symbols(&mut out, std::slice::from_ref(&tree));
drop(copy);
drop(tree);
out.matches('{').count() == out.matches('}').count()
&& out.matches('{').count() > DEPTH
});
assert!(handle.is_ok_and(|h| h.join().unwrap_or(false)));
}
fn tree_strategy() -> impl Strategy<Value = Vec<DocumentSymbol>> {
let leaf = Just(Vec::<DocumentSymbol>::new());
leaf.prop_recursive(4, 40, 4, |children| {
prop::collection::vec((0u32..30, 0u32..30, children), 0..4).prop_map(|nodes| {
nodes
.into_iter()
.map(|(a, b, kids)| {
let range = at(a.min(b), 0, 9);
let mut symbol = DocumentSymbol::new(
format!("s{a}-{b}"),
SymbolKind::Field,
range,
range,
);
symbol.children = kids;
symbol
})
.collect()
})
})
}
fn outline(symbols: &[DocumentSymbol], depth: usize, out: &mut Vec<(usize, String)>) {
for symbol in symbols {
out.push((depth, symbol.name.clone()));
outline(&symbol.children, depth + 1, out);
}
}
fn outline_json(value: &Value<'_>, depth: usize, out: &mut Vec<(usize, String)>) {
for entry in value.as_array().into_iter().flat_map(|a| a.iter()) {
out.push((depth, entry["name"].as_str().unwrap_or("?").to_owned()));
outline_json(&entry["children"], depth + 1, out);
}
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(512))]
#[test]
fn prop_written_symbols_hold_the_same_hierarchy(tree in tree_strategy()) {
let mut out = String::new();
write_symbols(&mut out, &tree);
let parsed = jsonx_lib::parse(&out);
prop_assert!(parsed.is_ok(), "{out}");
let json = parsed.map(Value::into_owned).unwrap_or_default();
let (mut want, mut got) = (Vec::new(), Vec::new());
outline(&tree, 0, &mut want);
outline_json(&json, 0, &mut got);
prop_assert_eq!(want, got);
}
}
}