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

Code before:
        // If we have `from ('.' | '...')* dotted_name <name>` we can suggest `import`.
        if (tokens_after_dots_before_last_name.is_empty() && dots_count > 0)
            || is_dotted_name(tokens_after_dots_before_last_name)
        {
            return Some(Completion::keyword("import"));
        }
    }

    None
}

fn is_dotted_name(tokens: &[Token]) -> bool {
    let tokens_len = tokens.len();
    if tokens_len == 1 && tokens[0].kind() == TokenKind::Name {
        return true;
    }
    if tokens_len >= 3 {
        let last_token = tokens.last().unwrap();
        let second_last_token = &tokens[tokens_len - 2];
        let tokens_to_third_last = &tokens[0..tokens_len - 2];
        return is_dotted_name(tokens_to_third_last)
            && second_last_token.kind() == TokenKind::Dot
            && last_token.kind() == TokenKind::Name;
    }
    false
}

/// Order completions according to the following rules:
///
/// 1) Names with no underscore prefix
/// 2) Names starting with `_` but not dunders
/// 3) `__dunder__` names
///
/// Among each category, type-check-only items are sorted last,
/// and otherwise completions are sorted lexicographically.
///
/// This has the effect of putting all dunder attributes after "normal"
/// attributes, and all single-underscore attributes after dunder attributes.
fn compare_suggestions(c1: &Completion, c2: &Completion) -> Ordering {
    fn key<'a>(completion: &'a Completion) -> (bool, bool, NameKind, bool, &'a Name) {
        (
            completion.module_name.is_some(),
            completion.builtin,
            NameKind::classify(&completion.name),
            completion.is_type_check_only,
            &completion.name,
        )
    }

    key(c1).cmp(&key(c2))
}

Review comment (suggestion):
I find this a bit easier to understand:

```suggestion
fn is_dotted_name(tokens: &[Token]) -> bool {
    match tokens {
        [] | [_, _] => false,
        [tok] => tok.kind() == TokenKind::Name,
        [dotted_name @ .., penultimate, last] => {
            last.kind() == TokenKind::Name
                && penultimate.kind() == TokenKind::Dot
                && is_dotted_name(dotted_name)
        }
    }
}
```

Code after:
/// Whether the last token is positioned within a string token (regular, f-string, t-string, etc).
///
/// Note that this will return `false` when the last token is positioned within an
/// interpolation block in an f-string or a t-string.
fn is_in_string(tokens: &[Token]) -> bool {
    tokens.last().is_some_and(|t| {
        matches!(
            t.kind(),
            TokenKind::String | TokenKind::FStringMiddle | TokenKind::TStringMiddle
        )
    })
}

/// Returns true when the tokens indicate that the definition of a new name is being introduced at the end.
fn is_in_definition_place(db: &dyn Db, tokens: &[Token], file: File) -> bool {
    let is_definition_keyword = |token: &Token| {
        if matches!(
            token.kind(),
            TokenKind::Def | TokenKind::Class | TokenKind::Type
        ) {
            true
        } else if token.kind() == TokenKind::Name {
            let source = source_text(db, file);
            &source[token.range()] == "type"
        } else {
            false
        }
    };

    tokens
        .len()
        .checked_sub(2)
        .and_then(|i| tokens.get(i))
        .is_some_and(is_definition_keyword)
}

/// Order completions according to the following rules:
///
/// 1) Names with no underscore prefix
/// 2) Names starting with `_` but not dunders
/// 3) `__dunder__` names
///
/// Among each category, type-check-only items are sorted last,
/// and otherwise completions are sorted lexicographically.
///
/// This has the effect of putting all dunder attributes after "normal"
/// attributes, and all single-underscore attributes after dunder attributes.
fn compare_suggestions(c1: &Completion, c2: &Completion) -> Ordering {
    fn key<'a>(completion: &'a Completion) -> (bool, bool, NameKind, bool, &'a Name) {
        (
            completion.module_name.is_some(),