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

Code before:
                    output.push((
                        *decl_id,
                        name.clone(),
                        Some(command.description().to_string()),
                        command.command_type(),
                    ));
                }
            }
        }

        output
    }

    pub fn get_span_contents(&self, span: Span) -> &[u8] {
        for file in &self.files {
            if file.covered_span.contains_span(span) {
                return &file.content
                    [(span.start - file.covered_span.start)..(span.end - file.covered_span.start)];
            }
        }
        &[0u8; 0]
    }

    // If the span's content starts with the given prefix, return two subspans
    // corresponding to this prefix, and the rest of the content.
    pub fn span_match_prefix(&self, span: Span, prefix: &[u8]) -> Option<(Span, Span)> {
        let contents = self.get_span_contents(span);

        if contents.starts_with(prefix) {
            span.split_at(prefix.len())
        } else {
            None
        }
    }

    // If the span's content ends with the given postfix, return two subspans
    // corresponding to the rest of the content, and this postfix.
    pub fn span_match_postfix(&self, span: Span, prefix: &[u8]) -> Option<(Span, Span)> {
        let contents = self.get_span_contents(span);

        if contents.ends_with(prefix) {
            span.split_at(span.len() - prefix.len())
        } else {
            None
        }
    }

    /// Get the global config from the engine state.
    ///
    /// Use [`Stack::get_config()`] instead whenever the `Stack` is available, as it takes into
    /// account local changes to `$env.config`.

Review comment (suggestion):
Nit
```suggestion
    /// If the span's content starts with the given prefix, return two subspans
    /// corresponding to this prefix, and the rest of the content.
    pub fn span_match_prefix(&self, span: Span, prefix: &[u8]) -> Option<(Span, Span)> {
```

Code after:
                    output.push((
                        *decl_id,
                        name.clone(),
                        Some(command.description().to_string()),
                        command.command_type(),
                    ));
                }
            }
        }

        output
    }

    pub fn get_span_contents(&self, span: Span) -> &[u8] {
        for file in &self.files {
            if file.covered_span.contains_span(span) {
                return &file.content
                    [(span.start - file.covered_span.start)..(span.end - file.covered_span.start)];
            }
        }
        &[0u8; 0]
    }

    /// If the span's content starts with the given prefix, return two subspans
    /// corresponding to this prefix, and the rest of the content.
    pub fn span_match_prefix(&self, span: Span, prefix: &[u8]) -> Option<(Span, Span)> {
        let contents = self.get_span_contents(span);

        if contents.starts_with(prefix) {
            span.split_at(prefix.len())
        } else {
            None
        }
    }

    /// If the span's content ends with the given postfix, return two subspans
    /// corresponding to the rest of the content, and this postfix.
    pub fn span_match_postfix(&self, span: Span, prefix: &[u8]) -> Option<(Span, Span)> {
        let contents = self.get_span_contents(span);

        if contents.ends_with(prefix) {
            span.split_at(span.len() - prefix.len())
        } else {
            None
        }
    }

    /// Get the global config from the engine state.
    ///
    /// Use [`Stack::get_config()`] instead whenever the `Stack` is available, as it takes into
    /// account local changes to `$env.config`.