Skip to content

Commit f1b9c35

Browse files
committed
add option resources-path and load-stdlib
1 parent 378d1f7 commit f1b9c35

File tree

12 files changed

+112
-38
lines changed

12 files changed

+112
-38
lines changed

crates/emmylua_check/src/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn load_workspace(
1111
ignore: Option<Vec<String>>,
1212
) -> Option<EmmyLuaAnalysis> {
1313
let mut analysis = EmmyLuaAnalysis::new();
14-
analysis.init_std_lib(false);
14+
analysis.init_std_lib(None);
1515

1616
let mut workspace_folders = vec![workspace_folder];
1717
for path in &workspace_folders {

crates/emmylua_code_analysis/resources/std/global.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ function select(index, ...) end
355355
---@field __eq? fun(t1,t2):boolean
356356
---@field __lt? fun(t1,t2):boolean
357357
---@field __le? fun(t1,t2):boolean
358-
---@field __index? table|(fun(t,k):any
359-
---@field __newindex? table|fun(t,k,v
358+
---@field __index? table|fun(t,k):any
359+
---@field __newindex? table|fun(t,k,v)
360360
---@field __call? fun(t,...): any...
361361
---@field __pairs? fun(t):((fun(t,k,v):any,any),any,any)
362362
---@field __close? fun(t,errobj):any

crates/emmylua_code_analysis/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ impl EmmyLuaAnalysis {
5252
}
5353
}
5454

55-
pub fn init_std_lib(&mut self, allow_create_resources_dir: bool) {
56-
let (std_root, files) = load_resource_std(allow_create_resources_dir);
55+
pub fn init_std_lib(&mut self, create_resources_dir: Option<String>) {
56+
let (std_root, files) = load_resource_std(create_resources_dir);
5757
self.compilation
5858
.get_db_mut()
5959
.get_module_index_mut()

crates/emmylua_code_analysis/src/resources/mod.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::path::{Path, PathBuf};
22

33
use include_dir::{include_dir, Dir, DirEntry};
44

@@ -7,20 +7,28 @@ use crate::{load_workspace_files, LuaFileInfo};
77
static RESOURCE_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/resources");
88
const VERSION: &str = env!("CARGO_PKG_VERSION");
99

10-
pub fn load_resource_std(allow_create_resources_dir: bool) -> (PathBuf, Vec<LuaFileInfo>) {
11-
let exe_path = std::env::current_exe().unwrap();
12-
let exe_dir = exe_path.parent().unwrap();
13-
let resoucres_dir = exe_dir.join("resources");
14-
let std_dir = resoucres_dir.join("std");
15-
16-
if allow_create_resources_dir {
17-
let result = load_resource_from_file_system();
10+
pub fn load_resource_std(create_resources_dir: Option<String>) -> (PathBuf, Vec<LuaFileInfo>) {
11+
if let Some(create_resources_dir) = create_resources_dir {
12+
let resource_path = if create_resources_dir.is_empty() {
13+
let exe_path = std::env::current_exe().unwrap();
14+
let exe_dir = exe_path.parent().unwrap();
15+
let resoucres_dir = exe_dir.join("resources");
16+
resoucres_dir
17+
} else {
18+
PathBuf::from(&create_resources_dir)
19+
};
20+
let std_dir = PathBuf::from(&create_resources_dir).join("std");
21+
let result = load_resource_from_file_system(&resource_path);
1822
match result {
1923
Some(files) => return (std_dir, files),
2024
None => {}
2125
}
2226
}
2327

28+
let exe_path = std::env::current_exe().unwrap();
29+
let exe_dir = exe_path.parent().unwrap();
30+
let resoucres_dir = exe_dir.join("resources");
31+
let std_dir = resoucres_dir.join("std");
2432
let files = load_resource_from_include_dir();
2533
let files = files
2634
.into_iter()
@@ -40,16 +48,12 @@ pub fn load_resource_std(allow_create_resources_dir: bool) -> (PathBuf, Vec<LuaF
4048
(std_dir, files)
4149
}
4250

43-
fn load_resource_from_file_system() -> Option<Vec<LuaFileInfo>> {
44-
let exe_path = std::env::current_exe().unwrap();
45-
let exe_dir = exe_path.parent().unwrap();
46-
let resoucres_dir = exe_dir.join("resources");
47-
51+
fn load_resource_from_file_system(resources_dir: &Path) -> Option<Vec<LuaFileInfo>> {
4852
if check_need_dump_to_file_system() {
49-
log::info!("Creating resources dir: {:?}", resoucres_dir);
53+
log::info!("Creating resources dir: {:?}", resources_dir);
5054
let files = load_resource_from_include_dir();
5155
for file in &files {
52-
let path = resoucres_dir.join(&file.path);
56+
let path = resources_dir.join(&file.path);
5357
let parent = path.parent().unwrap();
5458
if !parent.exists() {
5559
match std::fs::create_dir_all(parent) {
@@ -70,7 +74,7 @@ fn load_resource_from_file_system() -> Option<Vec<LuaFileInfo>> {
7074
}
7175
}
7276

73-
let version_path = resoucres_dir.join("version");
77+
let version_path = resources_dir.join("version");
7478
let content = format!("{}", VERSION);
7579
match std::fs::write(&version_path, content) {
7680
Ok(_) => {}
@@ -81,7 +85,7 @@ fn load_resource_from_file_system() -> Option<Vec<LuaFileInfo>> {
8185
}
8286
}
8387

84-
let std_dir = resoucres_dir.join("std");
88+
let std_dir = resources_dir.join("std");
8589
let match_pattern = vec!["**/*.lua".to_string()];
8690
let files = match load_workspace_files(&std_dir, &match_pattern, &Vec::new(), &Vec::new(), None)
8791
{

crates/emmylua_code_analysis/src/test_lib/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl VirtualWorkspace {
3535
pub fn new_with_init_std_lib() -> Self {
3636
let gen = VirtualUrlGenerator::new();
3737
let mut analysis = EmmyLuaAnalysis::new();
38-
analysis.init_std_lib(false);
38+
analysis.init_std_lib(None);
3939
let base = &gen.base;
4040
analysis.add_main_workspace(base.clone());
4141
VirtualWorkspace {

crates/emmylua_doc_cli/src/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use emmylua_code_analysis::{
77
#[allow(unused)]
88
pub fn load_workspace(workspace_folders: Vec<&str>) -> Option<EmmyLuaAnalysis> {
99
let mut analysis = EmmyLuaAnalysis::new();
10-
analysis.init_std_lib(false);
10+
analysis.init_std_lib(None);
1111

1212
let mut workspace_folders = workspace_folders
1313
.iter()

crates/emmylua_ls/src/cmd_args.rs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,33 @@ pub struct CmdArgs {
2929
)]
3030
pub port: u16,
3131

32-
/// Path to the log file
33-
#[structopt(long = "log-path", help = "Path to the log file", default_value = "")]
34-
pub log_path: String,
35-
3632
/// Logging level (e.g., "error", "warn", "info", "debug", "trace")
3733
#[structopt(long = "log-level", help = "Logging level", default_value = "info")]
3834
pub log_level: LogLevel,
35+
36+
/// Path to the log file
37+
#[structopt(
38+
long = "log-path",
39+
help = "Path to the log file. Use 'none' to disable log file output.",
40+
default_value = ""
41+
)]
42+
pub log_path: NoneableString,
43+
44+
/// Path to the resources and logs directory. Use 'none' to indicate that assets should not be output to the file system.
45+
#[structopt(
46+
long = "resources-path",
47+
help = "Path to the resources. Use 'none' to disable resources file output.",
48+
default_value = ""
49+
)]
50+
pub resources_path: NoneableString,
51+
52+
/// Whether to load the standard library.
53+
#[structopt(
54+
long = "load-stdlib",
55+
help = "Whether to load the standard library",
56+
default_value = "true"
57+
)]
58+
pub load_std_lib: CmdBool,
3959
}
4060

4161
/// Logging level enum
@@ -90,3 +110,44 @@ impl std::str::FromStr for Communication {
90110
}
91111
}
92112
}
113+
114+
/// A string that can be "None" to represent an empty option
115+
#[derive(Debug, Clone)]
116+
pub struct NoneableString(pub Option<String>);
117+
118+
impl std::str::FromStr for NoneableString {
119+
type Err = String;
120+
121+
fn from_str(s: &str) -> Result<Self, Self::Err> {
122+
if s.eq_ignore_ascii_case("none") {
123+
Ok(NoneableString(None))
124+
} else {
125+
Ok(NoneableString(Some(s.to_string())))
126+
}
127+
}
128+
}
129+
130+
#[allow(unused)]
131+
impl NoneableString {
132+
pub fn as_deref(&self) -> Option<&str> {
133+
self.0.as_deref()
134+
}
135+
}
136+
137+
#[derive(Debug, Clone)]
138+
pub struct CmdBool(pub bool);
139+
140+
impl std::str::FromStr for CmdBool {
141+
type Err = String;
142+
143+
fn from_str(s: &str) -> Result<Self, Self::Err> {
144+
match s.to_lowercase().as_str() {
145+
"true" => Ok(CmdBool(true)),
146+
"false" => Ok(CmdBool(false)),
147+
_ => Err(format!(
148+
"Invalid boolean value: '{}'. Please choose 'true' or 'false'",
149+
s
150+
)),
151+
}
152+
}
153+
}

crates/emmylua_ls/src/context/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ impl ServerContext {
3737
receiver: conn.receiver.clone(),
3838
}));
3939

40-
let mut analysis = EmmyLuaAnalysis::new();
41-
analysis.init_std_lib(true);
42-
43-
let analysis = Arc::new(RwLock::new(analysis));
40+
let analysis = Arc::new(RwLock::new(EmmyLuaAnalysis::new()));
4441
let status_bar = Arc::new(StatusBar::new(client.clone()));
4542
let file_diagnostic = Arc::new(FileDiagnostic::new(
4643
analysis.clone(),

crates/emmylua_ls/src/handlers/completion/test/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl CompletionVirtualWorkspace {
3838
pub fn new_with_init_std_lib() -> Self {
3939
let gen = VirtualUrlGenerator::new();
4040
let mut analysis = EmmyLuaAnalysis::new();
41-
analysis.init_std_lib(false);
41+
analysis.init_std_lib(None);
4242
let base = &gen.base;
4343
analysis.add_main_workspace(base.clone());
4444
CompletionVirtualWorkspace {

crates/emmylua_ls/src/handlers/hover/test/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl HoverVirtualWorkspace {
3636
pub fn new_with_init_std_lib() -> Self {
3737
let gen = VirtualUrlGenerator::new();
3838
let mut analysis = EmmyLuaAnalysis::new();
39-
analysis.init_std_lib(false);
39+
analysis.init_std_lib(None);
4040
let base = &gen.base;
4141
analysis.add_main_workspace(base.clone());
4242
HoverVirtualWorkspace {

0 commit comments

Comments
 (0)