2016-10-04 66 views
1

這是我前幾天做的這個問題的衍生Save file before running custom command in Sublime3在Sublime3中運行自定義命令之前保存所有文件

我設置在崇高的文本3自定義熱鍵綁定:

{ 
    "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鍵時。在運行REPL之前,# Save all files before running REPL下面的兩行應該保存所有打開的文件並保存未更改的內容(如answer中針對我的上一個問題所述)。

行的工作,即:他們保存文件。但他們也顯示兩個連續保存彈出窗口,要我保存REPL(?):

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

test.py是從哪兒腳本​​被稱爲文件。在取消兩個彈出窗口後,腳本正確執行。

我怎樣才能獲得​​腳本保存所有打開的未保存更改的文件執行前,沒有顯示這些惱人保存彈出窗口?

(在這一切背後的想法是模仿Ctrl+B的行爲,這將之前保存所有未保存的文件,以構建腳本)

回答

1

訣竅是隻保存那些骯髒和存在磁盤上的文件。

# 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'未定義'。我只在我的腳本中導入了'sublime_plugin',還應該導入什麼? – Gabriel

+1

這只是對活動窗口的引用。你可以使用'self.view.window()',因爲你已經可以從命令中訪問視圖的窗口。無論何時你需要直接訪問窗口,你都可以使用'sublime.active_window()',但是你需要'import sublime'。 –

+0

非常好,那只是我需要的。謝謝傑勒德! – Gabriel

相關問題