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

Code before:
    definitively_usable: Sort,
    /// This lets one sort completions based on whether they're in the
    /// current module or not. e.g., `Sort::Lower` for symbols outside
    /// the module and `Sort::Higher` for symbols inside the module
    /// that are already in scope.
    current_module: Sort,
    /// At time of writing (2025-11-11), keyword completions are
    /// classified as builtins, which makes them sort after everything
    /// else. But we probably want keyword completions to sort *before*
    /// anything else since they are so common. Moreover, it seems
    /// VS Code forcefully does this sorting. By doing it ourselves,
    /// we make our natural sorting match VS Code's, and thus our
    /// completion evaluation framework should be more representative
    /// of real world conditions.
    keyword: Sort,
    /// Sorts based on whether the symbol comes from the `builtins`
    /// module. i.e., Python's initial basis. We usually sort these
    /// lower to give priority to symbols in a tighter scope.
    builtin: Sort,
    /// Sorts based on the "kind" of a name. i.e., Its export status.
    /// We sort normal names the highest. Then dunder names and finally
    /// any other name that starts with a single underscore.
    name_kind: NameKind,
    /// Sorts based on whether this symbol is only available during
    /// type checking and not at runtime.
    type_check_only: Sort,
    /// The name of a symbol. This is kind of a last ditch thing that
    /// we fallback to in order to provide some stable and predictable
    /// ordering, but otherwise means we've mostly given up.
    name: &'a str,
}

/// An instruction to indicate an ordering preference.
#[derive(Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
enum Sort {
    /// Assign a higher rank. The suggestion will appear higher
    /// in the completion results.
    Higher,
    /// Assign an even rank. i.e., A "push" to the next
    /// criteria.
    #[default]
    Even,
    /// Assign a smaller rank. The suggestion will appear lower
    /// in the completion results.
    Lower,
}

/// Detect and add completions for unset arguments.
fn add_argument_completions<'db>(
    db: &'db dyn Db,
    file: File,

Review comment (suggestion):
I wonder if we should store the `CoveringNode` in `ScopedTarget` target to avoid re-traversing the AST multiple times to get the covering node

Code after:
    name: &'a str,
}

/// An instruction to indicate an ordering preference.
#[derive(Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
enum Sort {
    /// Assign a higher rank. The suggestion will appear higher
    /// in the completion results.
    Higher,
    /// Assign an even rank. i.e., A "push" to the next
    /// criteria.
    #[default]
    Even,
    /// Assign a smaller rank. The suggestion will appear lower
    /// in the completion results.
    Lower,
}

/// Detect and add completions for unset arguments.
fn add_argument_completions<'db>(
    db: &'db dyn Db,
    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, model.file(), cursor, completions);
                }
                return;
            }
            ast::AnyNodeRef::StmtClassDef(class_def) => {
                if let Some(arguments) = class_def.arguments.as_deref()
                    && arguments.range().contains_range(cursor.range)
                {
                    add_class_arg_completions(model, class_def, completions);
                }
                return;
            }
            node => {
                if node.is_statement() {
                    return;
                }
            }
        }
    }
}

/// Detect and add completions for unset class arguments.