在 Sublime3 中运行自定义命令之前保存所有文件 [英] Save all files before running custom command in Sublime3

查看:49
本文介绍了在 Sublime3 中运行自定义命令之前保存所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我几天前提出的这个问题的衍生在 Sublime3 中运行自定义命令之前保存文件.>

我在 Sublime Text 3 中设置了自定义键绑定:

<代码>{键":[f5"],命令":project_venv_repl"}

运行 project_venv_repl.py 脚本(请参阅此处了解它的作用):

import sublime_plugin类 ProjectVenvReplCommand(sublime_plugin.TextCommand):"""启动 SublimeREPL,尝试使用项目的指定蟒蛇解释器."""def 运行(自我,编辑,open_file='$file'):"""调用 project_venv_repl 命令"""# 在运行 REPL 之前保存所有文件 <---- 这两行对于 self.view.window().views() 中的 open_view:open_view.run_command("保存")cmd_list = [self.get_project_interpreter(), '-i', '-u']如果打开_文件:cmd_list.append(open_file)self.repl_open(cmd_list=cmd_list)# 其他功能...

f5 键时,这会在 SublimeREPL 中运行打开的文件被按下.# Save all files before running REPL 下面的两行应该在运行 REPL 之前保存所有打开的文件和未保存的更改(如 回答我之前的问题).

行工作,即:他们保存文件.但他们也连续显示两个Save弹出窗口,要求我保存 REPL (?):

*REPL* [/home/gabriel/.pyenv/versions/test-env/bin/python -i -u/home/gabriel/Github/test/test.py]

test.py 是调用脚本 project_venv_repl 的文件.在我取消这两个弹出窗口后,脚本正确执行.

我怎样才能让 project_venv_repl 脚本在执行之前保存所有打开的文件和未保存的更改,显示这些烦人的 Save 弹出窗口?

(这一切背后的想法是模仿 Ctrl+B 的行为,这将在构建脚本之前保存所有未保存的文件)

解决方案

诀窍是只保存磁盘上存在的脏文件.

# 用更改和文件名写出每个缓冲区(活动窗口).窗口 = sublime.active_window()在 window.views() 中查看:如果 view.is_dirty() 和 view.file_name():view.run_command('保存')

我在使用 PHPUNITKIT 时遇到了类似的问题.

<块引用>

save_all_on_run:只保存磁盘上存在且有脏缓冲区的文件

注意:save_all_on_run"选项不再保存不保存的文件存在于磁盘上.

此更改的原因是尝试保存不存在的文件在磁盘上用保存文件"对话框提示用户,这通常是不想要的行为.

也许可以添加另一个选项save_all_on_run_strict"选项稍后它会尝试保存甚至磁盘上不存在的文件.

https://github.com/gerardroche/sublime-phpunit/commit/3138e2b75a8fbb7a5cb8d7dacabc3cf72a77c1bf"/a>

This is a derivative of this question I made some days ago Save file before running custom command in Sublime3.

I've setup a custom keybind in Sublime Text 3:

{
    "keys": ["f5"],
    "command": "project_venv_repl"
}

to run the project_venv_repl.py script (see here to learn what it does):

import sublime_plugin


class ProjectVenvReplCommand(sublime_plugin.TextCommand):
    """
    Starts a SublimeREPL, attempting to use project's specified
    python interpreter.
    """
    def run(self, edit, open_file='$file'):
        """Called on project_venv_repl command"""

        # Save all files before running REPL <---- THESE TWO LINES
        for open_view in self.view.window().views():
            open_view.run_command("save")

        cmd_list = [self.get_project_interpreter(), '-i', '-u']

        if open_file:
            cmd_list.append(open_file)

        self.repl_open(cmd_list=cmd_list)

    # Other functions...

This runs the opened file in a SublimeREPL when the f5 key is pressed. The two lines below # Save all files before running REPL should save all opened files with unsaved changes, before running the REPL (as stated in the answer to my previous question).

The lines work, ie: they save the files. But they also display two consecutive Save pop-ups, asking me to save the REPL (?):

*REPL* [/home/gabriel/.pyenv/versions/test-env/bin/python -i -u /home/gabriel/Github/test/test.py]

test.py is the file from where the script project_venv_repl was called. After I cancel both pop-ups, the script is executed correctly.

How can I get the project_venv_repl script to save all opened files with unsaved changes before executing, without displaying these annoying Save pop-ups?

(The idea behind all this is to mimic the behavior of Ctrl+B, which will save all unsaved files prior to building the script)

解决方案

The trick is to only save for files that are dirty and exist on disk.

# Write out every buffer (active window) with changes and a file name.
window = sublime.active_window()
for view in window.views():
    if view.is_dirty() and view.file_name():
        view.run_command('save')

I had a similar issue with PHPUNITKIT.

save_all_on_run: Only save files that exist on disk and have dirty buffers

Note: the "save_all_on_run" option no longer saves files that don't exist on disk.

The reason for this change is trying to save a file that doesn't exist on disk prompts the user with a "save file" dialog, which is generally not desired behaviour.

Maybe another option "save_all_on_run_strict" option can be added later that will try to save even the files that don't exist on disk.

https://github.com/gerardroche/sublime-phpunit/commit/3138e2b75a8fbb7a5cb8d7dacabc3cf72a77c1bf

这篇关于在 Sublime3 中运行自定义命令之前保存所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