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

Code before:
    } else {
        let targets = convert_resolved_definitions_to_targets(db, definitions);
        Some(crate::NavigationTargets::unique(targets))
    }
}

pub(crate) fn find_goto_target(
    parsed: &ParsedModuleRef,
    offset: TextSize,
) -> Option<GotoTarget<'_>> {
    let token = parsed
        .tokens()
        .at_offset(offset)
        .max_by_key(|token| match token.kind() {
            TokenKind::Name
            | TokenKind::String
            | TokenKind::Complex
            | TokenKind::Float
            | TokenKind::Int => 1,

            TokenKind::Comment => -1,

            // if we have a<CURSOR>+b`, prefer the `+` token (by respecting the token ordering)
            // This matches VS Code's behavior where it sends the start of the clicked token as offset.
            kind if kind.as_binary_operator().is_some() || kind.as_unary_operator().is_some() => 1,
            _ => 0,
        })?;

    if token.kind().is_comment() {
        return None;
    }

    let covering_node = covering_node(parsed.syntax().into(), token.range())
        .find_first(|node| node.is_identifier() || node.is_expression())
        .ok()?;

    GotoTarget::from_covering_node(&covering_node, offset, parsed.tokens())
}

/// Helper function to resolve a module name and create a navigation target.
fn definitions_for_module<'db>(
    db: &'db dyn crate::Db,
    module_name_str: &str,
) -> Option<DefinitionsOrTargets<'db>> {
    use ty_python_semantic::{ModuleName, resolve_module};

    if let Some(module_name) = ModuleName::new(module_name_str) {
        if let Some(resolved_module) = resolve_module(db, &module_name) {
            if let Some(module_file) = resolved_module.file(db) {
                return Some(DefinitionsOrTargets::Definitions(vec![
                    ResolvedDefinition::Module(module_file),

Review comment (bug):
I wonder if it makes sense to change this to `node.is_stmt`. I don't think we ever want to go beyond the closest statement (my, now proven incorrect, assumption was that `expression` is a sufficient boundary but turns out it isn't)

Code after:
) -> Option<GotoTarget<'_>> {
    let token = parsed
        .tokens()
        .at_offset(offset)
        .max_by_key(|token| match token.kind() {
            TokenKind::Name
            | TokenKind::String
            | TokenKind::Complex
            | TokenKind::Float
            | TokenKind::Int => 1,

            TokenKind::Comment => -1,

            // if we have a<CURSOR>+b`, prefer the `+` token (by respecting the token ordering)
            // This matches VS Code's behavior where it sends the start of the clicked token as offset.
            kind if kind.as_binary_operator().is_some() || kind.as_unary_operator().is_some() => 1,
            _ => 0,
        })?;

    if token.kind().is_comment() {
        return None;
    }

    let covering_node = covering_node(parsed.syntax().into(), token.range())
        .find_first(|node| {
            node.is_identifier() || node.is_expression() || node.is_stmt_import_from()
        })
        .ok()?;

    GotoTarget::from_covering_node(&covering_node, offset, parsed.tokens())
}

/// Helper function to resolve a module name and create a navigation target.
fn definitions_for_module<'db>(
    model: &SemanticModel,
    module: Option<&str>,
    level: u32,
) -> Option<DefinitionsOrTargets<'db>> {
    let module = model.resolve_module(module, level)?;
    let file = module.file(model.db())?;
    Some(DefinitionsOrTargets::Definitions(vec![
        ResolvedDefinition::Module(file),
    ]))
}

/// Helper function to extract module component information from a dotted module name
fn find_module_component(
    full_module_name: &str,
    module_start: TextSize,
    offset: TextSize,
) -> Option<(usize, TextRange)> {