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

Code before:
            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)
}

/// when the tokens indicate that the last token should be a keyword, we provide completions for that
fn keyword_completions<'db>(tokens: &[Token]) -> Option<Completion<'db>> {
    is_in_import_statement(tokens)
}

fn is_in_import_statement<'db>(tokens: &[Token]) -> Option<Completion<'db>> {
    if token_suffix_by_kinds(tokens, [TokenKind::From, TokenKind::Name, TokenKind::Name]).is_some()
    {
        return Some(Completion::keyword("import"));
    }
    None
}

/// 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):
this works well for the simple case of `from <NAME> <CURSOR>`, but doesn't work for:
- `from <NAME>.<NAME> <CURSOR>`
- `from .<NAME> <CURSOR>`
- `from ...<NAME>.<NAME> <CURSOR>`

etc.

<img width="1498" height="364" alt="image" src="https://github.com/user-attachments/assets/b2e52aef-104e-4b35-88b7-261ea7fb6170" />


Code after:
                TK::Name | TK::Dot | TK::Ellipsis | TK::Case | TK::Match | TK::Type | TK::Unknown,
            ) => S::ModulePossiblyDotted,
            (S::ModulePossiblyDotted | S::ModuleOnlyDotted, TK::From) => return true,
            _ => return false,
        };
    }
    false
}

/// Looks for the text typed immediately before the cursor offset
/// given.
///
/// If there isn't any typed text or it could not otherwise be found,
/// then `None` is returned.
fn find_typed_text(
    db: &dyn Db,
    file: File,
    parsed: &ParsedModuleRef,
    offset: TextSize,
) -> Option<String> {
    let source = source_text(db, file);
    let tokens = tokens_start_before(parsed.tokens(), offset);
    let last = tokens.last()?;
    // It's odd to include `TokenKind::Import` here, but it
    // indicates that the user has typed `import`. This is
    // useful to know in some contexts.
    if !matches!(last.kind(), TokenKind::Name | TokenKind::Import) {
        return None;
    }
    // This one's weird, but if the cursor is beyond
    // what is in the closest `Name` token, then it's
    // likely we can't infer anything about what has
    // been typed. This likely means there is whitespace
    // or something that isn't represented in the token
    // stream. So just give up.
    if last.end() < offset {
        return None;
    }
    Some(source[last.range()].to_string())
}

/// Whether the last token is in a place where we should not provide completions.
fn is_in_no_completions_place(db: &dyn Db, tokens: &[Token], file: File) -> bool {
    is_in_comment(tokens) || is_in_string(tokens) || is_in_definition_place(db, tokens, file)
}

/// Whether the last token is within a comment or not.
fn is_in_comment(tokens: &[Token]) -> bool {
    tokens.last().is_some_and(|t| t.kind().is_comment())
}