Skip to content

Commit

Permalink
move RustAnalyzerArg associated methods to lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
bobozaur committed Jan 21, 2025
1 parent fd8742f commit 0058f38
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
27 changes: 27 additions & 0 deletions tools/rust_analyzer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,33 @@ where
serde_json::from_str(&buf).with_context(|| format!("failed to deserialize file: {path}"))
}

/// `rust-analyzer` associates workspaces with buildfiles. Therefore, when it passes in a
/// source file path, we use this function to identify the buildfile the file belongs to.
fn source_file_to_buildfile(file: &Utf8Path) -> anyhow::Result<Utf8PathBuf> {
// Skip the first element as it's always the full file path.
file.ancestors()
.skip(1)
.flat_map(|dir| BUILD_FILE_NAMES.iter().map(move |build| dir.join(build)))
.find(|p| p.exists())
.with_context(|| format!("no buildfile found for {file}"))
}

fn buildfile_to_targets(workspace: &Utf8Path, buildfile: &Utf8Path) -> anyhow::Result<String> {
log::info!("getting targets for buildfile: {buildfile}");

let parent_dir = buildfile
.strip_prefix(workspace)
.with_context(|| format!("{buildfile} not part of workspace"))?
.parent();

let targets = match parent_dir {
Some(p) if !p.as_str().is_empty() => format!("//{p}:all"),
_ => "//...".to_string(),
};

Ok(targets)
}

#[derive(Debug, Deserialize)]
struct ToolchainInfo {
sysroot: Utf8PathBuf,
Expand Down
35 changes: 4 additions & 31 deletions tools/rust_analyzer/rust_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};

use crate::{
aquery::{CrateSpec, CrateType},
ToolchainInfo, BUILD_FILE_NAMES,
buildfile_to_targets, source_file_to_buildfile, ToolchainInfo,
};

/// The argument that `rust-analyzer` can pass to the workspace discovery command.
Expand All @@ -32,41 +32,14 @@ impl RustAnalyzerArg {
) -> anyhow::Result<(Utf8PathBuf, String)> {
match self {
Self::Path(file) => {
let buildfile = Self::source_file_to_buildfile(&file)?;
Self::buildfile_to_targets(workspace, &buildfile).map(|t| (buildfile, t))
let buildfile = source_file_to_buildfile(&file)?;
buildfile_to_targets(workspace, &buildfile).map(|t| (buildfile, t))
}
Self::Buildfile(buildfile) => {
Self::buildfile_to_targets(workspace, &buildfile).map(|t| (buildfile, t))
buildfile_to_targets(workspace, &buildfile).map(|t| (buildfile, t))
}
}
}

/// `rust-analyzer` associates workspaces with buildfiles. Therefore, when it passes in a
/// source file path, we use this function to identify the buildfile the file belongs to.
fn source_file_to_buildfile(file: &Utf8Path) -> anyhow::Result<Utf8PathBuf> {
// Skip the first element as it's always the full file path.
file.ancestors()
.skip(1)
.flat_map(|dir| BUILD_FILE_NAMES.iter().map(move |build| dir.join(build)))
.find(|p| p.exists())
.with_context(|| format!("no buildfile found for {file}"))
}

fn buildfile_to_targets(workspace: &Utf8Path, buildfile: &Utf8Path) -> anyhow::Result<String> {
log::info!("getting targets for buildfile: {buildfile}");

let parent_dir = buildfile
.strip_prefix(workspace)
.with_context(|| format!("{buildfile} not part of workspace"))?
.parent();

let targets = match parent_dir {
Some(p) if !p.as_str().is_empty() => format!("//{p}:all"),
_ => "//...".to_string(),
};

Ok(targets)
}
}

impl FromStr for RustAnalyzerArg {
Expand Down

0 comments on commit 0058f38

Please sign in to comment.