1

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)

Community
  • 1
  • 1
Gabriel
  • 40,504
  • 73
  • 230
  • 404

1 Answers1

1

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

Gerard Roche
  • 6,162
  • 4
  • 43
  • 69
  • What is `sublime.active_window()`? This results in `NameError: global name 'sublime' is not defined`. I only imported `sublime_plugin` in my script, what else should I import? – Gabriel Oct 04 '16 at 16:19
  • 1
    It's just a reference to the active window. You can use `self.view.window()` because you already have access to the view 's window from within your command. Whenever you need access to the window directly you can use `sublime.active_window()`, but yes you will need to `import sublime`. – Gerard Roche Oct 04 '16 at 16:27
  • Excellent, that does just what I needed. Thank you Gerard! – Gabriel Oct 04 '16 at 16:29