Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

search: highlight file path matches#41296

Merged
tbliu98 merged 11 commits into
mainfrom
tl/implement-path-match-ranges
Sep 6, 2022
Merged

search: highlight file path matches#41296
tbliu98 merged 11 commits into
mainfrom
tl/implement-path-match-ranges

Conversation

@tbliu98

@tbliu98 tbliu98 commented Sep 4, 2022

Copy link
Copy Markdown
Contributor

Part of #18374

This PR adds highlighting for path matches in the search UI.

Right now, paths will only be highlighted if the search result is explicitly a PathMatch. In other words, if content and/or symbol matches are available to be rendered, then the rendering and highlighting of those match types takes precedent over path matches.

For example, if a file contains both a content and a path match on the string foo, and the query does not contain select:file or type:path, then the rendered result will show the content match highlighted and the path will not be highlighted. The reason for this decision is mainly to minimize added visual noise, as certain queries could potentially produce a huge amount of highlighting on the results page if we always highlighted path matches for every result type. Note that for the content match in the screenshot below, the path is not highlighted despite containing a match:

Screen Shot 2022-09-04 at 6 00 54 PM

Looking for feedback and thoughts on what I just outlined above.

Implementation details:

  • At job creation, the search patterns are compiled into a regex or a list of regexes that is populated as a field on the corresponding job. These regexes respect the case sensitivity setting that is set in the search bar.
  • During conversion of a searcher FileMatch or a Zoekt FileMatch to a result.FileMatch, match the file name against the regex and append the submatches as result.Ranges to the resulting result.FileMatch.
  • If the result.FileMatch does not have content or symbol matches, then frontend will send an EventPathMatch to the client, which then highlights the matched ranges in the file path using the highlightNode() function.

Test plan

Added unit tests
Manual testing to feel good about the additions

During construction of zoekt.GlobalTextSearchJob and zoekt.RepoSubsetTextSearchJob, compile the query patterns being sent to zoekt to regexps. These regexps are used during match construction to compute path match ranges. These path match ranges are propagated back up to the client (web app) where they are then highlighted in the UI.
During construction of searcher.TextSearchJob, compile the text pattern into a regexp. When converting searcher matches to result.FileMatches, use that regexp to compute path match ranges, which are then propagated back up to the client (web app) where they are highlighted in the UI.
@cla-bot cla-bot Bot added the cla-signed label Sep 4, 2022
@tbliu98 tbliu98 changed the title Tl/implement path match ranges search: highlight path matches Sep 4, 2022
@tbliu98 tbliu98 changed the title search: highlight path matches search: highlight file path matches Sep 4, 2022
@tbliu98 tbliu98 marked this pull request as ready for review September 4, 2022 23:35
@tbliu98 tbliu98 requested review from a team, camdencheek and rvantonder September 4, 2022 23:35
@sourcegraph-bot

sourcegraph-bot commented Sep 4, 2022

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff b38f7e3...b026def.

Notify File(s)
@beyang internal/search/job/jobutil/job.go
internal/search/job/jobutil/job_test.go
internal/search/searcher/search.go
internal/search/zoekt/indexed_search.go
internal/search/zoekt/indexed_search_test.go
internal/search/zoekt/symbol_search.go
@camdencheek cmd/frontend/internal/search/search.go
internal/search/job/jobutil/job.go
internal/search/job/jobutil/job_test.go
internal/search/searcher/search.go
internal/search/zoekt/indexed_search.go
internal/search/zoekt/indexed_search_test.go
internal/search/zoekt/symbol_search.go
@fkling client/search-ui/src/components/FileSearchResult.tsx
client/search-ui/src/components/RepoFileLink.tsx
@keegancsmith cmd/frontend/internal/search/search.go
internal/search/job/jobutil/job.go
internal/search/job/jobutil/job_test.go
internal/search/searcher/search.go
internal/search/zoekt/indexed_search.go
internal/search/zoekt/indexed_search_test.go
internal/search/zoekt/symbol_search.go
@limitedmage client/search-ui/src/components/FileSearchResult.tsx
client/search-ui/src/components/RepoFileLink.tsx

@camdencheek camdencheek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direction looks great! Small comments inline.

Right now, paths will only be highlighted if the search result is explicitly a PathMatch.

I'm not sure how I feel about this. In my mind, this violates the principle of least surprise. It looks weird to me to have paths highlighted on some results but not others.

This is especially true because we don't emit path matches if there are any content matches in the file. For example, in your screenshot, we don't emit a path match for internal/database/access_tokens.go because there is a content match for that file. This means the results won't visually represent the fact that the path internal/database/access_tokens.go is a match for your query.

Comment thread internal/search/zoekt/indexed_search.go Outdated
Comment thread internal/search/zoekt/indexed_search.go Outdated
Comment thread internal/search/zoekt/indexed_search.go Outdated
Comment thread internal/search/zoekt/indexed_search.go Outdated
Comment thread internal/search/job/jobutil/job.go
Comment thread internal/search/job/jobutil/job.go Outdated
Comment thread internal/search/zoekt/indexed_search.go Outdated
@tbliu98

tbliu98 commented Sep 6, 2022

Copy link
Copy Markdown
Contributor Author

This means the results won't visually represent the fact that the path internal/database/access_tokens.go is a match for your query.

Thanks for the feedback on this @camdencheek, I wasn't sure which way to go here. Initially I was worried about adding too much new visual noise, but I also think it makes sense to be consistent in how we display results. I can see how the current inconsistency could be surprising to a user.

I can add PathMatches as a field to our other match types in a follow-up PR to keep the scope of this one limited to getting the behavior right.

@tbliu98 tbliu98 force-pushed the tl/implement-path-match-ranges branch from 719461a to 4ddf85a Compare September 6, 2022 16:36

@limitedmage limitedmage left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frontend code looks good to me. I think we should update the color used to highlight though, the contrast is way too low. I opened https://github.com/sourcegraph/sourcegraph/issues/41378 and assigned to @almeidapaulooliveira to pick the right color. I don't know if that should be a blocker for this feature, @benvenker and @almeidapaulooliveira can decide.

@camdencheek camdencheek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of small comments inline, but this is looking good 🥳

I can add PathMatches as a field to our other match types in a follow-up PR to keep the scope of this one limited to getting the behavior right.

Sounds great!

Comment thread internal/search/job/jobutil/job.go Outdated
Features: *searchInputs.Features,
}

if patternInfo.PatternMatchesPath {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this condition should be inside getPathRegexpsFromTextPatternInfo, right? Because, even if PatternMatchesPath == false, we still want to get the highlighted ranges from patternInfo.IncludePatterns, yes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to think about it for a minute but yes I think you're right

Comment thread internal/search/zoekt/indexed_search.go Outdated
Since func(time.Time) time.Duration `json:"-"` // since if non-nil will be used instead of time.Since. For tests
Repos *IndexedRepoRevs // the set of indexed repository revisions to search.
Query zoektquery.Q
ZoektQueryRegexps []*regexp.Regexp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's not immiediately clear from context, could we add a comment explaining what this is for? Same applies to other places we add this field to a struct.

I'm not very good about this, but since jobs are now one of our primary forms of abstraction, it would be nice if we could understand them better at a glance without needing to go find where the field is used or how it's populated.

@tbliu98 tbliu98 merged commit b1c0221 into main Sep 6, 2022
@tbliu98 tbliu98 deleted the tl/implement-path-match-ranges branch September 6, 2022 21:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants