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

Code before:
                                    suggestion.span.end.min(end + input_start - offset);
                            }
                            if let Some(start) = read_span_field(span_rec, "start") {
                                suggestion.span.start = start + input_start - offset;
                            }
                            suggestion.span.start = suggestion.span.start.min(suggestion.span.end);
                        }
                    }
                    _ => (),
                }
            });

            return Some(SemanticSuggestion {
                suggestion,
                kind: Some(SuggestionKind::Value(value_type)),
            });
        }

        None
    })
    .collect()
}

fn read_span_field(span: &SharedCow<Record>, field: &str) -> Option<usize> {
    let Some(val) = span.get(field) else {
        log::error!("Span has no {field} field");
        return None;
    };
    let Ok(int_val) = val.as_int() else {
        log::error!("Expected span field {field} to be int");
        return None;
    };
    let Ok(val) = usize::try_from(int_val) else {
        log::error!("Couldn't convert span {field} to usize");
        return None;
    };

    Some(val)
}

pub struct CustomCompletion<T: Completer> {
    decl_id: DeclId,
    line: String,
    line_pos: usize,
    fallback: T,
}

impl<T: Completer> CustomCompletion<T> {
    pub fn new(decl_id: DeclId, line: String, line_pos: usize, fallback: T) -> Self {
        Self {
            decl_id,

Review comment (suggestion):
So there's an error for `{ end: xxx }`? I thought this is a valid value by design.

Code after:
                            }
                            if let Some(start) = read_span_field(span_rec, "start") {
                                suggestion.span.start = start + input_start;
                            }
                            if suggestion.span.start > suggestion.span.end {
                                suggestion.span.start = suggestion.span.end;
                                log::error!(
                                    "Custom span start ({}) is greater than end ({})",
                                    suggestion.span.start,
                                    suggestion.span.end
                                );
                            }
                        }
                    }
                    _ => (),
                }
            });

            return Some(SemanticSuggestion {
                suggestion,
                kind: Some(SuggestionKind::Value(value_type)),
            });
        }

        None
    })
    .collect()
}

fn read_span_field(span: &SharedCow<Record>, field: &str) -> Option<usize> {
    let Ok(val) = span.get(field)?.as_int() else {
        log::error!("Expected span field {field} to be int");
        return None;
    };
    let Ok(val) = usize::try_from(val) else {
        log::error!("Couldn't convert span {field} to usize");
        return None;
    };

    Some(val)
}

pub struct CustomCompletion<T: Completer> {
    decl_id: DeclId,
    line: String,
    line_pos: usize,
    fallback: T,
}

impl<T: Completer> CustomCompletion<T> {
    pub fn new(decl_id: DeclId, line: String, line_pos: usize, fallback: T) -> Self {