QScintilla/Editor.py

changeset 6105
cbd34d558bd9
parent 6101
b854a825d483
child 6111
d38b38117d83
--- a/QScintilla/Editor.py	Sat Feb 03 10:45:52 2018 +0100
+++ b/QScintilla/Editor.py	Sat Feb 03 16:15:24 2018 +0100
@@ -194,10 +194,11 @@
         self.notcoveredMarkers = []  # just a list of marker handles
         self.showingNotcoveredMarkers = False
         
+        self.lexer_ = None
+        
         self.__loadEditorConfig()
         
         self.condHistory = []
-        self.lexer_ = None
         self.__lexerReset = False
         self.completer = None
         self.encoding = self.__getEditorConfig("DefaultEncoding")
@@ -2960,6 +2961,8 @@
         """
         QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
         
+        self.__loadEditorConfig(fileName=fn)
+        
         try:
             if createIt and not os.path.exists(fn):
                 f = open(fn, "w")
@@ -2982,31 +2985,29 @@
                     .format(fn, str(why)))
             QApplication.restoreOverrideCursor()
             raise
-        fileEol = self.detectEolString(txt)
         
         modified = False
-        # TODO: editorconfig: indent_style
-        if (not Preferences.getEditor("TabForIndentation")) and \
+        
+        if (not self.__getEditorConfig("TabForIndentation")) and \
                 Preferences.getEditor("ConvertTabsOnLoad") and \
                 not (self.lexer_ and
                      self.lexer_.alwaysKeepTabs()):
-            txtExpanded = txt.expandtabs(Preferences.getEditor("TabWidth"))
+            txtExpanded = txt.expandtabs(self.__getEditorConfig("TabWidth"))
             if txtExpanded != txt:
                 modified = True
             txt = txtExpanded
-            del txtExpanded
         
         self.setText(txt)
         
         # get eric specific flags
         self.__processFlags()
         
-        # perform automatic eol conversion
-        # TODO: editorconfig: end_of_line
+        # perform automatic EOL conversion
         if self.__getEditorConfig("EOLMode", nodefault=True) or \
            Preferences.getEditor("AutomaticEOLConversion"):
             self.convertEols(self.eolMode())
         else:
+            fileEol = self.detectEolString(txt)
             self.setEolModeByEolString(fileEol)
         
         self.extractTasks()
@@ -3037,25 +3038,25 @@
         @param backup flag indicating to save a backup (boolean)
         @return flag indicating success (boolean)
         """
-        # TODO: editorconfig: apply end_of_line, trim_trailing_whitespace,
-        #       insert_final_newline
         config = self.__loadEditorConfigObject(fn)
         
-        # TODO: editorconfig: trim_trailing_whitespace
-        if Preferences.getEditor("StripTrailingWhitespace"):
+        eol = self.__getEditorConfig("EOLMode", nodefault=True, config=config)
+        if eol is not None:
+            self.convertEols(eol)
+        
+        if self.__getEditorConfig("StripTrailingWhitespace", config=config):
             self.__removeTrailingWhitespace()
         
         txt = self.text()
-        # work around glitch in scintilla: always make sure,
-        # that the last line is terminated properly
-        # TODO: editorconfig: insert_final_newline
-        eol = self.getLineSeparator()
-        if eol:
-            if len(txt) >= len(eol):
-                if txt[-len(eol):] != eol:
+        
+        if self.__getEditorConfig("InsertFinalNewline", config=config):
+            eol = self.getLineSeparator()
+            if eol:
+                if len(txt) >= len(eol):
+                    if txt[-len(eol):] != eol:
+                        txt += eol
+                else:
                     txt += eol
-            else:
-                txt += eol
         
         # create a backup file, if the option is set
         createBackup = backup and Preferences.getEditor("CreateBackupFile")
@@ -3205,8 +3206,13 @@
             # save to project, if a project is loaded
             if self.project.isOpen() and \
                     self.project.startswithProjectPath(fn):
-                # TODO: check against editorconfig
-                self.setEolModeByEolString(self.project.getEolString())
+                editorConfigEol = self.__getEditorConfig(
+                    "EOLMode", nodefault=True,
+                    config=self.__loadEditorConfigObject(fn))
+                if editorConfigEol is not None:
+                    self.setEolMode(editorConfigEol)
+                else:
+                    self.setEolModeByEolString(self.project.getEolString())
                 self.convertEols(self.eolMode())
         else:
             fn = self.fileName
