Skip to content

Commit

Permalink
open file pasted in working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Peter committed Apr 12, 2021
1 parent 9874609 commit 6514da0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
70 changes: 69 additions & 1 deletion spyder/plugins/workingdirectory/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
# Standard library imports
import os
import os.path as osp
import re

# Third party imports
from qtpy.compat import getexistingdirectory
from qtpy.QtCore import Signal, Slot
from qtpy.QtWidgets import QComboBox

# Local imports
from spyder.api.config.decorators import on_conf_change
Expand Down Expand Up @@ -49,6 +51,57 @@ class WorkingDirectoryToolbar(ApplicationToolbar):
ID = 'working_directory_toolbar'


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

edit_goto = Signal(str, int, str)

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()
QComboBox.focusOutEvent(self, event)

# --- Own methods
def valid_text(self):
"""Get valid version of current text."""
directory = self.currentText()
file = None
line_number = None
if directory:
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)
# It the directory is a file, open containing directory
if os.path.isfile(directory):
file = os.path.basename(directory)
directory = os.path.dirname(directory)

# If the directory name is malformed, open parent directory
if not os.path.isdir(directory):
directory = os.path.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)
return True


# --- Container
# ----------------------------------------------------------------------------
class WorkingDirectoryContainer(PluginMainContainer):
Expand All @@ -65,6 +118,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 @@ -76,7 +143,7 @@ def setup(self):
# Widgets
title = _('Current working directory')
self.toolbar = WorkingDirectoryToolbar(self, title)
self.pathedit = PathComboBox(
self.pathedit = WorkingDirectoryComboBox(
self,
adjust_to_contents=self.get_conf('working_dir_adjusttocontents'),
)
Expand All @@ -97,6 +164,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
1 change: 1 addition & 0 deletions spyder/plugins/workingdirectory/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def register(self):
if editor:
editor.sig_dir_opened.connect(
lambda path, plugin=editor: self.chdir(path, editor))
container.edit_goto.connect(editor.load)

if ipyconsole:
self.sig_current_directory_changed.connect(
Expand Down

0 comments on commit 6514da0

Please sign in to comment.