Skip to content

Commit 5e37f50

Browse files
committed
emmylua_check: print log warnings and errors to stderr
- Add `--verbose` flags. - Improve docs for `--ignore` and `--config`. - Add logging for include/excludes. Resolves #512
1 parent 3b14843 commit 5e37f50

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/emmylua_check/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ emmylua_parser.workspace = true
1919
serde.workspace = true
2020
serde_json.workspace = true
2121
lsp-types.workspace = true
22+
log.workspace = true
23+
fern.workspace = true
2224
rowan.workspace = true
2325
walkdir.workspace = true
2426
structopt.workspace = true

crates/emmylua_check/src/cmd_args.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,41 @@ use structopt::StructOpt;
44
#[derive(Debug, StructOpt, Clone)]
55
#[structopt(name = "emmylua-check", about = "EmmyLua Check")]
66
pub struct CmdArgs {
7-
#[structopt(short, long, parse(from_os_str), help = "Specify configuration file")]
7+
/// Specify configuration file path.
8+
/// If not provided, both ".emmyrc.json" and ".luarc.json" will be searched in the workspace
9+
/// directory
10+
#[structopt(short, long, parse(from_os_str))]
811
pub config: Option<std::path::PathBuf>,
912

10-
#[structopt(parse(from_os_str), help = "Path to the workspace directory")]
13+
/// Path to the workspace directory
14+
#[structopt(parse(from_os_str))]
1115
pub workspace: std::path::PathBuf,
1216

13-
#[structopt(
14-
short,
15-
long,
16-
help = "Comma separated list of ignore patterns",
17-
use_delimiter = true
18-
)]
17+
/// Comma separated list of ignore patterns.
18+
/// Patterns must follow glob syntax
19+
#[structopt(short, long, use_delimiter = true)]
1920
pub ignore: Option<Vec<String>>,
2021

22+
/// Specify output format
2123
#[structopt(
2224
long,
23-
help = "Specify output format (json or text)",
2425
default_value = "text",
2526
possible_values = &OutputFormat::variants(),
2627
case_insensitive = true
2728
)]
2829
pub output_format: OutputFormat,
2930

30-
#[structopt(
31-
long,
32-
help = "Specify output destination (stdout or a file path, only used when output_format is json)",
33-
default_value = "stdout",
34-
parse(try_from_str)
35-
)]
31+
/// Specify output destination (stdout or a file path, only used when output_format is json).
32+
#[structopt(long, default_value = "stdout", parse(try_from_str))]
3633
pub output: OutputDestination,
3734

38-
#[structopt(long, help = "Treat warnings as errors")]
35+
/// Treat warnings as errors
36+
#[structopt(long)]
3937
pub warnings_as_errors: bool,
38+
39+
/// Verbose output
40+
#[structopt(long)]
41+
pub verbose: bool,
4042
}
4143

4244
#[derive(Debug, Clone)]

crates/emmylua_check/src/init.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,29 @@ pub fn calculate_include_and_exclude(
110110

111111
for extension in &emmyrc.runtime.extensions {
112112
if extension.starts_with(".") {
113+
log::info!("Adding extension: **/*{}", extension);
113114
include.push(format!("**/*{}", extension));
114115
} else if extension.starts_with("*.") {
116+
log::info!("Adding extension: **/{}", extension);
115117
include.push(format!("**/{}", extension));
116118
} else {
119+
log::info!("Adding extension: {}", extension);
117120
include.push(extension.clone());
118121
}
119122
}
120123

121124
for ignore_glob in &emmyrc.workspace.ignore_globs {
125+
log::info!("Adding ignore glob: {}", ignore_glob);
122126
exclude.push(ignore_glob.clone());
123127
}
124128

125129
if let Some(ignore) = ignore {
130+
log::info!("Adding ignores from \"--ignore\": {:?}", ignore);
126131
exclude.extend(ignore);
127132
}
128133

129134
for dir in &emmyrc.workspace.ignore_dir {
135+
log::info!("Adding ignore dir: {}", dir);
130136
exclude_dirs.push(PathBuf::from(dir));
131137
}
132138

crates/emmylua_check/src/main.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ mod output;
44

55
use cmd_args::CmdArgs;
66
use emmylua_code_analysis::{DbIndex, FileId};
7+
use fern::Dispatch;
8+
use log::LevelFilter;
79
use output::output_result;
810
use std::{error::Error, path::PathBuf, sync::Arc};
911
use structopt::StructOpt;
@@ -17,6 +19,37 @@ async fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
1719
workspace = std::env::current_dir()?.join(workspace);
1820
}
1921

22+
let verbose = cmd_args.verbose;
23+
let logger = Dispatch::new()
24+
.format(move |out, message, record| {
25+
let (color, reset) = match record.level() {
26+
log::Level::Error => ("\x1b[31m", "\x1b[0m"), // Red
27+
log::Level::Warn => ("\x1b[33m", "\x1b[0m"), // Yellow
28+
log::Level::Info | log::Level::Debug | log::Level::Trace => ("", ""),
29+
};
30+
out.finish(format_args!(
31+
"{}{}: {}{}",
32+
color,
33+
record.level(),
34+
if verbose {
35+
format!("({}) {}", record.target(), message)
36+
} else {
37+
message.to_string()
38+
},
39+
reset
40+
))
41+
})
42+
.level(if verbose {
43+
LevelFilter::Info
44+
} else {
45+
LevelFilter::Warn
46+
})
47+
.chain(std::io::stderr());
48+
49+
if let Err(e) = logger.apply() {
50+
eprintln!("Failed to apply logger: {:?}", e);
51+
}
52+
2053
let analysis = match init::load_workspace(workspace.clone(), cmd_args.config, cmd_args.ignore) {
2154
Some(analysis) => analysis,
2255
None => {

0 commit comments

Comments
 (0)