Sublime Forum

How to enter text and set cursor to the first occurrence for all open files

#1

See the rough code of what I have atm. It currently searches the current doc and sets the cursor to the correct position.

How can I modify this to:

  1. Ask for input for the search string.
  2. Set the position of the cursor on all currently open files.
import sublime, sublime_plugin

class AllOpenFilesScrollToFoundText(sublime_plugin.TextCommand):
    def run(self, edit):

        # 1. I would like to loop over the open files which I cannot get to work.
        # for view in self.window.views():

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

          # 2. Here I would like a prompt to enter the text to find.
          # Using something like this which I cannot get to work.
          # searchStr = self.window.show_input_panel("Enter text to scroll to on all open windows:", '', None, None, None)
          searchStr = contents.find('TODO')

          cursors = view.sel()
          cursors.clear()
          location = sublime.Region(searchStr, searchStr)
          cursors.add(location)
          view.show_at_center(location)

0 Likes

#2

Your Window.show_input_panel's on_done callback is None. So I am not sure what are you expecting to happen ? You need to define a callback to which the entered text will be passed and then perform whatever you want to do with the entered text in that callback. Take a look at the method docs here

https://www.sublimetext.com/docs/api_reference.html#sublime.Window:ver-dev

2 Likes

#3

Thanks, this is probably not the prettiest of python as Iā€™m only starting to learn it.

Edit. Updated the code to be more verbose and a few other things mentioned in the header.

import sublime, sublime_plugin

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Written by StephenS. Forum thread: https://forum.sublimetext.com/t/how-to-enter-text-and-set-cursor-to-the-first-occurrence-for-all-open-files/56948
#
# INTIAL RELEASE:   2021.04.07
# REV 1:            2021.04.17
#                   Added the ability to select the found string and set_viewport_postion to it.
#                   Added putting the paths of the files where the string was found to your clipboard.
# REV 1.1:          2021.04.23
#                   Added the ability to select the text to find first which will be then populated to the 'Enter text to scroll' box
#                   Fixed the scrolling to the selected text with .show(highlightLocation).
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

class AllOpenFilesScrollToAndSelectFoundText(sublime_plugin.TextCommand):

  def run(self, edit):

    sel = self.view.sel()[0]
    selected = self.view.substr(sel)
    findStr = self.view.window().show_input_panel("Enter text to scroll to and select on all open tabs:", selected, self.on_done, None, None)

  def on_done(self, findStrVar):

    fileCnt = 0
    fileList = ""
    fileListPopupMsg = ""
    fileMsg1 = ""
    fileMsgCnt = ""

    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
      begin = contents.find(findStrVar)

      if begin != -1:

        # view.window().show_input_panel(str(begin), '', None, None, None) # Testing line.

        fileCnt += 1
        fileList += view.file_name() + "\r\n"
        fileListPopupMsg += view.file_name() + "<br>"

        end = begin + len(findStrVar)
        cursors = view.sel()
        cursors.clear()
        highlightLocation = sublime.Region(begin, end)
        cursors.add(highlightLocation)
        view.show(highlightLocation) # show - Scroll the view to show the given location, which may be a point, Region or Selection.
        view.show_at_center(highlightLocation)

    if fileCnt == 0: fileMsgCnt = "files" ; fileMsg1 = "have been"
    if fileCnt == 1: fileMsgCnt = "file" ; fileMsg1 = "has been" ; sublime.set_clipboard(fileList)
    if fileCnt > 1: fileMsgCnt = "files" ; fileMsg1 = "have been" ; sublime.set_clipboard(fileList)

    # 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 contain your text! Scrolling has been done to each.</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)
0 Likes