Python and Ruby Typing: added support for 'from ... import (...)' and changed the indentation when the previous line ends with '(', '[' or '{'.

Sat, 04 May 2019 14:34:51 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 04 May 2019 14:34:51 +0200
changeset 6997
24eabcea4c59
parent 6996
7d5a103bdb76
child 6998
a63ca2373f44

Python and Ruby Typing: added support for 'from ... import (...)' and changed the indentation when the previous line ends with '(', '[' or '{'.

docs/changelog file | annotate | diff | comparison | revisions
eric6/Preferences/ConfigurationPages/EditorTypingPage.py file | annotate | diff | comparison | revisions
eric6/Preferences/ConfigurationPages/EditorTypingPage.ui file | annotate | diff | comparison | revisions
eric6/Preferences/__init__.py file | annotate | diff | comparison | revisions
eric6/QScintilla/TypingCompleters/CompleterPython.py file | annotate | diff | comparison | revisions
eric6/QScintilla/TypingCompleters/CompleterRuby.py file | annotate | diff | comparison | revisions
--- a/docs/changelog	Sat May 04 14:01:19 2019 +0200
+++ b/docs/changelog	Sat May 04 14:34:51 2019 +0200
@@ -2,6 +2,14 @@
 ----------
 Version 19.6:
 - bug fixes
+- Editor
+  -- Python Typing
+     --- added support for 'from ... import (...)'
+     --- changed the indentation when the previous line ends with
+         '(', '[' or '{'
+  -- Ruby Typing
+     --- changed the indentation when the previous line ends with
+         '(', '[' or '{'
 - Third Party packages
   -- updated EditorConfig to 0.12.2
 
--- a/eric6/Preferences/ConfigurationPages/EditorTypingPage.py	Sat May 04 14:01:19 2019 +0200
+++ b/eric6/Preferences/ConfigurationPages/EditorTypingPage.py	Sat May 04 14:34:51 2019 +0200
@@ -53,10 +53,10 @@
             Preferences.getEditorTyping("Python/DedentElse"))
         self.pythonDedentExceptCheckBox.setChecked(
             Preferences.getEditorTyping("Python/DedentExcept"))
-        self.pythonDedentExceptPy24CheckBox.setChecked(
-            Preferences.getEditorTyping("Python/Py24StyleTry"))
         self.pythonInsertImportCheckBox.setChecked(
             Preferences.getEditorTyping("Python/InsertImport"))
