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