Sublime Forum

Searching through project for not found text (Opposite of find)

#1

I’m trying to search through my project for files that don’t contain a certain function.

Let’s say I want to find files that don’t contain the search (multiline text must work).

Is this possible or is there any plugins that do this?

0 Likes

#2
0 Likes

#3

Thanks.

Not so easy when I’m searching for a multiline string. In my case, there’s around 50.

0 Likes

#4

maybe you can share a concrete example? there’s nothing in that regex which restricts it to a single line string

0 Likes

#5

it’s regex, there’s no way of escaping 50 lines of code. :rofl:

Any code example you have. It can be anything.

YOUR_WORDS is a simple example that doesn’t need escaping. No point in even trying as it’s not feasible.

I will look to see if I can knock up a .py script. I’ve only just wet my toes in the language but I’m sure it’s doable.

0 Likes

#6

maybe just do a literal search then for your 50 lines of code, followed by a search for just \A, and take the results from the second search where there’s no match in the first one. But if you have 50 lines of code the same in many files, maybe it’s high time for a refactor.

0 Likes

#7

Maybe but using this will make life so much easier, I will be using this to search for files that also don’t contain the correct header info, licence info etc (It’s not just the code that I will be searching for).

I’ve yet to get it to work by searching the documents in the project without opening them first but this is a start.

For anyone wanting this here’s the all_open_files_inverse_find_text.py which should be placed in the user plugins folder.

import sublime, sublime_plugin

# What this does.
# Inverse search open files for text not found.

# How to use:
# 1. Open the files you want to inverse search.
# 2. Run the program and paste the text you want to inverse search for.
# 3. The program will then copy the file paths to your clipboard and display a
#    pop-up showing the list of files and with a file count too.

# TODO:
# Search through the existing project files instead without having to open them of course.

class AllOpenFilesInverseFindText(sublime_plugin.TextCommand):

  def run(self, edit):
    findStr = self.view.window().show_input_panel("Paste/enter text you want to inverse search for on all open tabs (Case sensitive):", '', self.on_done, None, None)

    htmlStrHelpMsg = """
    <style>
    body { margin: 10px }
    html { background-color: grey; border: 2px solid black; font-family: Arial, Helvetica, sans-serif }
    </style>
    <h3>Paste/enter text you want to inverse search for on all open tabs in the box at the bottom:</h3>
    HELP:<br>
    - If you\'ve multiple lines, it\'s best to copy and paste your string.<br>
    - Control + Enter to add a new line.<br>
    - Search is case sensitive.
    """

    self.view.show_popup(htmlStrHelpMsg, max_width=750, max_height=400)

  def on_done(self, findStrVar):

    fileList = ""
    fileListPopupMsg = ""
    fileCnt = 0

    for view in self.view.window().views():

      contents = view.substr(sublime.Region(0, view.size()))  # https://stackoverflow.com/questions/20182008/sublime-text-3-api-get-all-text-from-a-file
      isFound = contents.find(findStrVar)

      if view and view.file_name() and isFound == -1: # isFound will return -1 if string is not found.
        fileList += view.file_name() + "\r\n"
        fileListPopupMsg += view.file_name() + "<br>"
        fileCnt += 1

    sublime.set_clipboard(fileList)

    if fileCnt == 0: fileMsgCnt = "files" ; fileMsg1 = "have been"
    if fileCnt == 1: fileMsgCnt = "file" ; fileMsg1 = "has been"
    if fileCnt > 1: fileMsgCnt = "files" ; fileMsg1 = "have been"

    # Help on .show_popup: https://forum.sublimetext.com/t/dev-build-3070/14538/14
    htmlStrResult = """
    <style>
    body { margin: 10px }
    html { background-color: grey; border: 2px solid black; font-family: Arial, Helvetica, sans-serif }
    </style>
    <h2>Found """ + str(fileCnt) + """ """ + fileMsgCnt + """ that don\'t contain your text!</h2>
    <h3>(""" + str(fileCnt) + """ """ + fileMsgCnt + """ paths """ + fileMsg1 + """ also copied to your clipboard)</h3><br>
    \r\n\r\n""" + fileListPopupMsg

    self.view.show_popup(htmlStrResult, max_width=1000, max_height=400)

I’ve added it to the users file Tab Context.sublime-menu like so:

[
  { "caption": "Inverse Find Text\t- [ All Existing Files ]" , "command": "all_open_files_inverse_find_text", "mnemonic": "R" }
]
0 Likes