Only detect save for open-editor and config-edit.

Scope down the new trigger-on-save behavior to only open-editor and
config-edit. Other uses of the editor such as edit-url and edit-command
will behave as before.
This commit is contained in:
Ryan Roden-Corrent 2018-02-03 08:27:36 -05:00
parent ceab4a4c1f
commit 833df95485
4 changed files with 19 additions and 13 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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))

View File

@ -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: