Skip to content

Commit

Permalink
Merge pull request #14092 from impact27/fix_working_dir
Browse files Browse the repository at this point in the history
PR: Open file pasted into working directory toolbar
  • Loading branch information
ccordoba12 authored Jun 22, 2023
2 parents 0b7d9a2 + 049f77a commit 14b3fd5
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ include MANIFEST.in
include README.md
include LICENSE.txt
include changelogs/Spyder-5.md
include changelogs/Spyder-6.md
include AUTHORS.txt
include NOTICE.txt
include bootstrap.py
Expand Down
73 changes: 73 additions & 0 deletions spyder/plugins/workingdirectory/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import logging
import os
import os.path as osp
import re

# Third party imports
from qtpy.compat import getexistingdirectory
Expand Down Expand Up @@ -57,6 +58,9 @@ class WorkingDirectoryToolbar(ApplicationToolbar):


class WorkingDirectoryComboBox(PathComboBox):
"""Working directory combo box."""

edit_goto = Signal(str, int, str)

def __init__(self, parent):
super().__init__(
Expand All @@ -77,7 +81,61 @@ def enterEvent(self, event):
"""Set current path as the tooltip of the widget on hover."""
self.setToolTip(self.currentText())

def focusOutEvent(self, event):
"""Handle focus out event restoring the last valid selected path."""
if self.add_current_text_if_valid():
self.selected()
self.hide_completer()
hide_status = getattr(self.lineEdit(), 'hide_status_icon', None)
if hide_status:
hide_status()
super().focusOutEvent(event)

# ---- Own methods
def valid_text(self):
"""Get valid version of current text."""
directory = self.currentText()
file = None
line_number = None

if directory:
# Search for path/to/file.py:10 where 10 is the line number
match = re.fullmatch(r"(?:(\d+):)?(.+)", directory[::-1])
if match:
line_number, directory = match.groups()
if line_number:
line_number = int(line_number[::-1])
directory = directory[::-1]

directory = osp.abspath(directory)

# If the directory is actually a file, open containing directory
if osp.isfile(directory):
file = osp.basename(directory)
directory = osp.dirname(directory)

# If the directory name is malformed, open parent directory
if not osp.isdir(directory):
directory = osp.dirname(directory)

if self.is_valid(directory):
return directory, file, line_number

return self.selected_text, file, line_number

def add_current_text_if_valid(self):
"""Add current text to combo box history if valid."""
directory, file, line_number = self.valid_text()
if file:
self.edit_goto.emit(file, line_number, "")
if directory != self.currentText():
self.add_text(directory)
if directory:
return True


# ---- Container
# ----------------------------------------------------------------------------
class WorkingDirectorySpacer(QWidget):
ID = 'working_directory_spacer'

Expand Down Expand Up @@ -107,6 +165,20 @@ class WorkingDirectoryContainer(PluginMainContainer):
The new new working directory path.
"""

edit_goto = Signal(str, int, str)
"""
This signal is emitted when a file has been requested.
Parameters
----------
filename: str
The file to open.
line: int
The line to go to.
word: str
The word to go to in the line.
"""

# ---- PluginMainContainer API
# ------------------------------------------------------------------------
def setup(self):
Expand All @@ -128,6 +200,7 @@ def setup(self):

# Signals
self.pathedit.open_dir.connect(self.chdir)
self.pathedit.edit_goto.connect(self.edit_goto)
self.pathedit.activated[str].connect(self.chdir)

# Actions
Expand Down
2 changes: 2 additions & 0 deletions spyder/plugins/workingdirectory/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def on_preferences_available(self):
def on_editor_available(self):
editor = self.get_plugin(Plugins.Editor)
editor.sig_dir_opened.connect(self._editor_change_dir)
container = self.get_container()
container.edit_goto.connect(editor.load)

@on_plugin_available(plugin=Plugins.Explorer)
def on_explorer_available(self):
Expand Down
31 changes: 31 additions & 0 deletions spyder/plugins/workingdirectory/tests/test_workingdirectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
"""Tests for workingdirectory plugin."""

# Standard library imports
import os
import os.path as osp
import sys
from unittest.mock import Mock

# Third-party imports
import pytest
from qtpy.QtWidgets import QMainWindow
from qtpy.QtCore import Qt

# Local imports
from spyder.app.cli_options import get_options
Expand Down Expand Up @@ -116,5 +118,34 @@ def test_get_workingdir_cli(setup_workingdirectory):
CONF.reset_to_defaults()


def test_file_goto(qtbot, setup_workingdirectory):
"""
Test that putting a file in the workingdirectory emits a edit_goto signal.
"""
container = setup_workingdirectory.get_container()

signal_res = {}

def test_slot(filename, line, word):
signal_res["filename"] = filename
signal_res["line"] = line

container.edit_goto.connect(test_slot)

pathedit = container.pathedit
wd = setup_workingdirectory.get_workdir()
filename = osp.join(wd, "myfile_workingdirectory_test.py")
with open(filename, "w") as f:
f.write("\n" * 5)
with qtbot.waitSignal(container.edit_goto):
pathedit.add_text(filename + ":1")
qtbot.keyClick(pathedit, Qt.Key_Return)

assert signal_res["filename"] in filename
assert signal_res["line"] == 1

os.remove(filename)


if __name__ == "__main__":
pytest.main()
3 changes: 3 additions & 0 deletions spyder/widgets/comboboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,13 @@ def tab_complete(self):
If there is a single option available one tab completes the option.
"""
opts = self._complete_options()
if len(opts) == 0:
return
if len(opts) == 1:
self.set_current_text(opts[0] + os.sep)
self.hide_completer()
else:
self.set_current_text(osp.commonprefix(opts))
self.completer().complete()

def is_valid(self, qstr=None):
Expand Down

0 comments on commit 14b3fd5

Please sign in to comment.