2016-10-04 6 views
1

これは私が数日前に作ったこの質問の派生語ですSave file before running custom command in Sublime3Sublime3でカスタムコマンドを実行する前にすべてのファイルを保存

私はセットアップ崇高なテキスト3のカスタムキーバインドました:

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

これは、開かれたファイルを実行します:project_venv_repl.pyスクリプトを(それが何を学ぶためにhereを参照)を実行するには

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

f5キーが押されたときにはSublimeREPLにあります。 # Save all files before running REPL以下の2行は、REPLを実行する前に、未保存の変更があるすべての開いたファイルを保存する必要があります(これは前の質問にanswerとして記載されています)。

行は動作します。つまり、ファイルを保存します。しかし、彼らはまた、REPLを節約するために私を求めて、二つの連続保存ポップアップを表示する(?):

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

test.pyは、スクリプト​​が呼び出された場所からファイルです。両方のポップアップをキャンセルすると、スクリプトは正しく実行されます。

どのように私はすべてを保存する​​スクリプトがこれらの迷惑な保存ポップアップを表示せずに、実行する前に、変更を保存していないファイルを開いて入手できますか?

答えて

1

トリックだけ汚れていると、ディスク上に存在するファイルのために保存することです(このすべての背後にある考え方は、スクリプトを構築する前に、すべての未保存のファイルを保存します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

+0

'sublime.active_window()とは何ですか?これは、 'NameError:グローバル名 'の昇華が定義されていないという結果に終ります。スクリプトで 'sublime_plugin'をインポートしただけですが、他に何をインポートする必要がありますか? – Gabriel

+1

これは単にアクティブなウィンドウへの参照です。 'self.view.window()'を使うことができます。これはあなたのコマンド内からビューのウィンドウにアクセスできるからです。ウィンドウに直接アクセスする必要があるときはいつでも 'sublime.active_window()'を使うことができますが、 'sublime'をインポートする必要があります。 –

+0

優秀、それは私が必要としたものです。ありがとう、ジェラール! – Gabriel

関連する問題