Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): report the source path of diagnostics #23

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fmt;

use miette::{Diagnostic, LabeledSpan};
use miette::{Diagnostic, LabeledSpan, MietteSpanContents, SourceCode, SpanContents};
use thisctx::WithContext;
use thiserror::Error;

use crate::cli::IoPath;
use crate::icon::Icon;
use crate::input::InputLine;
use crate::runtime::Severity;
Expand Down Expand Up @@ -57,7 +58,8 @@ pub enum Error {

#[derive(Debug, Error)]
pub(crate) struct ObsoleteIcon<'a> {
pub source_code: &'a InputLine<'a>,
pub input: &'a InputLine<'a>,
pub path: &'a IoPath,
pub icon: &'a Icon,
pub span: (usize, usize),
pub candidates: &'a [&'a Icon],
Expand All @@ -69,9 +71,28 @@ impl fmt::Display for ObsoleteIcon<'_> {
}
}

impl SourceCode for ObsoleteIcon<'_> {
fn read_span<'a>(
&'a self,
span: &miette::SourceSpan,
lines_before: usize,
lines_after: usize,
) -> std::result::Result<Box<dyn SpanContents<'a> + 'a>, miette::MietteError> {
let contents = self.input.read_span(span, lines_before, lines_after)?;
Ok(Box::new(MietteSpanContents::new_named(
self.path.to_string(),
contents.data(),
*contents.span(),
contents.line(),
contents.column(),
contents.line_count(),
)))
}
}

impl Diagnostic for ObsoleteIcon<'_> {
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
Some(self.source_code)
fn source_code(&self) -> Option<&dyn SourceCode> {
Some(self)
}

