Sublime Forum

Wait until is_loading finnish

#1

new_view = window.open_file(SomePath)
new_view return blank because the file is loading.
How do I get that new_view ?

0 Likes

Open Multiple Files and Replace Contents
#2

Please check the API documentation.

sublimetext.com/docs/3/api_ … blime.View

There is a method view#is_loading

0 Likes

#3

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]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)[/code]
0 Likes

#4

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.

0 Likes

#5

Oh, thanks, I’m almost there:[code]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)[/code]

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.

0 Likes

#6

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.

0 Likes

#7

100% works now, finally:

[code]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)[/code]

Big Thanks to both of you!

1 Like

#8

But anyway, I think a better approach should be showing the find panel and feed it with the ‘abc’ text.
Possible?

0 Likes