Skip to content

Commit 12e32d5

Browse files
committed
feat: New std translation
仅限于 emmylua_ls. 在启动时会根据翻译文件生成对应语言std目录到文件系统中, 例如 resources/std-zh_CN
1 parent 5dd442c commit 12e32d5

File tree

6 files changed

+431
-14
lines changed

6 files changed

+431
-14
lines changed

Cargo.lock

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

crates/emmylua_code_analysis/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub use emmylua_codestyle::*;
2727
pub use locale::get_locale_code;
2828
use lsp_types::Uri;
2929
pub use profile::Profile;
30+
pub use resources::get_best_resources_dir;
31+
pub use resources::load_resource_from_include_dir;
3032
use resources::load_resource_std;
3133
pub use semantic::*;
3234
use std::{collections::HashSet, path::PathBuf, sync::Arc};

crates/emmylua_code_analysis/src/resources/mod.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ mod best_resource_path;
22

33
use std::path::{Path, PathBuf};
44

5-
use best_resource_path::get_best_resources_dir;
5+
pub use best_resource_path::get_best_resources_dir;
66
use include_dir::{Dir, DirEntry, include_dir};
77

8-
use crate::{LuaFileInfo, load_workspace_files};
8+
use crate::{LuaFileInfo, get_locale_code, load_workspace_files};
99

1010
static RESOURCE_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/resources");
1111
const VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -14,13 +14,15 @@ pub fn load_resource_std(
1414
create_resources_dir: Option<String>,
1515
is_jit: bool,
1616
) -> (PathBuf, Vec<LuaFileInfo>) {
17+
// 指定了输出的资源目录, 目前只有 lsp 会指定
1718
if let Some(create_resources_dir) = create_resources_dir {
1819
let resource_path = if create_resources_dir.is_empty() {
1920
get_best_resources_dir()
2021
} else {
2122
PathBuf::from(&create_resources_dir)
2223
};
23-
let std_dir = PathBuf::from(&resource_path).join("std");
24+
// 此时会存在 i18n, 我们需要根据当前语言环境切换到对应语言的 std 目录
25+
let std_dir = get_std_dir(&resource_path);
2426
let result = load_resource_from_file_system(&resource_path);
2527
if let Some(mut files) = result {
2628
if !is_jit {
@@ -29,7 +31,7 @@ pub fn load_resource_std(
2931
return (std_dir, files);
3032
}
3133
}
32-
34+
// 没有指定资源目录, 那么直接使用默认的资源目录, 此时不会存在 i18n
3335
let resoucres_dir = get_best_resources_dir();
3436
let std_dir = resoucres_dir.join("std");
3537
let files = load_resource_from_include_dir();
@@ -60,19 +62,20 @@ pub fn load_resource_std(
6062
fn remove_jit_resource(files: &mut Vec<LuaFileInfo>) {
6163
files.retain(|file| {
6264
let path = Path::new(&file.path);
63-
let should_remove = path.ends_with("std/jit.lua")
64-
|| path.ends_with("std/jit/profile.lua")
65-
|| path.ends_with("std/jit/util.lua")
66-
|| path.ends_with("std/string/buffer.lua")
67-
|| path.ends_with("std/table/clear.lua")
68-
|| path.ends_with("std/table/new.lua")
69-
|| path.ends_with("std/ffi.lua");
65+
let should_remove = path.ends_with("jit.lua")
66+
|| path.ends_with("jit/profile.lua")
67+
|| path.ends_with("jit/util.lua")
68+
|| path.ends_with("string/buffer.lua")
69+
|| path.ends_with("table/clear.lua")
70+
|| path.ends_with("table/new.lua")
71+
|| path.ends_with("ffi.lua");
7072

7173
!should_remove
7274
});
7375
}
7476

7577
fn load_resource_from_file_system(resources_dir: &Path) -> Option<Vec<LuaFileInfo>> {
78+
// lsp i18n 的资源在更早之前的 crates\emmylua_ls\src\handlers\initialized\std_i18n.rs 中写入到文件系统
7679
if check_need_dump_to_file_system() {
7780
log::info!("Creating resources dir: {:?}", resources_dir);
7881
let files = load_resource_from_include_dir();
@@ -109,7 +112,7 @@ fn load_resource_from_file_system(resources_dir: &Path) -> Option<Vec<LuaFileInf
109112
}
110113
}
111114

112-
let std_dir = resources_dir.join("std");
115+
let std_dir = get_std_dir(&resources_dir);
113116
let match_pattern = vec!["**/*.lua".to_string()];
114117
let files = match load_workspace_files(&std_dir, &match_pattern, &Vec::new(), &Vec::new(), None)
115118
{
@@ -146,7 +149,7 @@ fn check_need_dump_to_file_system() -> bool {
146149
false
147150
}
148151

149-
fn load_resource_from_include_dir() -> Vec<LuaFileInfo> {
152+
pub fn load_resource_from_include_dir() -> Vec<LuaFileInfo> {
150153
let mut files = Vec::new();
151154
walk_resource_dir(&RESOURCE_DIR, &mut files);
152155
files
@@ -170,3 +173,11 @@ fn walk_resource_dir(dir: &Dir, files: &mut Vec<LuaFileInfo>) {
170173
}
171174
}
172175
}
176+
177+
// 优先使用当前语言环境的 std-{locale} 目录, 否则回退到默认的 std 目录
178+
fn get_std_dir(resources_dir: &Path) -> PathBuf {
179+
let locale = get_locale_code(&rust_i18n::locale());
180+
Some(resources_dir.join(format!("std-{locale}")))
181+
.filter(|p| locale != "en" && p.exists())
182+
.unwrap_or_else(|| resources_dir.join("std"))
183+
}

crates/emmylua_ls/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ itertools.workspace = true
4040
dirs.workspace = true
4141
wax.workspace = true
4242
internment.workspace = true
43+
include_dir.workspace = true
4344

4445
[dependencies.clap]
4546
workspace = true

crates/emmylua_ls/src/handlers/initialized/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod client_config;
22
mod codestyle;
33
mod collect_files;
44
mod locale;
5+
mod std_i18n;
56

67
use std::{path::PathBuf, sync::Arc};
78

@@ -12,7 +13,9 @@ use crate::{
1213
WorkspaceFileMatcher, WorkspaceFolder, get_client_id, load_emmy_config,
1314
},
1415
handlers::{
15-
initialized::collect_files::calculate_include_and_exclude,
16+
initialized::{
17+
collect_files::calculate_include_and_exclude, std_i18n::try_generate_translated_std,
18+
},
1619
text_document::register_files_watch,
1720
},
1821
logger::init_logger,
@@ -246,6 +249,7 @@ pub async fn init_std_lib(
246249
if cmd_args.load_stdlib.0 {
247250
// double update config
248251
analysis.update_config(emmyrc);
252+
try_generate_translated_std();
249253
analysis.init_std_lib(cmd_args.resources_path.0.clone());
250254
}
251255

0 commit comments

Comments
 (0)