Home Download Buy Blog Forum Support

How to get the specific view's on modified event

How to get the specific view's on modified event

Postby aponxi on Sun Dec 23, 2012 4:26 pm

How can I bind on modified event to a specific view?

On my desperate last attempt what I came up is this:

Basically I want to get the input_view and output_view (output) into a variable and when input_view changes I want to update the output view. However the event listener - or the updates to the output view - should only be active when the plug in is toggled on.

The following code prints "modified" whenever ANY view is modified and not just the one I want... How can I make this work?

I appreciate the help!

Code: Select all
class ToggleWatch(TextCommand):
   

   def is_enabled(self):
      return isCoffee(self.view)

   def run(self, edit):
      watch_mode = True
      print "watch_mode " , watch_mode
      Modified.inputview = self.view
      Modified.inputself = self
      Modified.output = self.view.window().new_file()
      output = Modified.output
      output.set_scratch(True)


      output.set_syntax_file('Packages/JavaScript/JavaScript.tmLanguage')
      no_wrapper = settings.get('noWrapper', True)

      args = ['-p']
      if no_wrapper:
         args = ['-b'] + args

      print "watch_mode yes"
      #while True :
      #   time.sleep(1)
      #   print "refreshed"
      res = brew(args, Text.get(self.view))
      if res["okay"] is True:
         output.insert(edit, 0, res["out"])
      else:
         output.insert(edit, 0, res["err"].split("\n")[0])

class Modified(sublime_plugin.EventListener):
    def __init__(self):
        pass

    def on_modified(inputself, inputview ):
        print "modified"
aponxi
 
Posts: 7
Joined: Sat Dec 22, 2012 11:53 pm

Re: How to get the specific view's on modified event

Postby agibsonsw on Sun Dec 23, 2012 8:18 pm

Hello. Firstly, we cannot pass arguments to on_modified (AFAIK); after all, how would we supply these?
We cannot bind on_modified to a particular view. Instead, we can store details about which particular view is being edited.

In the following code I use the function isView to confirm that the modified event is taking place in a view, rather than some panel.
I use a dictionary (edit_info) in the event listener. When a view is edited for the first time I create a key based on the views' id. I can then
store whatever information I need (for this particular view) in the dictionary.

You could perhaps store the view-object itself (or regions) in the dictionary, so that you might more easily update the buffer and
activate a particular view. I didn't need this for the code below and, if I recall correctly, I had some problems with this previously.
Specifically, the view object from a Text or WindowCommand is not the same as that discovered in the event listener - which is why
I chose to use their id.

Be aware, of course, that modifying your output view will (I believe?) trigger another on_modified event: perhaps your use of a scratch
buffer circumvents this? Anyway, I hope that the following code may prove of some use.

Code: Select all
def isView(view_id):
    # are they modifying a view (rather than a panel, etc.)
    if not view_id: return False
    window = sublime.active_window()
    view = window.active_view() if window != None else None
    return (view is not None and view.id() == view_id)


class CaptureEditing(sublime_plugin.EventListener):
    edit_info = {}
    def on_modified(self, view):
        vid = view.id()
        if not isView(vid):
            # I only want to use views, not
            # the input-panel, etc..
            return
        if not CaptureEditing.edit_info.has_key(vid):
            # create a dictionary entry based on the
            # current views' id
            CaptureEditing.edit_info[vid] = {}
        cview = CaptureEditing.edit_info[vid]
        # I can now store details of the current edit
        # in the edit_info dictionary, via cview.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: How to get the specific view's on modified event

Postby agibsonsw on Sun Dec 23, 2012 8:35 pm

Also AFAIK we cannot turn an event listener on and off. You could use a global boolean which is initially false, and your TextCommand could set this to True. The event listener would immediately return if this value is false.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: How to get the specific view's on modified event

Postby aponxi on Sun Dec 23, 2012 9:17 pm

Thank you for your help, I deeply appreciate it! I will try to implement that on my code.. Will post the results :)
aponxi
 
Posts: 7
Joined: Sat Dec 22, 2012 11:53 pm

Re: How to get the specific view's on modified event

Postby agibsonsw on Sun Dec 23, 2012 9:23 pm

aponxi wrote:Thank you for your help, I deeply appreciate it! I will try to implement that on my code.. Will post the results :)

Not a problem. You might also consider (possibly) on_activated, so that your output-view is only updated when it is needed (perhaps..). Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: How to get the specific view's on modified event

Postby aponxi on Tue Dec 25, 2012 5:50 pm

Thanks to you agibsonsw, the following plugin for CoffeeScript has been patched.

Repo :

https://github.com/aponxi/Better-CoffeeScript-Sublime-Plugin
aponxi
 
Posts: 7
Joined: Sat Dec 22, 2012 11:53 pm

Re: How to get the specific view's on modified event

Postby FichteFoll on Sat Jan 19, 2013 4:05 am

agibsonsw wrote:Also AFAIK we cannot turn an event listener on and off. You could use a global boolean which is initially false, and your TextCommand could set this to True. The event listener would immediately return if this value is false.


Apparently, you can turn of specific event listeners but this is really hacky and requires messing with the sublime_plugin module located in the install directory. You would just have to iterate over e.g. `all_callbacks['on_modified']`, compare its base with your even listener (because it is instanced) and remove it from the list. However, usually you won't need to do this and it is simpler to just add a condition at the beginning of the event handler method. (And you should only do this if you know what you are doing.)

Just for completion's sake.
FichteFoll
 
Posts: 406
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany

Re: How to get the specific view's on modified event

Postby agibsonsw on Sat Jan 19, 2013 3:12 pm

I suppose it is also possible to place the event-listener code in a package and dynamically disable this package - even if the package consisted of a single file. I haven't tried it though ;)
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests