|
1 | 1 | use emmylua_parser::{ |
2 | | - LuaAst, LuaAstNode, LuaAstToken, LuaDocDescriptionOwner, LuaDocTagAs, LuaDocTagModule, |
3 | | - LuaDocTagOverload, LuaDocTagParam, LuaDocTagReturn, LuaDocTagType, LuaExpr, LuaLocalName, |
4 | | - LuaVarExpr, |
| 2 | + BinaryOperator, LuaAst, LuaAstNode, LuaAstToken, LuaBlock, LuaDocDescriptionOwner, LuaDocTagAs, |
| 3 | + LuaDocTagCast, LuaDocTagModule, LuaDocTagOverload, LuaDocTagParam, LuaDocTagReturn, |
| 4 | + LuaDocTagType, LuaExpr, LuaLocalName, LuaNameToken, LuaVarExpr, |
5 | 5 | }; |
6 | 6 |
|
7 | 7 | use crate::{ |
8 | 8 | db_index::{ |
9 | 9 | LuaDeclId, LuaDocParamInfo, LuaDocReturnInfo, LuaMemberId, LuaOperator, LuaPropertyOwnerId, |
10 | 10 | LuaSignatureId, LuaType, |
11 | 11 | }, |
12 | | - InFiled, |
| 12 | + InFiled, TypeAssertion, |
13 | 13 | }; |
14 | 14 |
|
15 | 15 | use super::{ |
@@ -238,3 +238,82 @@ pub fn analyze_as(analyzer: &mut DocAnalyzer, tag: LuaDocTagAs) -> Option<()> { |
238 | 238 |
|
239 | 239 | Some(()) |
240 | 240 | } |
| 241 | + |
| 242 | +pub fn analyze_cast(analyzer: &mut DocAnalyzer, tag: LuaDocTagCast) -> Option<()> { |
| 243 | + let name_token = tag.get_name_token(); |
| 244 | + if let Some(name_token) = name_token { |
| 245 | + analyze_cast_with_name_token(analyzer, name_token, tag); |
| 246 | + } |
| 247 | + |
| 248 | + Some(()) |
| 249 | +} |
| 250 | + |
| 251 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 252 | +enum CastAction { |
| 253 | + Force, |
| 254 | + Add, |
| 255 | + Remove, |
| 256 | +} |
| 257 | + |
| 258 | +fn analyze_cast_with_name_token( |
| 259 | + analyzer: &mut DocAnalyzer, |
| 260 | + name_token: LuaNameToken, |
| 261 | + tag: LuaDocTagCast, |
| 262 | +) -> Option<()> { |
| 263 | + let name = name_token.get_name_text(); |
| 264 | + let decl_index = analyzer.db.get_decl_index(); |
| 265 | + let decl_tree = decl_index.get_decl_tree(&analyzer.file_id)?; |
| 266 | + let decl = decl_tree.find_local_decl(name, tag.get_position())?; |
| 267 | + let decl_id = decl.get_id(); |
| 268 | + |
| 269 | + let effect_range = tag.ancestors::<LuaBlock>().next()?.get_range(); |
| 270 | + |
| 271 | + for cast_op_type in tag.get_op_types() { |
| 272 | + let action = match cast_op_type.get_op() { |
| 273 | + Some(op) => { |
| 274 | + if op.get_op() == BinaryOperator::OpAdd { |
| 275 | + CastAction::Add |
| 276 | + } else { |
| 277 | + CastAction::Remove |
| 278 | + } |
| 279 | + } |
| 280 | + None => CastAction::Force, |
| 281 | + }; |
| 282 | + |
| 283 | + if cast_op_type.is_nullable() { |
| 284 | + let flow_chain = analyzer |
| 285 | + .db |
| 286 | + .get_flow_index_mut() |
| 287 | + .get_or_create_flow_chain(analyzer.file_id, decl_id); |
| 288 | + match action { |
| 289 | + CastAction::Add => { |
| 290 | + flow_chain.add_type_assert(TypeAssertion::Add(LuaType::Nil), effect_range); |
| 291 | + } |
| 292 | + CastAction::Remove => { |
| 293 | + flow_chain.add_type_assert(TypeAssertion::Remove(LuaType::Nil), effect_range); |
| 294 | + } |
| 295 | + _ => {} |
| 296 | + } |
| 297 | + } else if let Some(typ) = cast_op_type.get_type() { |
| 298 | + let typ = infer_type(analyzer, typ); |
| 299 | + let flow_chain = analyzer |
| 300 | + .db |
| 301 | + .get_flow_index_mut() |
| 302 | + .get_or_create_flow_chain(analyzer.file_id, decl_id); |
| 303 | + |
| 304 | + match action { |
| 305 | + CastAction::Add => { |
| 306 | + flow_chain.add_type_assert(TypeAssertion::Add(typ), effect_range); |
| 307 | + } |
| 308 | + CastAction::Remove => { |
| 309 | + flow_chain.add_type_assert(TypeAssertion::Remove(typ), effect_range); |
| 310 | + } |
| 311 | + CastAction::Force => { |
| 312 | + flow_chain.add_type_assert(TypeAssertion::Force(typ), effect_range); |
| 313 | + } |
| 314 | + } |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + Some(()) |
| 319 | +} |
0 commit comments