|
| 1 | +use code_analysis::{humanize_type, DbIndex, LuaPropertyOwnerId, LuaSignatureId, LuaType}; |
| 2 | + |
| 3 | +pub fn render_const_type(db: &DbIndex, typ: &LuaType) -> String { |
| 4 | + let const_value = humanize_type(db, typ); |
| 5 | + |
| 6 | + match typ { |
| 7 | + LuaType::IntegerConst(_) | LuaType::DocIntergerConst(_) => { |
| 8 | + format!("integer = {}", const_value) |
| 9 | + } |
| 10 | + LuaType::FloatConst(_) => format!("number = {}", const_value), |
| 11 | + LuaType::StringConst(_) | LuaType::DocStringConst(_) => format!("string = {}", const_value), |
| 12 | + _ => const_value, |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +pub fn render_function_type(db: &DbIndex, typ: &LuaType, func_name: &str, is_local: bool) -> String { |
| 17 | + match typ { |
| 18 | + LuaType::Function => { |
| 19 | + format!( |
| 20 | + "{}function {}()", |
| 21 | + if is_local { "local " } else { "" }, |
| 22 | + func_name |
| 23 | + ) |
| 24 | + } |
| 25 | + LuaType::DocFunction(lua_func) => { |
| 26 | + let async_prev = if lua_func.is_async() { "async " } else { "" }; |
| 27 | + let local_prev = if is_local { "local " } else { "" }; |
| 28 | + let params = lua_func |
| 29 | + .get_params() |
| 30 | + .iter() |
| 31 | + .map(|param| { |
| 32 | + let name = param.0.clone(); |
| 33 | + if let Some(ty) = ¶m.1 { |
| 34 | + format!("{}: {}", name, humanize_type(db, ty)) |
| 35 | + } else { |
| 36 | + name.to_string() |
| 37 | + } |
| 38 | + }) |
| 39 | + .collect::<Vec<_>>(); |
| 40 | + |
| 41 | + let rets = lua_func.get_ret(); |
| 42 | + |
| 43 | + let ret_strs = rets |
| 44 | + .iter() |
| 45 | + .map(|ty| humanize_type(db, ty)) |
| 46 | + .collect::<Vec<_>>() |
| 47 | + .join(","); |
| 48 | + |
| 49 | + let mut result = String::new(); |
| 50 | + result.push_str(async_prev); |
| 51 | + result.push_str(local_prev); |
| 52 | + result.push_str("function "); |
| 53 | + result.push_str(func_name); |
| 54 | + result.push_str("("); |
| 55 | + if params.len() > 1 { |
| 56 | + result.push_str("\n"); |
| 57 | + for param in ¶ms { |
| 58 | + result.push_str(" "); |
| 59 | + result.push_str(param); |
| 60 | + result.push_str(",\n"); |
| 61 | + } |
| 62 | + result.pop(); // Remove the last comma |
| 63 | + result.pop(); // Remove the last newline |
| 64 | + result.push_str("\n"); |
| 65 | + } else { |
| 66 | + result.push_str(¶ms.join(", ")); |
| 67 | + } |
| 68 | + result.push_str(")"); |
| 69 | + if ret_strs.len() > 15 { |
| 70 | + result.push_str("\n"); |
| 71 | + } |
| 72 | + |
| 73 | + if !ret_strs.is_empty() { |
| 74 | + result.push_str("-> "); |
| 75 | + result.push_str(&ret_strs); |
| 76 | + } |
| 77 | + |
| 78 | + result |
| 79 | + } |
| 80 | + LuaType::Signature(signature_id) => { |
| 81 | + render_signature_type(db, signature_id.clone(), func_name, is_local).unwrap_or(format!( |
| 82 | + "{}function {}", |
| 83 | + if is_local { "local " } else { "" }, |
| 84 | + func_name |
| 85 | + )) |
| 86 | + } |
| 87 | + _ => format!( |
| 88 | + "{}function {}", |
| 89 | + if is_local { "local " } else { "" }, |
| 90 | + func_name |
| 91 | + ), |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +fn render_signature_type( |
| 96 | + db: &DbIndex, |
| 97 | + signature_id: LuaSignatureId, |
| 98 | + func_name: &str, |
| 99 | + is_local: bool, |
| 100 | +) -> Option<String> { |
| 101 | + let signature = db.get_signature_index().get(&signature_id)?; |
| 102 | + let mut async_prev = ""; |
| 103 | + if let Some(property) = db |
| 104 | + .get_property_index() |
| 105 | + .get_property(LuaPropertyOwnerId::Signature(signature_id)) |
| 106 | + { |
| 107 | + async_prev = if property.is_async { "async " } else { "" }; |
| 108 | + } |
| 109 | + |
| 110 | + let local_prev = if is_local { "local " } else { "" }; |
| 111 | + let params = signature |
| 112 | + .get_type_params() |
| 113 | + .iter() |
| 114 | + .map(|param| { |
| 115 | + let name = param.0.clone(); |
| 116 | + if let Some(ty) = ¶m.1 { |
| 117 | + format!("{}: {}", name, humanize_type(db, ty)) |
| 118 | + } else { |
| 119 | + name.to_string() |
| 120 | + } |
| 121 | + }) |
| 122 | + .collect::<Vec<_>>(); |
| 123 | + |
| 124 | + let rets = &signature.return_docs; |
| 125 | + |
| 126 | + let mut result = String::new(); |
| 127 | + result.push_str(async_prev); |
| 128 | + result.push_str(local_prev); |
| 129 | + result.push_str("function "); |
| 130 | + result.push_str(func_name); |
| 131 | + result.push_str("("); |
| 132 | + if params.len() > 1 { |
| 133 | + result.push_str("\n"); |
| 134 | + for param in ¶ms { |
| 135 | + result.push_str(" "); |
| 136 | + result.push_str(param); |
| 137 | + result.push_str(",\n"); |
| 138 | + } |
| 139 | + result.pop(); // Remove the last comma |
| 140 | + result.pop(); // Remove the last newline |
| 141 | + result.push_str("\n"); |
| 142 | + } else { |
| 143 | + result.push_str(¶ms.join(", ")); |
| 144 | + } |
| 145 | + result.push_str(")"); |
| 146 | + match rets.len() { |
| 147 | + 0 => {} |
| 148 | + 1 => { |
| 149 | + result.push_str(" -> "); |
| 150 | + let type_text = humanize_type(db, &rets[0].type_ref); |
| 151 | + let name = rets[0].name.clone().unwrap_or("".to_string()); |
| 152 | + let detail = if rets[0].description.is_some() { |
| 153 | + format!(" -- {}", rets[0].description.as_ref().unwrap()) |
| 154 | + } else { |
| 155 | + "".to_string() |
| 156 | + }; |
| 157 | + result.push_str(format!("{}{}{}", name, type_text, detail).as_str()); |
| 158 | + } |
| 159 | + _ => { |
| 160 | + result.push_str("\n"); |
| 161 | + for ret in rets { |
| 162 | + let type_text = humanize_type(db, &ret.type_ref); |
| 163 | + let name = ret.name.clone().unwrap_or("".to_string()); |
| 164 | + let detail = if ret.description.is_some() { |
| 165 | + format!(" -- {}", ret.description.as_ref().unwrap()) |
| 166 | + } else { |
| 167 | + "".to_string() |
| 168 | + }; |
| 169 | + result.push_str(format!(" -> {}{}{}\n", name, type_text, detail).as_str()); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + Some(result) |
| 175 | +} |
0 commit comments