Sublime Forum

How can I create a view similar to find in files?

#1

Hi,

find in files creates a new view with the results from the search. When I doule-click on a search result, ST opens the file at that location. How can I implement something like that in a plugin? Below is an example using the output panel. It create the panel and inserts the string “click me!”. How can I manage that double clicking on the text opens a different file on a specific location?

Thanks!

window = sublime.active_window()
s = "click me!"
output = window.create_output_panel("test")
output.run_command('erase_view')
output.run_command('append', {'characters': s})
window.run_command("show_panel", {"panel": "output. test"})
0 Likes

Build 3080, 'next_result' doesn't work
#2

I don’t know how the find panel results buffer operates in that regard, but you could maybe hook into the double click behavior by abusing on_text_command and looking for the command that gets run when you double-click, which looks like so:

command: drag_select {"event": {"button": 1, "x": 467.5, "y": 412.5}} command: drag_select {"by": "words", "event": {"button": 1, "x": 467.5, "y": 412.5}}

first line is the first click, second line is the second click. You should be able to accurately filter the second command if it happens within a short time after the first command, has by: "words" set and similar x-y coordinates (maybe off by ±10).
But first you need to set some specific setting to your output panel that you can check for existance/value with the view the drag_select command is run on so that it doesn’t trigger on any view.

Hope this helps.

Note: It is required that the default mousemap is not changed for this, or at least it’s not guaranteed to work if it was changed.

0 Likes

#3

Thanks! I really appreciate that you are taking the time to answer many of the questions about “Plugin Development”!

0 Likes

#4

Not exactly what you want, but you can use the internal parser from ST used by build and search output with something like this:

s = "myfilename:10:12" file_regex = "(.+?):(\d):(\d)" output.settings().set("result_file_regex", file_regex)
You can double-click a result or use (SHIFT+)F4 to open the file.

The regexp for the search output is:

[code]>>> view.settings().get(“result_file_regex”)
‘^([A-Za-z\\/<].*):$’

view.settings().get(“result_line_regex”)
‘^ +([0-9]+):’[/code]

0 Likes

#5

Oh my god, so that’s how it’s done!

That’s … kind of brilliant actually. I didn’t know the result regexes worked for normal views as well. I thought it was an output panel-only thing.

0 Likes

#6

Not brillant, is stupid, the complete thing is hidden somewhere with a bunch of hidden regex. >.<
So to remove that annoying “double click to select a word opens a file” you can add something like this:

class AllowDoubleClickWordsInFindResultsEventListener(sublime_plugin.EventListener):
	def on_activated(self, view):
		if view.name() == 'Find Results':
			view.settings().set("result_file_regex", '')
			view.settings().set("result_line_regex", '')

Thanks >.<

0 Likes

#7

if you erase result_file_regex and result_line_regex “next_result” stops working >.<

0 Likes

#8

github.com/SublimeTextIssues/Core/issues/433

0 Likes