Home Download Buy Blog Forum Support

open_file + set_syntax_file

open_file + set_syntax_file

Postby FobbyDi on Wed Jun 04, 2014 11:32 am

Hi, I can't figure out what is wrong, please help me (OS: Windows, Sublime Text 3 Build 3059)

I wrote simple plugin:
Code: Select all
class OpenHostsFileCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view.window().open_file('c:/windows/system32/drivers/etc/hosts')
        view.set_syntax_file('Packages/INI/INI.tmLanguage')


Menu item:
Code: Select all
            ...
            {
                "caption": "Open System Hosts File",
                "command": "open_hosts_file"
            }
            ...


In this case file is opening but function set_syntax_file does not do anything.
Opened file still has default syntax (Plain Text).
FobbyDi
 
Posts: 2
Joined: Wed Jun 04, 2014 11:12 am

Re: open_file + set_syntax_file

Postby sapphirehamster on Wed Jun 04, 2014 9:01 pm

You need to wait until the view is finished loading. Something along these lines:

Code: Select all
    def run(self, edit):
        view = self.view.window().open_file('/private/etc/hosts')
        def set_syntax():
            if view.is_loading():
                sublime.set_timeout_async(set_syntax, 0.1)
            else:
                view.set_syntax_file('Packages/INI/INI.tmLanguage')
        set_syntax()
sapphirehamster
 
Posts: 91
Joined: Sun Jul 01, 2012 11:19 pm

Re: open_file + set_syntax_file

Postby tito on Wed Jun 04, 2014 9:44 pm

Try first with view.settings().set('syntax', 'path to syntax file'), before going to the is_loading loop
Give APIs, let the community build the rest!
https://github.com/titoBouzout
tito
 
Posts: 878
Joined: Thu Sep 29, 2011 2:27 pm
Location: Montevideo, Uruguay

Re: open_file + set_syntax_file

Postby FobbyDi on Thu Jun 05, 2014 8:32 am

Thank you!

Set syntax via settings not working,
I think you can call view.settings().get('syntax') but not view.settings().set('syntax', '...').

Now I know that I need to wait until view is loaded.
So I found another solution rather then going to the is_loading loop:

Code: Select all
class OpenHostsFileCommand(sublime_plugin.TextCommand, sublime_plugin.EventListener):
    __path = None

    def __init__(self, view = None):
        self.view = view;

        if sublime.platform() == 'windows':
            self.__path = 'c:\\windows\\system32\\drivers\\etc\\hosts'
        else:
            self.__path = '\\etc\\hosts'


    def run(self, edit):
        self.view.window().open_file(self.__path)

    def on_load(self, view):
        if view.file_name().lower() == self.__path:
            view.set_syntax_file('Packages/INI/INI.tmLanguage')
FobbyDi
 
Posts: 2
Joined: Wed Jun 04, 2014 11:12 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 5 guests