Skip to content

Commit fd47887

Browse files
committed
refactor: Define a TargetSpec wrapper type
1 parent 27502d5 commit fd47887

File tree

4 files changed

+73
-53
lines changed

4 files changed

+73
-53
lines changed

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use triomphe::Arc;
3535
use vfs::{AbsPath, AbsPathBuf, FileId, VfsPath};
3636

3737
use crate::{
38-
cargo_target_spec::CargoTargetSpec,
3938
config::{Config, RustfmtConfig, WorkspaceSymbolConfig},
4039
diff::diff,
4140
global_state::{GlobalState, GlobalStateSnapshot},
@@ -50,6 +49,7 @@ use crate::{
5049
self, CrateInfoResult, ExternalDocsPair, ExternalDocsResponse, FetchDependencyListParams,
5150
FetchDependencyListResult, PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams,
5251
},
52+
target_spec::TargetSpec,
5353
};
5454

5555
pub(crate) fn handle_workspace_reload(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {
@@ -771,8 +771,8 @@ pub(crate) fn handle_parent_module(
771771
Some(&crate_id) => crate_id,
772772
None => return Ok(None),
773773
};
774-
let cargo_spec = match CargoTargetSpec::for_file(&snap, file_id)? {
775-
Some(it) => it,
774+
let cargo_spec = match TargetSpec::for_file(&snap, file_id)? {
775+
Some(TargetSpec::Cargo(it)) => it,
776776
None => return Ok(None),
777777
};
778778

@@ -804,7 +804,7 @@ pub(crate) fn handle_runnables(
804804
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
805805
let line_index = snap.file_line_index(file_id)?;
806806
let offset = params.position.and_then(|it| from_proto::offset(&line_index, it).ok());
807-
let cargo_spec = CargoTargetSpec::for_file(&snap, file_id)?;
807+
let target_spec = TargetSpec::for_file(&snap, file_id)?;
808808

809809
let expect_test = match offset {
810810
Some(offset) => {
@@ -821,7 +821,7 @@ pub(crate) fn handle_runnables(
821821
if should_skip_for_offset(&runnable, offset) {
822822
continue;
823823
}
824-
if should_skip_target(&runnable, cargo_spec.as_ref()) {
824+
if should_skip_target(&runnable, target_spec.as_ref()) {
825825
continue;
826826
}
827827
let mut runnable = to_proto::runnable(&snap, runnable)?;
@@ -834,8 +834,8 @@ pub(crate) fn handle_runnables(
834834

835835
// Add `cargo check` and `cargo test` for all targets of the whole package
836836
let config = snap.config.runnables();
837-
match cargo_spec {
838-
Some(spec) => {
837+
match target_spec {
838+
Some(TargetSpec::Cargo(spec)) => {
839839
let all_targets = !snap.analysis.is_crate_no_std(spec.crate_id)?;
840840
for cmd in ["check", "test"] {
841841
let mut cargo_args =
@@ -1351,14 +1351,14 @@ pub(crate) fn handle_code_lens(
13511351
}
13521352

13531353
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1354-
let cargo_target_spec = CargoTargetSpec::for_file(&snap, file_id)?;
1354+
let target_spec = TargetSpec::for_file(&snap, file_id)?;
13551355

13561356
let annotations = snap.analysis.annotations(
13571357
&AnnotationConfig {
1358-
binary_target: cargo_target_spec
1358+
binary_target: target_spec
13591359
.map(|spec| {
13601360
matches!(
1361-
spec.target_kind,
1361+
spec.target_kind(),
13621362
TargetKind::Bin | TargetKind::Example | TargetKind::Test
13631363
)
13641364
})
@@ -1764,8 +1764,8 @@ pub(crate) fn handle_open_cargo_toml(
17641764
let _p = tracing::span!(tracing::Level::INFO, "handle_open_cargo_toml").entered();
17651765
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
17661766

1767-
let cargo_spec = match CargoTargetSpec::for_file(&snap, file_id)? {
1768-
Some(it) => it,
1767+
let cargo_spec = match TargetSpec::for_file(&snap, file_id)? {
1768+
Some(TargetSpec::Cargo(it)) => it,
17691769
None => return Ok(None),
17701770
};
17711771

@@ -1894,8 +1894,8 @@ fn runnable_action_links(
18941894
return None;
18951895
}
18961896

1897-
let cargo_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
1898-
if should_skip_target(&runnable, cargo_spec.as_ref()) {
1897+
let target_spec = TargetSpec::for_file(snap, runnable.nav.file_id).ok()?;
1898+
if should_skip_target(&runnable, target_spec.as_ref()) {
18991899
return None;
19001900
}
19011901

@@ -1960,13 +1960,13 @@ fn prepare_hover_actions(
19601960
.collect()
19611961
}
19621962

1963-
fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>) -> bool {
1963+
fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&TargetSpec>) -> bool {
19641964
match runnable.kind {
19651965
RunnableKind::Bin => {
19661966
// Do not suggest binary run on other target than binary
19671967
match &cargo_spec {
19681968
Some(spec) => !matches!(
1969-
spec.target_kind,
1969+
spec.target_kind(),
19701970
TargetKind::Bin | TargetKind::Example | TargetKind::Test
19711971
),
19721972
None => true,
@@ -2043,9 +2043,9 @@ fn run_rustfmt(
20432043
}
20442044
RustfmtConfig::CustomCommand { command, args } => {
20452045
let cmd = PathBuf::from(&command);
2046-
let workspace = CargoTargetSpec::for_file(snap, file_id)?;
2047-
let mut cmd = match workspace {
2048-
Some(spec) => {
2046+
let target_spec = TargetSpec::for_file(snap, file_id)?;
2047+
let mut cmd = match target_spec {
2048+
Some(TargetSpec::Cargo(spec)) => {
20492049
// approach: if the command name contains a path separator, join it with the workspace root.
20502050
// however, if the path is absolute, joining will result in the absolute path being preserved.
20512051
// as a fallback, rely on $PATH-based discovery.

crates/rust-analyzer/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
pub mod cli;
1515

1616
mod caps;
17-
mod cargo_target_spec;
1817
mod diagnostics;
1918
mod diff;
2019
mod dispatch;
@@ -25,6 +24,7 @@ mod main_loop;
2524
mod mem_docs;
2625
mod op_queue;
2726
mod reload;
27+
mod target_spec;
2828
mod task_pool;
2929
mod version;
3030

crates/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use serde_json::to_value;
2020
use vfs::AbsPath;
2121

2222
use crate::{
23-
cargo_target_spec::CargoTargetSpec,
2423
config::{CallInfoConfig, Config},
2524
global_state::GlobalStateSnapshot,
2625
line_index::{LineEndings, LineIndex, PositionEncoding},
@@ -30,6 +29,7 @@ use crate::{
3029
LspError,
3130
},
3231
lsp_ext::{self, SnippetTextEdit},
32+
target_spec::{CargoTargetSpec, TargetSpec},
3333
};
3434

3535
pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position {
@@ -1344,10 +1344,10 @@ pub(crate) fn runnable(
13441344
runnable: Runnable,
13451345
) -> Cancellable<lsp_ext::Runnable> {
13461346
let config = snap.config.runnables();
1347-
let target_spec = CargoTargetSpec::for_file(snap, runnable.nav.file_id)?;
1347+
let target_spec = TargetSpec::for_file(snap, runnable.nav.file_id)?;
13481348

13491349
match target_spec {
1350-
Some(spec) => {
1350+
Some(TargetSpec::Cargo(spec)) => {
13511351
let workspace_root = spec.workspace_root.clone();
13521352
let target = spec.target.clone();
13531353

crates/rust-analyzer/src/cargo_target_spec.rs renamed to crates/rust-analyzer/src/target_spec.rs

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! See `CargoTargetSpec`
1+
//! See `TargetSpec`
22
33
use std::mem;
44

@@ -10,6 +10,55 @@ use vfs::AbsPathBuf;
1010

1111
use crate::global_state::GlobalStateSnapshot;
1212

13+
/// A target represents a thing we can build or test.
14+
///
15+
/// We use it to calculate the CLI arguments required to build, run or
16+
/// test the target.
17+
#[derive(Clone)]
18+
pub(crate) enum TargetSpec {
19+
Cargo(CargoTargetSpec),
20+
}
21+
22+
impl TargetSpec {
23+
pub(crate) fn for_file(
24+
global_state_snapshot: &GlobalStateSnapshot,
25+
file_id: FileId,
26+
) -> Cancellable<Option<Self>> {
27+
let crate_id = match &*global_state_snapshot.analysis.crates_for(file_id)? {
28+
&[crate_id, ..] => crate_id,
29+
_ => return Ok(None),
30+
};
31+
32+
match global_state_snapshot.cargo_target_for_crate_root(crate_id) {
33+
Some((cargo_ws, target)) => {
34+
let target_data = &cargo_ws[target];
35+
let package_data = &cargo_ws[target_data.package];
36+
let res = CargoTargetSpec {
37+
workspace_root: cargo_ws.workspace_root().to_path_buf(),
38+
cargo_toml: package_data.manifest.clone(),
39+
crate_id,
40+
package: cargo_ws.package_flag(package_data),
41+
target: target_data.name.clone(),
42+
target_kind: target_data.kind,
43+
required_features: target_data.required_features.clone(),
44+
features: package_data.features.keys().cloned().collect(),
45+
};
46+
Ok(Some(TargetSpec::Cargo(res)))
47+
}
48+
None => {
49+
tracing::debug!(?crate_id, "no target found");
50+
Ok(None)
51+
}
52+
}
53+
}
54+
55+
pub(crate) fn target_kind(&self) -> TargetKind {
56+
match self {
57+
TargetSpec::Cargo(cargo) => cargo.target_kind,
58+
}
59+
}
60+
}
61+
1362
/// Abstract representation of Cargo target.
1463
///
1564
/// We use it to cook up the set of cli args we need to pass to Cargo to
@@ -120,35 +169,6 @@ impl CargoTargetSpec {
120169
(args, extra_args)
121170
}
122171

123-
pub(crate) fn for_file(
124-
global_state_snapshot: &GlobalStateSnapshot,
125-
file_id: FileId,
126-
) -> Cancellable<Option<CargoTargetSpec>> {
127-
let crate_id = match &*global_state_snapshot.analysis.crates_for(file_id)? {
128-
&[crate_id, ..] => crate_id,
129-
_ => return Ok(None),
130-
};
131-
let (cargo_ws, target) = match global_state_snapshot.cargo_target_for_crate_root(crate_id) {
132-
Some(it) => it,
133-
None => return Ok(None),
134-
};
135-
136-
let target_data = &cargo_ws[target];
137-
let package_data = &cargo_ws[target_data.package];
138-
let res = CargoTargetSpec {
139-
workspace_root: cargo_ws.workspace_root().to_path_buf(),
140-
cargo_toml: package_data.manifest.clone(),
141-
package: cargo_ws.package_flag(package_data),
142-
target: target_data.name.clone(),
143-
target_kind: target_data.kind,
144-
required_features: target_data.required_features.clone(),
145-
features: package_data.features.keys().cloned().collect(),
146-
crate_id,
147-
};
148-
149-
Ok(Some(res))
150-
}
151-
152172
pub(crate) fn push_to(self, buf: &mut Vec<String>, kind: &RunnableKind) {
153173
buf.push("--package".to_owned());
154174
buf.push(self.package);

0 commit comments

Comments
 (0)