Repository: meilisearch/meilisearch
File: meilisearch-lib/src/index/search.rs (Rust)

Code before:

                while let Some((word, token)) =
                    tokens.next_if(|(_, token)| matcher.matches(token).is_none())
                {
                    buffer.push((word, token));
                }

                match tokens.next() {
                    Some(token) => {
                        let mut total_count: usize = buffer
                            .iter()
                            .filter(|(_, token)| token.is_separator().is_none())
                            .count();

                        let crop_len_before = crop_len / 2;
                        // check if start will be cropped.
                        crop_marker_before = total_count > crop_len_before;

                        let before_iter = buffer.into_iter().skip_while(move |(_, token)| {
                            if token.is_separator().is_none() {
                                total_count -= 1;
                            }
                            total_count >= crop_len_before
                        });

                        // rebalance remaning word count after the match.
                        let crop_len_after = if crop_marker_before {
                            crop_len.saturating_sub(crop_len_before + 1)
                        } else {
                            crop_len.saturating_sub(total_count + 1)
                        };

                        let mut taken_after = 0;
                        let after_iter = tokens.take_while(move |(_, token)| {
                            let take = taken_after < crop_len_after;
                            if token.is_separator().is_none() {
                                taken_after += 1;
                            }
                            take
                        });

                        let iter = before_iter.chain(Some(token)).chain(after_iter);

                        Box::new(iter)
                    }
                    // If no word matches in the attribute
                    None => {
                        let mut count = 0;
                        let mut tokens = buffer.into_iter();
                        let mut out: String = tokens
                            .by_ref()

Review comment (suggestion):
```suggestion
                        // rebalance remaining word count after the match.
```

Code after:
                            .count();

                        let crop_len_before = crop_len / 2;
                        // check if start will be cropped.
                        crop_marker_before = total_count > crop_len_before;

                        let before_iter = buffer.into_iter().skip_while(move |(_, token)| {
                            if token.is_separator().is_none() {
                                total_count -= 1;
                            }
                            total_count >= crop_len_before
                        });

                        // rebalance remaining word count after the match.
                        let crop_len_after = if crop_marker_before {
                            crop_len.saturating_sub(crop_len_before + 1)
                        } else {
                            crop_len.saturating_sub(total_count + 1)
                        };

                        let mut taken_after = 0;
                        let after_iter = tokens.take_while(move |(_, token)| {
                            let take = taken_after < crop_len_after;
                            if token.is_separator().is_none() {
                                taken_after += 1;
                            }
                            take
                        });

                        let iter = before_iter.chain(Some(token)).chain(after_iter);

                        Box::new(iter)
                    }
                    // If no word matches in the attribute
                    None => {
                        let mut count = 0;
                        let mut tokens = buffer.into_iter();
                        let mut out: String = tokens
                            .by_ref()
                            .take_while(move |(_, token)| {
                                let take = count < crop_len;
                                if token.is_separator().is_none() {
                                    count += 1;
                                }
                                take
                            })
                            .map(|(word, _)| word)
                            .collect();

                        // if there are remaining tokens after formatted interval,
                        // put a crop marker at the end.