Sublime Forum

How to save Find in folder command panel settings

#1

Usually when refactoring python code I use a lot the “Find in folder…” side bar command.

There is a problem of usability though, each time you spawn a new find_in_folder command the last used settings will be resetted.

For instance, imagine you want to refactor a project with hundred of thousands of files and you just want to refactor the .py files, to avoid the file searching taking few minutes you speed it up by using the “Add include filter” and typing ".py", so far so good. Problem comes when you want to make few dozens of different replacements… each time you spawn “Find in folder…” you need to type the “Add include filter” over and over cos the command is forgetting the last settings, that’s really annoying for such a common feature. Not only that, sometimes you forget you haven’t typed this filter and the searching takes a while and after that you realize you’ve forgotten to include the filter to the search to be fast :confused:

At this point, I wanted to take a look of the FindInFolder command source code to see whether I could change the behaviour myself so I’ve used the nice and useful old package Edit preferences to see where this command lives:

https://dl.dropboxusercontent.com/s/ym5ysnqts3idmo8/sublime_text_2018-06-27_14-20-40.png

but when I try to open it I’ll get:

Traceback (most recent call last):
  File "list_commands in D:\sources\personal\sublimetext3\Data\Installed Packages\Edit Preferences.sublime-package", line 71, in on_select
  File "./python3.3/inspect.py", line 715, in getsourcelines
  File "./python3.3/inspect.py", line 563, in findsource
OSError: could not get source code

Anyway, before debugging that package or command I’ve decided to stop here and asking first so maybe someone knows already how to save the last settings when using the “Find in folder…” command.

Thanks in advance.

0 Likes

#2

By opening directly the Default.sublime-package side_bar.py we can see:

import os
import functools

import sublime
import sublime_plugin

...

class FindInFolderCommand(sublime_plugin.WindowCommand):
    def run(self, dirs):
        self.window.run_command("show_panel", {"panel": "find_in_files", "where": ",".join(dirs)})

    def is_visible(self, dirs):
        return len(dirs) > 0

So… does anyone know how to modify FindInFolderCommand to store/restore the last settings used?

0 Likes

#3

I believe the “Where” field’s history entries are stored in the active .sublime-session, though I don’t recall off the top of my head if there are easy API hooks to get at that. (It is just JSON, though.) You could create a new command which invokes the panel with the most recent “Where” history value, or you could create a set of default “includes/excludes” as a key in the project file or something, and append that to the ",".join(dirs) if you want to keep the ability to right-click on a particular folder in the sidebar.

1 Like

#4

It’s a very long-standing bug in Edit Preferences that nobody has fixed so far (including myself).

1 Like

#6

Thx to post the issue, I do use quite a lot this nice package so when I find some spare time maybe I’ll give it a shot, hopefully won’t be too tricky

0 Likes

#7

Ok, following your advice I think this will do:

class FindInFolderCommand(sublime_plugin.WindowCommand):
    def run(self, dirs):
        where = dirs
        dct = self.window.project_data().get("find_in_files", None)
        if dct:
            where += dct.get("where", [])

        self.window.run_command(
            "show_panel", {"panel": "find_in_files", "where": ",".join(where)})

    def is_visible(self, dirs):
        return len(dirs) > 0

and storing in the .sublime-project, ie:

...
"find_in_files": {
    "where": ["*.py", "<open files>"]
}
...

Thx

1 Like