gpt4 book ai didi

python - 在 Sublime 3 中运行自定义命令之前保存所有文件

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:29 28 4
gpt4 key购买 nike

这是我几天前提出的这个问题的衍生Save file before running custom command in Sublime3 .

我在 Sublime Text 3 中设置了自定义按键绑定(bind):

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

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

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...

这将运行 SublimeREPL 中打开的文件当按下 f5 键时。 # Save all files before running REPL 下面的两行应该在运行 REPL 之前保存所有打开的文件以及未保存的更改(如我上一个问题的 answer 中所述)。

这些行有效,即:它们保存文件。但它们还显示两个连续的“保存”弹出窗口,要求我保存 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 脚本在执行前保存所有打开的文件以及未保存的更改,不显示这些烦人的保存弹出窗口?

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

最佳答案

诀窍是仅保存磁盘上存在的脏文件。

# 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')

我对 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

关于python - 在 Sublime 3 中运行自定义命令之前保存所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39852604/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com