summaryrefslogtreecommitdiff
path: root/support/texlab/crates/commands/src/dep_graph.rs
blob: 86b49da0ac34ea5b5a3809915ed3c11dc60bee67 (plain)
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
use std::io::Write;

use anyhow::Result;
use base_db::Workspace;
use itertools::Itertools;
use rustc_hash::FxHashMap;

pub fn show_dependency_graph(workspace: &Workspace) -> Result<String> {
    let mut documents = FxHashMap::default();

    let mut writer = Vec::new();
    writeln!(&mut writer, "digraph G {{")?;
    writeln!(&mut writer, "rankdir = LR;")?;

    for (i, document) in workspace.iter().enumerate() {
        let node = format!("v{i:0>5}");

        let label = document.uri.as_str();
        let shape = if document
            .data
            .as_tex()
            .map_or(false, |data| data.semantics.can_be_root)
        {
            "tripleoctagon"
        } else if document
            .data
            .as_tex()
            .map_or(false, |data| data.semantics.can_be_compiled)
        {
            "doubleoctagon"
        } else {
            "octagon"
        };

        writeln!(&mut writer, "\t{node} [label=\"{label}\", shape={shape}];")?;
        documents.insert(&document.uri, node);
    }

    for edge in workspace
        .graphs()
        .values()
        .flat_map(|graph| &graph.edges)
        .unique()
    {
        let source = &documents[&edge.source];
        let target = &documents[&edge.target];
        let label = match &edge.data {
            base_db::deps::EdgeData::DirectLink(data) => &data.link.path.text,
            base_db::deps::EdgeData::AdditionalFiles => "<project>",
            base_db::deps::EdgeData::Artifact => "<artifact>",
            base_db::deps::EdgeData::FileList(_) => "<fls>",
        };

        writeln!(&mut writer, "\t{source} -> {target} [label=\"{label}\"];")?;
    }

    writeln!(&mut writer, "}}")?;
    Ok(String::from_utf8(writer)?)
}