Better structure

This commit is contained in:
Aleksey Kladov 2020-05-15 01:58:39 +02:00
parent 220813dcb0
commit f1a5c489fd
4 changed files with 15 additions and 20 deletions

1
Cargo.lock generated
View file

@ -1376,7 +1376,6 @@ name = "rust-analyzer"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"cargo_metadata",
"crossbeam-channel", "crossbeam-channel",
"env_logger", "env_logger",
"globset", "globset",

View file

@ -12,6 +12,11 @@ use std::{
use cargo_metadata::Message; use cargo_metadata::Message;
use crossbeam_channel::{never, select, unbounded, Receiver, RecvError, Sender}; use crossbeam_channel::{never, select, unbounded, Receiver, RecvError, Sender};
pub use cargo_metadata::diagnostic::{
Applicability, Diagnostic, DiagnosticLevel, DiagnosticSpan,
DiagnosticSpanMacroExpansion,
};
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum FlycheckConfig { pub enum FlycheckConfig {
CargoCommand { command: String, all_targets: bool, all_features: bool, extra_args: Vec<String> }, CargoCommand { command: String, all_targets: bool, all_features: bool, extra_args: Vec<String> },
@ -52,7 +57,7 @@ pub enum CheckTask {
ClearDiagnostics, ClearDiagnostics,
/// Request adding a diagnostic with fixes included to a file /// Request adding a diagnostic with fixes included to a file
AddDiagnostic { workspace_root: PathBuf, diagnostic: cargo_metadata::diagnostic::Diagnostic }, AddDiagnostic { workspace_root: PathBuf, diagnostic: Diagnostic },
/// Request check progress notification to client /// Request check progress notification to client
Status(Status), Status(Status),
@ -239,12 +244,6 @@ impl FlycheckThread {
} }
} }
// #[derive(Debug)]
// pub struct DiagnosticWithFixes {
// diagnostic: Diagnostic,
// fixes: Vec<CodeAction>,
// }
enum CheckEvent { enum CheckEvent {
Begin, Begin,
Msg(cargo_metadata::Message), Msg(cargo_metadata::Message),

View file

@ -29,7 +29,6 @@ rustc-hash = "1.1.0"
serde = { version = "1.0.106", features = ["derive"] } serde = { version = "1.0.106", features = ["derive"] }
serde_json = "1.0.48" serde_json = "1.0.48"
threadpool = "1.7.1" threadpool = "1.7.1"
cargo_metadata = "0.10.0"
stdx = { path = "../stdx" } stdx = { path = "../stdx" }

View file

@ -1,13 +1,5 @@
//! This module provides the functionality needed to convert diagnostics from //! This module provides the functionality needed to convert diagnostics from
//! `cargo check` json format to the LSP diagnostic format. //! `cargo check` json format to the LSP diagnostic format.
use cargo_metadata::diagnostic::{
Applicability, Diagnostic as RustDiagnostic, DiagnosticLevel, DiagnosticSpan,
DiagnosticSpanMacroExpansion,
};
use lsp_types::{
CodeAction, Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, DiagnosticTag,
Location, NumberOrString, Position, Range, TextEdit, Url, WorkspaceEdit,
};
use std::{ use std::{
collections::HashMap, collections::HashMap,
fmt::Write, fmt::Write,
@ -15,6 +7,12 @@ use std::{
str::FromStr, str::FromStr,
}; };
use lsp_types::{
CodeAction, Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, DiagnosticTag,
Location, NumberOrString, Position, Range, TextEdit, Url, WorkspaceEdit,
};
use ra_flycheck::{Applicability, DiagnosticLevel, DiagnosticSpan, DiagnosticSpanMacroExpansion};
/// Converts a Rust level string to a LSP severity /// Converts a Rust level string to a LSP severity
fn map_level_to_severity(val: DiagnosticLevel) -> Option<DiagnosticSeverity> { fn map_level_to_severity(val: DiagnosticLevel) -> Option<DiagnosticSeverity> {
match val { match val {
@ -91,7 +89,7 @@ fn map_secondary_span_to_related(
} }
/// Determines if diagnostic is related to unused code /// Determines if diagnostic is related to unused code
fn is_unused_or_unnecessary(rd: &RustDiagnostic) -> bool { fn is_unused_or_unnecessary(rd: &ra_flycheck::Diagnostic) -> bool {
if let Some(code) = &rd.code { if let Some(code) = &rd.code {
match code.code.as_str() { match code.code.as_str() {
"dead_code" | "unknown_lints" | "unreachable_code" | "unused_attributes" "dead_code" | "unknown_lints" | "unreachable_code" | "unused_attributes"
@ -122,7 +120,7 @@ enum MappedRustChildDiagnostic {
} }
fn map_rust_child_diagnostic( fn map_rust_child_diagnostic(
rd: &RustDiagnostic, rd: &ra_flycheck::Diagnostic,
workspace_root: &PathBuf, workspace_root: &PathBuf,
) -> MappedRustChildDiagnostic { ) -> MappedRustChildDiagnostic {
let spans: Vec<&DiagnosticSpan> = rd.spans.iter().filter(|s| s.is_primary).collect(); let spans: Vec<&DiagnosticSpan> = rd.spans.iter().filter(|s| s.is_primary).collect();
@ -179,7 +177,7 @@ pub(crate) struct MappedRustDiagnostic {
/// ///
/// If the diagnostic has no primary span this will return `None` /// If the diagnostic has no primary span this will return `None`
pub(crate) fn map_rust_diagnostic_to_lsp( pub(crate) fn map_rust_diagnostic_to_lsp(
rd: &RustDiagnostic, rd: &ra_flycheck::Diagnostic,
workspace_root: &PathBuf, workspace_root: &PathBuf,
) -> Vec<MappedRustDiagnostic> { ) -> Vec<MappedRustDiagnostic> {
let primary_spans: Vec<&DiagnosticSpan> = rd.spans.iter().filter(|s| s.is_primary).collect(); let primary_spans: Vec<&DiagnosticSpan> = rd.spans.iter().filter(|s| s.is_primary).collect();