-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.rs
521 lines (465 loc) · 16.1 KB
/
json.rs
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use std::{
fmt, fs,
path::{absolute, Path, PathBuf},
process::Command,
str::FromStr,
};
use serde::{Deserialize, Deserializer, Serialize};
use tempdir::TempDir;
use url::Url;
use crate::{error::Error, parser::types::LuaKind};
// -------------------------------------------------------------------------------------------------
pub struct JsonDoc {}
impl JsonDoc {
/// Generate a list of definitions from a file
pub fn get(path: &Path) -> Result<Vec<Definition>, Error> {
let defs = Self::export(path)?;
Ok(Self::strip(path, defs))
}
/// Export and parse the JSON docs from lua-language-server
fn export(path: &Path) -> Result<Vec<Definition>, Error> {
let tmp_dir = TempDir::new("docs")?;
let tmp_path = tmp_dir.path();
let ls_filename = if cfg!(windows) {
"lua-language-server.exe"
} else {
"lua-language-server"
};
// allow running from within the "generate" path and from the repository root path
let ls_path = PathBuf::from("./lua-language-server/bin/").join(ls_filename);
if !ls_path.exists() {
Self::download_luals()?;
}
if !ls_path.exists() {
return Err(Error::Exec(format!(
"lua-language-server binary does not exist: '{}'",
absolute(ls_path.clone())
.unwrap_or(ls_path)
.to_string_lossy()
)));
}
let output = Command::new(ls_path)
.arg("--doc")
.arg(path)
.arg("--doc_out_path")
.arg(tmp_path)
.arg("--logpath")
.arg(tmp_path)
.output()?;
if !output.status.success() {
Err(Error::Exec(
String::from_utf8(output.stderr).unwrap_or("unknown error".to_string()),
))
} else {
let json_doc_path = tmp_dir.path().join("doc.json");
let json_doc = fs::read_to_string(json_doc_path)?;
Ok(serde_json::from_str(&json_doc)?)
}
}
fn download_luals() -> Result<(), Error> {
use decompress::{decompress, ExtractOptsBuilder};
use reqwest::{
blocking::Client,
header::{HeaderMap, USER_AGENT},
};
let mut headers = HeaderMap::new();
headers.insert(
USER_AGENT,
format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
.parse()
.unwrap(),
);
// download
const LUA_LS_VERSION: &str = "3.9.3";
const LUA_LS_RELEASES_URL: &str =
"https://github.com/LuaLS/lua-language-server/releases/download";
#[cfg(target_os = "linux")]
const LUA_LS_BINARY: &str = "linux-x64.tar.gz";
#[cfg(target_os = "windows")]
const LUA_LS_BINARY: &str = "win32-x64.zip";
#[cfg(target_os = "macos")]
const LUA_LS_BINARY: &str = "darwin-arm64.tar.gz";
let luals_url = format!("{LUA_LS_RELEASES_URL}/{LUA_LS_VERSION}/lua-language-server-{LUA_LS_VERSION}-{LUA_LS_BINARY}");
println!("Downloading lua-ls from {}...", luals_url);
let http = Client::builder().default_headers(headers).build()?;
let request = http.get(luals_url).send()?;
let response = request.error_for_status()?;
let content = response.bytes()?;
let tmp_dir = TempDir::new("docs")?;
let tmp_file = tmp_dir.path().join(LUA_LS_BINARY);
fs::write(tmp_file.clone(), content).expect("Unable to write file");
// decompress
let dest_dir = PathBuf::from("./lua-language-server");
fs::create_dir(dest_dir.clone())?;
println!(
"Extracting lua-ls to {}...",
dest_dir.as_path().to_string_lossy()
);
decompress(
tmp_file.clone(),
dest_dir.clone(),
&ExtractOptsBuilder::default()
.build()
.expect("failed to build default decompression options"),
)?;
fs::remove_file(tmp_file)?;
// patch
Self::patch_file(
// raise enum display limits
&dest_dir.clone().join("script/config/template.lua"),
r#"(\['Lua.hover.enumsLimit'\]\s*=\s*Type.Integer\s*>>\s*)5(,)"#,
r#"${1}100${2}"#,
)?;
Self::patch_file(
// don't resolve aliases - we do
&dest_dir.clone().join("script/config/template.lua"),
r#"(\['Lua.hover.expandAlias'\]\s*=\s*Type.Boolean\s*>>\s*)true(,)"#,
r#"${1}false${2}"#,
)?;
Self::patch_file(
// avoid truncating output in general
&dest_dir.clone().join("script/vm/infer.lua"),
r#"if \#view > 200 then"#,
r#"if false then -- no limit"#,
)?;
Ok(())
}
fn patch_file(path: &Path, search: &str, replace: &str) -> Result<(), Error> {
println!("Patching: `{}`", path.to_string_lossy());
let content = fs::read_to_string(path)?;
let re = regex::Regex::new(search)?;
let result = re.replace_all(&content, replace);
if content != result {
fs::write(path, result.as_bytes())?;
Ok(())
} else {
Err(Error::Patch(regex::Error::Syntax(
"patch does not apply".to_string(),
)))
}
}
fn file_url_matches(file_url: &str, base_path: &Path) -> bool {
assert!(
Url::from_str(file_url).is_ok(),
"expecting an url file string"
);
let file_path = Url::from_str(file_url)
.unwrap()
.to_file_path()
.unwrap_or_default()
.canonicalize()
.unwrap_or_default();
let base_path = base_path.canonicalize().unwrap_or_default();
file_path.starts_with(base_path)
}
/// Exclude standard lua
fn strip(path: &Path, defs: Vec<Definition>) -> Vec<Definition> {
defs.into_iter()
.map(|d| {
// remove standard define from the list of defines (for type())
let mut def = d.clone();
def.defines
.retain(|define| Self::file_url_matches(&define.file, path));
def
})
.collect()
}
}
// -------------------------------------------------------------------------------------------------
#[derive(Debug, PartialEq, Serialize, Clone, Eq, PartialOrd, Ord, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum VisibleType {
Public,
Private,
Package,
Protected,
}
// -------------------------------------------------------------------------------------------------
#[derive(Debug, Serialize, PartialEq, Deserialize, Clone, Eq, PartialOrd, Ord)]
pub enum Doc {
#[serde(rename = "doc.field")]
Field,
#[serde(rename = "doc.enum")]
Enum,
#[serde(rename = "doc.alias")]
Alias,
#[serde(rename = "doc.class")]
Class,
#[serde(rename = "doc.extends.name")]
ExtendsName,
#[serde(rename = "doc.type")]
Type,
#[serde(rename = "doc.type.name")]
TypeName,
#[serde(rename = "doc.type.integer")]
TypeInteger,
#[serde(rename = "doc.type.number")]
TypeNumber,
#[serde(rename = "doc.type.array")]
TypeArray,
#[serde(rename = "doc.type.boolean")]
TypeBoolean,
#[serde(rename = "doc.type.string")]
TypeString,
#[serde(rename = "doc.type.table")]
TypeTable,
#[serde(rename = "doc.type.sign")]
TypeSign,
#[serde(rename = "doc.type.function")]
TypeFunction,
}
// -------------------------------------------------------------------------------------------------
#[derive(Debug, PartialEq, Serialize, Clone, Eq, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum Type {
Doc(Doc),
// fields, defines
GetField,
SetField,
SetMethod,
// definitions
#[serde(rename = "type")]
Def,
Variable,
#[serde(rename = "luals.config")]
LuaLsConfig,
// defines
TableField,
SetGlobal,
// function returns
#[serde(rename = "function.return")]
FunctionReturn,
// extends
Lua(LuaKind),
}
impl<'de> Deserialize<'de> for Type {
fn deserialize<D>(deserializer: D) -> Result<Type, D::Error>
where
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(deserializer)?;
match s {
"type" => Ok(Type::Def),
"variable" => Ok(Type::Variable),
"getfield" => Ok(Type::GetField),
"setfield" => Ok(Type::SetField),
"setmethod" => Ok(Type::SetMethod),
"setglobal" => Ok(Type::SetGlobal),
"tablefield" => Ok(Type::TableField),
"function.return" => Ok(Type::FunctionReturn),
"luals.config" => Ok(Type::LuaLsConfig),
_ => {
let quoted = format!("\"{}\"", s);
match serde_json::from_str::<LuaKind>(quoted.as_str()) {
Ok(lk) => Ok(Type::Lua(lk)),
Err(_) => match serde_json::from_str::<Doc>(quoted.as_str()) {
Ok(d) => Ok(Type::Doc(d)),
Err(_) => Err(serde::de::Error::unknown_variant(s, &[])),
},
}
}
}
}
}
// -------------------------------------------------------------------------------------------------
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Field {
pub desc: Option<String>,
pub rawdesc: Option<String>,
pub name: String,
#[serde(rename = "type")]
pub lua_type: Type,
pub file: String,
pub start: u32,
pub finish: u32,
pub visible: VisibleType,
#[serde(default, deserialize_with = "deserialize_extends")]
pub extends: Option<Extend>,
}
impl fmt::Display for Field {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} : {:?}",
self.name,
self.lua_type,
// self.extends.clone().map(|e| e.view).unwrap_or_default()
)
}
}
impl Field {
pub fn is_field(&self) -> bool {
self.lua_type == Type::Doc(Doc::Field)
|| (self.lua_type == Type::SetField
&& !Self::extend_has_type(&self.extends, Type::Lua(LuaKind::Function)))
}
pub fn is_function(&self) -> bool {
self.lua_type == Type::SetMethod
|| (self.lua_type == Type::SetField
&& Self::extend_has_type(&self.extends, Type::Lua(LuaKind::Function)))
}
fn extend_has_type(e: &Option<Extend>, t: Type) -> bool {
if let Some(e) = e {
e.lua_type == t
} else {
false
}
}
}
// -------------------------------------------------------------------------------------------------
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Definition {
#[serde(rename = "type")]
pub lua_type: Type,
pub name: String,
pub desc: Option<String>,
pub rawdesc: Option<String>,
pub defines: Vec<Define>,
#[serde(default)]
pub fields: Vec<Field>,
}
impl fmt::Display for Definition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} : {:?} d[{}] f[{}]{}{}",
self.name,
self.lua_type,
self.defines.len(),
self.fields.len(),
// "",
if !self.defines.is_empty() {
format!(
"\n{}",
self.defines
.iter()
.enumerate()
.map(|(i, d)| format!(" <{}> - {}", i, d))
.collect::<Vec<String>>()
.join("\n")
)
} else {
String::from("")
},
if !self.fields.is_empty() {
format!(
"\n{}",
self.fields
.clone()
.iter()
.enumerate()
.map(|(i, f)| format!(" [{}] - {}", i, f))
.collect::<Vec<String>>()
.join("\n")
)
} else {
String::from("")
},
)
}
}
// -------------------------------------------------------------------------------------------------
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Define {
#[serde(rename = "type")]
pub lua_type: Type,
pub file: String,
pub start: u32,
pub finish: u32,
#[serde(default, deserialize_with = "deserialize_extends")]
pub extends: Option<Extend>,
}
impl fmt::Display for Define {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.lua_type,)
}
}
// -------------------------------------------------------------------------------------------------
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ExtendType {
#[serde(rename = "type")]
pub lua_type: Type,
pub start: u32,
pub finish: u32,
pub view: String,
}
// -------------------------------------------------------------------------------------------------
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Extend {
#[serde(rename = "type")]
pub lua_type: Type,
pub types: Option<Vec<ExtendType>>,
pub start: u32,
pub finish: u32,
pub view: String,
pub desc: Option<String>,
pub rawdesc: Option<String>,
/// Only present for functions (type = "function") with args
#[serde(default)]
pub args: Vec<ArgDef>,
/// Only present for functions (type = "function") with returns
#[serde(default)]
pub returns: Vec<ReturnDef>,
}
// -------------------------------------------------------------------------------------------------
/// Extends can be either null, an object or an array of objects
fn deserialize_extends<'de, D>(deserializer: D) -> Result<Option<Extend>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum ExtendInput {
None,
Map(Extend),
Array(Vec<Extend>),
Object(Extend),
Other(serde_json::Value),
}
impl From<ExtendInput> for Option<Extend> {
fn from(input: ExtendInput) -> Self {
match input {
ExtendInput::None => None,
ExtendInput::Map(extend) => Some(extend),
// there is only one possible extends per define in the API
ExtendInput::Array(vec) => vec.first().cloned(),
ExtendInput::Object(extend) => Some(extend),
#[allow(unused_variables)]
ExtendInput::Other(value) => {
#[cfg(debug_assertions)]
panic!("Unexpected extend {:?}", value);
#[cfg(not(debug_assertions))]
None
}
}
}
}
Ok(ExtendInput::deserialize(deserializer)?.into())
}
// -------------------------------------------------------------------------------------------------
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ArgType {
#[serde(rename = "self")]
SelfArg,
#[serde(rename = "local")]
Local,
#[serde(rename = "...")]
Variadic,
}
// -------------------------------------------------------------------------------------------------
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ArgDef {
#[serde(rename = "type")]
pub lua_type: ArgType,
pub name: Option<String>,
pub view: String,
}
// -------------------------------------------------------------------------------------------------
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ReturnDef {
// NB: type for returns will always be "function.return"
#[serde(rename = "type")]
pub lua_type: Type,
pub name: Option<String>,
pub view: String,
}