QScintilla/Shell.py

changeset 6576
ea60ea85067a
parent 6573
ccac2d1f6858
child 6578
2fb729cca297
--- a/QScintilla/Shell.py	Sat Nov 03 14:16:43 2018 +0100
+++ b/QScintilla/Shell.py	Sat Nov 03 14:19:21 2018 +0100
@@ -41,18 +41,24 @@
     """
     Class implementing the containing widget for the shell.
     """
-    def __init__(self, dbs, vm, horizontal=True, parent=None):
+    def __init__(self, dbs, vm, project, horizontal=True, parent=None):
         """
         Constructor
         
         @param dbs reference to the debug server object
+        @type DebugServer
         @param vm reference to the viewmanager object
-        @param horizontal flag indicating a horizontal layout (boolean)
-        @param parent parent widget (QWidget)
+        @type ViewManager
+        @param project reference to the project object
+        @type Project
+        @param horizontal flag indicating a horizontal layout
+        @type bool
+        @param parent parent widget
+        @type QWidget
         """
         super(ShellAssembly, self).__init__(parent)
         
-        self.__shell = Shell(dbs, vm, False, self)
+        self.__shell = Shell(dbs, vm, project, False, self)
         
         from UI.SearchWidget import SearchWidget
         self.__searchWidget = SearchWidget(self.__shell, self, horizontal)
@@ -116,15 +122,20 @@
     historyStyleChanged = pyqtSignal(ShellHistoryStyle)
     queueText = pyqtSignal(str)
     
-    def __init__(self, dbs, vm, windowedVariant, parent=None):
+    def __init__(self, dbs, vm, project, windowedVariant, parent=None):
         """
         Constructor
         
         @param dbs reference to the debug server object
+        @type DebugServer
         @param vm reference to the viewmanager object
+        @type ViewManager
+        @param project reference to the project object
+        @type Project
         @param windowedVariant flag indicating the shell window variant
-            (boolean)
-        @param parent parent widget (QWidget)
+        @type bool
+        @param parent parent widget
+        @type QWidget
         """
         super(Shell, self).__init__(parent)
         self.setUtf8(True)
@@ -133,6 +144,7 @@
         self.__mainWindow = parent
         self.__lastSearch = ()
         self.__windowed = windowedVariant
+        self.__currentVenv = ""
         
         self.linesepRegExp = r"\r\n|\n|\r"
         
@@ -266,7 +278,7 @@
         if not self.__windowed:
             # Create a little language context menu
             self.lmenu = QMenu(self.tr('Start'))
-            self.lmenu.aboutToShow.connect(self.__showLanguageMenu)
+            self.lmenu.aboutToShow.connect(self.__showStartMenu)
             self.lmenu.triggered.connect(self.__startDebugClient)
             
             # Create the history context menu
@@ -351,17 +363,24 @@
         self.__blockTextProcessing = False
         self.queueText.connect(self.__concatenateText, Qt.QueuedConnection)
         
+        self.__project = project
+        if self.__project:
+            self.__project.projectOpened.connect(self.__projectOpened)
+            self.__project.projectClosed.connect(self.__projectClosed)
+        
         self.grabGesture(Qt.PinchGesture)
     
-    def __showLanguageMenu(self):
+    def __showStartMenu(self):
         """
-        Private slot to prepare the language submenu.
+        Private slot to prepare the start submenu.
         """
         self.lmenu.clear()
-        clientLanguages = self.dbs.getSupportedLanguages(shellOnly=True)
-        for language in sorted(clientLanguages):
-            act = self.lmenu.addAction(language)
-            act.setData(language)
+        venvManager = e5App().getObject("VirtualEnvManager")
+        for venvName in sorted(venvManager.getVirtualenvNames()):
+            self.lmenu.addAction(venvName)
+        if self.__project.isOpen():
+            self.lmenu.addSeparator()
+            self.lmenu.addAction(self.tr("Project"))
         
     def __resizeLinenoMargin(self):
         """
@@ -579,14 +598,19 @@
         self.inCommandExecution = False
         self.interruptCommandExecution = False
         
-    def __clientCapabilities(self, cap, clType):
+    def __clientCapabilities(self, cap, clType, venvName):
         """
         Private slot to handle the reporting of the clients capabilities.
         
