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

Code before:
                    "description" => {
                        // Convert the value to string
                        if let Ok(desc_str) = value.coerce_string() {
                            // Update the suggestion value
                            suggestion.description = Some(desc_str);
                        }
                    }
                    "style" => {
                        // Convert the value to string
                        suggestion.style = match value {
                            Value::String { val, .. } => Some(lookup_ansi_color_style(val)),
                            Value::Record { .. } => Some(color_record_to_nustyle(value)),
                            _ => None,
                        };
                    }
                    "span" => {
                        if let Value::Record { val: span, .. } = value {
                            let start = span
                                .get("start")
                                .and_then(|val| val.as_int().ok())
                                .and_then(|x| usize::try_from(x).ok());
                            let end = span
                                .get("end")
                                .and_then(|val| val.as_int().ok())
                                .and_then(|x| usize::try_from(x).ok());
                            // TODO: error on invalid spans?
                            match (start, end) {
                                (Some(start), Some(end)) => {
                                    suggestion.span = reedline::Span {
                                        start: start.min(end),
                                        end,
                                    };
                                }
                                (Some(start), None) => {
                                    suggestion.span.start = start.min(suggestion.span.end);
                                }
                                (None, Some(end)) => {
                                    suggestion.span.end = end.max(suggestion.span.start);
                                }
                                _ => (),
                            }
                            if let (Some(start), Some(end)) = (start, end) {
                                suggestion.span = reedline::Span {
                                    start: start.min(end),
                                    end,
                                };
                            }
                        }
                    }
                    _ => (),
                }

Review comment (suggestion):
We really should, but right now we can just log an error or something. I really should've done that in my PR. Not a blocker for this PR though

Code after:
                    "description" => {
                        // Convert the value to string
                        if let Ok(desc_str) = value.coerce_string() {
                            // Update the suggestion value
                            suggestion.description = Some(desc_str);
                        }
                    }
                    "style" => {
                        // Convert the value to string
                        suggestion.style = match value {
                            Value::String { val, .. } => Some(lookup_ansi_color_style(val)),
                            Value::Record { .. } => Some(color_record_to_nustyle(value)),
                            _ => None,
                        };
                    }
                    "span" => {
                        if let Value::Record { val: span, .. } = value {
                            let start = span
                                .get("start")
                                .and_then(|val| val.as_int().ok())
                                .and_then(|x| usize::try_from(x).ok());
                            let end = span
                                .get("end")
                                .and_then(|val| val.as_int().ok())
                                .and_then(|x| usize::try_from(x).ok());
                            // TODO: error on invalid spans?
                            match (start, end) {
                                (Some(start), Some(end)) => {
                                    suggestion.span = reedline::Span {
                                        start: start.min(end),
                                        end,
                                    };
                                }
                                (Some(start), None) => {
                                    suggestion.span.start = start.min(suggestion.span.end);
                                }
                                (None, Some(end)) => {
                                    suggestion.span.end = end.max(suggestion.span.start);
                                }
                                _ => (),
                            }
                        }
                    }
                    _ => (),
                }
            });

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