Repository: nushell/nushell
File: crates/nu-protocol/src/engine/state_working_set.rs (Rust)

Code before:

            // check overlay in delta
            for overlay_frame in scope_frame.active_overlays(&mut removed_overlays).rev() {
                visibility.append(&overlay_frame.visibility);

                if self.search_predecls {
                    if let Some(decl_id) = overlay_frame.predecls.get(name) {
                        if visibility.is_decl_id_visible(decl_id) {
                            return Some(*decl_id);
                        }
                    }
                }

                if let Some(decl_id) = overlay_frame.get_decl(name) {
                    if visibility.is_decl_id_visible(&decl_id) {
                        return Some(decl_id);
                    }
                }
            }
        }

        // check overlay in perma
        for overlay_frame in self
            .permanent_state
            .active_overlays(&removed_overlays)
            .rev()
        {
            visibility.append(&overlay_frame.visibility);

            if let Some(decl_id) = overlay_frame.get_decl(name) {
                if visibility.is_decl_id_visible(&decl_id) {
                    return Some(decl_id);
                }
            }
        }

        None
    }

    pub fn find_module(&self, name: &[u8]) -> Option<ModuleId> {
        let mut removed_overlays = vec![];

        for scope_frame in self.delta.scope.iter().rev() {
            for overlay_frame in scope_frame.active_overlays(&mut removed_overlays).rev() {
                if let Some(module_id) = overlay_frame.modules.get(name) {
                    return Some(*module_id);
                }
            }
        }

        for overlay_frame in self

Review comment (suggestion):
As this whole `find_decl` stuff is a bit confusing and stretched over the different state parts, maybe worth a doccomment what the particular methods are useful for.

Code after:

            // check overlay in delta
            for overlay_frame in scope_frame.active_overlays(&mut removed_overlays).rev() {
                visibility.append(&overlay_frame.visibility);

                if self.search_predecls {
                    if let Some(decl_id) = overlay_frame.predecls.get(name) {
                        if visibility.is_decl_id_visible(decl_id) {
                            return Some(*decl_id);
                        }
                    }
                }

                if let Some(decl_id) = overlay_frame.get_decl(name) {
                    if visibility.is_decl_id_visible(&decl_id) {
                        return Some(decl_id);
                    }
                }
            }
        }

        // check overlay in perma
        self.permanent_state.find_decl(name, &removed_overlays)
    }

    pub fn find_decl_name(&self, decl_id: DeclId) -> Option<&[u8]> {
        let mut removed_overlays = vec![];

        let mut visibility: Visibility = Visibility::new();

        for scope_frame in self.delta.scope.iter().rev() {
            if self.search_predecls {
                for (name, id) in scope_frame.predecls.iter() {
                    if id == &decl_id {
                        return Some(name);
                    }
                }
            }

            // check overlay in delta
            for overlay_frame in scope_frame.active_overlays(&mut removed_overlays).rev() {
                visibility.append(&overlay_frame.visibility);

                if self.search_predecls {
                    for (name, id) in overlay_frame.predecls.iter() {
                        if id == &decl_id {
                            return Some(name);
                        }
                    }
                }