|
| 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 | +} |
0 commit comments