Skip to content

Commit

Permalink
feat: provides a centralized way to provide diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Aug 22, 2020
1 parent fecfa94 commit 4fd59be
Show file tree
Hide file tree
Showing 28 changed files with 674 additions and 918 deletions.
3 changes: 2 additions & 1 deletion crates/mun_compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ mun_syntax = { version = "=0.2.0", path="../mun_syntax" }
mun_hir = { version = "=0.2.0", path="../mun_hir" }
mun_target = { version = "=0.2.0", path="../mun_target" }
mun_project = { version = "=0.1.0", path = "../mun_project" }
annotate-snippets = { version = "0.6.1", features = ["color"] }
mun_diagnostics = { version = "=0.1.0", path = "../mun_diagnostics" }
annotate-snippets = { version = "0.9.0", features = ["color"] }
unicode-segmentation = "1.6.0"
ansi_term = "0.12.1"
walkdir = "2.3"
Expand Down
216 changes: 0 additions & 216 deletions crates/mun_compiler/src/annotate.rs

This file was deleted.

155 changes: 0 additions & 155 deletions crates/mun_compiler/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,158 +1,3 @@
use mun_hir::diagnostics::DiagnosticSink;
use mun_hir::{FileId, HirDatabase, Module};

use std::cell::RefCell;

use crate::diagnostics_snippets;
use annotate_snippets::{
display_list::DisplayList, formatter::DisplayListFormatter, snippet::Snippet,
};

/// Emits all specified diagnostic messages to the given stream
pub fn emit_diagnostics<'a>(
writer: &mut dyn std::io::Write,
diagnostics: impl IntoIterator<Item = &'a Snippet>,
colors: bool,
) -> Result<(), anyhow::Error> {
let dlf = DisplayListFormatter::new(colors, false);
for diagnostic in diagnostics.into_iter() {
let dl = DisplayList::from(diagnostic.clone());
writeln!(writer, "{}", dlf.format(&dl))?;
}
Ok(())
}

/// Constructs diagnostic messages for the given file.
pub fn diagnostics(db: &dyn HirDatabase, file_id: FileId) -> Vec<Snippet> {
let parse = db.parse(file_id);

let mut result = Vec::new();
// Replace every `\t` symbol by one whitespace in source code because in console it is
// displaying like 1-4 spaces(depending on it position) and by this it breaks highlighting.
// In future here, instead of `replace("\t", " ")`, can be implemented algorithm that
// correctly replace each `\t` into 1-4 space.
let source_code = db.file_text(file_id).to_string().replace("\t", " ");

let relative_file_path = db.file_relative_path(file_id).to_string();

let line_index = db.line_index(file_id);

result.extend(parse.errors().iter().map(|err| {
diagnostics_snippets::syntax_error(
err,
db,
&parse,
&relative_file_path,
&source_code,
&line_index,
)
}));

let result = RefCell::new(result);
let mut sink = DiagnosticSink::new(|d| {
result
.borrow_mut()
.push(diagnostics_snippets::generic_error(
d,
db,
&parse,
&relative_file_path,
&source_code,
&line_index,
));
})
.on::<mun_hir::diagnostics::UnresolvedValue, _>(|d| {
result
.borrow_mut()
.push(diagnostics_snippets::unresolved_value_error(
d,
db,
&parse,
&relative_file_path,
&source_code,
&line_index,
));
})
.on::<mun_hir::diagnostics::UnresolvedType, _>(|d| {
result
.borrow_mut()
.push(diagnostics_snippets::unresolved_type_error(
d,
db,
&parse,
&relative_file_path,
&source_code,
&line_index,
));
})
.on::<mun_hir::diagnostics::ExpectedFunction, _>(|d| {
result
.borrow_mut()
.push(diagnostics_snippets::expected_function_error(
d,
db,
&parse,
&relative_file_path,
&source_code,
&line_index,
));
})
.on::<mun_hir::diagnostics::MismatchedType, _>(|d| {
result
.borrow_mut()
.push(diagnostics_snippets::mismatched_type_error(
d,
db,
&parse,
&relative_file_path,
&source_code,
&line_index,
));
})
.on::<mun_hir::diagnostics::DuplicateDefinition, _>(|d| {
result
.borrow_mut()
.push(diagnostics_snippets::duplicate_definition_error(
d,
db,
&parse,
&relative_file_path,
&source_code,
&line_index,
));
})
.on::<mun_hir::diagnostics::PossiblyUninitializedVariable, _>(|d| {
result
.borrow_mut()
.push(diagnostics_snippets::possibly_uninitialized_variable_error(
d,
db,
&parse,
&relative_file_path,
&source_code,
&line_index,
));
})
.on::<mun_hir::diagnostics::AccessUnknownField, _>(|d| {
result
.borrow_mut()
.push(diagnostics_snippets::access_unknown_field_error(
d,
db,
&parse,
&relative_file_path,
&source_code,
&line_index,
));
});

Module::from(file_id).diagnostics(db, &mut sink);

drop(sink);

result.into_inner()
}

#[cfg(test)]
mod tests {
use crate::{Config, DisplayColor, Driver, PathOrInline, RelativePathBuf};
Expand Down
Loading

0 comments on commit 4fd59be

Please sign in to comment.