Skip to content

search frontend: detect and highlight patterns in query

Administrator requested to merge rvt/client-pattern-highlighting into main

Created by: rvantonder

Patterns now highlight wherever they are used, based on patterntype, if set in the query string. Example Code Insights page:

Before:

Screen Shot 2022-07-07 at 3 23 41 PM

After:

Screen Shot 2022-07-07 at 3 22 01 PM

The query highlighter doesn't take a parameter to know how to highlight patterns (so I can't tell whether to highlight .* as regex or not). This PR adds an optional parameter so it can force highlighting on the patterntype if it's explicitly added.

But that's not enough, because everything that uses the query highlight component today doesn't set the patterntype (because it doesn't exist), so there's no way to just add highlighting, unless... well, if those queries happen to specify patterntype in the query string, then we can detect it.

So now:

  • we detect patterntype in the query string and tokenize based on that value if it exists. it means scanning every twice is unavoidable. this is the cost of making a decision to include a value inside a string that defines how that string itself should be interpreted. bad decision and the bane of patterntype

  • if we don't detect patterntype in the query string, and the query highlighter is not told how to scan/highlight the query, then it scans/highlights as literal. When https://github.com/sourcegraph/sourcegraph/pull/38141 is merged it will scan/highlight as standard.

Test plan

Added test.

App preview:

Check out the client app preview documentation to learn more.

Merge request reports

Loading