在Sublime3中运行自定义命令之前保存文件

时间:2016-09-27 21:13:23

标签: sublimetext3 sublimerepl

这个问题类似于这个问题Is it possible to chain key binding commands in sublime text 2?自那个问题已经过去了几年(并给出了答案),我使用的是Sublime Text 3(不是2),所以我相信这个新问题是合理的

我已经设置了自定义键绑定:

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {

    element = elementName

    if(elementName as NSString) .isEqualToString("Song")
    {
        elements = NSMutableDictionary()
        elements = [:]
        title1 = NSMutableString()
        title1 = " "
        name = NSMutableString()
        name = " "
        time = NSMutableString()
        time = " "

    }
}

func parser(parser: NSXMLParser, foundCharacters string: String) {

    if element .isEqualToString("title")
    {
        title1.appendString(string)
    }
    else if element.isEqualToString("name")
    {
        name.appendString(string)
    }
    else if element.isEqualToString("time")
    {
        time.appendString(string)
    }
}


func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
    if (elementName as NSString) .isEqualToString("Organisation")
    {
        if !title1 .isEqual(nil)

        {
            elements.setObject(title1, forKey: "title")
        }

        if !name .isEqual(nil)
        {
            elements.setObject(name, forKey: "name")
        }
        if !time.isEqual(nil)
        {
            elements.setObject(time, forKey: "time")
        }

        myArray.addObject(elements)
    }
}

运行{ "keys": ["f5"], "command": "project_venv_repl" } 脚本:

project_venv_repl.py

当按下import sublime_plugin class ProjectVenvReplCommand(sublime_plugin.TextCommand): """ Starts a SublimeREPL, attempting to use project's specified python interpreter. Instructions to make this file work taken from: https://stackoverflow.com/a/25002696/1391441 """ def run(self, edit, open_file='$file'): """Called on project_venv_repl command""" cmd_list = [self.get_project_interpreter(), '-i', '-u'] if open_file: cmd_list.append(open_file) self.repl_open(cmd_list=cmd_list) def get_project_interpreter(self): """Return the project's specified python interpreter, if any""" settings = self.view.settings() return settings.get('python_interpreter', '/usr/bin/python') def repl_open(self, cmd_list): """Open a SublimeREPL using provided commands""" self.view.window().run_command( 'repl_open', { 'encoding': 'utf8', 'type': 'subprocess', 'cmd': cmd_list, 'cwd': '$file_path', 'syntax': 'Packages/Python/Python.tmLanguage' } ) 键时,它会在SublimeREPL中运行打开的文件。

我需要的是一种模仿“构建”快捷方式(f5)的方法。这是:当按下Ctrl+B键时,在运行f5命令之前,当前(打开的)文件应保存

是否可以将此指令添加到project_venv_repl脚本或keybind定义中的project_venv_repl.py行?

1 个答案:

答案 0 :(得分:2)

没有必要做任何花哨的事情。如果您只想在运行REPL之前保存当前视图,请编辑ProjectVenvReplCommandrun()方法(在执行project_venv_repl命令时调用)并添加开头的第一行:

self.view.run_command("save")

这将默认保存当前视图,除非之前没有保存过,在这种情况下,“另存为...”对话框将像往常一样打开。

如果要在窗口中保存所有打开的文件,可以使用以下代码:

for open_view in self.view.window().views():
    open_view.run_command("save")