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

Commit 9bbe734

Browse files
authored
Merge pull request #1512 from Xanewok/refactor-arc-handling
Clean up a bit
2 parents ff0b905 + a689be2 commit 9bbe734

File tree

12 files changed

+118
-103
lines changed

12 files changed

+118
-103
lines changed

rls-vfs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub enum VfsSpan {
5050
Utf16CodeUnit(SpanData),
5151
}
5252

53+
#[allow(clippy::len_without_is_empty)]
5354
impl VfsSpan {
5455
pub fn from_usv(span: span::Span<span::ZeroIndexed>, len: Option<u64>) -> VfsSpan {
5556
VfsSpan::UnicodeScalarValue(SpanData { span, len })

rls/src/actions/hover.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,11 @@ fn tooltip_local_variable_usage(
255255
doc_url: Option<String>,
256256
) -> Vec<MarkedString> {
257257
debug!("tooltip_local_variable_usage: {}", def.name);
258-
let vfs = ctx.vfs.clone();
259258

260259
let the_type = def.value.trim().into();
261260
let mut context = String::new();
262261
if ctx.config.lock().unwrap().show_hover_context {
263-
match vfs.load_line(&def.span.file, def.span.range.row_start) {
262+
match ctx.vfs.load_line(&def.span.file, def.span.range.row_start) {
264263
Ok(line) => {
265264
context.push_str(line.trim());
266265
}
@@ -282,7 +281,7 @@ fn tooltip_local_variable_usage(
282281
fn tooltip_type(ctx: &InitActionContext, def: &Def, doc_url: Option<String>) -> Vec<MarkedString> {
283282
debug!("tooltip_type: {}", def.name);
284283

285-
let vfs = ctx.vfs.clone();
284+
let vfs = &ctx.vfs;
286285

287286
let the_type = || def.value.trim().into();
288287
let the_type = def_decl(def, &vfs, the_type);
@@ -299,7 +298,7 @@ fn tooltip_field_or_variant(
299298
) -> Vec<MarkedString> {
300299
debug!("tooltip_field_or_variant: {}", def.name);
301300

302-
let vfs = ctx.vfs.clone();
301+
let vfs = &ctx.vfs;
303302

304303
let the_type = def.value.trim().into();
305304
let docs = def_docs(def, &vfs);
@@ -315,7 +314,7 @@ fn tooltip_struct_enum_union_trait(
315314
) -> Vec<MarkedString> {
316315
debug!("tooltip_struct_enum_union_trait: {}", def.name);
317316

318-
let vfs = ctx.vfs.clone();
317+
let vfs = &ctx.vfs;
319318
let fmt_config = ctx.fmt_config();
320319
// We hover often, so use the in-process one to speed things up.
321320
let fmt = Rustfmt::Internal;
@@ -341,7 +340,7 @@ fn tooltip_struct_enum_union_trait(
341340
fn tooltip_mod(ctx: &InitActionContext, def: &Def, doc_url: Option<String>) -> Vec<MarkedString> {
342341
debug!("tooltip_mod: name: {}", def.name);
343342

344-
let vfs = ctx.vfs.clone();
343+
let vfs = &ctx.vfs;
345344

346345
let the_type = def.value.trim();
347346
let the_type = the_type.replace("\\\\", "/");
@@ -370,7 +369,7 @@ fn tooltip_function_method(
370369
) -> Vec<MarkedString> {
371370
debug!("tooltip_function_method: {}", def.name);
372371

373-
let vfs = ctx.vfs.clone();
372+
let vfs = &ctx.vfs;
374373
let fmt_config = ctx.fmt_config();
375374
// We hover often, so use the in-process one to speed things up.
376375
let fmt = Rustfmt::Internal;
@@ -441,11 +440,9 @@ fn tooltip_static_const_decl(
441440
) -> Vec<MarkedString> {
442441
debug!("tooltip_static_const_decl: {}", def.name);
443442

444-
let vfs = ctx.vfs.clone();
443+
let vfs = &ctx.vfs;
445444

446-
let the_type = def.value.trim().into();
447-
448-
let the_type = def_decl(def, &vfs, || the_type);
445+
let the_type = def_decl(def, &vfs, || def.value.trim().into());
449446
let docs = def_docs(def, &vfs);
450447
let context = None;
451448

@@ -684,15 +681,14 @@ fn racer_match_to_def(ctx: &InitActionContext, m: &racer::Match) -> Option<Def>
684681
/// Uses racer to synthesize a `Def` for the given `span`. If no appropriate
685682
/// match is found with coordinates, `None` is returned.
686683
fn racer_def(ctx: &InitActionContext, span: &Span<ZeroIndexed>) -> Option<Def> {
687-
let vfs = ctx.vfs.clone();
688684
let file_path = &span.file;
689685

690686
if !file_path.as_path().exists() {
691687
error!("racer_def: skipping non-existant file: {:?}", file_path);
692688
return None;
693689
}
694690

695-
let name = vfs.load_line(file_path.as_path(), span.range.row_start).ok().and_then(|line| {
691+
let name = ctx.vfs.load_line(file_path.as_path(), span.range.row_start).ok().and_then(|line| {
696692
let col_start = span.range.col_start.0 as usize;
697693
let col_end = span.range.col_end.0 as usize;
698694
line.get(col_start..col_end).map(ToOwned::to_owned)

rls/src/actions/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ impl ActionContext {
8686
let ctx = match *self {
8787
ActionContext::Uninit(ref uninit) => {
8888
let ctx = InitActionContext::new(
89-
uninit.analysis.clone(),
90-
uninit.vfs.clone(),
91-
uninit.config.clone(),
89+
Arc::clone(&uninit.analysis),
90+
Arc::clone(&uninit.vfs),
91+
Arc::clone(&uninit.config),
9292
client_capabilities,
9393
current_project,
9494
uninit.pid,
@@ -189,7 +189,7 @@ impl InitActionContext {
189189
pid: u32,
190190
client_supports_cmd_run: bool,
191191
) -> InitActionContext {
192-
let build_queue = BuildQueue::new(vfs.clone(), config.clone());
192+
let build_queue = BuildQueue::new(Arc::clone(&vfs), Arc::clone(&config));
193193
let analysis_queue = Arc::new(AnalysisQueue::init());
194194
InitActionContext {
195195
analysis,
@@ -226,7 +226,7 @@ impl InitActionContext {
226226
info!("loading cargo project model");
227227
let pm = ProjectModel::load(&self.current_project.join("Cargo.toml"), &self.vfs)?;
228228
let pm = Arc::new(pm);
229-
*self.project_model.lock().unwrap() = Some(pm.clone());
229+
*self.project_model.lock().unwrap() = Some(Arc::clone(&pm));
230230
Ok(pm)
231231
}
232232
}
@@ -245,7 +245,7 @@ impl InitActionContext {
245245
}
246246
}
247247
}
248-
racer::FileCache::new(RacerVfs(self.vfs.clone()))
248+
racer::FileCache::new(RacerVfs(Arc::clone(&self.vfs)))
249249
}
250250

251251
pub fn racer_session<'c>(&self, cache: &'c racer::FileCache) -> racer::Session<'c> {
@@ -326,15 +326,15 @@ impl InitActionContext {
326326
let pbh = {
327327
let config = self.config.lock().unwrap();
328328
PostBuildHandler {
329-
analysis: self.analysis.clone(),
330-
analysis_queue: self.analysis_queue.clone(),
331-
previous_build_results: self.previous_build_results.clone(),
332-
file_to_crates: self.file_to_crates.clone(),
329+
analysis: Arc::clone(&self.analysis),
330+
analysis_queue: Arc::clone(&self.analysis_queue),
331+
previous_build_results: Arc::clone(&self.previous_build_results),
332+
file_to_crates: Arc::clone(&self.file_to_crates),
333333
project_path: project_path.to_owned(),
334334
show_warnings: config.show_warnings,
335335
related_information_support: self.client_capabilities.related_information_support,
336-
shown_cargo_error: self.shown_cargo_error.clone(),
337-
active_build_count: self.active_build_count.clone(),
336+
shown_cargo_error: Arc::clone(&self.shown_cargo_error),
337+
active_build_count: Arc::clone(&self.active_build_count),
338338
crate_blacklist: config.crate_blacklist.as_ref().clone(),
339339
notifier: Box::new(BuildDiagnosticsNotifier::new(out.clone())),
340340
blocked_threads: vec![],

rls/src/actions/notifications.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::Span;
55
use log::{debug, trace, warn};
66
use rls_vfs::{Change, VfsSpan};
77
use std::sync::atomic::Ordering;
8+
use std::sync::Arc;
89

910
use crate::build::*;
1011
use crate::lsp_data::request::{RangeFormatting, RegisterCapability, UnregisterCapability};
@@ -170,7 +171,7 @@ impl BlockingNotificationAction for DidChangeConfiguration {
170171

171172
if needs_inference {
172173
let project_dir = ctx.current_project.clone();
173-
let config = ctx.config.clone();
174+
let config = Arc::clone(&ctx.config);
174175
// Will lock and access Config just outside the current scope
175176
thread::spawn(move || {
176177
let mut config = config.lock().unwrap();

rls/src/actions/post_build.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl PostBuildHandler {
5454

5555
// Emit appropriate diagnostics using the ones from build.
5656
self.handle_messages(&cwd, &messages);
57-
let analysis_queue = self.analysis_queue.clone();
57+
let analysis_queue = Arc::clone(&self.analysis_queue);
5858

5959
{
6060
let mut files_to_crates = self.file_to_crates.lock().unwrap();
@@ -232,10 +232,13 @@ pub struct AnalysisQueue {
232232
impl AnalysisQueue {
233233
// Create a new queue and start the worker thread.
234234
pub fn init() -> AnalysisQueue {
235-
let queue = Arc::new(Mutex::new(Vec::new()));
236-
let queue_clone = queue.clone();
237-
let worker_thread =
238-
thread::spawn(move || AnalysisQueue::run_worker_thread(queue_clone)).thread().clone();
235+
let queue = Arc::default();
236+
let worker_thread = thread::spawn({
237+
let queue = Arc::clone(&queue);
238+
|| AnalysisQueue::run_worker_thread(queue)
239+
})
240+
.thread()
241+
.clone();
239242

240243
AnalysisQueue { cur_cwd: Mutex::new(None), queue, worker_thread }
241244
}
@@ -293,6 +296,7 @@ impl Drop for AnalysisQueue {
293296
}
294297
}
295298

299+
#[allow(clippy::large_enum_variant)]
296300
enum QueuedJob {
297301
Job(Job),
298302
Terminate,

rls/src/actions/requests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ impl RequestAction for Definition {
183183
// Save-analysis thread.
184184
let file_path = parse_file_path!(&params.text_document.uri, "goto_def")?;
185185
let span = ctx.convert_pos_to_span(file_path.clone(), params.position);
186-
let analysis = ctx.analysis.clone();
187186

188-
if let Ok(out) = analysis.goto_def(&span) {
187+
if let Ok(out) = ctx.analysis.goto_def(&span) {
189188
let result = vec![ls_util::rls_to_location(&out)];
190189
trace!("goto_def (compiler): {:?}", result);
191190
Ok(result)

rls/src/build/cargo.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,48 +40,50 @@ pub(super) fn cargo(
4040
package_arg: PackageArg,
4141
progress_sender: Sender<ProgressUpdate>,
4242
) -> BuildResult {
43-
let compilation_cx = internals.compilation_cx.clone();
44-
let config = internals.config.clone();
45-
let vfs = internals.vfs.clone();
46-
let env_lock = internals.env_lock.clone();
47-
48-
let diagnostics = Arc::new(Mutex::new(vec![]));
49-
let diagnostics_clone = diagnostics.clone();
50-
let analysis = Arc::new(Mutex::new(vec![]));
51-
let analysis_clone = analysis.clone();
52-
let input_files = Arc::new(Mutex::new(HashMap::new()));
53-
let input_files_clone = input_files.clone();
54-
let out = Arc::new(Mutex::new(vec![]));
55-
let out_clone = out.clone();
43+
let compilation_cx = Arc::clone(&internals.compilation_cx);
44+
let config = Arc::clone(&internals.config);
45+
let vfs = Arc::clone(&internals.vfs);
46+
let env_lock = Arc::clone(&internals.env_lock);
47+
48+
let diagnostics = Arc::default();
49+
let analysis = Arc::default();
50+
let input_files = Arc::default();
51+
let out = Arc::default();
5652

5753
// Cargo may or may not spawn threads to run the various builds, since
5854
// we may be in separate threads we need to block and wait our thread.
5955
// However, if Cargo doesn't run a separate thread, then we'll just wait
6056
// forever. Therefore, we spawn an extra thread here to be safe.
61-
let handle = thread::spawn(move || {
62-
run_cargo(
63-
compilation_cx,
64-
package_arg,
65-
config,
66-
vfs,
67-
env_lock,
68-
diagnostics,
69-
analysis,
70-
input_files,
71-
out,
72-
progress_sender,
73-
)
57+
let handle = thread::spawn({
58+
let diagnostics = Arc::clone(&diagnostics);
59+
let analysis = Arc::clone(&analysis);
60+
let input_files = Arc::clone(&input_files);
61+
let out = Arc::clone(&out);
62+
|| {
63+
run_cargo(
64+
compilation_cx,
65+
package_arg,
66+
config,
67+
vfs,
68+
env_lock,
69+
diagnostics,
70+
analysis,
71+
input_files,
72+
out,
73+
progress_sender,
74+
)
75+
}
7476
});
7577

7678
match handle.join().map_err(|_| failure::err_msg("thread panicked")).and_then(|res| res) {
7779
Ok(ref cwd) => {
78-
let diagnostics = Arc::try_unwrap(diagnostics_clone).unwrap().into_inner().unwrap();
79-
let analysis = Arc::try_unwrap(analysis_clone).unwrap().into_inner().unwrap();
80-
let input_files = Arc::try_unwrap(input_files_clone).unwrap().into_inner().unwrap();
80+
let diagnostics = Arc::try_unwrap(diagnostics).unwrap().into_inner().unwrap();
81+
let analysis = Arc::try_unwrap(analysis).unwrap().into_inner().unwrap();
82+
let input_files = Arc::try_unwrap(input_files).unwrap().into_inner().unwrap();
8183
BuildResult::Success(cwd.clone(), diagnostics, analysis, input_files, true)
8284
}
8385
Err(error) => {
84-
let stdout = String::from_utf8(out_clone.lock().unwrap().to_owned()).unwrap();
86+
let stdout = String::from_utf8(out.lock().unwrap().to_owned()).unwrap();
8587

8688
let (manifest_path, manifest_error_range) = {
8789
let mae = error.downcast_ref::<ManifestAwareError>();
@@ -257,7 +259,7 @@ fn run_cargo_ws(
257259
analysis,
258260
input_files,
259261
progress_sender,
260-
reached_primary.clone(),
262+
Arc::clone(&reached_primary),
261263
);
262264

263265
let exec = Arc::new(exec) as Arc<dyn Executor>;
@@ -363,9 +365,9 @@ impl Executor for RlsExecutor {
363365
.as_cargo_mut()
364366
.expect("build plan should be properly initialized before running Cargo");
365367

366-
let only_primary = |unit: &Unit<'_>| self.is_primary_package(unit.pkg.package_id());
368+
let only_primary = |unit: Unit<'_>| self.is_primary_package(unit.pkg.package_id());
367369

368-
plan.emplace_dep_with_filter(unit, cx, &only_primary);
370+
plan.emplace_dep_with_filter(*unit, cx, &only_primary);
369371
}
370372

371373
fn force_rebuild(&self, unit: &Unit<'_>) -> bool {

0 commit comments

Comments
 (0)