on_load方法没有按预期工作

时间:2017-03-30 17:49:02

标签: sublimetext2 sublimetext3 sublimetext sublime-text-plugin

以下是on_load方法的简单测试。

import sublime_plugin

class OnLoadTest(sublime_plugin.EventListener):
    def on_load(self, view):
        print("Tested")

如果我打开一些文件,然后关闭此文件(Ctrl-W),然后重新打开它(Ctrl-Shift-T),该插件工作正常。

但是,如果我打开某个文件,然后关闭编辑器,然后重新打开编辑器,则不会启动该插件。 (尽管该文件已成功重新打开,但感谢我的偏好中的"hot_exit": true"remember_open_files": true。)

是一些错误还是我缺乏技能?

我使用ST3,构建3126。

1 个答案:

答案 0 :(得分:3)

多年来,这是一个错误还是有意识的设计决策indexed property,但是众所周知。

从上一个会话恢复时,所有打开的文件都会恢复到它们所处的状态,其中包括所选文本,未保存的更改,修改的设置等内容。 Sublime启动并在插入代码之前或之后执行这些任务,以便尽可能快地启动。

如果plugin_loaded()在从恢复的会话中返回时需要再次执行某些操作,则可以通过定义模块级import sublime import sublime_plugin import os def plugin_loaded (): # Show project in all views of all windows for window in sublime.windows (): for view in window.views (): show_project (view) def show_project(view): # Sometimes a view is not associated with a window if view.window() is None: return # Is there a project file defined? project_file = view.window ().project_file_name () if project_file is not None: # Get the project filename without path or extension project_name = os.path.splitext (os.path.basename (project_file))[0] view.set_status ("00ProjectName", "[" + project_name + "]") # Display the current project name in the status bar class ProjectInStatusbar(sublime_plugin.EventListener): # When you create a new empty file def on_new(self, view): show_project (view) # When you load an existing file def on_load(self, view): show_project (view) # When you use File > New view into file on an existing file def on_clone(self, view): show_project (view) 函数来检测插件的加载时间,Sublime一旦所有东西都加载就会调用。在其中,您可以扫描所有窗口和文件并采取一些措施。

一个例子可能是:

void table_destroy(htable* ht) {

    entry *del;
    int i;
    for(i = 0; i < ht->size; i++) {
      while( (del = ht->entries[i]) ) {
        ht->entries[i] = del->next;
        free(del);
      }
    }
    free(ht->entries);
    free(ht); 
}
相关问题