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

Commit 4c18bc7

Browse files
committed
WIP: Use configurable blacklist
1 parent d099747 commit 4c18bc7

File tree

9 files changed

+97
-55
lines changed

9 files changed

+97
-55
lines changed

Cargo.lock

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ test = false
2121
path = "rls/src/main.rs"
2222

2323
[dependencies]
24-
rls-analysis = "0.17.0"
25-
rls-blacklist = "0.1.3"
24+
# rls-analysis = "0.17.0"
25+
rls-analysis = { path = "rls-analysis" }
2626
rls-data = "0.19"
2727
# FIXME: Release rls-rustc 0.6.0 to crates.io
2828
rls-rustc = { version = "0.6.0", path = "rls-rustc" }

rls-analysis/examples/print-crate-id.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ fn main() {
4242
std::process::exit(1);
4343
}
4444
let loader = Loader::new(PathBuf::from(env::args().nth(1).unwrap()));
45-
let crates = rls_analysis::read_analysis_from_files(&loader, Default::default(), &[]);
45+
let crates =
46+
rls_analysis::read_analysis_from_files(&loader, Default::default(), &[] as &[&str]);
4647

4748
for krate in &crates {
4849
println!("Crate {:?} data version {:?}", krate.id, krate.analysis.version);

rls-analysis/src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub use raw::{name_space_for_def_kind, read_analysis_from_files, Crate, CrateId,
2525
pub use symbol_query::SymbolQuery;
2626

2727
use std::collections::HashMap;
28+
use std::fmt::Debug;
2829
use std::path::{Path, PathBuf};
2930
use std::sync::Mutex;
3031
use std::time::{Instant, SystemTime};
@@ -85,8 +86,6 @@ impl Id {
8586
/// Used to indicate a missing index in the Id.
8687
pub const NULL: Id = Id(u64::MAX);
8788

88-
type Blacklist<'a> = &'a [&'static str];
89-
9089
macro_rules! clone_field {
9190
($field: ident) => {
9291
|x| x.$field.clone()
@@ -126,7 +125,7 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
126125
analysis: Vec<data::Analysis>,
127126
path_prefix: &Path,
128127
base_dir: &Path,
129-
blacklist: Blacklist<'_>,
128+
blacklist: &[impl AsRef<str> + Debug],
130129
) -> AResult<()> {
131130
self.reload_with_blacklist(path_prefix, base_dir, blacklist)?;
132131

@@ -143,14 +142,14 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
143142
}
144143

145144
pub fn reload(&self, path_prefix: &Path, base_dir: &Path) -> AResult<()> {
146-
self.reload_with_blacklist(path_prefix, base_dir, &[])
145+
self.reload_with_blacklist(path_prefix, base_dir, &[] as &[&str])
147146
}
148147

149148
pub fn reload_with_blacklist(
150149
&self,
151150
path_prefix: &Path,
152151
base_dir: &Path,
153-
blacklist: Blacklist<'_>,
152+
blacklist: &[impl AsRef<str> + Debug],
154153
) -> AResult<()> {
155154
trace!("reload_with_blacklist {:?} {:?} {:?}", path_prefix, base_dir, blacklist);
156155
let empty = self.analysis.lock()?.is_none();
@@ -173,14 +172,14 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
173172

174173
/// Reloads the entire project's analysis data.
175174
pub fn hard_reload(&self, path_prefix: &Path, base_dir: &Path) -> AResult<()> {
176-
self.hard_reload_with_blacklist(path_prefix, base_dir, &[])
175+
self.hard_reload_with_blacklist(path_prefix, base_dir, &[] as &[&str])
177176
}
178177

179178
pub fn hard_reload_with_blacklist(
180179
&self,
181180
path_prefix: &Path,
182181
base_dir: &Path,
183-
blacklist: Blacklist<'_>,
182+
blacklist: &[impl AsRef<str> + Debug],
184183
) -> AResult<()> {
185184
trace!("hard_reload {:?} {:?}", path_prefix, base_dir);
186185
// We're going to create a dummy AnalysisHost that we will fill with data,
@@ -542,7 +541,7 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
542541

543542
impl ::std::fmt::Display for Id {
544543
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
545-
self.0.fmt(f)
544+
::std::fmt::Display::fmt(&self.0, f)
546545
}
547546
}
548547

rls-analysis/src/raw.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ pub use data::{
66
};
77

88
use std::collections::HashMap;
9+
use std::fmt::Debug;
910
use std::fs::File;
1011
use std::io::{self, Read};
1112
use std::path::{Path, PathBuf};
1213
use std::time::{Instant, SystemTime};
1314

1415
use crate::listings::{DirectoryListing, ListingKind};
15-
use crate::{AnalysisLoader, Blacklist};
16+
use crate::AnalysisLoader;
1617

1718
#[derive(Debug)]
1819
pub struct Crate {
@@ -45,7 +46,7 @@ impl Crate {
4546
pub fn read_analysis_from_files<L: AnalysisLoader>(
4647
loader: &L,
4748
crate_timestamps: HashMap<PathBuf, SystemTime>,
48-
crate_blacklist: Blacklist<'_>,
49+
crate_blacklist: &[impl AsRef<str> + Debug],
4950
) -> Vec<Crate> {
5051
let mut result = vec![];
5152

@@ -92,8 +93,8 @@ pub fn read_analysis_from_files<L: AnalysisLoader>(
9293
result
9394
}
9495

95-
fn ignore_data(file_name: &str, crate_blacklist: Blacklist<'_>) -> bool {
96-
crate_blacklist.iter().any(|name| file_name.starts_with(&format!("lib{}-", name)))
96+
fn ignore_data(file_name: &str, crate_blacklist: &[impl AsRef<str>]) -> bool {
97+
crate_blacklist.iter().any(|name| file_name.starts_with(&format!("lib{}-", name.as_ref())))
9798
}
9899

99100
fn read_file_contents(path: &Path) -> io::Result<String> {

rls/src/actions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ impl InitActionContext {
335335
related_information_support: self.client_capabilities.related_information_support,
336336
shown_cargo_error: self.shown_cargo_error.clone(),
337337
active_build_count: self.active_build_count.clone(),
338-
use_black_list: config.use_crate_blacklist,
338+
crate_blacklist: config.crate_blacklist.as_ref().clone(),
339339
notifier: Box::new(BuildDiagnosticsNotifier::new(out.clone())),
340340
blocked_threads: vec![],
341341
_token: token,

rls/src/actions/post_build.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::actions::diagnostics::{parse_diagnostics, Diagnostic, ParsedDiagnosti
1616
use crate::actions::progress::DiagnosticsNotifier;
1717
use crate::build::{BuildResult, Crate};
1818
use crate::concurrency::JobToken;
19+
use crate::config::CrateBlacklist;
1920
use crate::lsp_data::{PublishDiagnosticsParams, Range};
2021

2122
use failure;
@@ -35,7 +36,7 @@ pub struct PostBuildHandler {
3536
pub file_to_crates: Arc<Mutex<HashMap<PathBuf, HashSet<Crate>>>>,
3637
pub project_path: PathBuf,
3738
pub show_warnings: bool,
38-
pub use_black_list: bool,
39+
pub crate_blacklist: CrateBlacklist,
3940
pub related_information_support: bool,
4041
pub shown_cargo_error: Arc<AtomicBool>,
4142
pub active_build_count: Arc<AtomicUsize>,
@@ -172,28 +173,15 @@ impl PostBuildHandler {
172173
}
173174

174175
fn reload_analysis_from_disk(&self, cwd: &Path) {
175-
if self.use_black_list {
176-
self.analysis
177-
.reload_with_blacklist(&self.project_path, cwd, &rls_blacklist::CRATE_BLACKLIST)
178-
.unwrap();
179-
} else {
180-
self.analysis.reload(&self.project_path, cwd).unwrap();
181-
}
176+
self.analysis
177+
.reload_with_blacklist(&self.project_path, cwd, &self.crate_blacklist.0[..])
178+
.unwrap();
182179
}
183180

184181
fn reload_analysis_from_memory(&self, cwd: &Path, analysis: Vec<Analysis>) {
185-
if self.use_black_list {
186-
self.analysis
187-
.reload_from_analysis(
188-
analysis,
189-
&self.project_path,
190-
cwd,
191-
&rls_blacklist::CRATE_BLACKLIST,
192-
)
193-
.unwrap();
194-
} else {
195-
self.analysis.reload_from_analysis(analysis, &self.project_path, cwd, &[]).unwrap();
196-
}
182+
self.analysis
183+
.reload_from_analysis(analysis, &self.project_path, cwd, &self.crate_blacklist.0[..])
184+
.unwrap();
197185
}
198186

199187
fn finalize(mut self) {

rls/src/build/cargo.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -498,19 +498,23 @@ impl Executor for RlsExecutor {
498498
cmd.get_envs(),
499499
);
500500

501-
if rls_blacklist::CRATE_BLACKLIST.contains(&&*crate_name) {
501+
let (crate_blacklist, full_docs) = {
502+
let config = self.config.lock().unwrap();
503+
(config.crate_blacklist.clone(), *config.full_docs.clone().as_ref())
504+
};
505+
if crate_blacklist.as_ref().0.contains(&crate_name) {
502506
// By running the original command (rather than using our shim), we
503507
// avoid producing save-analysis data.
504508
trace!("crate is blacklisted");
505509
return cargo_cmd.exec();
506510
}
507511
// Only include public symbols in externally compiled deps data
508-
let mut save_config = rls_data::config::Config::default();
509-
save_config.pub_only = true;
510-
save_config.reachable_only = true;
511-
save_config.full_docs =
512-
self.config.lock().map(|config| *config.full_docs.as_ref()).unwrap();
513-
let save_config = serde_json::to_string(&save_config)?;
512+
let save_config = serde_json::to_string(&rls_data::config::Config {
513+
pub_only: true,
514+
reachable_only: true,
515+
full_docs,
516+
..Default::default()
517+
})?;
514518
cmd.env("RUST_SAVE_ANALYSIS_CONFIG", &OsString::from(save_config));
515519

516520
return cmd.exec();

rls/src/config.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::io::sink;
88
use std::marker::PhantomData;
99
use std::path::{Path, PathBuf};
1010
use std::str::FromStr;
11+
use std::sync::Arc;
1112

1213
use cargo::core::{Shell, Workspace};
1314
use cargo::util::{homedir, important_paths, Config as CargoConfig};
@@ -135,7 +136,9 @@ pub struct Config {
135136
/// `true` to build the project only when a file got saved and not on file change.
136137
/// Default: `false`.
137138
pub build_on_save: bool,
138-
pub use_crate_blacklist: bool,
139+
/// Blacklist of crates for RLS to skip. By default omits `winapi`, Unicode
140+
/// table crates, `serde`, `libc`, `glium` and other.
141+
pub crate_blacklist: Inferrable<CrateBlacklist>,
139142
/// The Cargo target directory. If set, overrides the default one.
140143
pub target_dir: Inferrable<Option<PathBuf>>,
141144
pub features: Vec<String>,
@@ -185,7 +188,7 @@ impl Default for Config {
185188
show_warnings: true,
186189
clear_env_rust_log: true,
187190
build_on_save: false,
188-
use_crate_blacklist: true,
191+
crate_blacklist: Inferrable::Inferred(CrateBlacklist::default()),
189192
target_dir: Inferrable::Inferred(None),
190193
features: vec![],
191194
all_features: false,
@@ -432,10 +435,65 @@ impl Default for FmtConfig {
432435
}
433436
}
434437

438+
#[derive(Clone, Debug, PartialEq)]
439+
/// List of crates for which IDE analysis should not be generated
440+
pub struct CrateBlacklist(pub Arc<[String]>);
441+
442+
impl<'de> Deserialize<'de> for CrateBlacklist {
443+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
444+
where
445+
D: Deserializer<'de>,
446+
{
447+
let boxed = <Box<[String]> as serde::Deserialize>::deserialize(deserializer)?;
448+
Ok(CrateBlacklist(boxed.into()))
449+
}
450+
}
451+
452+
impl Default for CrateBlacklist {
453+
fn default() -> Self {
454+
CrateBlacklist(
455+
[
456+
"cocoa",
457+
"gleam",
458+
"glium",
459+
"idna",
460+
"libc",
461+
"openssl",
462+
"rustc_serialize",
463+
"serde",
464+
"serde_json",
465+
"typenum",
466+
"unicode_normalization",
467+
"unicode_segmentation",
468+
"winapi",
469+
]
470+
.iter()
471+
.map(ToString::to_string)
472+
.collect::<Vec<_>>()
473+
.into(),
474+
)
475+
}
476+
}
477+
435478
#[test]
436479
fn clippy_preference_from_str() {
437480
assert_eq!(ClippyPreference::from_str("Optin"), Ok(ClippyPreference::OptIn));
438481
assert_eq!(ClippyPreference::from_str("OFF"), Ok(ClippyPreference::Off));
439482
assert_eq!(ClippyPreference::from_str("opt-in"), Ok(ClippyPreference::OptIn));
440483
assert_eq!(ClippyPreference::from_str("on"), Ok(ClippyPreference::On));
441484
}
485+
486+
#[test]
487+
fn blacklist_default() {
488+
let value = serde_json::json!({});
489+
let config = Config::try_deserialize(&value, &mut Default::default(), &mut vec![]).unwrap();
490+
assert_eq!(config.crate_blacklist.as_ref(), &CrateBlacklist::default());
491+
let value = serde_json::json!({"crate_blacklist": []});
492+
493+
let config = Config::try_deserialize(&value, &mut Default::default(), &mut vec![]).unwrap();
494+
assert_eq!(&*config.crate_blacklist.as_ref().0, &[] as &[String]);
495+
496+
let value = serde_json::json!({"crate_blacklist": ["serde"]});
497+
let config = Config::try_deserialize(&value, &mut Default::default(), &mut vec![]).unwrap();
498+
assert_eq!(&*config.crate_blacklist.as_ref().0, &["serde".to_string()]);
499+
}

0 commit comments

Comments
 (0)