Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 96d7461

Browse files
authored
Merge pull request #500 from Eijebong/features
Add support for cargo features
2 parents 47b5b13 + 82d5b0b commit 96d7461

File tree

6 files changed

+116
-1
lines changed

6 files changed

+116
-1
lines changed

src/build/cargo.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ fn run_cargo(compilation_cx: Arc<Mutex<CompilationContext>>,
147147
// TODO: Support more crate target types
148148
&[], false, &[], false, &[], false,
149149
false),
150+
features: &opts.features,
151+
all_features: opts.all_features,
152+
no_default_features: opts.no_default_features,
150153
.. CompileOptions::default(&config, CompileMode::Check)
151154
};
152155

@@ -438,6 +441,9 @@ struct CargoOptions {
438441
bins: bool,
439442
all: bool,
440443
exclude: Vec<String>,
444+
all_features: bool,
445+
no_default_features: bool,
446+
features: Vec<String>,
441447
}
442448

443449
impl Default for CargoOptions {
@@ -450,6 +456,9 @@ impl Default for CargoOptions {
450456
bins: false,
451457
all: false,
452458
exclude: vec![],
459+
all_features: false,
460+
no_default_features: false,
461+
features: vec![],
453462
}
454463
}
455464
}
@@ -466,6 +475,9 @@ impl CargoOptions {
466475
package,
467476
all,
468477
target: config.target.clone(),
478+
features: config.features.clone(),
479+
all_features: config.all_features,
480+
no_default_features: config.no_default_features,
469481
.. CargoOptions::default()
470482
}
471483
} else {
@@ -486,6 +498,9 @@ impl CargoOptions {
486498
lib,
487499
bin,
488500
target: config.target.clone(),
501+
features: config.features.clone(),
502+
all_features: config.all_features,
503+
no_default_features: config.no_default_features,
489504
.. CargoOptions::default()
490505
}
491506
}

src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ pub struct Config {
117117
/// Cargo target dir. If set overrides the default one.
118118
#[serde(skip_deserializing, skip_serializing)]
119119
pub target_dir: Option<PathBuf>,
120+
pub features: Vec<String>,
121+
pub all_features: bool,
122+
pub no_default_features: bool,
120123
}
121124

122125
impl Default for Config {
@@ -138,6 +141,9 @@ impl Default for Config {
138141
build_on_save: false,
139142
use_crate_blacklist: true,
140143
target_dir: None,
144+
features: vec![],
145+
all_features: false,
146+
no_default_features: false,
141147
};
142148
result.normalise();
143149
result

src/test/mod.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,73 @@ fn test_find_impls() {
712712
// ]);
713713
}
714714

715+
#[test]
716+
fn test_features() {
717+
let mut env = Environment::new("features");
718+
719+
let root_path = env.cache.abs_path(Path::new("."));
720+
let messages = vec![
721+
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string()
722+
];
723+
724+
env.with_config(|c| c.features = vec!["foo".to_owned()]);
725+
let (mut server, results) = env.mock_server(messages);
726+
// Initialise and build.
727+
assert_eq!(ls_server::LsService::handle_message(&mut server),
728+
ls_server::ServerStateChange::Continue);
729+
expect_messages(results.clone(), &[ExpectedMessage::new(Some(0)).expect_contains("capabilities"),
730+
ExpectedMessage::new(None).expect_contains("beginBuild"),
731+
ExpectedMessage::new(None).expect_contains("diagnosticsBegin"),
732+
ExpectedMessage::new(None).expect_contains(r#""message":"cannot find struct, variant or union type `Bar` in this scope""#),
733+
ExpectedMessage::new(None).expect_contains("diagnosticsEnd")]);
734+
}
735+
736+
#[test]
737+
fn test_all_features() {
738+
let mut env = Environment::new("features");
739+
740+
let root_path = env.cache.abs_path(Path::new("."));
741+
let messages = vec![
742+
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string()
743+
];
744+
745+
env.with_config(|c| c.all_features = true);
746+
let (mut server, results) = env.mock_server(messages);
747+
// Initialise and build.
748+
assert_eq!(ls_server::LsService::handle_message(&mut server),
749+
ls_server::ServerStateChange::Continue);
750+
expect_messages(results.clone(), &[ExpectedMessage::new(Some(0)).expect_contains("capabilities"),
751+
ExpectedMessage::new(None).expect_contains("beginBuild"),
752+
ExpectedMessage::new(None).expect_contains("diagnosticsBegin"),
753+
ExpectedMessage::new(None).expect_contains("diagnosticsEnd")]);
754+
}
755+
756+
#[test]
757+
fn test_no_default_features() {
758+
let mut env = Environment::new("features");
759+
760+
let root_path = env.cache.abs_path(Path::new("."));
761+
let messages = vec![
762+
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string()
763+
];
764+
765+
env.with_config(|c| {
766+
c.no_default_features = true;
767+
c.features = vec!["foo".to_owned(), "bar".to_owned()]
768+
});
769+
let (mut server, results) = env.mock_server(messages);
770+
// Initialise and build.
771+
assert_eq!(ls_server::LsService::handle_message(&mut server),
772+
ls_server::ServerStateChange::Continue);
773+
expect_messages(results.clone(), &[ExpectedMessage::new(Some(0)).expect_contains("capabilities"),
774+
ExpectedMessage::new(None).expect_contains("beginBuild"),
775+
ExpectedMessage::new(None).expect_contains("diagnosticsBegin"),
776+
ExpectedMessage::new(None).expect_contains(r#""message":"cannot find struct, variant or union type `Baz` in this scope""#),
777+
ExpectedMessage::new(None).expect_contains("diagnosticsEnd")]);
778+
}
779+
715780
// #[test]
716-
// fn test_handle_utf8_directory) {
781+
// fn test_handle_utf8_directory() {
717782
// let mut env = Environment::new("unicødë");
718783
//
719784
// let root_path = env.cache.abs_path(Path::new("."));
@@ -733,3 +798,4 @@ fn test_find_impls() {
733798
// .expect_contains(root_url.path())
734799
// .expect_contains("struct is never used: `Unused`"),
735800
// ExpectedMessage::new(None).expect_contains("diagnosticsEnd")]);
801+
// }

test_data/features/Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test_data/features/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "features"
3+
version = "0.1.0"
4+
authors = ["Bastien Orivel <eijebong@bananium.fr>"]
5+
6+
[features]
7+
default = ["baz"]
8+
foo = []
9+
bar = []
10+
baz = []

test_data/features/src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[cfg(feature = "foo")]
2+
pub struct Foo;
3+
4+
#[cfg(feature = "bar")]
5+
pub struct Bar;
6+
7+
#[cfg(feature = "baz")]
8+
pub struct Baz;
9+
10+
fn main() {
11+
Foo {};
12+
Bar {};
13+
Baz {};
14+
}

0 commit comments

Comments
 (0)