Repository: astral-sh/ruff
File: crates/ty_ide/src/completion.rs (Rust)

Code before:
/// Detect and add completions for unset arguments.
fn add_argument_completions<'db>(
    db: &'db dyn Db,
    file: File,
    model: &SemanticModel<'db>,
    cursor: &ContextCursor<'_>,
    completions: &mut Completions<'db>,
) {
    for node in cursor.covering_node(cursor.range).ancestors() {
        match node {
            ast::AnyNodeRef::ExprCall(call)
                if call.arguments.range().contains_range(cursor.range) =>
            {
                add_function_arg_completions(db, file, cursor, completions);
                return;
            }
            ast::AnyNodeRef::StmtClassDef(class_def)
                if class_def
                    .arguments
                    .iter()
                    .any(|node| node.range().contains_range(cursor.range)) =>
            {
                add_class_arg_completions(model, class_def, completions);
                return;
            }
            _ => {}
        }
    }
}

/// Detect and add completions for unset class arguments.
///
/// Some arguments we know are always valid and thus they are easy
/// to provide. The `metaclass` keyword is always valid.
/// For `typing.TypedDict` subclasses, we add
/// `TypedDict` specific keywords like `total`.
fn add_class_arg_completions<'db>(
    model: &SemanticModel<'db>,
    class_def: &ast::StmtClassDef,
    completions: &mut Completions<'db>,
) {
    let is_set = |name| {
        class_def
            .arguments
            .as_ref()
            .is_some_and(|args| args.find_keyword(name).is_some())
    };

    if !is_set("metaclass") {
        let ty = Some(KnownClass::Type.to_subclass_of(model.db()));
        completions.add(Completion::argument("metaclass", ty, None));

Review comment (suggestion):
I think we should always exit after reaching a call or class def statement
```suggestion
            ast::AnyNodeRef::ExprCall(call)=>
            {
                if call.arguments.range().contains_range(cursor.range) {
	                add_function_arg_completions(db, file, cursor, completions);
                }
                return;
            }
            ast::AnyNodeRef::StmtClassDef(class_def) {
            	if let Some(arguments) = class_def.arguments.as_ref() &&
            		arguments.range().contains_range(cursor.range)) {
	                add_class_arg_completions(model, class_def, completions);
            		}
  
                return;
            }
            node => {
            	if node.is_statement() {
            		return;
          		}
            }
```

Code after:
    }
}

/// Detect and add completions for unset class arguments.
///
/// Some arguments we know are always valid and thus they are easy
/// to provide. The `metaclass` keyword is always valid.
/// For `typing.TypedDict` subclasses, we add
/// `TypedDict` specific keywords like `total`.
fn add_class_arg_completions<'db>(
    model: &SemanticModel<'db>,
    class_def: &ast::StmtClassDef,
    completions: &mut Completions<'db>,
) {
    let is_set = |name| {
        class_def
            .arguments
            .as_ref()
            .is_some_and(|args| args.find_keyword(name).is_some())
    };

    if !is_set("metaclass") {
        let ty = Some(KnownClass::Type.to_subclass_of(model.db()));
        completions.add(Completion::argument("metaclass", ty, None));
    }

    let is_typed_dict = class_def
        .inferred_type(model)
        .and_then(Type::as_class_literal)
        .is_some_and(|t| t.is_typed_dict(model.db()));

    // TODO: Handle PEP 728 that adds two extra keywords,
    // closed and extra_items.
    //
    // See https://peps.python.org/pep-0728/
    if is_typed_dict && !is_set("total") {
        let ty = Some(KnownClass::Bool.to_instance(model.db()));
        completions.add(Completion::argument("total", ty, None));
    }
}

/// Detect and add completions for unset function arguments.
///
/// Suggestions are only provided if the cursor is currently inside a
/// function call and the function arguments have not 1) already been
/// set and 2) been defined as positional-only.
fn add_function_arg_completions<'db>(
    db: &'db dyn Db,
    file: File,
    cursor: &ContextCursor<'_>,
    completions: &mut Completions<'db>,