From 9e80c8571da68cf8b6f032294e767afc657d8fe8 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 15 May 2023 11:59:03 +0200 Subject: [PATCH] internal: Inline handlers module --- crates/hir-ty/src/layout/adt.rs | 14 ++++---- crates/rust-analyzer/src/handlers.rs | 45 ----------------------- crates/rust-analyzer/src/lib.rs | 6 +++- crates/rust-analyzer/src/main_loop.rs | 52 +++++++++++++++++++++++---- 4 files changed, 58 insertions(+), 59 deletions(-) delete mode 100644 crates/rust-analyzer/src/handlers.rs diff --git a/crates/hir-ty/src/layout/adt.rs b/crates/hir-ty/src/layout/adt.rs index 9dbf9b2419..81793e3795 100644 --- a/crates/hir-ty/src/layout/adt.rs +++ b/crates/hir-ty/src/layout/adt.rs @@ -38,18 +38,18 @@ pub fn layout_of_adt_query( .map(|(fd, _)| layout_of_ty(db, &field_ty(db, def, fd, &subst), cx.krate)) .collect::, _>>() }; - let (variants, is_enum, is_union, repr) = match def { + let (variants, repr) = match def { AdtId::StructId(s) => { let data = db.struct_data(s); let mut r = SmallVec::<[_; 1]>::new(); r.push(handle_variant(s.into(), &data.variant_data)?); - (r, false, false, data.repr.unwrap_or_default()) + (r, data.repr.unwrap_or_default()) } AdtId::UnionId(id) => { let data = db.union_data(id); let mut r = SmallVec::new(); r.push(handle_variant(id.into(), &data.variant_data)?); - (r, false, true, data.repr.unwrap_or_default()) + (r, data.repr.unwrap_or_default()) } AdtId::EnumId(e) => { let data = db.enum_data(e); @@ -63,19 +63,19 @@ pub fn layout_of_adt_query( ) }) .collect::, _>>()?; - (r, true, false, data.repr.unwrap_or_default()) + (r, data.repr.unwrap_or_default()) } }; let variants = variants.iter().map(|x| x.iter().collect::>()).collect::>(); let variants = variants.iter().map(|x| x.iter().collect()).collect(); - if is_union { + if matches!(def, AdtId::UnionId(..)) { cx.layout_of_union(&repr, &variants).ok_or(LayoutError::Unknown) } else { cx.layout_of_struct_or_enum( &repr, &variants, - is_enum, + matches!(def, AdtId::EnumId(..)), is_unsafe_cell(db, def), layout_scalar_valid_range(db, def), |min, max| repr_discr(&dl, &repr, min, max).unwrap_or((Integer::I8, false)), @@ -95,7 +95,7 @@ pub fn layout_of_adt_query( // .iter_enumerated() // .any(|(i, v)| v.discr != ty::VariantDiscr::Relative(i.as_u32())) repr.inhibit_enum_layout_opt(), - !is_enum + !matches!(def, AdtId::EnumId(..)) && variants .iter() .next() diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs deleted file mode 100644 index c19be19654..0000000000 --- a/crates/rust-analyzer/src/handlers.rs +++ /dev/null @@ -1,45 +0,0 @@ -//! This module is responsible for implementing handlers for Language Server -//! Protocol. The majority of requests are fulfilled by calling into the -//! `ide` crate. - -use ide::AssistResolveStrategy; -use lsp_types::{Diagnostic, DiagnosticTag, NumberOrString, Url}; - -use vfs::FileId; - -use crate::{global_state::GlobalStateSnapshot, to_proto, Result}; - -pub(crate) mod request; -pub(crate) mod notification; - -pub(crate) fn publish_diagnostics( - snap: &GlobalStateSnapshot, - file_id: FileId, -) -> Result> { - let _p = profile::span("publish_diagnostics"); - let line_index = snap.file_line_index(file_id)?; - - let diagnostics: Vec = snap - .analysis - .diagnostics(&snap.config.diagnostics(), AssistResolveStrategy::None, file_id)? - .into_iter() - .map(|d| Diagnostic { - range: to_proto::range(&line_index, d.range), - severity: Some(to_proto::diagnostic_severity(d.severity)), - code: Some(NumberOrString::String(d.code.as_str().to_string())), - code_description: Some(lsp_types::CodeDescription { - href: Url::parse(&format!( - "https://rust-analyzer.github.io/manual.html#{}", - d.code.as_str() - )) - .unwrap(), - }), - source: Some("rust-analyzer".to_string()), - message: d.message, - related_information: None, - tags: if d.unused { Some(vec![DiagnosticTag::UNNECESSARY]) } else { None }, - data: None, - }) - .collect(); - Ok(diagnostics) -} diff --git a/crates/rust-analyzer/src/lib.rs b/crates/rust-analyzer/src/lib.rs index 32dc3750fd..65de4366e9 100644 --- a/crates/rust-analyzer/src/lib.rs +++ b/crates/rust-analyzer/src/lib.rs @@ -25,7 +25,6 @@ mod diff; mod dispatch; mod from_proto; mod global_state; -mod handlers; mod line_index; mod lsp_utils; mod main_loop; @@ -38,6 +37,11 @@ mod task_pool; mod to_proto; mod version; +mod handlers { + pub(crate) mod notification; + pub(crate) mod request; +} + pub mod config; pub mod lsp_ext; diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index ea8dbc5438..f06abe0763 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -764,15 +764,55 @@ impl GlobalState { let snapshot = self.snapshot(); self.task_pool.handle.spawn(move || { + let _p = profile::span("publish_diagnostics"); let diagnostics = subscriptions .into_iter() .filter_map(|file_id| { - crate::handlers::publish_diagnostics(&snapshot, file_id) - .ok() - .map(|diags| (file_id, diags)) + let line_index = snapshot.file_line_index(file_id).ok()?; + Some(( + file_id, + line_index, + snapshot + .analysis + .diagnostics( + &snapshot.config.diagnostics(), + ide::AssistResolveStrategy::None, + file_id, + ) + .ok()?, + )) }) - .collect::>(); - Task::Diagnostics(diagnostics) - }) + .map(|(file_id, line_index, it)| { + ( + file_id, + it.into_iter() + .map(move |d| lsp_types::Diagnostic { + range: crate::to_proto::range(&line_index, d.range), + severity: Some(crate::to_proto::diagnostic_severity(d.severity)), + code: Some(lsp_types::NumberOrString::String( + d.code.as_str().to_string(), + )), + code_description: Some(lsp_types::CodeDescription { + href: lsp_types::Url::parse(&format!( + "https://rust-analyzer.github.io/manual.html#{}", + d.code.as_str() + )) + .unwrap(), + }), + source: Some("rust-analyzer".to_string()), + message: d.message, + related_information: None, + tags: if d.unused { + Some(vec![lsp_types::DiagnosticTag::UNNECESSARY]) + } else { + None + }, + data: None, + }) + .collect::>(), + ) + }); + Task::Diagnostics(diagnostics.collect()) + }); } }