Skip to content

Commit 6a07290

Browse files
committed
support emmylua_check as lib
1 parent 5cffbb2 commit 6a07290

4 files changed

Lines changed: 111 additions & 15 deletions

File tree

crates/emmylua_check/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ log.workspace = true
2323
fern.workspace = true
2424
rowan.workspace = true
2525
walkdir.workspace = true
26-
tokio.workspace = true
2726
tokio-util.workspace = true
2827
clap.workspace = true
2928
ansi_term.workspace = true
29+
tokio.workspace = true

crates/emmylua_check/src/init.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{path::PathBuf, str::FromStr, sync::Arc};
22

33
use emmylua_code_analysis::{
4-
load_configs, load_workspace_files, update_code_style, EmmyLuaAnalysis, Emmyrc, LuaFileInfo,
4+
load_configs, load_workspace_files, update_code_style, DbIndex, EmmyLuaAnalysis, Emmyrc, FileId, LuaFileInfo
55
};
66

77
fn root_from_configs(config_paths: &Vec<PathBuf>, fallback: &PathBuf) -> PathBuf {
@@ -174,3 +174,15 @@ pub fn calculate_include_and_exclude(
174174

175175
(include, exclude, exclude_dirs)
176176
}
177+
178+
pub fn get_need_check_ids(db: &DbIndex, files: Vec<FileId>, workspace: &PathBuf) -> Vec<FileId> {
179+
let mut need_check_files = Vec::new();
180+
for file_id in files {
181+
let file_path = db.get_vfs().get_file_path(&file_id).unwrap();
182+
if file_path.starts_with(workspace) {
183+
need_check_files.push(file_id);
184+
}
185+
}
186+
187+
need_check_files
188+
}

crates/emmylua_check/src/lib.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
mod cmd_args;
2+
mod init;
3+
mod output;
4+
mod terminal_display;
5+
6+
use cmd_args::CmdArgs;
7+
use fern::Dispatch;
8+
use log::LevelFilter;
9+
use output::output_result;
10+
use std::{error::Error, sync::Arc};
11+
use tokio_util::sync::CancellationToken;
12+
13+
use crate::init::get_need_check_ids;
14+
15+
#[allow(unused)]
16+
async fn run_check(cmd_args: CmdArgs) -> Result<(), Box<dyn Error + Sync + Send>> {
17+
let mut workspace = cmd_args.workspace;
18+
if !workspace.is_absolute() {
19+
workspace = std::env::current_dir()?.join(workspace);
20+
}
21+
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+
53+
let analysis = match init::load_workspace(workspace.clone(), cmd_args.config, cmd_args.ignore) {
54+
Some(analysis) => analysis,
55+
None => {
56+
return Err("Failed to load workspace".into());
57+
}
58+
};
59+
60+
let files = analysis.compilation.get_db().get_vfs().get_all_file_ids();
61+
let db = analysis.compilation.get_db();
62+
let need_check_files = get_need_check_ids(db, files, &workspace);
63+
64+
let (sender, receiver) = tokio::sync::mpsc::channel(100);
65+
let analysis = Arc::new(analysis);
66+
let db = analysis.compilation.get_db();
67+
for file_id in need_check_files.clone() {
68+
let sender = sender.clone();
69+
let analysis = analysis.clone();
70+
tokio::spawn(async move {
71+
let cancel_token = CancellationToken::new();
72+
let diagnostics = analysis.diagnose_file(file_id, cancel_token);
73+
sender.send((file_id, diagnostics)).await.unwrap();
74+
});
75+
}
76+
77+
let exit_code = output_result(
78+
need_check_files.len(),
79+
db,
80+
workspace,
81+
receiver,
82+
cmd_args.output_format,
83+
cmd_args.output,
84+
cmd_args.warnings_as_errors,
85+
)
86+
.await;
87+
88+
if exit_code != 0 {
89+
return Err(format!("exit code: {}", exit_code).into());
90+
}
91+
92+
eprintln!("Check finished");
93+
Ok(())
94+
}

crates/emmylua_check/src/main.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ mod terminal_display;
55

66
use clap::Parser;
77
use cmd_args::CmdArgs;
8-
use emmylua_code_analysis::{DbIndex, FileId};
98
use fern::Dispatch;
109
use log::LevelFilter;
1110
use output::output_result;
12-
use std::{error::Error, path::PathBuf, sync::Arc};
11+
use std::{error::Error, sync::Arc};
1312
use tokio_util::sync::CancellationToken;
1413

14+
use crate::init::get_need_check_ids;
15+
1516
#[tokio::main]
1617
async fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
1718
let cmd_args = CmdArgs::parse();
@@ -95,14 +96,3 @@ async fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
9596
Ok(())
9697
}
9798

98-
fn get_need_check_ids(db: &DbIndex, files: Vec<FileId>, workspace: &PathBuf) -> Vec<FileId> {
99-
let mut need_check_files = Vec::new();
100-
for file_id in files {
101-
let file_path = db.get_vfs().get_file_path(&file_id).unwrap();
102-
if file_path.starts_with(workspace) {
103-
need_check_files.push(file_id);
104-
}
105-
}
106-
107-
need_check_files
108-
}

0 commit comments

Comments
 (0)