-
Notifications
You must be signed in to change notification settings - Fork 597
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
Provide intention to fill struct members #5945
Comments
Hi @mkaput can you provide a link to this on the codebase |
Hey! Here's the starting point: cairo/crates/cairo-lang-language-server/src/ide/code_actions/mod.rs Lines 34 to 78 in cdaceb9
You can follow the implementation of unused variable intention as the hooking point. First, you need to assign an error code to this particular diagnostic; you can You will need to get information about struct members. This is provided in the cairo/crates/cairo-lang-semantic/src/db.rs Lines 233 to 238 in 50d465c
|
Hi @mkaput I'm having issues getting the struct_Id, also there is no fix struct field error, i created an error code for missing members |
That's good, that's what was supposed to be done!
You need to get the cairo/crates/cairo-lang-language-server/src/ide/completion/completions.rs Lines 241 to 244 in cdaceb9
and you have the member names 🙂 You can check this PR, maybe it will help you with implementation |
@the-first-elder any progress? how can we help? |
This is what i have currently done, I do need some guidance in getting it right. pub fn fill_struct_fields(
db: &AnalysisDatabase,
node: &SyntaxNode,
diagnostic: Diagnostic,
uri: Url,
) -> CodeAction {
// Resolve the StructId from the SyntaxNode
let struct_id = resolve_struct_id(db, node).expect("Failed to resolve StructId");
let members = get_struct_members(db.upcast(), struct_id)
.expect("Failed to extract struct members");
// Create the text edit
let text_edit = TextEdit {
range: diagnostic.range,
new_text: format!("{{ {} }}", generate_default_values(&members)),
};
CodeAction {
title: "Fill missing struct fields".to_string(),
edit: Some(WorkspaceEdit {
changes: Some(HashMap::from([(uri, vec![text_edit])])),
document_changes: None,
change_annotations: None,
}),
diagnostics: Some(vec![diagnostic]),
..Default::default()
}
}
fn resolve_struct_id(db: &AnalysisDatabase, node: &SyntaxNode) -> Option<StructId> {
let syntax_db = db.upcast();
if node.kind(syntax_db) == SyntaxKind::ExprStructCtorCall {
let lookup_item_id = db.find_lookup_item(&node)?;
let function_id = lookup_item_id.function_with_body()?;
let expr_id = db.lookup_expr_by_ptr(function_id, node.stable_ptr()).ok()?;
let semantic_expr = db.expr_semantic(function_id, expr_id);
let ty = semantic_expr.ty();
if ty.is_missing(db) {
return None;
}
let (_, long_ty) = peel_snapshots(db, ty);
if let TypeLongId::Concrete(ConcreteTypeId::Struct(concrete_struct_id)) = long_ty {
return Some(concrete_struct_id.struct_id(db));
}
}
None
}
fn get_struct_members(db: &dyn SemanticGroup, struct_id: StructId) -> Maybe<OrderedHashMap<SmolStr, Member>> {
db.struct_members(struct_id)
}
fn generate_default_values(members: &OrderedHashMap<SmolStr, Member>) -> String {
members.iter().map(|(name, _)| format!("{}: default_value", name)).collect::<Vec<_>>().join(", ")
} |
@the-first-elder looks like a solid work! Have you tried if it works? You can do it via building the LS from source and setting {
"cairo1.languageServerPath": "/Users/piotrmagiera/SWM/Dev/cairo/target/release/cairo-language-server",
"cairo1.preferScarbLanguageServer": false,
} in Regarding the implementation I am worried that checking if the syntax node is of kind Regarding the |
Just put |
i get this when i try to hover |
Does it happen on your branch? Or the main one? |
mine but same on main as well |
Could you provide reproduction for that in another issue? |
Unassigning due to lack of activity. |
It might be helpful to outline the main areas where incremental updates could have the biggest impact, especially if the goal is to improve responsiveness with large files. Is there any specific guidance on this? |
@poolcleaner6 I'm responding on CairoLS Telegram channel, that's a more proper place for this topic than this issue :) |
Implement the following intention in CairoLS:
The text was updated successfully, but these errors were encountered: