Commit b5c52237 authored by manuk's avatar manuk Committed by Commit Bot

[Omnibox] Move HistoryProvider::SpansFromTermMatch to promote reuse.

This is the 3rd refactoring CL aimed at reducing duplication and
inconsistency for classifying omnibox results. This CL

1) Creates a autocomplete_match_classification file to group static
classification methods. This organizes related code adjacently, promotes
code reuse instead of duplicated code per provider, and avoids providers
calling into other providers' static classification methods.

2) Renames HistoryProvider::SpansFromTermMatch to ClassifyTermMatches
and moves it to autocomplete_match_classification. It is responsible for
creating ACMatchClassifications from TermMatches.

3) Previously, SpansFromTermMatches accepted the bool param is_url which
determined how to style matches and non-matches. To make the method
reusable by the search provider, which uses reversed styling compared
to other providers for non-URL text, ClassifyTermMatches is overloaded
with a more generic signature that parameterizes the match and non-match
styles.

Bug: 366623
Change-Id: I60dfb82305f6e81fa00ff86662e63d2f9e24bb99
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1546783
Commit-Queue: manuk hovanesian <manukh@chromium.org>
Reviewed-by: default avatarTommy Li <tommycli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#646455}
parent 43bd746d
......@@ -83,6 +83,8 @@ jumbo_static_library("browser") {
"autocomplete_input.h",
"autocomplete_match.cc",
"autocomplete_match.h",
"autocomplete_match_classification.cc",
"autocomplete_match_classification.h",
"autocomplete_match_type.cc",
"autocomplete_match_type.h",
"autocomplete_provider.cc",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "autocomplete_match_classification.h"
#include "base/i18n/case_conversion.h"
#include "base/strings/utf_string_conversions.h"
#include "components/omnibox/browser/scored_history_match.h"
#include "in_memory_url_index_types.h"
ACMatchClassifications ClassifyTermMatches(TermMatches matches,
size_t text_length,
int match_style,
int non_match_style) {
ACMatchClassifications classes;
if (matches.empty()) {
if (text_length)
classes.push_back(ACMatchClassification(0, non_match_style));
return classes;
}
if (matches[0].offset)
classes.push_back(ACMatchClassification(0, non_match_style));
size_t match_count = matches.size();
for (size_t i = 0; i < match_count;) {
size_t offset = matches[i].offset;
classes.push_back(ACMatchClassification(offset, match_style));
// Skip all adjacent matches.
do {
offset += matches[i].length;
++i;
} while ((i < match_count) && (offset == matches[i].offset));
if (offset < text_length)
classes.push_back(ACMatchClassification(offset, non_match_style));
}
return classes;
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_MATCH_CLASSIFICATION_H_
#define COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_MATCH_CLASSIFICATION_H_
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/in_memory_url_index_types.h"
// Return an ACMatchClassifications structure given the |matches| to highlight.
// |matches| can be retrieved from calling FindTermMatches. |text_length| should
// be the full length (not the length of the truncated text clean returns) of
// the text being classified. It is used to ensure the the trailing
// classification is correct; i.e. if matches end at 20, and text_length is
// greater than 20, ClassifyTermMatches will add a non_match_style
// classification with offset 20. |match_style| and |non_match_style| specify
// the classifications to use for matched and non-matched text.
ACMatchClassifications ClassifyTermMatches(TermMatches matches,
size_t text_length,
int match_style,
int non_match_style);
#endif // COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_MATCH_CLASSIFICATION_H_
......@@ -11,6 +11,7 @@
#include "components/history/core/browser/history_service.h"
#include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_match_classification.h"
#include "components/omnibox/browser/in_memory_url_index_types.h"
using bookmarks::BookmarkModel;
......@@ -44,31 +45,11 @@ ACMatchClassifications HistoryProvider::SpansFromTermMatch(
const TermMatches& matches,
size_t text_length,
bool is_url) {
ACMatchClassification::Style url_style =
ACMatchClassification::Style non_match_style =
is_url ? ACMatchClassification::URL : ACMatchClassification::NONE;
ACMatchClassifications spans;
if (matches.empty()) {
if (text_length)
spans.push_back(ACMatchClassification(0, url_style));
return spans;
}
if (matches[0].offset)
spans.push_back(ACMatchClassification(0, url_style));
size_t match_count = matches.size();
for (size_t i = 0; i < match_count;) {
size_t offset = matches[i].offset;
spans.push_back(ACMatchClassification(
offset, ACMatchClassification::MATCH | url_style));
// Skip all adjacent matches.
do {
offset += matches[i].length;
++i;
} while ((i < match_count) && (offset == matches[i].offset));
if (offset < text_length)
spans.push_back(ACMatchClassification(offset, url_style));
}
return spans;
return ClassifyTermMatches(matches, text_length,
ACMatchClassification::MATCH | non_match_style,
non_match_style);
}
HistoryProvider::HistoryProvider(AutocompleteProvider::Type type,
......
......@@ -29,6 +29,8 @@ class HistoryProvider : public AutocompleteProvider {
// Fill and return an ACMatchClassifications structure given the |matches|
// to highlight.
// TODO (manukh) replace calls to SpansFromTermMatch with calls to
// ClassifyTermMatches (autocomplete_match_classification.h)
static ACMatchClassifications SpansFromTermMatch(const TermMatches& matches,
size_t text_length,
bool is_url);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment