sourcegraph / sourcegraph-public-snapshot

Code AI platform with Code Search & Cody
https://sourcegraph.com
Other
10.11k stars 1.29k forks source link

Add Ability To Filter Commented Code From Code Search #14811

Open tistru opened 4 years ago

tistru commented 4 years ago

Feature request description

Requested by customer: https://app.hubspot.com/contacts/2762526/company/693777200

Customer requested the ability to filter code comments out of search results. So for example, if searching for a function name 'foo', the results could be limited specifically to in code matches and not any 'foo' instances contained within comments.

Describe alternatives you've considered.

Currently, there are several workarounds to accommodate this that we communicated to them. For example, the user could perform a literal search for the 'foo(' string to get primarily function references (possible there can be a 'foo()' in comments as well). They could also use structural search to find only blocks with the 'foo {...}" designation. But we currently don't have a direct ability to filter out comments from results.

github-actions[bot] commented 4 years ago

Heads up @lguychard - the "team/search" label was applied to this issue.

rvantonder commented 4 years ago

@tistru structural search will not match content in comments. You don't need to match on blocks using structural search, you can still use a literal search like foo, and it won't match comments. Be sure the set the lang: filter. This should work for basically any language. If there are still matches in comments for your language/file that you don't expect, please give me a specific example and I'll make it work in structural search. For something like foo(, just complete the balanced parentheses like foo(...), it doesn't need to be a block. You do need to add that bit of effort for adding the closing ) for structural search, but it'll work. We're working on ways to autocomplete ) for convenience.

Because this issue exists, it seems like this is something we should advertise in the docs (or add a tab to remove comments, say), but we basically already support this functionality. Same with strings, structural search won't match in strings. Please CC me in any relevant support threads.

tistru commented 4 years ago

Thank you for the explanation @rvantonder . To make sure I understand you correctly, you mentioned that performing a structural search with a literal string should not match content in comments correct? I tried this briefly with the following query and found that it did return results in comments (scroll down a bit to see): https://sourcegraph.com/search?q=bitbucket+repo:github.com/sourcegraph/sourcegraph&patternType=structural

I think in general the answer may be to use structural search in which case we'd want to make it clear in the docs with some example queries for reference. But it seems like currently this is not the case based on my testing.

rvantonder commented 4 years ago

To make sure I understand you correctly, you mentioned that performing a structural search with a literal string should not match content in comments correct? I tried this briefly with the following query and found that it did return results in comments (scroll down a bit to see):

You do currently need to set the lang: filter (or specify like, file:\.go$) since comment syntax is different across languages. To have this work by default in absence of file/lang, I'd need to make some changes, but I think structural search is still the easy answer here.

oacnhpkqxjhufwumkkqnshvlmheokqv commented 2 years ago

Adding a search filter to exclude comments, or search only comments, is much more useful to the user than having to switch to structural search.

Sourcegraph is like the Splunk for code... in that after 2 years of using both, I still don't know how to use either of them, too many of these mental hoops to jump through each time I want to use it.

@mike-r-mclaughlin can we give this one another bump? Thanks!