forked from EmmyLuaLs/emmylua-analyzer-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
187 lines (170 loc) · 6.04 KB
/
mod.rs
File metadata and controls
187 lines (170 loc) · 6.04 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
mod config_loader;
mod configs;
mod flatten_config;
use std::{
collections::{HashMap, HashSet},
path::{Path, PathBuf},
};
pub use config_loader::load_configs;
pub use configs::EmmyrcFilenameConvention;
pub use configs::EmmyrcLuaVersion;
use configs::{EmmyrcCodeAction, EmmyrcDocumentColor};
use configs::{
EmmyrcCodeLen, EmmyrcCompletion, EmmyrcDiagnostic, EmmyrcHover, EmmyrcInlayHint,
EmmyrcInlineValues, EmmyrcReference, EmmyrcResource, EmmyrcRuntime, EmmyrcSemanticToken,
EmmyrcSignature, EmmyrcStrict, EmmyrcWorkspace,
};
use emmylua_parser::{LuaLanguageLevel, ParserConfig, SpecialFunction};
use regex::Regex;
use rowan::NodeCache;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[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: EmmyrcCodeLen,
#[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,
}
impl Emmyrc {
pub fn get_parse_config<'cache>(
&self,
node_cache: &'cache mut NodeCache,
) -> ParserConfig<'cache> {
let level = &self.runtime.version;
let lua_language_level = match level {
EmmyrcLuaVersion::Lua51 => LuaLanguageLevel::Lua51,
EmmyrcLuaVersion::Lua52 => LuaLanguageLevel::Lua52,
EmmyrcLuaVersion::Lua53 => LuaLanguageLevel::Lua53,
EmmyrcLuaVersion::Lua54 => LuaLanguageLevel::Lua54,
EmmyrcLuaVersion::LuaJIT => LuaLanguageLevel::LuaJIT,
EmmyrcLuaVersion::LuaLatest => LuaLanguageLevel::Lua54,
EmmyrcLuaVersion::Lua55 => LuaLanguageLevel::Lua55,
};
let mut special_like = HashMap::new();
for name in self.runtime.require_like_function.iter() {
special_like.insert(name.clone(), SpecialFunction::Require);
}
ParserConfig::new(lua_language_level, Some(node_cache), special_like)
}
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.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);
fn canonicalize_path(path_buf: PathBuf) -> String {
let new_path_buf = path_buf.canonicalize().unwrap_or(path_buf);
new_path_buf.to_string_lossy().to_string()
}
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 = canonicalize_path(home_dir.join(&path[1..]));
} else if path.starts_with("./") {
path = canonicalize_path(workspace.join(&path[2..]));
} else if PathBuf::from(&path).is_absolute() {
path = path.to_string();
} else {
path = canonicalize_path(workspace.join(&path));
}
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 {
caps[0].to_string()
}
})
.to_string()
}