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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! DFG command - Show data flow graph
//!
//! Extracts and displays the data flow graph with def-use chains.
//! Auto-routes through daemon when available for ~35x speedup.
use std::path::PathBuf;
use anyhow::Result;
use clap::Args;
use tldr_core::{get_dfg_context, Language};
use tldr_core::types::DfgInfo;
use crate::commands::daemon_router::{params_with_file_function, try_daemon_route};
use crate::output::{format_dfg_text, OutputFormat, OutputWriter};
/// Extract data flow graph for a function
#[derive(Debug, Args)]
pub struct DfgArgs {
/// Source file path
pub file: PathBuf,
/// Function name to analyze
pub function: String,
/// Programming language (auto-detected from file extension if not specified)
#[arg(long, short = 'l')]
pub lang: Option<Language>,
}
impl DfgArgs {
/// Run the dfg command
pub fn run(&self, format: OutputFormat, quiet: bool) -> Result<()> {
let writer = OutputWriter::new(format, quiet);
// Determine language from file extension or argument
let language = self.lang.unwrap_or_else(|| {
Language::from_path(&self.file).unwrap_or(Language::Python)
});
// Try daemon first for cached result (use file's parent as project root)
let project = self.file.parent().unwrap_or(&self.file);
if let Some(dfg) = try_daemon_route::<DfgInfo>(
project,
"dfg",
params_with_file_function(&self.file, &self.function),
) {
// Output based on format
if writer.is_text() {
let text = format_dfg_text(&dfg);
writer.write_text(&text)?;
return Ok(());
} else {
writer.write(&dfg)?;
return Ok(());
}
}
// Fallback to direct compute
writer.progress(&format!(
"Extracting DFG for {} in {}...",
self.function,
self.file.display()
));
// Get DFG
let dfg = get_dfg_context(
self.file.to_str().unwrap_or_default(),
&self.function,
language,
)?;
// Output based on format
if writer.is_text() {
let text = format_dfg_text(&dfg);
writer.write_text(&text)?;
} else {
writer.write(&dfg)?;
}
Ok(())
}
}