I’ve written a .py
file that does an inverse search through the currently open files for the string entered.
I would like some pointers on how to make this work for the files in the current project.
Here’s the code:
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)