Repository: nushell/nushell
File: crates/nu-cli/src/completions/completer.rs (Rust)

Code before:
            .iter()
            .map(|attr| &attr.expr)
            .chain(Some(ab.item.as_ref()))
            .find_map(|expr| expr.find_map(working_set, &closure))
            .or(Some(expr))
            .map(FindMapResult::Found)
            .unwrap_or_default(),
        _ => FindMapResult::Continue,
    }
}

/// Before completion, an additional character `a` is added to the source as a placeholder for correct parsing results.
/// This function helps to strip it
fn strip_placeholder_if_any<'a>(
    working_set: &'a StateWorkingSet,
    span: &Span,
    strip: bool,
) -> (Span, &'a [u8]) {
    let new_span = if strip {
        let new_end = std::cmp::max(span.end - 1, span.start);
        Span::new(span.start, new_end)
    } else {
        span.to_owned()
    };
    let prefix = working_set.get_span_contents(new_span);
    (new_span, prefix)
}

/// Given a span with noise,
/// 1. Call `rsplit` to get the last token
/// 2. Strip the last placeholder from the token
fn strip_placeholder_with_rsplit<'a>(
    working_set: &'a StateWorkingSet,
    span: &Span,
    predicate: impl FnMut(&u8) -> bool,
    strip: bool,
) -> (Span, &'a [u8]) {
    let span_content = working_set.get_span_contents(*span);
    let mut prefix = span_content
        .rsplit(predicate)
        .next()
        .unwrap_or(span_content);
    let start = span.end.saturating_sub(prefix.len());
    if strip && !prefix.is_empty() {
        prefix = &prefix[..prefix.len() - 1];
    }
    let end = start + prefix.len();
    (Span::new(start, end), prefix)
}

#[derive(Clone)]

Review comment (refactor):
Nitpick: I feel like `extract` would be better than `check`

Code after:
            .head
            .find_map(working_set, &closure)
            .map(FindMapResult::Found)
            // e.g. use std/util [<tab>
            .or_else(|| {
                (fcp.head.span.contains(pos) && matches!(fcp.head.expr, Expr::List(_)))
                    .then_some(FindMapResult::Continue)
            })
            .or(Some(FindMapResult::Found(expr)))
            .unwrap_or_default(),
        Expr::Var(_) => FindMapResult::Found(expr),
        Expr::AttributeBlock(ab) => ab
            .attributes
            .iter()
            .map(|attr| &attr.expr)
            .chain(Some(ab.item.as_ref()))
            .find_map(|expr| expr.find_map(working_set, &closure))
            .or(Some(expr))
            .map(FindMapResult::Found)
            .unwrap_or_default(),
        _ => FindMapResult::Continue,
    }
}

/// Helper function to extract file-path expression from redirection target
fn check_redirection_target(target: &RedirectionTarget, pos: usize) -> Option<&Expression> {
    let expr = target.expr();
    expr.and_then(|expression| {
        if let Expr::String(_) = expression.expr
            && expression.span.contains(pos)
        {
            expr
        } else {
            None
        }
    })
}

/// For redirection target completion
/// https://github.com/nushell/nushell/issues/16827
fn check_redirection_in_block(block: &Block, pos: usize) -> Option<&Expression> {
    block.pipelines.iter().find_map(|pipeline| {
        pipeline.elements.iter().find_map(|element| {
            element.redirection.as_ref().and_then(|redir| match redir {
                PipelineRedirection::Single { target, .. } => check_redirection_target(target, pos),
                PipelineRedirection::Separate { out, err } => check_redirection_target(out, pos)
                    .or_else(|| check_redirection_target(err, pos)),
            })
        })
    })
}