|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use emmylua_code_analysis::{humanize_type, LuaSignatureId, LuaType, RenderLevel, SemanticModel}; |
| 4 | +use emmylua_parser::{LuaAstNode, LuaClosureExpr}; |
| 5 | +use itertools::Itertools; |
| 6 | +use lsp_types::{InlayHint, InlayHintKind, InlayHintLabel, InlayHintLabelPart, Location}; |
| 7 | + |
| 8 | +pub fn build_closure_hint( |
| 9 | + semantic_model: &SemanticModel, |
| 10 | + result: &mut Vec<InlayHint>, |
| 11 | + closure: LuaClosureExpr, |
| 12 | +) -> Option<()> { |
| 13 | + if !semantic_model.get_emmyrc().hint.param_hint { |
| 14 | + return Some(()); |
| 15 | + } |
| 16 | + let file_id = semantic_model.get_file_id(); |
| 17 | + let signature_id = LuaSignatureId::from_closure(file_id, &closure); |
| 18 | + let signature = semantic_model |
| 19 | + .get_db() |
| 20 | + .get_signature_index() |
| 21 | + .get(&signature_id)?; |
| 22 | + |
| 23 | + let lua_params = closure.get_params_list()?; |
| 24 | + let signature_params = signature.get_type_params(); |
| 25 | + let mut lua_params_map = HashMap::new(); |
| 26 | + for param in lua_params.get_params() { |
| 27 | + if let Some(name_token) = param.get_name_token() { |
| 28 | + let name = name_token.get_name_text().to_string(); |
| 29 | + lua_params_map.insert(name, param); |
| 30 | + } else if param.is_dots() { |
| 31 | + lua_params_map.insert("...".to_string(), param); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + let document = semantic_model.get_document(); |
| 36 | + for (signature_param_name, typ) in &signature_params { |
| 37 | + if let Some(typ) = typ { |
| 38 | + if typ.is_any() { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + if let Some(lua_param) = lua_params_map.get(signature_param_name) { |
| 43 | + let lsp_range = document.to_lsp_range(lua_param.get_range())?; |
| 44 | + // 构造 label |
| 45 | + let mut label_parts = build_label_parts(semantic_model, &typ); |
| 46 | + // 为空时添加默认值 |
| 47 | + if label_parts.is_empty() { |
| 48 | + let typ_desc = format!( |
| 49 | + ": {}", |
| 50 | + humanize_type(semantic_model.get_db(), &typ, RenderLevel::Simple) |
| 51 | + ); |
| 52 | + label_parts.push(InlayHintLabelPart { |
| 53 | + value: typ_desc, |
| 54 | + location: Some( |
| 55 | + get_type_location(semantic_model, typ) |
| 56 | + .unwrap_or(Location::new(document.get_uri(), lsp_range)), |
| 57 | + ), |
| 58 | + ..Default::default() |
| 59 | + }); |
| 60 | + } |
| 61 | + let hint = InlayHint { |
| 62 | + kind: Some(InlayHintKind::TYPE), |
| 63 | + label: InlayHintLabel::LabelParts(label_parts), |
| 64 | + position: lsp_range.end, |
| 65 | + text_edits: None, |
| 66 | + tooltip: None, |
| 67 | + padding_left: Some(true), |
| 68 | + padding_right: None, |
| 69 | + data: None, |
| 70 | + }; |
| 71 | + result.push(hint); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + Some(()) |
| 77 | +} |
| 78 | + |
| 79 | +fn build_label_parts(semantic_model: &SemanticModel, typ: &LuaType) -> Vec<InlayHintLabelPart> { |
| 80 | + let mut parts: Vec<InlayHintLabelPart> = Vec::new(); |
| 81 | + match typ { |
| 82 | + LuaType::Union(union) => { |
| 83 | + for typ in union.get_types() { |
| 84 | + if let Some(part) = get_part(semantic_model, typ) { |
| 85 | + parts.push(part); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + _ => { |
| 90 | + if let Some(part) = get_part(semantic_model, typ) { |
| 91 | + parts.push(part); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + // 去重 |
| 96 | + let parts: Vec<InlayHintLabelPart> = parts |
| 97 | + .into_iter() |
| 98 | + .unique_by(|part| part.value.clone()) |
| 99 | + .collect(); |
| 100 | + // 将 "?" 标签移到最后 |
| 101 | + let mut normal_parts = Vec::new(); |
| 102 | + let mut nil_parts = Vec::new(); |
| 103 | + for part in parts { |
| 104 | + if part.value == "?" { |
| 105 | + nil_parts.push(part); |
| 106 | + } else { |
| 107 | + normal_parts.push(part); |
| 108 | + } |
| 109 | + } |
| 110 | + normal_parts.append(&mut nil_parts); |
| 111 | + let mut result = Vec::new(); |
| 112 | + for (i, part) in normal_parts.into_iter().enumerate() { |
| 113 | + let mut part = part; |
| 114 | + if part.value != "?" { |
| 115 | + part.value = format!("{}{}", if i == 0 { ": " } else { "|" }, part.value); |
| 116 | + } |
| 117 | + result.push(part); |
| 118 | + } |
| 119 | + result |
| 120 | +} |
| 121 | + |
| 122 | +fn get_part(semantic_model: &SemanticModel, typ: &LuaType) -> Option<InlayHintLabelPart> { |
| 123 | + match typ { |
| 124 | + LuaType::Union(_) => None, |
| 125 | + LuaType::Nil => { |
| 126 | + return Some(InlayHintLabelPart { |
| 127 | + value: "?".to_string(), |
| 128 | + location: get_type_location(semantic_model, typ), |
| 129 | + ..Default::default() |
| 130 | + }); |
| 131 | + } |
| 132 | + _ => { |
| 133 | + let value = humanize_type(semantic_model.get_db(), typ, RenderLevel::Simple); |
| 134 | + let location = get_type_location(semantic_model, typ); |
| 135 | + return Some(InlayHintLabelPart { |
| 136 | + value, |
| 137 | + location, |
| 138 | + ..Default::default() |
| 139 | + }); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +fn get_type_location(semantic_model: &SemanticModel, typ: &LuaType) -> Option<Location> { |
| 145 | + match typ { |
| 146 | + LuaType::Ref(id) => { |
| 147 | + let type_decl = semantic_model |
| 148 | + .get_db() |
| 149 | + .get_type_index() |
| 150 | + .get_type_decl(&id)?; |
| 151 | + let location = type_decl.get_locations().first()?; |
| 152 | + let document = semantic_model.get_document_by_file_id(location.file_id)?; |
| 153 | + let lsp_range = document.to_lsp_range(location.range)?; |
| 154 | + Some(Location::new(document.get_uri(), lsp_range)) |
| 155 | + } |
| 156 | + LuaType::Any => get_base_type_location(semantic_model, "any"), |
| 157 | + LuaType::Nil => get_base_type_location(semantic_model, "nil"), |
| 158 | + LuaType::Unknown => get_base_type_location(semantic_model, "string"), |
| 159 | + LuaType::Userdata => get_base_type_location(semantic_model, "userdata"), |
| 160 | + LuaType::Function => get_base_type_location(semantic_model, "function"), |
| 161 | + LuaType::Thread => get_base_type_location(semantic_model, "thread"), |
| 162 | + LuaType::Table => get_base_type_location(semantic_model, "table"), |
| 163 | + _ if typ.is_string() => get_base_type_location(semantic_model, "string"), |
| 164 | + _ if typ.is_integer() => get_base_type_location(semantic_model, "integer"), |
| 165 | + _ if typ.is_number() => get_base_type_location(semantic_model, "number"), |
| 166 | + _ if typ.is_boolean() => get_base_type_location(semantic_model, "boolean"), |
| 167 | + _ => None, |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +fn get_base_type_location(semantic_model: &SemanticModel, name: &str) -> Option<Location> { |
| 172 | + let type_decl = semantic_model |
| 173 | + .get_db() |
| 174 | + .get_type_index() |
| 175 | + .find_type_decl(semantic_model.get_file_id(), name)?; |
| 176 | + let location = type_decl.get_locations().first()?; |
| 177 | + let document = semantic_model.get_document_by_file_id(location.file_id)?; |
| 178 | + let lsp_range = document.to_lsp_range(location.range)?; |
| 179 | + Some(Location::new(document.get_uri(), lsp_range)) |
| 180 | +} |
0 commit comments