Skip to content

Commit 5281138

Browse files
committed
fix std resource load, and cli tool relative path issue
1 parent 2d96991 commit 5281138

File tree

6 files changed

+43
-44
lines changed

6 files changed

+43
-44
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# CHANGELOG
22

3-
# 0.4.3(unreleased)
3+
# 0.4.4(unreleased)
4+
5+
# 0.4.3
6+
7+
`FIX` Fix std resource loaded for cli tools
48

59
# 0.4.2
610

crates/emmylua_check/src/main.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@ use tokio_util::sync::CancellationToken;
1212
#[tokio::main]
1313
async fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
1414
let cmd_args = CmdArgs::from_args();
15-
let analysis =
16-
match init::load_workspace(cmd_args.workspace.clone(), cmd_args.config, cmd_args.ignore) {
17-
Some(analysis) => analysis,
18-
None => {
19-
eprintln!("Failed to load workspace");
20-
return Err("Failed to load workspace".into());
21-
}
22-
};
23-
2415
let mut workspace = cmd_args.workspace;
2516
if !workspace.is_absolute() {
2617
workspace = std::env::current_dir()?.join(workspace);
2718
}
2819

20+
let analysis = match init::load_workspace(workspace.clone(), cmd_args.config, cmd_args.ignore) {
21+
Some(analysis) => analysis,
22+
None => {
23+
eprintln!("Failed to load workspace");
24+
return Err("Failed to load workspace".into());
25+
}
26+
};
27+
2928
let files = analysis.compilation.get_db().get_vfs().get_all_file_ids();
3029
let db = analysis.compilation.get_db();
3130
let need_check_files = get_need_check_ids(db, files, &workspace);
@@ -50,13 +49,13 @@ async fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
5049
receiver,
5150
cmd_args.output_format,
5251
cmd_args.output,
53-
cmd_args.warnings_as_errors
52+
cmd_args.warnings_as_errors,
5453
)
5554
.await;
5655

5756
if exit_code != 0 {
5857
return Err(format!("exit code: {}", exit_code).into());
59-
}
58+
}
6059

6160
eprintln!("Check finished");
6261
Ok(())

crates/emmylua_code_analysis/src/lib.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ impl EmmyLuaAnalysis {
4646
}
4747

4848
pub fn init_std_lib(&mut self, allow_create_resources_dir: bool) {
49-
let files = load_resource_std(allow_create_resources_dir);
49+
let (std_root, files) = load_resource_std(allow_create_resources_dir);
50+
self.add_workspace_root(std_root);
5051
let files = files
5152
.into_iter()
5253
.filter_map(|file| {
@@ -121,22 +122,6 @@ impl EmmyLuaAnalysis {
121122
updated_files
122123
}
123124

124-
// pub fn parrallel_update_files_by_uri(
125-
// &mut self,
126-
// files: Vec<(Uri, Option<String>)>,
127-
// ) -> Vec<FileId> {
128-
// let mut removed_files = HashSet::new();
129-
// let mut updated_files = HashSet::new();
130-
// {
131-
// let _p = Profile::new("parrallel update files");
132-
// }
133-
// self.compilation
134-
// .remove_index(removed_files.into_iter().collect());
135-
// let updated_files: Vec<FileId> = updated_files.into_iter().collect();
136-
// self.compilation.update_index(updated_files.clone());
137-
// updated_files
138-
// }
139-
140125
pub fn update_files_by_path(&mut self, files: Vec<(PathBuf, Option<String>)>) -> Vec<FileId> {
141126
let files = files
142127
.into_iter()

crates/emmylua_code_analysis/src/resources/mod.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
1+
use std::path::PathBuf;
2+
13
use include_dir::{include_dir, Dir, DirEntry};
24

35
use crate::{load_workspace_files, LuaFileInfo};
46

57
static RESOURCE_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/resources");
68

7-
pub fn load_resource_std(allow_create_resources_dir: bool) -> Vec<LuaFileInfo> {
9+
pub fn load_resource_std(allow_create_resources_dir: bool) -> (PathBuf, Vec<LuaFileInfo>) {
10+
let exe_path = std::env::current_exe().unwrap();
11+
let exe_dir = exe_path.parent().unwrap();
12+
let resoucres_dir = exe_dir.join("resources");
13+
let std_dir = resoucres_dir.join("std");
14+
815
if allow_create_resources_dir {
916
let result = load_resource_from_file_system();
1017
match result {
11-
Some(files) => return files,
18+
Some(files) => return (std_dir, files),
1219
None => {}
1320
}
1421
}
1522

1623
let files = load_resource_from_include_dir();
17-
return files
24+
let files = files
1825
.into_iter()
1926
.filter_map(|file| {
2027
if file.path.ends_with(".lua") {
21-
Some(file)
28+
let path = std_dir.join(&file.path).to_str().unwrap().to_string();
29+
Some(LuaFileInfo {
30+
path,
31+
content: file.content,
32+
})
2233
} else {
2334
None
2435
}
2536
})
2637
.collect::<_>();
38+
39+
(std_dir, files)
2740
}
2841

2942
fn load_resource_from_file_system() -> Option<Vec<LuaFileInfo>> {
@@ -83,7 +96,7 @@ fn walk_resource_dir(dir: &Dir, files: &mut Vec<LuaFileInfo>) {
8396
DirEntry::File(file) => {
8497
let path = file.path();
8598
let content = file.contents_utf8().unwrap();
86-
99+
87100
files.push(LuaFileInfo {
88101
path: path.to_str().unwrap().to_string(),
89102
content: content.to_string(),
@@ -94,4 +107,4 @@ fn walk_resource_dir(dir: &Dir, files: &mut Vec<LuaFileInfo>) {
94107
}
95108
}
96109
}
97-
}
110+
}

crates/emmylua_doc_cli/src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ mod markdown_generator;
77

88
fn main() {
99
let args = CmdArgs::from_args();
10-
let analysis = init::load_workspace(vec![args.input.to_str().unwrap()]);
10+
let mut input = args.input;
11+
if input.is_relative() {
12+
input = std::env::current_dir().ok().unwrap().join(&input);
13+
}
14+
15+
let analysis = init::load_workspace(vec![input.to_str().unwrap()]);
1116
if let Some(mut analysis) = analysis {
12-
markdown_generator::generate_markdown(&mut analysis, &args.input, &args.output);
17+
markdown_generator::generate_markdown(&mut analysis, &input, &args.output);
1318
}
1419
}

crates/emmylua_doc_cli/src/markdown_generator/typ_gen.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ pub fn generate_type_markdown(
3838
}
3939

4040
fn check_filter(db: &DbIndex, typ: &LuaTypeDecl, workspace: &Path) -> Option<()> {
41-
// if workspace is relative path, convert it to absolute path
42-
let workspace = if workspace.is_relative() {
43-
std::env::current_dir().ok()?.join(workspace)
44-
} else {
45-
workspace.to_path_buf()
46-
};
47-
4841
let location = typ.get_locations();
4942
for loc in location {
5043
let file_id = loc.file_id;

0 commit comments

Comments
 (0)