Stream: t-compiler/wg-rls-2.0

Topic: using ra as a library


tcjqlwnsqthqcakslc (Mar 21 2020 at 11:17, on Zulip):

Hi everyone, is it possible to get a list of all symbols including their type?

This is what I currently have, but I get empty lists.

const LOCAL_CODE: SourceRootId = SourceRootId(0);
const SYSROOT: SourceRootId = SourceRootId(1);

struct PackageRootWrapper(PackageRoot);

impl Debug for PackageRootWrapper {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        f.debug_struct("PackageRoot")
            .field("path", self.0.path())
            .field("is_member", &self.0.is_member())
            .finish()
    }
}

fn main() -> anyhow::Result<()> {
    let mut analysis_host = AnalysisHost::default();
    let mut change = AnalysisChange::new();

    let workspace = ProjectWorkspace::discover_with_sysroot(
        Path::new("/home/user/rust/analyzer-test"),
        true,
        &CargoFeatures::default(),
    )?;
    dbg!(workspace
        .to_roots()
        .iter()
        .map(|pr| PackageRootWrapper(pr.clone()))
        .collect::<Vec<_>>());

    for (i, pr) in workspace.to_roots().iter().enumerate() {
        change.add_root(SourceRootId(i as u32), pr.is_member());
        change.set_debug_root_path(
            SourceRootId(i as u32),
            pr.path().to_str().unwrap().to_string(),
        );
    }

    let mut next_file_id = 0u32;
    let mut file_id_map: FxHashMap<PathBuf, FileId> = FxHashMap::default();
    let mut get_or_insert_file_id = |p: &Path| -> Option<FileId> {
        let p = p.to_path_buf();
        match file_id_map.get(&p) {
            Some(id) => Some(*id),
            None => {
                next_file_id += 1;
                file_id_map.insert(p, FileId(next_file_id));
                Some(FileId(next_file_id))
            }
        }
    };

    change.add_root(LOCAL_CODE, true);
    change.add_file(
        LOCAL_CODE,
        FileId(0),
        RelativePathBuf::from_path("src/main.rs").unwrap(),
        Arc::new("fn main() {}".to_string()),
    );
    let crate_graph = workspace.to_crate_graph(
        &CfgOptions::default(),
        &FxHashMap::default(),
        &mut get_or_insert_file_id,
    );
    change.set_crate_graph(crate_graph);

    analysis_host.apply_change(change);
    let mut query = Query::new("to".to_string());
    query.libs();
    let db: &mut RootDatabase =
        Any::downcast_mut::<RootDatabase>(analysis_host.raw_database_mut()).unwrap();
    dbg!(symbol_index::world_symbols(db, query));
    dbg!(db.library_roots());
    dbg!(db.local_roots());
    dbg!(db.library_symbols(SourceRootId(0)));
    // dbg!(an.symbol_search(query));
    Ok(())
}
matklad (Mar 21 2020 at 11:19, on Zulip):

Take a look at https://github.com/rust-analyzer/rust-analyzer/blob/10867336e627f84a4886592c0a2764f5105bd0ce/crates/rust-analyzer/src/cli/analysis_stats.rs#L75-L77, which collects all functions

tcjqlwnsqthqcakslc (Mar 21 2020 at 11:42, on Zulip):

thanks, I'll look into that

Last update: May 29 2020 at 17:45UTC