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

Enable nil diagnostics for multiple files #126

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
91 changes: 64 additions & 27 deletions crates/nil/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use anyhow::anyhow;
use anyhow::{Context, Result};
use argh::FromArgs;
use codespan_reporting::diagnostic::Severity;
use codespan_reporting::term::termcolor::WriteColor;
use ide::{AnalysisHost, Severity};
use ide::AnalysisHost;
use ide::Change;
use ide::FileId;
use ide::FileSet;
use ide::SourceRoot;
use ide::VfsPath;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use std::sync::Arc;
Expand Down Expand Up @@ -39,15 +46,15 @@ enum Subcommand {

#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "diagnostics")]
/// Check and print diagnostics for a file.
/// Check and print diagnostics for Nix files.
/// Exit with non-zero code if there are any diagnostics. (`1` for errors, `2` if only warnings)
/// WARNING: The output format is for human and should not be relied on.
struct DiagnosticsArgs {
/// nix file to check, or read from stdin for `-`.
/// nix files to check, or read from stdin for `-`.
/// NB. You need `--` before `-` for paths starting with `-`,
/// to disambiguous it from flags.
#[argh(positional)]
path: PathBuf,
paths: Vec<PathBuf>,
}

#[derive(Debug, FromArgs)]
Expand Down Expand Up @@ -111,32 +118,12 @@ fn main() {
}

fn main_diagnostics(args: DiagnosticsArgs) {
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

let ret = (|| -> Result<Option<Severity>> {
let path = &*args.path;

let src = if path.as_os_str() == "-" {
io::read_to_string(io::stdin().lock()).context("Failed to read from stdin")?
} else {
fs::read_to_string(path).context("Failed to read file")?
};

let (analysis, file) = AnalysisHost::new_single_file(&src);
let diags = analysis
.snapshot()
.diagnostics(file)
.expect("No cancellation");
let ret = diagnostics_for_files(args.paths);

let mut writer = StandardStream::stdout(ColorChoice::Auto);
emit_diagnostics(path, &src, &mut writer, &mut diags.iter().cloned())?;

Ok(diags.iter().map(|diag| diag.severity()).max())
})();
match ret {
Ok(None) => process::exit(0),
Ok(Some(max_severity)) => {
if max_severity > Severity::Warning {
if max_severity > ide::Severity::Warning {
process::exit(1)
} else {
process::exit(0)
Expand All @@ -149,6 +136,56 @@ fn main_diagnostics(args: DiagnosticsArgs) {
}
}

fn diagnostics_for_files(paths: Vec<PathBuf>) -> Result<Option<ide::Severity>> {
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
if paths.is_empty() {
return Err(anyhow!("No files given"));
}

let mut sources = Vec::new();
let mut file_set = FileSet::default();
let mut change = Change::default();
for (id, path) in paths.iter().enumerate() {
let file = FileId(id as u32);

let src = if path.as_os_str() == "-" {
io::read_to_string(io::stdin().lock()).context("Failed to read from stdin")?
} else {
fs::read_to_string(path).context("Failed to read file")?
};

let src: Arc<str> = src.into();
sources.push((file, path, src.clone()));

change.change_file(file, src);
file_set.insert(file, VfsPath::new(path));
}

change.set_roots(vec![SourceRoot::new_local(file_set, None)]);

let mut analysis = AnalysisHost::new();
analysis.apply_change(change);

let snapshot = analysis.snapshot();

let mut writer = StandardStream::stdout(ColorChoice::Auto);
let mut max_severity = None;
for (id, path, src) in sources {
let diagnostics = snapshot.diagnostics(id).expect("No cancellation");
emit_diagnostics(path, &src, &mut writer, &mut diagnostics.iter().cloned())?;

max_severity = std::cmp::max(
max_severity,
diagnostics
.iter()
.map(|diagnostic| diagnostic.severity())
.max(),
);
}

Ok(max_severity)
}

fn main_parse(args: ParseArgs) {
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

Expand Down Expand Up @@ -255,7 +292,7 @@ fn emit_diagnostics(
writer: &mut dyn WriteColor,
diags: &mut dyn Iterator<Item = ide::Diagnostic>,
) -> Result<()> {
use codespan_reporting::diagnostic::{Diagnostic, Label, Severity};
use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term;

Expand Down