tree_sitter_cli/
util.rs

1use std::{
2    path::{Path, PathBuf},
3    process::{Child, ChildStdin, Command, Stdio},
4    sync::{
5        atomic::{AtomicUsize, Ordering},
6        Arc,
7    },
8};
9
10use anyhow::{anyhow, Context, Result};
11use indoc::indoc;
12use log::error;
13use tree_sitter::{Parser, Tree};
14use tree_sitter_config::Config;
15use tree_sitter_loader::Config as LoaderConfig;
16
17const HTML_HEADER: &[u8] = b"
18<!DOCTYPE html>
19
20<style>
21svg { width: 100%; }
22</style>
23
24";
25
26#[must_use]
27pub fn lang_not_found_for_path(path: &Path, loader_config: &LoaderConfig) -> String {
28    let path = path.display();
29    format!(
30        indoc! {"
31            No language found for path `{}`
32
33            If a language should be associated with this file extension, please ensure the path to `{}` is inside one of the following directories as specified by your 'config.json':\n\n{}\n
34            If the directory that contains the relevant grammar for `{}` is not listed above, please add the directory to the list of directories in your config file, {}
35        "},
36        path,
37        path,
38        loader_config
39            .parser_directories
40            .iter()
41            .enumerate()
42            .map(|(i, d)| format!("  {}. {}", i + 1, d.display()))
43            .collect::<Vec<_>>()
44            .join("  \n"),
45        path,
46        if let Ok(Some(config_path)) = Config::find_config_file() {
47            format!("located at {}", config_path.display())
48        } else {
49            String::from("which you need to create by running `tree-sitter init-config`")
50        }
51    )
52}
53
54#[must_use]
55pub fn cancel_on_signal() -> Arc<AtomicUsize> {
56    let result = Arc::new(AtomicUsize::new(0));
57    ctrlc::set_handler({
58        let flag = result.clone();
59        move || {
60            flag.store(1, Ordering::Relaxed);
61        }
62    })
63    .expect("Error setting Ctrl-C handler");
64    result
65}
66
67pub struct LogSession {
68    path: PathBuf,
69    dot_process: Option<Child>,
70    dot_process_stdin: Option<ChildStdin>,
71    open_log: bool,
72}
73
74pub fn print_tree_graph(tree: &Tree, path: &str, quiet: bool) -> Result<()> {
75    let session = LogSession::new(path, quiet)?;
76    tree.print_dot_graph(session.dot_process_stdin.as_ref().unwrap());
77    Ok(())
78}
79
80pub fn log_graphs(parser: &mut Parser, path: &str, open_log: bool) -> Result<LogSession> {
81    let session = LogSession::new(path, open_log)?;
82    parser.print_dot_graphs(session.dot_process_stdin.as_ref().unwrap());
83    Ok(session)
84}
85
86impl LogSession {
87    fn new(path: &str, open_log: bool) -> Result<Self> {
88        use std::io::Write;
89
90        let mut dot_file = std::fs::File::create(path)?;
91        dot_file.write_all(HTML_HEADER)?;
92        let mut dot_process = Command::new("dot")
93            .arg("-Tsvg")
94            .stdin(Stdio::piped())
95            .stdout(dot_file)
96            .spawn()
97            .with_context(|| {
98                "Failed to run the `dot` command. Check that graphviz is installed."
99            })?;
100        let dot_stdin = dot_process
101            .stdin
102            .take()
103            .ok_or_else(|| anyhow!("Failed to open stdin for `dot` process."))?;
104        Ok(Self {
105            path: PathBuf::from(path),
106            dot_process: Some(dot_process),
107            dot_process_stdin: Some(dot_stdin),
108            open_log,
109        })
110    }
111}
112
113impl Drop for LogSession {
114    fn drop(&mut self) {
115        use std::fs;
116
117        drop(self.dot_process_stdin.take().unwrap());
118        let output = self.dot_process.take().unwrap().wait_with_output().unwrap();
119        if output.status.success() {
120            if self.open_log && fs::metadata(&self.path).unwrap().len() > HTML_HEADER.len() as u64 {
121                webbrowser::open(&self.path.to_string_lossy()).unwrap();
122            }
123        } else {
124            error!(
125                "Dot failed: {} {}",
126                String::from_utf8_lossy(&output.stdout),
127                String::from_utf8_lossy(&output.stderr)
128            );
129        }
130    }
131}