diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 55af2c50a..094563bcc 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1621,7 +1621,7 @@ class CommandDispatcher: caret_position = elem.caret_position() - ed = editor.ExternalEditor(self._tabbed_browser) + ed = editor.ExternalEditor(watch=True, parent=self._tabbed_browser) ed.file_updated.connect(functools.partial( self.on_file_updated, elem)) ed.edit(text, caret_position) diff --git a/qutebrowser/config/configcommands.py b/qutebrowser/config/configcommands.py index faf31d718..1a07b2020 100644 --- a/qutebrowser/config/configcommands.py +++ b/qutebrowser/config/configcommands.py @@ -263,7 +263,7 @@ class ConfigCommands: except configexc.ConfigFileErrors as e: message.error(str(e)) - ed = editor.ExternalEditor(self._config) + ed = editor.ExternalEditor(watch=True, parent=self._config) if not no_source: ed.file_updated.connect(on_file_updated) diff --git a/qutebrowser/misc/editor.py b/qutebrowser/misc/editor.py index b9a3ed97f..9565ce28b 100644 --- a/qutebrowser/misc/editor.py +++ b/qutebrowser/misc/editor.py @@ -41,22 +41,27 @@ class ExternalEditor(QObject): closed. _proc: The GUIProcess of the editor. _watcher: A QFileSystemWatcher to watch the edited file for changes. + Only set if watch=True. """ file_updated = pyqtSignal(str) - def __init__(self, parent=None): + def __init__(self, parent=None, watch=False): super().__init__(parent) self._filename = None self._proc = None self._remove_file = None - self._watcher = QFileSystemWatcher(parent=self) + if watch: + self._watcher = QFileSystemWatcher(parent=self) + else: + self._watcher = None self._content = None def _cleanup(self): """Clean up temporary files after the editor closed.""" assert self._remove_file is not None - self._watcher.removePaths(self._watcher.files()) + if self._watcher: + self._watcher.removePaths(self._watcher.files()) if self._filename is None or not self._remove_file: # Could not create initial file. return @@ -154,8 +159,9 @@ class ExternalEditor(QObject): editor = config.val.editor.command executable = editor[0] - self._watcher.addPath(self._filename) - self._watcher.fileChanged.connect(self._on_file_changed) + if self._watcher: + self._watcher.addPath(self._filename) + self._watcher.fileChanged.connect(self._on_file_changed) args = [self._sub_placeholder(arg, line, column) for arg in editor[1:]] log.procs.debug("Calling \"{}\" with args {}".format(executable, args)) diff --git a/tests/unit/misc/test_editor.py b/tests/unit/misc/test_editor.py index 3f7d8acaf..37506d923 100644 --- a/tests/unit/misc/test_editor.py +++ b/tests/unit/misc/test_editor.py @@ -170,18 +170,18 @@ def test_modify(qtbot, editor, initial_text, edited_text): with open(editor._filename, 'r', encoding='utf-8') as f: assert f.read() == initial_text - with qtbot.wait_signal(editor.file_updated) as blocker: - with open(editor._filename, 'w', encoding='utf-8') as f: - f.write(edited_text) + with open(editor._filename, 'w', encoding='utf-8') as f: + f.write(edited_text) - with qtbot.assert_not_emitted(editor.file_updated): + with qtbot.wait_signal(editor.file_updated) as blocker: editor._proc.finished.emit(0, QProcess.NormalExit) assert blocker.args == [edited_text] -def test_modify_multiple(qtbot, editor): - """Test that multiple saves all trigger file_updated.""" +def test_modify_watch(qtbot): + """Test that saving triggers file_updated when watch=True.""" + editor = editormod.ExternalEditor(watch=True) editor.edit('foo') with qtbot.wait_signal(editor.file_updated) as blocker: