Skip to content

Commit 7fccf56

Browse files
committed
Improve doc gen
1 parent 804e44e commit 7fccf56

File tree

9 files changed

+195
-2
lines changed

9 files changed

+195
-2
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use std::path::Path;
2+
3+
use emmylua_code_analysis::{
4+
humanize_type, DbIndex, LuaDecl, LuaDeclId, LuaMemberOwner, LuaPropertyOwnerId, LuaType,
5+
RenderLevel,
6+
};
7+
use tera::{Context, Tera};
8+
9+
use crate::markdown_generator::{
10+
escape_type_name, mod_gen::generate_member_owner_module, IndexStruct,
11+
};
12+
13+
use super::{render::{render_const_type, render_function_type}, MkdocsIndex};
14+
15+
pub fn generate_global_markdown(
16+
db: &DbIndex,
17+
tl: &Tera,
18+
decl_id: &LuaDeclId,
19+
input: &Path,
20+
output: &Path,
21+
mkdocs_index: &mut MkdocsIndex,
22+
) -> Option<()> {
23+
check_filter(db, decl_id, input)?;
24+
25+
let mut context = tera::Context::new();
26+
let decl = db.get_decl_index().get_decl(decl_id)?;
27+
let name = decl.get_name();
28+
context.insert("global_name", name);
29+
30+
let mut template_name = "lua_global_template.tl";
31+
match &decl.get_type()? {
32+
LuaType::TableConst(table) => {
33+
let member_owner = LuaMemberOwner::Element(table.clone());
34+
generate_member_owner_module(db, member_owner, name, &mut context)?;
35+
}
36+
_ => {
37+
template_name = "lua_global_template_simple.tl";
38+
generate_simple_global(db, decl, &mut context);
39+
}
40+
}
41+
42+
let render_text = match tl.render(&template_name, &context) {
43+
Ok(text) => text,
44+
Err(e) => {
45+
eprintln!("Failed to render template: {}", e);
46+
return None;
47+
}
48+
};
49+
50+
let file_name = format!("{}.md", escape_type_name(&decl.get_name()));
51+
mkdocs_index.globals.push(IndexStruct {
52+
name: decl.get_name().to_string(),
53+
file: format!("globals/{}", file_name.clone()),
54+
});
55+
56+
let outpath = output.join(file_name);
57+
println!("output global file: {}", outpath.display());
58+
match std::fs::write(outpath, render_text) {
59+
Ok(_) => {}
60+
Err(e) => {
61+
eprintln!("Failed to write file: {}", e);
62+
return None;
63+
}
64+
}
65+
Some(())
66+
}
67+
68+
fn check_filter(db: &DbIndex, decl_id: &LuaDeclId, workspace: &Path) -> Option<()> {
69+
let file_id = decl_id.file_id;
70+
let file_path = db.get_vfs().get_file_path(&file_id)?;
71+
if !file_path.starts_with(workspace) {
72+
return None;
73+
}
74+
75+
let decl = db.get_decl_index().get_decl(decl_id)?;
76+
let ty = decl.get_type()?;
77+
match ty {
78+
LuaType::Ref(_) | LuaType::Def(_) => return None,
79+
_ => {}
80+
}
81+
82+
Some(())
83+
}
84+
85+
fn generate_simple_global(db: &DbIndex, decl: &LuaDecl, context: &mut Context) -> Option<()> {
86+
let property_owner_id = LuaPropertyOwnerId::LuaDecl(decl.get_id());
87+
let property = db.get_property_index().get_property(property_owner_id);
88+
89+
let description = if let Some(property) = property {
90+
*property
91+
.description
92+
.clone()
93+
.unwrap_or("".to_string().into())
94+
} else {
95+
"".to_string()
96+
};
97+
context.insert("description", &description);
98+
99+
let name = decl.get_name();
100+
let ty = decl.get_type().unwrap_or(&LuaType::Unknown);
101+
if ty.is_function() {
102+
let display = render_function_type(db, ty, &name, false);
103+
context.insert("display", &display);
104+
} else if ty.is_const() {
105+
let typ_display = render_const_type(db, &ty);
106+
let display = format!("```lua\n{}: {}\n```\n", name, typ_display);
107+
context.insert("display", &display);
108+
} else {
109+
let typ_display = humanize_type(db, &ty, RenderLevel::Detailed);
110+
let dispaly = format!("```lua\n{} : {}\n```\n", name, typ_display);
111+
context.insert("display", &dispaly);
112+
}
113+
114+
Some(())
115+
}

crates/emmylua_doc_cli/src/markdown_generator/index_gen.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub fn generate_index(tl: &Tera, mkdocs: &mut MkdocsIndex, output: &std::path::P
66
let mut context = tera::Context::new();
77
mkdocs.types.sort_by(|a, b| a.name.cmp(&b.name));
88
mkdocs.modules.sort_by(|a, b| a.name.cmp(&b.name));
9+
mkdocs.globals.sort_by(|a, b| a.name.cmp(&b.name));
910

1011

1112
if !mkdocs.types.is_empty() {
@@ -14,6 +15,9 @@ pub fn generate_index(tl: &Tera, mkdocs: &mut MkdocsIndex, output: &std::path::P
1415
if !mkdocs.modules.is_empty() {
1516
context.insert("modules", &mkdocs.modules);
1617
}
18+
if !mkdocs.globals.is_empty() {
19+
context.insert("globals", &mkdocs.globals);
20+
}
1721
let index_path = output.join("docs/index.md");
1822
let index_text = match tl.render("index_template.tl", &context) {
1923
Ok(text) => text,

crates/emmylua_doc_cli/src/markdown_generator/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod mod_gen;
44
mod render;
55
mod typ_gen;
66
mod mixin_copy;
7+
mod global_gen;
78

89
use std::path::PathBuf;
910

@@ -39,6 +40,16 @@ pub fn generate_markdown(
3940
std::fs::create_dir_all(&module_out).ok()?;
4041
}
4142

43+
let global_out = docs_dir.join("globals");
44+
if !global_out.exists() {
45+
println!("Creating globals directory: {:?}", global_out);
46+
std::fs::create_dir_all(&global_out).ok()?;
47+
} else {
48+
println!("Clearing globals directory: {:?}", global_out);
49+
std::fs::remove_dir_all(&global_out).ok()?;
50+
std::fs::create_dir_all(&global_out).ok()?;
51+
}
52+
4253
let tl = init_tl::init_tl(override_template)?;
4354
let mut mkdocs_index = MkdocsIndex::default();
4455
let db = analysis.compilation.get_db();
@@ -54,6 +65,12 @@ pub fn generate_markdown(
5465
mod_gen::generate_module_markdown(db, &tl, module, &input, &module_out, &mut mkdocs_index);
5566
}
5667

68+
let decl_index = db.get_decl_index();
69+
let globals = decl_index.get_global_decls();
70+
for global_decl_id in globals {
71+
global_gen::generate_global_markdown(db, &tl, &global_decl_id, &input, &global_out, &mut mkdocs_index);
72+
}
73+
5774
index_gen::generate_index(&tl, &mut mkdocs_index, &output);
5875

5976
if let Some(mixin) = mixin {
@@ -74,6 +91,7 @@ struct MemberDisplay {
7491
struct MkdocsIndex {
7592
pub types: Vec<IndexStruct>,
7693
pub modules: Vec<IndexStruct>,
94+
pub globals: Vec<IndexStruct>,
7795
}
7896

7997
#[derive(Debug, Serialize, Deserialize)]

crates/emmylua_doc_cli/src/markdown_generator/mod_gen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn check_filter(db: &DbIndex, file_id: FileId, workspace: &Path) -> Option<()> {
7979
Some(())
8080
}
8181

82-
fn generate_member_owner_module(
82+
pub fn generate_member_owner_module(
8383
db: &DbIndex,
8484
member_owner: LuaMemberOwner,
8585
owner_name: &str,

crates/emmylua_doc_cli/src/markdown_generator/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,6 @@ fn render_typ(db: &DbIndex, typ: &LuaType) -> String {
219219
LuaType::FloatConst(_) => "number".to_string(),
220220
LuaType::StringConst(_) => "string".to_string(),
221221
LuaType::BooleanConst(_) => "boolean".to_string(),
222-
_ => humanize_type(db, typ, RenderLevel::Detailed),
222+
_ => humanize_type(db, typ, RenderLevel::Simple),
223223
}
224224
}

crates/emmylua_doc_cli/template/index_template.tl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@
1111
{% for item in modules -%}
1212
- [{{ item.name }}]({{ item.file }})
1313
{% endfor %}
14+
{% endif %}
15+
16+
{% if globals %}
17+
## Globals
18+
{% for item in globals -%}
19+
- [{{ item.name }}]({{ item.file }})
20+
{% endfor %}
1421
{% endif %}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# global {{ global_name }}
2+
3+
{% if description %}
4+
{{ description }}
5+
{% endif %}
6+
---
7+
{% if methods %}
8+
## methods
9+
---
10+
{% for method in methods %}
11+
### {{ method.name }}
12+
---
13+
{{ method.display }}
14+
15+
{% if method.description %}
16+
{{ method.description }}
17+
{% endif %}
18+
{% endfor %}
19+
{% endif %}
20+
21+
{% if fields %}
22+
## fields
23+
---
24+
{% for field in fields %}
25+
### {{ field.name }}
26+
---
27+
{{ field.display }}
28+
29+
{% if field.description %}
30+
{{ field.description }}
31+
{% endif %}
32+
{% endfor %}
33+
{% endif %}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# global {{ global_name }}
2+
3+
---
4+
{% if display %}
5+
{{ display }}
6+
{% endif %}
7+
8+
{% if description %}
9+
{{ description }}
10+
{% endif %}

crates/emmylua_doc_cli/template/mkdocs_template.tl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,11 @@ nav:
3535
- {{ item.name }} : {{ item.file }}
3636
{% endfor %}
3737
{% endif %}
38+
{% if globals %}
39+
- Globals:
40+
{% for item in globals -%}
41+
- {{ item.name }} : {{ item.file }}
42+
{% endfor %}
43+
{% endif %}
3844
extra:
3945
footer: ""

0 commit comments

Comments
 (0)