How to get user input before saving a file in Sublime Text

I am creating a plugin in Sublime Text that asks the user for a password to encrypt a file before saving it. There is a hook in the API that runs before saving, so my naive implementation is:

class TranscryptEventListener(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        # If document is set to encode on save
        if view.settings().get('ON_SAVE'):
            self.view = view
            # Prompt user for password
            message = "Create a Password:"
            view.window().show_input_panel(message, "", self.on_done, None, None)

    def on_done(self, password):
        self.view.run_command("encode", {password": password})

The problem is that by the time the input panel for the user to enter the password appears, the document has already been saved (despite the fact that the trigger was "on_pre_save"). Then, as soon as the user clicks enter, the document is encrypted perfectly, but the situation is that there is a plaintext file and a modified buffer filled with encrypted text.

Therefore, I need to make Sublime Text until the user enters the password before saving. Is there any way to do this?

:

def on_pre_save(self, view, encode=False):
    if view.settings().get('ON_SAVE') and not view.settings().get('ENCODED'):
        self.view = view
        message = "Create a Password:"
        view.window().show_input_panel(message, "", self.on_done, None, None)

def on_done(self, password):
    self.view.run_command("encode", {password": password})
    self.view.settings().set('ENCODED', True)
    self.view.run_command('save')
    self.view.settings().set('ENCODED', False)

, , plaintext , . ?

: , , save. , on_text_command on_window_command, , save ( , -? on_application_command). ?

: , TextCommand , , .

+4
1

, , , . , show_input_panel . , "" . on_pre_save TextCommand . , , . , , ST3, , , ST2.

+2

Source: https://habr.com/ru/post/1544404/


All Articles