Home Download Buy Blog Forum Support

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

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

Postby robertcollier4 on Tue Feb 26, 2013 11:34 am

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: Select all
{ "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()


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???
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm

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

Postby bizoo on Tue Feb 26, 2013 11:46 am

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 ?
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

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

Postby robertcollier4 on Tue Feb 26, 2013 12:07 pm

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.
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm

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

Postby bizoo on Tue Feb 26, 2013 1:30 pm

Work for me from both edit view and find panel:
Code: Select all
[(149, 163)]
ExampleCommand
[(149, 163)]
ExampleCommand


Code: Select all
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 ?
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

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

Postby robertcollier4 on Tue Feb 26, 2013 2:45 pm

bizoo wrote: 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 ?

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.
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 10 guests