-
Notifications
You must be signed in to change notification settings - Fork 53
feat: return_cast class field #907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @phanen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the type-checking capabilities by extending the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a valuable feature to support @return_cast on class fields, enabling more precise type narrowing. The changes are well-structured, touching the parser, analyzer, and type inference logic, and are accompanied by comprehensive tests that cover the new functionality well. I've made a few suggestions to improve code readability and reduce minor duplication, but overall, this is a solid implementation.
| for ancestor in name_expr.syntax().ancestors() { | ||
| if let Some(return_cast_tag) = LuaDocTagReturnCast::cast(ancestor.clone()) { | ||
| // Find the LuaComment that contains this tag | ||
| for comment_ancestor in return_cast_tag.syntax().ancestors() { | ||
| if let Some(comment) = LuaComment::cast(comment_ancestor) { | ||
| // Get the owner (function) of this comment | ||
| if let Some(owner) = comment.get_owner() { | ||
| if let LuaAst::LuaClosureExpr(closure) = owner { | ||
| let sig_id = LuaSignatureId::from_closure( | ||
| semantic_model.get_file_id(), | ||
| &closure, | ||
| ); | ||
| if let Some(sig) = | ||
| semantic_model.get_db().get_signature_index().get(&sig_id) | ||
| { | ||
| // Check if the owner function is a method | ||
| if sig.is_method(semantic_model, None) { | ||
| return Some(()); | ||
| } | ||
| // Check if self is a parameter | ||
| if sig.find_param_idx("self").is_some() { | ||
| return Some(()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The nested for loops for traversing ancestors can be made more concise and readable by using ancestors::<T>().next() and if let chains. This would flatten the code and make the intent clearer.
if let Some(return_cast_tag) = name_expr.ancestors::<LuaDocTagReturnCast>().next() {
if let Some(comment) = return_cast_tag.ancestors::<LuaComment>().next() {
if let Some(owner) = comment.get_owner() {
if let LuaAst::LuaClosureExpr(closure) = owner {
let sig_id = LuaSignatureId::from_closure(
semantic_model.get_file_id(),
&closure,
);
if let Some(sig) =
semantic_model.get_db().get_signature_index().get(&sig_id)
{
// Check if the owner function is a method or has a 'self' param
if sig.is_method(semantic_model, None) || sig.find_param_idx("self").is_some() {
return Some(());
}
}
}
}
}
}| let base_matches = match &self_var_ref_id { | ||
| VarRefId::VarRef(self_decl_id) => { | ||
| decl_or_member.as_decl_id() == Some(*self_decl_id) | ||
| } | ||
| VarRefId::SelfRef(self_decl_or_member) => { | ||
| decl_or_member == self_decl_or_member | ||
| } | ||
| VarRefId::IndexRef(self_decl_or_member, _) => { | ||
| decl_or_member == self_decl_or_member | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The match statement for determining base_matches has identical logic for the VarRefId::SelfRef and VarRefId::IndexRef arms. You can combine these two arms using | to reduce code duplication and simplify the logic.
let base_matches = match &self_var_ref_id {
VarRefId::VarRef(self_decl_id) => {
decl_or_member.as_decl_id() == Some(*self_decl_id)
}
VarRefId::SelfRef(self_decl_or_member)
| VarRefId::IndexRef(self_decl_or_member, _) => {
decl_or_member == self_decl_or_member
}
};| match parse_cast_expr(p) { | ||
| Ok(_) => {} | ||
| Err(e) => { | ||
| return Err(e); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of code are written by ai now, it seems work as my expected.