Skip to content

limit length of matching characters to length of line minus offset

Administrator requested to merge vo/search-results-highlight into master

Created by: vanesa

Issue https://github.com/sourcegraph/enterprise/issues/11960 revealed that using a quantifier combined with regex that takes any character and line breaks (e.g. test(.|\s)*) will lead to a wrong number of matched characters per line when using indexed search.

Example: searching for repo:sourcegraph test(.|\s)* shows these results with no highlighting: image

When using indexed search, the length (number of matching characters per line) with the above mentioned search actually returns the number of matching characters from the offset to the end of the file, leading to this condition in dom.tsx to return and cancel the highlighting.

    if (length > node.textContent!.length - start) {
        return
    }

length can be traced down to textsearch.go. If the search is conducted with index search, printing offset, length and l.Line, show that l.Line is in fact a combination off all lines separated by \n starting from the offset.

...,"lineMatches":[{"preview":"group :development, :test do\n  gem 'byebug'\n  gem 'annotate'\nend\n","lineNumber":31,"offsetAndLengths":[[21,44]]}]}],"alert":null,"elapsedMilliseconds":14}}}}

The same search using graphQL however returns the correct line and thus length.

...,"lineMatches":[{"preview":"group :development, :test do","lineNumber":31,"offsetAndLengths":[[21,7]]}]}],"alert":null,"elapsedMilliseconds":63}}}}

This current solution is a workaround, as the source of the problem most likely is located in zoekt, where MatchLength does not seem to be limited to matching text within a line when a search is performed with e.g. test(.|\s)*: https://sourcegraph.com/github.com/sourcegraph/zoekt/-/blob/api.go#L80-90 @keegancsmith would you want to investigate this?

The workaround splits the lines by \n and limits the length of m.MatchLength by len(l.Line)-m.LineOffset

This PR updates the CHANGELOG.md file to describe any user-facing changes.

Merge request reports

Loading