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

Code before:
    pub fn new() -> Self {
        Self::default()
    }
}

impl Completer for DotNuCompletion {
    fn fetch(
        &mut self,
        working_set: &StateWorkingSet,
        stack: &Stack,
        prefix: &[u8],
        span: Span,
        offset: usize,
        _pos: usize,
        options: &CompletionOptions,
    ) -> Vec<SemanticSuggestion> {
        let prefix_str = String::from_utf8_lossy(prefix);
        let start_with_backquote = prefix_str.starts_with('`');
        let end_with_backquote = prefix_str.ends_with('`');
        let prefix_str = prefix_str.replace('`', "");
        // e.g. `./`, `..\`
        let cwd_only = prefix_str.starts_with('.')
            && prefix_str
                .chars()
                .find(|c| *c != '.')
                .is_some_and(|c| c == SEP);
        let mut search_dirs: Vec<PathBuf> = vec![];

        let (base, partial) = if let Some((parent, remain)) = prefix_str.rsplit_once(is_separator) {
            // If prefix_str is only a word we want to search in the current dir.
            // "/xx" should be split to "/" and "xx".
            if parent.is_empty() {
                (MAIN_SEPARATOR_STR, remain)
            } else {
                (parent, remain)
            }
        } else {
            (".", prefix_str.as_str())
        };
        let base_dir = base.replace(is_separator, MAIN_SEPARATOR_STR);

        // Fetch the lib dirs
        // NOTE: `$env.NU_LIB_DIRS` will be overshadowed by a default const `NU_LIB_DIRS`
        // set `const NU_LIB_DIRS = [paths]` instead
        let lib_dirs: Vec<PathBuf> = working_set
            .find_variable(b"$NU_LIB_DIRS")
            .and_then(|vid| working_set.get_variable(vid).const_val.as_ref())
            .or(working_set.get_env_var("NU_LIB_DIRS"))
            .map(|lib_dirs| {
                lib_dirs
                    .as_list()

Review comment (suggestion):
@blindFS Will this only look for backslashes on Windows, rather than `./` too? If so, it might be better to use `is_separator`

Code after:
pub struct DotNuCompletion {}

impl DotNuCompletion {
    pub fn new() -> Self {
        Self::default()
    }
}

impl Completer for DotNuCompletion {
    fn fetch(
        &mut self,
        working_set: &StateWorkingSet,
        stack: &Stack,
        prefix: &[u8],
        span: Span,
        offset: usize,
        _pos: usize,
        options: &CompletionOptions,
    ) -> Vec<SemanticSuggestion> {
        let prefix_str = String::from_utf8_lossy(prefix);
        let start_with_backquote = prefix_str.starts_with('`');
        let end_with_backquote = prefix_str.ends_with('`');
        let prefix_str = prefix_str.replace('`', "");
        // e.g. `./`, `..\`, `/`
        let not_lib_dirs = prefix_str
            .chars()
            .find(|c| *c != '.')
            .is_some_and(is_separator);
        let mut search_dirs: Vec<PathBuf> = vec![];

        let (base, partial) = if let Some((parent, remain)) = prefix_str.rsplit_once(is_separator) {
            // If prefix_str is only a word we want to search in the current dir.
            // "/xx" should be split to "/" and "xx".
            if parent.is_empty() {
                (MAIN_SEPARATOR_STR, remain)
            } else {
                (parent, remain)
            }
        } else {
            (".", prefix_str.as_str())
        };
        let base_dir = base.replace(is_separator, MAIN_SEPARATOR_STR);

        // Fetch the lib dirs
        // NOTE: 2 ways to setup `NU_LIB_DIRS`
        // 1. `const NU_LIB_DIRS = [paths]`, equal to `nu -I paths`
        // 2. `$env.NU_LIB_DIRS = [paths]`
        let const_lib_dirs = working_set
            .find_variable(b"$NU_LIB_DIRS")
            .and_then(|vid| working_set.get_variable(vid).const_val.as_ref());
        let env_lib_dirs = working_set.get_env_var("NU_LIB_DIRS");