Skip to main content

rust_analyzer/cli/
symbols.rs

1//! Read Rust code on stdin, print syntax tree on stdout.
2use ide::{Analysis, FileStructureConfig};
3use ide_db::base_db::AbsPathBuf;
4use triomphe::Arc;
5
6use crate::cli::{flags, read_stdin};
7
8impl flags::Symbols {
9    pub fn run(self) -> anyhow::Result<()> {
10        let text = read_stdin()?;
11        let cwd = AbsPathBuf::assert_utf8(std::env::current_dir()?);
12        let (analysis, file_id) = Analysis::from_single_file(text, Arc::new(cwd));
13        let structure = analysis
14            // The default setting in config.rs (document_symbol_search_excludeLocals) is to exclude
15            // locals because it is unlikely that users want document search to return the names of
16            // local variables, but here we include them deliberately.
17            .file_structure(&FileStructureConfig { exclude_locals: false }, file_id)
18            .unwrap();
19        for s in structure {
20            println!("{s:?}");
21        }
22        Ok(())
23    }
24}