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

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

/// Whether the last token is positioned within a string token (regular, f-string, t-string, etc).
///

Review comment (suggestion):
(optional, obviously)

```suggestion
    if last.kind() != TokenKind::Name && !last.kind().is_keyword() {
```

Code after:
            (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. And this applies also
    // to the other keywords.
    if !matches!(last.kind(), TokenKind::Name) && !last.kind().is_keyword() {
        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,
    file: File,
    tokens: &[Token],
    typed: Option<&str>,
) -> bool {
    is_in_comment(tokens) || is_in_string(tokens) || is_in_definition_place(db, file, tokens, typed)
}

/// Whether the last token is within a comment or not.