16

Every time I search for a function inside of hundreds of files, I see so many matches for comments which have no effect in the code.

Can someone limit Sublime Text's search scope to real code, and exclude comments?

I use Sublime Text 3 for developing a C++ program.

1
  • 1
    I am the person that has offered the +100 bounty. I just leave this comment just in case someone want to use a comment to ask me anything. You can also comment in my answer if you want to.
    – sergioFC
    Commented Mar 8, 2016 at 22:29

2 Answers 2

8
+100

I created a Plugin that search for a given string inside a given scope. The default scope selector is -comment effectively searching outside of comments. The text to search for is taken from the current selection. The results are presented in the drop-down menu

Basically I combined two API methods:

  • view.find_all(pattern) that searches for a pattern in the given view.
  • view.match_selector(position, scope_selecor) that check if the given position is inside the given scope.
12
  • Your solution meets all bounty requirements so you deserve the bounty. Good job.
    – sergioFC
    Commented Mar 12, 2016 at 18:03
  • Thanks, tell me if you need help tweaking it 😃
    – gwenzek
    Commented Mar 12, 2016 at 18:04
  • You're welcome. I was trying to modify the plugin to find new words while typing but I was unable to do it. This would be cool, but note that it is not really necessary, at least for me.
    – sergioFC
    Commented Mar 12, 2016 at 18:08
  • I'm sorry what are you trying to do? Would you like to type the searched text in a textbox instead of using the current selection?
    – gwenzek
    Commented Mar 12, 2016 at 18:15
  • 1
    @prosti copy paste it in your Packages/User folder. To go there use the 'Browse Package' menu in Sublime
    – gwenzek
    Commented Apr 23, 2016 at 16:42
7

You could use regex to find patters matching the regex you give. Design the regex according to match your.

You can give regex by turning on the 'Regular Expression' flag

By this way you can search without comments

Example

You can have this regex to match your case if you want to match alone in single line comments.

^(?!\/\/)([^\/\n]*)YOUR_SEARCH_TERM

If you want to match also in multi line comments use this.

^(?!(\/\/|(\/\*(.|\n)*([^\*])(?=\/))))YOUR_SEARCH_TERM
4
  • 1
    Comments have their own complexity. Could you please give a complete answer?
    – barej
    Commented Mar 8, 2016 at 14:06
  • Now have a look above. @barej Commented Mar 8, 2016 at 16:20
  • But it searched in comments too for me : image
    – barej
    Commented Mar 10, 2016 at 11:38
  • Multi comments not handled earlier. now check the answer above. @barej Commented Mar 10, 2016 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.