Sublime Forum

How to get self.view.sel() when in find panel?

#1

Hi - I need to write a command that can access the self.view.sel() object when a command is invoked from the Find Panel. Eventually the plugin will save the current selection, execute a search and do some things with those selected search results, and then it will restore the previously saved selection. I have everything working when I invoke the command hotkey from the Editor, but it breaks when I invoke the command hotkey from the Find Panel.

I found the problem is due to being unable to get the Editor self.view.sel() object when the hotkey command is invoked from the Find Panel. Here is what I have boiled it down to and I am hoping someone can help.

[code]{ “keys”: “f5”], “command”: “modify_selection_from_findpanel” },

import sublime, sublime_plugin
class ModifySelectionFromFindpanelCommand(sublime_plugin.TextCommand):
def run(self, edit):
print("SELF.VIEW IS: " + str(self.view))
print("SELF.VIEW.WINDOW().VIEWS() IS: " + str(self.view.window().views()))
print("SUBLIME.WINDOWS() IS: " + str(sublime.windows()))
print("SUBLIME.ACTIVE_WINDOW().VIEWS() IS: " + str(sublime.active_window().views()))
print("SUBLIME.ACTIVE_WINDOW().ACTIVE_VIEW() IS: " + str(sublime.active_window().active_view()))
self.view.sel().clear()[/code]

When I push F5 when focused inside the Editor - it works and the selection is removed. However when I push F5 when in the find panel - self.view is a different object. I have tried sublime.active_window().active_view() and also self.view.window().active_view() but neither is able to give me the view object of the main editor window through which I can access sel(). How can I access sel() from a hotkey command invoked from the find panel?

When I push F5 when in the Editor, the Console shows:
SELF.VIEW IS: <sublime.View object at 0x0143B6B0>
SELF.VIEW.WINDOW().VIEWS() IS: <sublime.View object at 0x0165A310>, <sublime.View object at 0x0165A8F0>]
SUBLIME.WINDOWS() IS: <sublime.Window object at 0x0165A930>]
SUBLIME.ACTIVE_WINDOW().VIEWS() IS: <sublime.View object at 0x0165A8F0>, <sublime.View object at 0x0165A3D0>]
SUBLIME.ACTIVE_WINDOW().ACTIVE_VIEW() IS: <sublime.View object at 0x0165A970>

When I push F5 when in the Find Panel, the Console shows:
SELF.VIEW IS: <sublime.View object at 0x0165ABF0>
SELF.VIEW.WINDOW().VIEWS() IS: <sublime.View object at 0x01669270>, <sublime.View object at 0x016692B0>]
SUBLIME.WINDOWS() IS: <sublime.Window object at 0x01669290>]
SUBLIME.ACTIVE_WINDOW().VIEWS() IS: <sublime.View object at 0x016692B0>, <sublime.View object at 0x01669230>]
SUBLIME.ACTIVE_WINDOW().ACTIVE_VIEW() IS: <sublime.View object at 0x016692D0>

I need to get <sublime.View object at 0x0143B6B0> when self.view=<sublime.View object at 0x0165ABF0> but I cannot find <sublime.View object at 0x0143B6B0> showing up in any accessible object???

0 Likes

#2

Not sure sublime.View return always the same address for the same view, try view.file_name().
What happened if you use a sublime_plugin.WindowCommand in place of sublime_plugin.TextCommand ?

0 Likes

#3

view.file_name() shows the correct name of the file when in the Editor and it shows “None” when in the Find Panel.

With plugin type sublime_plugin.WindowCommand - I am unable to get a working sel() item at all. Even when focused in the Editor, self.window.active_view().sel().clear() does not work. It shows the same Window whether launched from Editor or Find Panel - but I am unable to get the view I need.

sublime_plugin.WindowCommand Invoked from Editor:
STR(SELF.WINDOW IS: <sublime.Window object at 0x016339B0>
STR(SELF.WINDOW.ACTIVE_VIEW() IS: <sublime.View object at 0x01633A90>
STR(SELF.WINDOW.VIEWS() IS: <sublime.View object at 0x0163E250>, <sublime.View object at 0x0163E5D0>, <sublime.View object at 0x0163E030>]

sublime_plugin.WindowCommand Invoked from Find Panel
STR(SELF.WINDOW IS: <sublime.Window object at 0x016339B0>
STR(SELF.WINDOW.ACTIVE_VIEW() IS: <sublime.View object at 0x0163EC70>
STR(SELF.WINDOW.VIEWS() IS: <sublime.View object at 0x0163ECD0>, <sublime.View object at 0x0163E4F0>, <sublime.View object at 0x0163EDB0>]

So still unable to access sel() from the Find Panel. If anyone knows of any other methods that can be tried would be appreciated. I need to access the Editor view object from a global context.

0 Likes

#4

Work for me from both edit view and find panel:

(149, 163)] ExampleCommand (149, 163)] ExampleCommand

class ExampleCommand(sublime_plugin.WindowCommand): def run(self): print self.window.active_view().sel() print self.window.active_view().substr(self.window.active_view().sel()[0])
However the actual selection in the edit view is not refreshed immediately, need probably a begin_edit/end_Edit to work correctly.
But I don’t think modifying selections while the find panel is open is a good idea.
Is this related to your other post ? And if so, did you read my answer ?

0 Likes

#5

[quote=“bizoo”] I don’t think modifying selections while the find panel is open is a good idea.
Is this related to your other post ? And if so, did you read my answer ?[/quote]

Thanks to your more elegant way of doing it directly with find_all and add_regions(“bookmarks”), the script this was intended for is finished and working now.

0 Likes