-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathmod.rs
More file actions
234 lines (214 loc) · 7.39 KB
/
mod.rs
File metadata and controls
234 lines (214 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
mod config_loader;
mod configs;
mod flatten_config;
use std::{
collections::{HashMap, HashSet},
path::{Path, PathBuf},
};
pub use config_loader::{load_configs, load_configs_raw};
pub use configs::{
DiagnosticSeveritySetting, DocSyntax, EmmyrcCodeAction, EmmyrcCodeLens, EmmyrcCompletion,
EmmyrcDiagnostic, EmmyrcDoc, EmmyrcDocumentColor, EmmyrcExternalTool, EmmyrcFilenameConvention,
EmmyrcHover, EmmyrcInlayHint, EmmyrcInlineValues, EmmyrcLuaVersion, EmmyrcReference,
EmmyrcReformat, EmmyrcResource, EmmyrcRuntime, EmmyrcSemanticToken, EmmyrcSignature,
EmmyrcStrict, EmmyrcWorkspace, EmmyrcWorkspaceModuleMap,
};
use emmylua_parser::{LuaLanguageLevel, LuaNonStdSymbolSet, ParserConfig, SpecialFunction};
use regex::Regex;
use rowan::NodeCache;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::process::Command;
#[derive(Serialize, Deserialize, Debug, JsonSchema, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Emmyrc {
#[serde(rename = "$schema")]
#[serde(skip_serializing_if = "Option::is_none")]
pub schema: Option<String>,
#[serde(default)]
pub completion: EmmyrcCompletion,
#[serde(default)]
pub diagnostics: EmmyrcDiagnostic,
#[serde(default)]
pub signature: EmmyrcSignature,
#[serde(default)]
pub hint: EmmyrcInlayHint,
#[serde(default)]
pub runtime: EmmyrcRuntime,
#[serde(default)]
pub workspace: EmmyrcWorkspace,
#[serde(default)]
pub resource: EmmyrcResource,
#[serde(default)]
pub code_lens: EmmyrcCodeLens,
#[serde(default)]
pub strict: EmmyrcStrict,
#[serde(default)]
pub semantic_tokens: EmmyrcSemanticToken,
#[serde(default)]
pub references: EmmyrcReference,
#[serde(default)]
pub hover: EmmyrcHover,
#[serde(default)]
pub document_color: EmmyrcDocumentColor,
#[serde(default)]
pub code_action: EmmyrcCodeAction,
#[serde(default)]
pub inline_values: EmmyrcInlineValues,
#[serde(default)]
pub doc: EmmyrcDoc,
#[serde(default)]
pub format: EmmyrcReformat,
}
impl Emmyrc {
pub fn get_parse_config<'cache>(
&self,
node_cache: &'cache mut NodeCache,
) -> ParserConfig<'cache> {
let lua_language_level = self.get_language_level();
let mut special_like = HashMap::new();
for (name, func) in self.runtime.special.iter() {
if let Some(func) = (*func).into() {
special_like.insert(name.clone(), func);
}
}
for name in self.runtime.require_like_function.iter() {
special_like.insert(name.clone(), SpecialFunction::Require);
}
let mut non_std_symbols = LuaNonStdSymbolSet::new();
for symbol in self.runtime.nonstandard_symbol.iter() {
non_std_symbols.add((*symbol).into());
}
ParserConfig::new(
lua_language_level,
Some(node_cache),
special_like,
non_std_symbols,
true,
)
}
pub fn get_language_level(&self) -> LuaLanguageLevel {
match self.runtime.version {
EmmyrcLuaVersion::Lua51 => LuaLanguageLevel::Lua51,
EmmyrcLuaVersion::Lua52 => LuaLanguageLevel::Lua52,
EmmyrcLuaVersion::Lua53 => LuaLanguageLevel::Lua53,
EmmyrcLuaVersion::Lua54 => LuaLanguageLevel::Lua54,
EmmyrcLuaVersion::LuaJIT => LuaLanguageLevel::LuaJIT,
// wait lua5.5 release
EmmyrcLuaVersion::LuaLatest => LuaLanguageLevel::Lua54,
EmmyrcLuaVersion::Lua55 => LuaLanguageLevel::Lua55,
}
}
pub fn pre_process_emmyrc(&mut self, workspace_root: &Path) {
fn process_and_dedup<'a>(
iter: impl Iterator<Item = &'a String>,
workspace_root: &Path,
) -> Vec<String> {
let mut seen = HashSet::new();
iter.map(|root| pre_process_path(root, workspace_root))
.filter(|path| seen.insert(path.clone()))
.collect()
}
self.workspace.workspace_roots =
process_and_dedup(self.workspace.workspace_roots.iter(), workspace_root);
self.workspace.library = process_and_dedup(self.workspace.library.iter(), workspace_root);
self.workspace.package_dirs =
process_and_dedup(self.workspace.package_dirs.iter(), workspace_root);
self.workspace.ignore_dir =
process_and_dedup(self.workspace.ignore_dir.iter(), workspace_root);
self.resource.paths = process_and_dedup(self.resource.paths.iter(), workspace_root);
}
}
fn pre_process_path(path: &str, workspace: &Path) -> String {
let mut path = path.to_string();
path = replace_env_var(&path);
// ${workspaceFolder} == {workspaceFolder}
path = path.replace("$", "");
let workspace_str = match workspace.to_str() {
Some(path) => path,
None => {
log::error!("Warning: workspace path is not valid UTF-8");
return path;
}
};
path = replace_placeholders(&path, workspace_str);
if path.starts_with('~') {
let home_dir = match dirs::home_dir() {
Some(path) => path,
None => {
log::error!("Warning: Home directory not found");
return path;
}
};
path = home_dir.join(&path[2..]).to_string_lossy().to_string();
} else if path.starts_with("./") {
path = workspace.join(&path[2..]).to_string_lossy().to_string();
} else if PathBuf::from(&path).is_absolute() {
path = path.to_string();
} else {
path = workspace.join(&path).to_string_lossy().to_string();
}
path
}
// compact luals
fn replace_env_var(path: &str) -> String {
let re = match Regex::new(r"\$(\w+)") {
Ok(re) => re,
Err(_) => {
log::error!("Warning: Failed to create regex for environment variable replacement");
return path.to_string();
}
};
re.replace_all(path, |caps: ®ex::Captures| {
let key = &caps[1];
std::env::var(key).unwrap_or_else(|_| {
log::error!("Warning: Environment variable {} is not set", key);
String::new()
})
})
.to_string()
}
fn replace_placeholders(input: &str, workspace_folder: &str) -> String {
let re = match Regex::new(r"\{([^}]+)\}") {
Ok(re) => re,
Err(_) => {
log::error!("Warning: Failed to create regex for placeholder replacement");
return input.to_string();
}
};
re.replace_all(input, |caps: ®ex::Captures| {
let key = &caps[1];
if key == "workspaceFolder" {
workspace_folder.to_string()
} else if let Some(env_name) = key.strip_prefix("env:") {
std::env::var(env_name).unwrap_or_default()
} else if key == "luarocks" {
get_luarocks_deploy_dir()
} else {
caps[0].to_string()
}
})
.to_string()
}
fn get_luarocks_deploy_dir() -> String {
Command::new("luarocks")
.args(["config", "deploy_lua_dir"])
.output()
.ok()
.and_then(|output| {
if output.status.success() {
Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
} else {
None
}
})
.unwrap_or_default()
}
#[cfg(test)]
mod test {
use super::pre_process_path;
#[test]
fn pre_proc_path() {
panic!()
}
}