-        @param cap client capabilities (integer)
-        @param clType type of the debug client (string)
+        @param cap client capabilities
+        @type int
+        @param clType type of the debug client
+        @type str
+        @param venvName name of the virtual environment
+        @type str
         """
         self.clientCapabilities = cap
+        self.__currentVenv = venvName
         if clType != self.clientType:
             self.clientType = clType
             self.__bindLexer(self.clientType)
@@ -767,19 +791,25 @@
         else:
             self.dbs.remoteBanner()
         
-    def __writeBanner(self, version, platform, dbgclient):
+    def __writeBanner(self, version, platform, dbgclient, venvName):
         """
         Private method to write a banner with info from the debug client.
         
-        @param version interpreter version string (string)
-        @param platform platform of the remote interpreter (string)
-        @param dbgclient debug client variant used (string)
+        @param version interpreter version string
+        @type str
+        @param platform platform of the remote interpreter
+        @type str
+        @param dbgclient debug client variant used
+        @type str
+        @param venvName name of the virtual environment
+        @type str
         """
         super(Shell, self).clear()
         if self.passive and not self.dbs.isConnected():
             self.__write(self.tr('Passive Debug Mode'))
             self.__write(self.tr('\nNot connected'))
         else:
+            self.__currentVenv = venvName
             version = version.replace("#", self.tr("No."))
             if platform != "" and dbgclient != "":
                 self.__write(
@@ -787,6 +817,8 @@
                         .format(version, platform, dbgclient))
             else:
                 self.__write(version)
+            if venvName:
+                self.__write("\n[{0}]".format(venvName))
         self.__write('\n')
         
         self.__write(sys.ps1)
@@ -1710,36 +1742,24 @@
                     else:
                         self.__setHistoryIndex(historyIndex)
             
-            if cmd.startswith('start '):
+            if cmd == 'start' or cmd.startswith('start '):
                 if not self.passive:
                     cmdList = cmd.split(None, 1)
                     if len(cmdList) < 2:
-                        self.dbs.startClient(False)  # same as reset
+                        self.dbs.startClient(False)  # start default backend
                     else:
-                        clientLanguages = self.dbs.getSupportedLanguages(
-                            shellOnly=True)
-                        language = cmdList[1]
-                        if language not in clientLanguages:
-                            language = cmdList[1].capitalize()
-                            if language not in clientLanguages:
-                                language = ""
-                        if language:
-                            self.dbs.startClient(False, language)
+                        venvName = cmdList[1]
+                        if venvName == self.tr("Project"):
+                            if self.__project.isOpen():
+                                self.dbs.startClient(False, forProject=True)
+                            else:
+                                self.dbs.startClient(
+                                    False, venvName=self.__currentVenv)
+                                # same as reset
                         else:
-                            # language not supported or typo
-                            self.__write(
-                                self.tr(
-                                    'Shell language "{0}" not supported.\n')
-                                .format(cmdList[1]))
-                            self.__clientStatement(False)
-                        return
-                    cmd = ''
-            elif cmd == 'languages':
-                s = '{0}\n'.format(', '.join(sorted(
-                    self.dbs.getSupportedLanguages(shellOnly=True))))
-                self.__write(s)
-                self.__clientStatement(False)
-                return
+                            self.dbs.startClient(False, venvName=venvName)
+                    self.__getBanner()
+                    return
             elif cmd == 'clear':
                 # Display the banner.
                 self.__getBanner()
@@ -1748,7 +1768,7 @@
                 else:
                     cmd = ''
             elif cmd == 'reset':
-                self.dbs.startClient(False)
+                self.dbs.startClient(False, venvName=self.__currentVenv)
                 if self.passive:
                     return
                 else:
@@ -1903,8 +1923,11 @@
         
         @param action context menu action that was triggered (QAction)
         """
-        language = action.data()
-        self.dbs.startClient(False, language)
+        venvName = action.text()
+        if venvName == self.tr("Project"):
+            self.dbs.startClient(False, forProject=True)
+        else:
+            self.dbs.startClient(False, venvName=venvName)
         self.__getBanner()
         
     def handlePreferencesChanged(self):
@@ -2213,3 +2236,23 @@
         @rtype bool
         """
         return self.__historyStyle != ShellHistoryStyle.Disabled
+    
+    #################################################################
+    ## Project Support
+    #################################################################
+    
+    def __projectOpened(self):
+        """
+        Private slot to start the shell for the opened project.
+        """
+        if Preferences.getProject("RestartShellForProject"):
+            self.dbs.startClient(False, forProject=True)
+            self.__getBanner()
+    
+    def __projectClosed(self):
+        """
+        Private slot to restart the default shell when the project is closed.
+        """
+        if Preferences.getProject("RestartShellForProject"):
+            self.dbs.startClient(False)
+            self.__getBanner()

eric ide

mercurial