+        self.pythonImportBraceTypeCheckBox.setChecked(
+            Preferences.getEditorTyping("Python/ImportBraceType"))
         self.pythonInsertSelfCheckBox.setChecked(
             Preferences.getEditorTyping("Python/InsertSelf"))
         self.pythonInsertBlankCheckBox.setChecked(
@@ -113,12 +113,12 @@
             "Python/DedentExcept",
             self.pythonDedentExceptCheckBox.isChecked())
         Preferences.setEditorTyping(
-            "Python/Py24StyleTry",
-            self.pythonDedentExceptPy24CheckBox.isChecked())
-        Preferences.setEditorTyping(
             "Python/InsertImport",
             self.pythonInsertImportCheckBox.isChecked())
         Preferences.setEditorTyping(
+            "Python/ImportBraceType",
+            self.pythonImportBraceTypeCheckBox.isChecked())
+        Preferences.setEditorTyping(
             "Python/InsertSelf",
             self.pythonInsertSelfCheckBox.isChecked())
         Preferences.setEditorTyping(
--- a/eric6/Preferences/ConfigurationPages/EditorTypingPage.ui	Sat May 04 14:01:19 2019 +0200
+++ b/eric6/Preferences/ConfigurationPages/EditorTypingPage.ui	Sat May 04 14:34:51 2019 +0200
@@ -162,10 +162,10 @@
           <item>
            <widget class="QCheckBox" name="pythonIndentBraceCheckBox">
             <property name="toolTip">
-             <string>Select to indent to the brace level after typing 'return'</string>
+             <string>Select to indent after typing 'return'</string>
             </property>
             <property name="text">
-             <string>After '(' indent to its level</string>
+             <string>Automatic indent after '('</string>
             </property>
            </widget>
           </item>
@@ -200,6 +200,16 @@
            </widget>
           </item>
           <item>
+           <widget class="QCheckBox" name="pythonInsertImportCheckBox">
+            <property name="toolTip">
+             <string>Select to insert the 'import ' string when the line starts with 'from ...'</string>
+            </property>
+            <property name="text">
+             <string>Automatic insertion of the 'import' string on 'from xxx'</string>
+            </property>
+           </widget>
+          </item>
+          <item>
            <layout class="QHBoxLayout">
             <item>
              <spacer>
@@ -218,15 +228,15 @@
              </spacer>
             </item>
             <item>
-             <widget class="QCheckBox" name="pythonDedentExceptPy24CheckBox">
+             <widget class="QCheckBox" name="pythonImportBraceTypeCheckBox">
               <property name="enabled">
                <bool>false</bool>
               </property>
               <property name="toolTip">
-               <string>Select to treat code as Python 2.4 code</string>
+               <string>Select to create import statements of the form 'from ... import (...)'</string>
               </property>
               <property name="text">
-               <string>Python 2.4 style 'try:' blocks</string>
+               <string>Generate '(...)' type import</string>
               </property>
              </widget>
             </item>
@@ -246,16 +256,6 @@
            </layout>
           </item>
           <item>
-           <widget class="QCheckBox" name="pythonInsertImportCheckBox">
-            <property name="toolTip">
-             <string>Select to insert the 'import' string</string>
-            </property>
-            <property name="text">
-             <string>Automatic insertion of the 'import' string on 'from xxx'</string>
-            </property>
-           </widget>
-          </item>
-          <item>
            <widget class="QCheckBox" name="pythonInsertSelfCheckBox">
             <property name="toolTip">
              <string>Select to insert the 'self' string when declaring a method</string>
@@ -349,10 +349,10 @@
           <item>
            <widget class="QCheckBox" name="rubyIndentBraceCheckBox">
             <property name="toolTip">
-             <string>Select to indent to the brace level after typing 'return'</string>
+             <string>Select to indent after typing 'return'</string>
             </property>
             <property name="text">
-             <string>After '(' indent to its level</string>
+             <string>Automatic indent after '('</string>
             </property>
            </widget>
           </item>
@@ -441,8 +441,8 @@
   <tabstop>pythonInsertQuoteCheckBox</tabstop>
   <tabstop>pythonDedentElseCheckBox</tabstop>
   <tabstop>pythonDedentExceptCheckBox</tabstop>
-  <tabstop>pythonDedentExceptPy24CheckBox</tabstop>
   <tabstop>pythonInsertImportCheckBox</tabstop>
+  <tabstop>pythonImportBraceTypeCheckBox</tabstop>
   <tabstop>pythonInsertSelfCheckBox</tabstop>
   <tabstop>pythonInsertBlankCheckBox</tabstop>
   <tabstop>pythonDedentDefCheckBox</tabstop>
@@ -464,12 +464,12 @@
    <slot>setEnabled(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>99</x>
-     <y>114</y>
+     <x>118</x>
+     <y>113</y>
     </hint>
     <hint type="destinationlabel">
-     <x>116</x>
-     <y>144</y>
+     <x>162</x>
+     <y>139</y>
     </hint>
    </hints>
   </connection>
@@ -480,8 +480,8 @@
    <slot>setChecked(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>196</x>
-     <y>114</y>
+     <x>215</x>
+     <y>113</y>
     </hint>
     <hint type="destinationlabel">
      <x>198</x>
@@ -490,18 +490,18 @@
    </hints>
   </connection>
   <connection>
-   <sender>pythonDedentExceptCheckBox</sender>
+   <sender>pythonInsertImportCheckBox</sender>
    <signal>toggled(bool)</signal>
-   <receiver>pythonDedentExceptPy24CheckBox</receiver>
+   <receiver>pythonImportBraceTypeCheckBox</receiver>
    <slot>setEnabled(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>69</x>
-     <y>279</y>
+     <x>82</x>
+     <y>271</y>
     </hint>
     <hint type="destinationlabel">
-     <x>80</x>
-     <y>305</y>
+     <x>92</x>
+     <y>297</y>
     </hint>
    </hints>
   </connection>
--- a/eric6/Preferences/__init__.py	Sat May 04 14:01:19 2019 +0200
+++ b/eric6/Preferences/__init__.py	Sat May 04 14:34:51 2019 +0200
@@ -696,8 +696,8 @@
         "Python/InsertQuote": True,
         "Python/DedentElse": True,
         "Python/DedentExcept": True,
-        "Python/Py24StyleTry": True,
         "Python/InsertImport": True,
+        "Python/ImportBraceType": False,
         "Python/InsertSelf": True,
         "Python/InsertBlank": True,
         "Python/ColonDetection": True,
--- a/eric6/QScintilla/TypingCompleters/CompleterPython.py	Sat May 04 14:01:19 2019 +0200
+++ b/eric6/QScintilla/TypingCompleters/CompleterPython.py	Sat May 04 14:34:51 2019 +0200
@@ -16,7 +16,7 @@
 import re
 
 from PyQt5.QtCore import QRegExp
-from PyQt5.Qsci import QsciLexerPython
+from PyQt5.Qsci import QsciLexerPython, QsciScintilla
 
 from .CompleterBase import CompleterBase
 
@@ -78,10 +78,10 @@
             Preferences.getEditorTyping("Python/DedentElse")
         self.__dedentExcept = \
             Preferences.getEditorTyping("Python/DedentExcept")
-        self.__py24StyleTry = \
-            Preferences.getEditorTyping("Python/Py24StyleTry")
         self.__insertImport = \
             Preferences.getEditorTyping("Python/InsertImport")
+        self.__importBraceType = \
+            Preferences.getEditorTyping("Python/ImportBraceType")
         self.__insertSelf = \
             Preferences.getEditorTyping("Python/InsertSelf")
         self.__insertBlank = \
@@ -148,8 +148,12 @@
         elif char == ' ':
             txt = self.editor.text(line)[:col]
             if self.__insertImport and self.__importRX.exactMatch(txt):
-                self.editor.insert('import ')
-                self.editor.setCursorPosition(line, col + 7)
+                if self.__importBraceType:
+                    self.editor.insert('import ()')
+                    self.editor.setCursorPosition(line, col + 8)
+                else:
+                    self.editor.insert('import ')
+                    self.editor.setCursorPosition(line, col + 7)
             elif self.__dedentElse and self.__elifRX.exactMatch(txt):
                 self.__dedentToIf()
             elif self.__dedentExcept and self.__exceptRX.exactMatch(txt):
@@ -211,23 +215,31 @@
             if self.__indentBrace:
                 txt = self.editor.text(line - 1)
                 if re.search(":\r?\n", txt) is None:
-                    openCount = len(re.findall("[({[]", txt))
-                    closeCount = len(re.findall(r"[)}\]]", txt))
-                    if openCount > closeCount:
-                        openCount = 0
-                        closeCount = 0
-                        openList = list(re.finditer("[({[]", txt))
-                        index = len(openList) - 1
-                        while index > -1 and openCount == closeCount:
-                            lastOpenIndex = openList[index].start()
-                            txt2 = txt[lastOpenIndex:]
-                            openCount = len(re.findall("[({[]", txt2))
-                            closeCount = len(re.findall(r"[)}\]]", txt2))
-                            index -= 1
-                        if openCount > closeCount and lastOpenIndex > col:
-                            self.editor.insert(' ' * (lastOpenIndex - col + 1))
-                            self.editor.setCursorPosition(
-                                line, lastOpenIndex + 1)
+                    stxt = txt.strip()
+                    if stxt and stxt[-1] in ("(", "[", "{"):
+                        # indent one more level
+                        self.editor.indent(line)
+                        self.editor.editorCommand(QsciScintilla.SCI_VCHOME)
+                    else:
+                        # indent to the level of the opening brace
+                        openCount = len(re.findall("[({[]", txt))
+                        closeCount = len(re.findall(r"[)}\]]", txt))
+                        if openCount > closeCount:
+                            openCount = 0
+                            closeCount = 0
+                            openList = list(re.finditer("[({[]", txt))
+                            index = len(openList) - 1
+                            while index > -1 and openCount == closeCount:
+                                lastOpenIndex = openList[index].start()
+                                txt2 = txt[lastOpenIndex:]
+                                openCount = len(re.findall("[({[]", txt2))
+                                closeCount = len(re.findall(r"[)}\]]", txt2))
+                                index -= 1
+                            if openCount > closeCount and lastOpenIndex > col:
+                                self.editor.insert(
+                                    ' ' * (lastOpenIndex - col + 1))
+                                self.editor.setCursorPosition(
+                                    line, lastOpenIndex + 1)
     
     def __dedentToIf(self):
         """
@@ -313,26 +325,15 @@
         while tryLine >= 0:
             txt = self.editor.text(tryLine)
             edInd = self.editor.indentation(tryLine)
-            if self.__py24StyleTry:
-                if (self.__exceptcRX.indexIn(txt) == 0 or
-                    self.__exceptRX.indexIn(txt) == 0 or
-                    self.__finallyRX.indexIn(txt) == 0) and \
-                        edInd <= indentation:
-                    indentation = edInd - 1
-                elif self.__tryRX.indexIn(txt) == 0 and edInd <= indentation:
-                    self.editor.cancelList()
-                    self.editor.setIndentation(line, edInd)
-                    break
-            else:
-                if self.__finallyRX.indexIn(txt) == 0 and edInd <= indentation:
-                    indentation = edInd - 1
-                elif (self.__tryRX.indexIn(txt) == 0 or
-                      self.__exceptcRX.indexIn(txt) == 0 or
-                      self.__exceptRX.indexIn(txt) == 0) and \
-                        edInd <= indentation:
-                    self.editor.cancelList()
-                    self.editor.setIndentation(line, edInd)
-                    break
+            if self.__finallyRX.indexIn(txt) == 0 and edInd <= indentation:
+                indentation = edInd - 1
+            elif (self.__tryRX.indexIn(txt) == 0 or
+                  self.__exceptcRX.indexIn(txt) == 0 or
+                  self.__exceptRX.indexIn(txt) == 0) and \
+                    edInd <= indentation:
+                self.editor.cancelList()
+                self.editor.setIndentation(line, edInd)
+                break
             tryLine -= 1
     
     def __dedentDefStatement(self):
--- a/eric6/QScintilla/TypingCompleters/CompleterRuby.py	Sat May 04 14:01:19 2019 +0200
+++ b/eric6/QScintilla/TypingCompleters/CompleterRuby.py	Sat May 04 14:34:51 2019 +0200
@@ -12,7 +12,7 @@
 import re
 
 from PyQt5.QtCore import QRegExp
-from PyQt5.Qsci import QsciLexerRuby
+from PyQt5.Qsci import QsciLexerRuby, QsciScintilla
 
 from .CompleterBase import CompleterBase
 
@@ -141,22 +141,30 @@
             elif self.__insertHereDoc and self.__hereRX.exactMatch(txt):
                 self.editor.insert(self.__hereRX.cap(1))
             elif self.__indentBrace and re.search(":\r?\n", txt) is None:
-                openCount = len(re.findall("[({[]", txt))
-                closeCount = len(re.findall(r"[)}\]]", txt))
-                if openCount > closeCount:
-                    openCount = 0
-                    closeCount = 0
-                    openList = list(re.finditer("[({[]", txt))
-                    index = len(openList) - 1
-                    while index > -1 and openCount == closeCount:
-                        lastOpenIndex = openList[index].start()
-                        txt2 = txt[lastOpenIndex:]
-                        openCount = len(re.findall("[({[]", txt2))
-                        closeCount = len(re.findall(r"[)}\]]", txt2))
-                        index -= 1
-                    if openCount > closeCount and lastOpenIndex > col:
-                        self.editor.insert(' ' * (lastOpenIndex - col + 1))
-                        self.editor.setCursorPosition(line, lastOpenIndex + 1)
+                stxt = txt.strip()
+                if stxt and stxt[-1] in ("(", "[", "{"):
+                    # indent one more level
+                    self.editor.indent(line)
+                    self.editor.editorCommand(QsciScintilla.SCI_VCHOME)
+                else:
+                    # indent to the level of the opening brace
+                    openCount = len(re.findall("[({[]", txt))
+                    closeCount = len(re.findall(r"[)}\]]", txt))
+                    if openCount > closeCount:
+                        openCount = 0
+                        closeCount = 0
+                        openList = list(re.finditer("[({[]", txt))
+                        index = len(openList) - 1
+                        while index > -1 and openCount == closeCount:
+                            lastOpenIndex = openList[index].start()
+                            txt2 = txt[lastOpenIndex:]
+                            openCount = len(re.findall("[({[]", txt2))
+                            closeCount = len(re.findall(r"[)}\]]", txt2))
+                            index -= 1
+                        if openCount > closeCount and lastOpenIndex > col:
+                            self.editor.insert(' ' * (lastOpenIndex - col + 1))
+                            self.editor.setCursorPosition(line,
+                                                          lastOpenIndex + 1)
     
     def __inComment(self, line, col):
         """

eric ide

mercurial