Sublime Forum

Save find results and reopen them with syntax highlight

#1

Context:

Sometimes I search some text on big projects, resulting on many matches on multiple files. I scan the result page and proceed to remove false positives and trim the output to what is useful to me. I use this as a new starting point.

Idea:

It would we useful to save this new results to a file and be able to reopen it later with syntax highlight and the ability to click on found keywords to browse the original file.

.

I found a Find Results.tmLanguage file and manage to assign this syntax to *.search files, but it lacks the ability to browse those results, as clicking on them do nothing.

A workaround is to search for anything and then paste the contents of my saved search on the new “Find result” page, to enable the file navigation, but it looks like a hack and not like a feature…

0 Likes

#2

Run this in the console with your find results view active:

view.settings().set("result_file_regex", '^([^ \t].*):$'); view.settings().set("result_line_regex", '^ +([0-9]+):')
2 Likes

#3

Here’s an example of a standalone plugin that catches when you open a *.search file and automatically applies the correct syntax and result regexes in order to add clickable files. Use Tools > Developer > New Plugin... from the menu, replace the stub file with the following, then save it in the default location (your User package as a py file).

import sublime
import sublime_plugin


class SearchResultListener(sublime_plugin.EventListener):
    def on_load(self, view):
        if view.file_name().endswith(".search"):
            view.assign_syntax("Packages/Default/Find Results.hidden-tmLanguage")
            view.settings().set("result_file_regex", r'^([^ \t].*):$')
            view.settings().set("result_line_regex", r"^ +([0-9]+):")

            # In order for the regex options to take effect, another view needs
            # to be given the focus temporarily.
            view.window().new_file()
            view.window().run_command("close_file")
            view.window().focus_view(view)

This notices when a file with an extension of .search is opened, and then automatically applies the appropriate syntax and result regular expressions to enable the ability to navigate the search results again. With this you don’t need to have the setting you applied that makes the syntax apply via settings.

Note also that per the following forum post, in order for the result regex to take effect properly the view needs to be refocused; otherwise you get obscure messages in the console. As such the plugin also temporarily creates a new file, then closes it.

6 Likes