fn severity(&self) -> Option<miette::Severity> {
Expand Down
17 changes: 9 additions & 8 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,24 @@ impl Runtime {
pub fn check(
&self,
context: &mut CheckerContext,
input: &IoPath,
path: &IoPath,
mut output: Option<&mut String>,
) -> error::Result<bool> {
info!("Check input file from '{}'", input);
info!("Check input from '{}'", path);

if context.size_limit != 0 && input.file_size()?.unwrap_or(0) >= context.size_limit {
warn!("Skip large file '{}'", input);
if context.size_limit != 0 && path.file_size()?.unwrap_or(0) >= context.size_limit {
warn!("Skip large file '{}'", path);
return Ok(false);
}

let mut reader = input.open()?;
let mut reader = path.open()?;
let Some(mut line) = reader.next_line()? else {
return Ok(false);
};

if !context.include_binary && line.content_type() == content_inspector::ContentType::BINARY
{
warn!("Skip binary file '{}'", input);
warn!("Skip binary file '{}'", path);
return Ok(false);
}

Expand All @@ -153,7 +153,8 @@ impl Runtime {
match context.format {
OutputFormat::Console => {
let diag = error::ObsoleteIcon {
source_code: &line,
input: &line,
path,
icon,
span: (start, end),
candidates: &candidates,
Expand All @@ -170,7 +171,7 @@ impl Runtime {
OutputFormat::Json => {
let diag = DiagOutput {
severity: Severity::Info,
path: input.to_string(),
path: path.to_string(),
ty: DiagType::Obsolete {
span: (start, end),
name: icon.name.clone(),
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/check.stderr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INFO Check input file from 'tests/test-data.txt'
INFO Check input from 'tests/test-data.txt'
14 changes: 7 additions & 7 deletions tests/cli/check.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
☞ Found obsolete icon U+F752
╭─[1:24]
╭─[tests/test-data.txt:1:24]
1 │ mdi-folder_multiple = ""
· ┬
· ╰── Icon 'mdi-folder_multiple' is marked as obsolete
Expand All @@ -12,7 +12,7 @@
4. 󱑾 U+F147E md-folder_multiple_plus

☞ Found obsolete icon U+F719
╭─[4:26]
╭─[tests/test-data.txt:4:26]
3 │
4 │ mdi-file_document_box = ""
· ┬
Expand All @@ -26,7 +26,7 @@
4. 󱪗 U+F1A97 md-file_document_alert

☞ Found obsolete icon U+F719
╭─[8:26]
╭─[tests/test-data.txt:8:26]
7 │ # Test autofix from histroy
8 │ mdi-file_document_box = ""
· ┬
Expand All @@ -40,7 +40,7 @@
4. 󱪗 U+F1A97 md-file_document_alert

☞ Found obsolete icon U+F7A1
╭─[11:12]
╭─[tests/test-data.txt:11:12]
10 │
11 │ mdi-git = "",
· ┬
Expand All @@ -52,7 +52,7 @@
2.  U+0E65D seti-git

☞ Found obsolete icon U+F9E8
╭─[14:12]
╭─[tests/test-data.txt:14:12]
13 │
14 │ mdi-tab = "裡"
· ─┬
Expand All @@ -63,7 +63,7 @@
1. 󰓩 U+F04E9 md-tab

☞ Found obsolete icon U+FC0A
╭─[17:24]
╭─[tests/test-data.txt:17:24]
16 │
17 │ mdi-rhombus_outline = "ﰊ"
· ┬
Expand All @@ -77,7 +77,7 @@
4. 󱓝 U+F14DD md-rhombus_split_outline

☞ Found obsolete icon U+F554
╭─[20:26]
╭─[tests/test-data.txt:20:26]
19 │
20 │ mdi-arrow_right_thick = ""
· ┬
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/check_json.stderr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INFO Check input file from 'tests/test-data.txt'
INFO Check input from 'tests/test-data.txt'
2 changes: 1 addition & 1 deletion tests/cli/check_with_db.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
INFO Load input from 'tests/test-icons.json'
INFO Check input file from 'tests/test-data.txt'
INFO Check input from 'tests/test-data.txt'
4 changes: 2 additions & 2 deletions tests/cli/check_with_db.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
☞ Found obsolete icon U+F719
╭─[4:26]
╭─[tests/test-data.txt:4:26]
3 │
4 │ mdi-file_document_box = ""
· ┬
Expand All @@ -10,7 +10,7 @@
1. 󰈙 U+F0219 md-file_document

☞ Found obsolete icon U+F719
╭─[8:26]
╭─[tests/test-data.txt:8:26]
7 │ # Test autofix from histroy
8 │ mdi-file_document_box = ""
· ┬
Expand Down
16 changes: 8 additions & 8 deletions tests/cli/fix.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
INFO Check input file from 'tests/test-data.txt'
INFO Check input from 'tests/test-data.txt'
☞ Found obsolete icon U+F752
╭─[1:24]
╭─[tests/test-data.txt:1:24]
1 │ mdi-folder_multiple = ""
· ┬
· ╰── Icon 'mdi-folder_multiple' is marked as obsolete
Expand All @@ -14,7 +14,7 @@

# Autofix with substitution '󰉓'
☞ Found obsolete icon U+F719
╭─[4:26]
╭─[tests/test-data.txt:4:26]
3 │
4 │ mdi-file_document_box = ""
· ┬
Expand All @@ -29,7 +29,7 @@

# Autofix with the first suggestion '󰈙'
☞ Found obsolete icon U+F719
╭─[8:26]
╭─[tests/test-data.txt:8:26]
7 │ # Test autofix from histroy
8 │ mdi-file_document_box = ""
· ┬
Expand All @@ -44,7 +44,7 @@

# Autofix with the first suggestion '󰈙'
☞ Found obsolete icon U+F7A1
╭─[11:12]
╭─[tests/test-data.txt:11:12]
10 │
11 │ mdi-git = "",
· ┬
Expand All @@ -57,7 +57,7 @@

# Autofix with substitution '󰊢'
☞ Found obsolete icon U+F9E8
╭─[14:12]
╭─[tests/test-data.txt:14:12]
13 │
14 │ mdi-tab = "裡"
· ─┬
Expand All @@ -69,7 +69,7 @@

# Autofix with substitution '󰓩'
☞ Found obsolete icon U+FC0A
╭─[17:24]
╭─[tests/test-data.txt:17:24]
16 │
17 │ mdi-rhombus_outline = "ﰊ"
· ┬
Expand All @@ -84,7 +84,7 @@

# Autofix with substitution '󰜌'
☞ Found obsolete icon U+F554
╭─[20:26]
╭─[tests/test-data.txt:20:26]
19 │
20 │ mdi-arrow_right_thick = ""
· ┬
Expand Down
16 changes: 8 additions & 8 deletions tests/cli/fix_with_exact_subs.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
INFO Load input from 'src/icons.json'
INFO Load input from 'tests/test-substitutions.json'
INFO Check input file from 'tests/test-data.txt'
INFO Check input from 'tests/test-data.txt'
☞ Found obsolete icon U+F752
╭─[1:24]
╭─[tests/test-data.txt:1:24]
1 │ mdi-folder_multiple = ""
· ┬
· ╰── Icon 'mdi-folder_multiple' is marked as obsolete
Expand All @@ -16,7 +16,7 @@

# Autofix with the first suggestion '󰉓'
☞ Found obsolete icon U+F719
╭─[4:26]
╭─[tests/test-data.txt:4:26]
3 │
4 │ mdi-file_document_box = ""
· ┬
Expand All @@ -31,7 +31,7 @@

# Autofix with substitution '󰈙'
☞ Found obsolete icon U+F719
╭─[8:26]
╭─[tests/test-data.txt:8:26]
7 │ # Test autofix from histroy
8 │ mdi-file_document_box = ""
· ┬
Expand All @@ -46,7 +46,7 @@

# Autofix with substitution '󰈙'
☞ Found obsolete icon U+F7A1
╭─[11:12]
╭─[tests/test-data.txt:11:12]
10 │
11 │ mdi-git = "",
· ┬
Expand All @@ -59,7 +59,7 @@

# Autofix with the first suggestion '󰊢'
☞ Found obsolete icon U+F9E8
╭─[14:12]
╭─[tests/test-data.txt:14:12]
13 │
14 │ mdi-tab = "裡"
· ─┬
Expand All @@ -71,7 +71,7 @@

# Autofix with the first suggestion '󰓩'
☞ Found obsolete icon U+FC0A
╭─[17:24]
╭─[tests/test-data.txt:17:24]
16 │
17 │ mdi-rhombus_outline = "ﰊ"
· ┬
Expand All @@ -86,7 +86,7 @@

# Autofix with the first suggestion '󰜌'
☞ Found obsolete icon U+F554
╭─[20:26]
╭─[tests/test-data.txt:20:26]
19 │
20 │ mdi-arrow_right_thick = ""
· ┬
Expand Down
16 changes: 8 additions & 8 deletions tests/cli/fix_with_prefix_subs.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
INFO Load input from 'src/icons.json'
INFO Check input file from 'tests/test-data.txt'
INFO Check input from 'tests/test-data.txt'
☞ Found obsolete icon U+F752
╭─[1:24]
╭─[tests/test-data.txt:1:24]
1 │ mdi-folder_multiple = ""
· ┬
· ╰── Icon 'mdi-folder_multiple' is marked as obsolete
Expand All @@ -15,7 +15,7 @@

# Autofix with substitution '󰉓'
☞ Found obsolete icon U+F719
╭─[4:26]
╭─[tests/test-data.txt:4:26]
3 │
4 │ mdi-file_document_box = ""
· ┬
Expand All @@ -30,7 +30,7 @@

# Autofix with the first suggestion '󰈙'
☞ Found obsolete icon U+F719
╭─[8:26]
╭─[tests/test-data.txt:8:26]
7 │ # Test autofix from histroy
8 │ mdi-file_document_box = ""
· ┬
Expand All @@ -45,7 +45,7 @@

# Autofix with the first suggestion '󰈙'
☞ Found obsolete icon U+F7A1
╭─[11:12]
╭─[tests/test-data.txt:11:12]
10 │
11 │ mdi-git = "",
· ┬
Expand All @@ -58,7 +58,7 @@

# Autofix with substitution '󰊢'
☞ Found obsolete icon U+F9E8
╭─[14:12]
╭─[tests/test-data.txt:14:12]
13 │
14 │ mdi-tab = "裡"
· ─┬
Expand All @@ -70,7 +70,7 @@

# Autofix with substitution '󰓩'
☞ Found obsolete icon U+FC0A
╭─[17:24]
╭─[tests/test-data.txt:17:24]
16 │
17 │ mdi-rhombus_outline = "ﰊ"
· ┬
Expand All @@ -85,7 +85,7 @@

# Autofix with substitution '󰜌'
☞ Found obsolete icon U+F554
╭─[20:26]
╭─[tests/test-data.txt:20:26]
19 │
20 │ mdi-arrow_right_thick = ""
· ┬
Expand Down
Loading