@@ -4336,20 +4342,24 @@
             self.setMarginWidth(
                 self.__linenoMargin, '8' * (len(str(self.lines())) + 1))
     
-    def __setTextDisplay(self):
-        """
-        Private method to configure the text display.
-        """
-        # TODO: editorconfig: tab_width
-        self.setTabWidth(Preferences.getEditor("TabWidth"))
-        # TODO: editorconfig: indent_size
-        self.setIndentationWidth(Preferences.getEditor("IndentWidth"))
-        # TODO: editorconfig: indent_style
+    def __setTabAndIndent(self):
+        """
+        Private method to set indentation size and style and tab width.
+        """
+        self.setTabWidth(self.__getEditorConfig("TabWidth"))
+        self.setIndentationWidth(self.__getEditorConfig("IndentWidth"))
         if self.lexer_ and self.lexer_.alwaysKeepTabs():
             self.setIndentationsUseTabs(True)
         else:
             self.setIndentationsUseTabs(
-                Preferences.getEditor("TabForIndentation"))
+                self.__getEditorConfig("TabForIndentation"))
+    
+    def __setTextDisplay(self):
+        """
+        Private method to configure the text display.
+        """
+        self.__setTabAndIndent()
+        
         self.setTabIndents(Preferences.getEditor("TabIndents"))
         self.setBackspaceUnindents(Preferences.getEditor("TabIndents"))
         self.setIndentationGuides(Preferences.getEditor("IndentationGuides"))
@@ -4466,11 +4476,12 @@
         if self.fileName and \
            self.project.isOpen() and \
            self.project.isProjectFile(self.fileName):
-            # TODO: editorconfig: end_of_line
-            eolMode = self.__getEditorConfig("EOLMode")
+            eolMode = self.__getEditorConfig("EOLMode", nodefault=True)
             if eolMode is None:
-                eolMode = self.project.getEolString()
-            self.setEolModeByEolString(eolMode)
+                eolStr = self.project.getEolString()
+                self.setEolModeByEolString(eolStr)
+            else:
+                self.setEolMode(eolMode)
         else:
             eolMode = self.__getEditorConfig("EOLMode")
             eolMode = QsciScintilla.EolMode(eolMode)
@@ -7279,7 +7290,11 @@
             self.__setSpellingLanguage(self.project.getProjectSpellLanguage(),
                                        pwl=pwl, pel=pel)
         
-        self.setEolModeByEolString(self.project.getEolString())
+        editorConfigEol = self.__getEditorConfig("EOLMode", nodefault=True)
+        if editorConfigEol is not None:
+            self.setEolMode(editorConfigEol)
+        else:
+            self.setEolModeByEolString(self.project.getEolString())
         self.convertEols(self.eolMode())
     
     def addedToProject(self):
@@ -7315,7 +7330,7 @@
             pass
     
     #######################################################################
-    ## Spellchecking related methods
+    ## Spell checking related methods
     #######################################################################
     
     def __setSpellingLanguage(self, language, pwl="", pel=""):
@@ -8062,6 +8077,9 @@
             fileName = self.fileName
         
         self.__editorConfig = self.__loadEditorConfigObject(fileName)
+        
+        if fileName:
+            self.__setTabAndIndent()
     
     def __loadEditorConfigObject(self, fileName):
         """
@@ -8130,6 +8148,20 @@
                     value = None
             elif option == "DefaultEncoding":
                 value = config["charset"]
+            elif option == "InsertFinalNewline":
+                value = Utilities.toBool(config["insert_final_newline"])
+            elif option == "StripTrailingWhitespace":
+                value = Utilities.toBool(config["trim_trailing_whitespace"])
+            elif option == "TabWidth":
+                value = int(config["tab_width"])
+            elif option == "IndentWidth":
+                value = config["indent_size"]
+                if value == "tab":
+                    value = self.__getEditorConfig("TabWidth", config=config)
+                else:
+                    value = int(value)
+            elif option == "TabForIndentation":
+                value = config["indent_style"] == "tab"
         except KeyError:
             value = None
         
@@ -8138,3 +8170,14 @@
             value = Preferences.getEditor(option)
         
         return value
+    
+    def getEditorConfig(self, option):
+        """
+        Public method to get the requested option via EditorConfig.
+        
+        @param option Preferences option key
+        @type str
+        @return value of requested setting
+        @rtype any
+        """
+        return self.__getEditorConfig(option)

eric ide

mercurial