Home Download Buy Blog Forum Support

wait until is_loading finnish

wait until is_loading finnish

Postby Binocular222 on Thu Dec 05, 2013 3:53 pm

new_view = window.open_file(SomePath)
new_view return blank because the file is loading.
How do I get that new_view ?
Binocular222
 
Posts: 55
Joined: Sat May 18, 2013 4:10 pm

Re: wait until is_loading finnish

Postby skuroda on Fri Dec 06, 2013 6:39 am

Please check the API documentation.

https://www.sublimetext.com/docs/3/api_ ... blime.View

There is a method view#is_loading
skuroda
 
Posts: 687
Joined: Fri Oct 05, 2012 8:11 am

Re: wait until is_loading finnish

Postby Binocular222 on Fri Dec 06, 2013 11:08 am

I know that is_loading(), but I have to make ST sleep until is_loading return false, then continue my script. I'm considering callback but haven't ye figure out how to setup. This is my latest fail trial: open file xxx.txt, then select the word 'abc'
Code: Select all
import sublime, sublime_plugin, os

class examplCommand(sublime_plugin.TextCommand):
    def run(self, view):
        window = self.view.window()
        new_view = window.open_file('D:\\Doc\\Temporary\\xxx.txt')
        main_function(parallel_function, new_view)

    def main_function(self, new_view, callback):
        while not new_view.is_loading() :
            callback(new_view)
    def parallel_function(self, view):
        sels = self.view.sel()
        new_sels = []
        for sel in sels:
            new_sels.append(self.view.find('abc', sel.end()))
        sels.clear()
        for sel in new_sels:
            sels.add(sel)
Binocular222
 
Posts: 55
Joined: Sat May 18, 2013 4:10 pm

Re: wait until is_loading finnish

Postby skuroda on Fri Dec 06, 2013 5:53 pm

Couple of points. First, your arguments are reversed for main function. Second, you don't need to use a while loop. Instead, you can use sublime.set_timeout. Using a while loop will block the thread, whereas set_timeout will not. For an example of how to do this, see here. The implementation of setup_view can be found here. Note that if the view is not yet ready, I simply perform another sublime.set_timeout call to wait for a little while longer.
skuroda
 
Posts: 687
Joined: Fri Oct 05, 2012 8:11 am

Re: wait until is_loading finnish

Postby Binocular222 on Sat Dec 07, 2013 3:23 am

Oh, thanks, I'm almost there:
Code: Select all
import sublime, sublime_plugin, os

class examplCommand(sublime_plugin.TextCommand):
    def run(self, view):
        window = self.view.window()
        new_view = window.open_file('D:\\Doc\\Temporary\\xxx.txt')
        sublime.set_timeout(lambda: self.select_text(new_view), 10)

    def select_text(self, view):
        if not view.is_loading():
            sublime.status_message('this line is processed')
            sels = self.view.sel()
            new_sels = []
            for sel in sels:
                new_sels.append(self.view.find('abc', sel.end()))
            sels.clear()
            for sel in new_sels:
                sels.add(sel)

I got the sublime.status_message('this line is processed') works. But the subsequent line doesn't select the text 'abc'.
So, the trouble portion is reduced to only a few lines. Sorry for being dumb, but please help me with the final lines.
Binocular222
 
Posts: 55
Joined: Sat May 18, 2013 4:10 pm

Re: wait until is_loading finnish

Postby sapphirehamster on Sat Dec 07, 2013 2:09 pm

In select_text you are using "self.view", which is the original view. I think you want just "view" which is the new view.

Also, I think you missed skuroda's last point: "Note that if the view is not yet ready, I simply perform another sublime.set_timeout call to wait for a little while longer." In your last sample code, if the view isn't ready in 10ms, then nothing will happen.
sapphirehamster
 
Posts: 91
Joined: Sun Jul 01, 2012 11:19 pm

Re: wait until is_loading finnish

Postby Binocular222 on Sat Dec 07, 2013 2:21 pm

100% works now, finally:
Code: Select all
import sublime, sublime_plugin, os, re

class examplCommand(sublime_plugin.TextCommand):
    def run(self, view):
        window = self.view.window()
        view = window.open_file('D:\\Doc\\Temporary\\xxx.txt')
        sublime.set_timeout(lambda: self.select_text(view), 10)

    def select_text(self, view):
        if not view.is_loading():
            sublime.status_message('this line is processed')
            sels = view.sel()
            new_sels = []
            for sel in sels:
                new_sels.append(view.find('abc', sel.end()))
            sels.clear()
            for sel in new_sels:
                sels.add(sel)
        else:
            sublime.set_timeout(lambda: self.select_text(view), 10)

Big Thanks to both of you!
Binocular222
 
Posts: 55
Joined: Sat May 18, 2013 4:10 pm

Re: wait until is_loading finnish

Postby Binocular222 on Sat Dec 07, 2013 2:39 pm

But anyway, I think a better approach should be showing the find panel and feed it with the 'abc' text.
Possible?
Binocular222
 
Posts: 55
Joined: Sat May 18, 2013 4:10 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 2 guests