QScintilla/Editor.py

changeset 6101
b854a825d483
parent 6099
a7fecbc392d7
child 6105
cbd34d558bd9
--- a/QScintilla/Editor.py	Thu Feb 01 19:32:19 2018 +0100
+++ b/QScintilla/Editor.py	Fri Feb 02 19:42:01 2018 +0100
@@ -3037,6 +3037,10 @@
         @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"):
             self.__removeTrailingWhitespace()
@@ -3079,7 +3083,7 @@
         # now write text to the file fn
         try:
             editorConfigEncoding = self.__getEditorConfig(
-                "DefaultEncoding", nodefault=True)
+                "DefaultEncoding", nodefault=True, config=config)
             self.encoding = Utilities.writeEncodedFile(
                 fn, txt, self.encoding, forcedEncoding=editorConfigEncoding)
             if createBackup and perms_valid:
@@ -8057,22 +8061,34 @@
         if not fileName:
             fileName = self.fileName
         
+        self.__editorConfig = self.__loadEditorConfigObject(fileName)
+    
+    def __loadEditorConfigObject(self, fileName):
+        """
+        Private method to load the EditorConfig properties for the given
+        file name.
+        
+        @param fileName name of the file
+        @type str
+        @return EditorConfig dictionary
+        @rtype dict
+        """
+        editorConfig = {}
+        
         if fileName:
             try:
-                self.__editorConfig = \
-                    editorconfig.get_properties(fileName)
+                editorConfig = editorconfig.get_properties(fileName)
             except editorconfig.EditorConfigError:
                 E5MessageBox.warning(
                     self,
                     self.tr("EditorConfig Properties"),
                     self.tr("""<p>The EditorConfig properties for file"""
                             """ <b>{0}</b> could not be loaded.</p>""")
-                    .format(self.fileName))
-                self.__editorConfig = {}
-        else:
-            self.__editorConfig = {}
-    
-    def __getEditorConfig(self, option, nodefault=False):
+                    .format(fileName))
+        
+        return editorConfig
+    
+    def __getEditorConfig(self, option, nodefault=False, config=None):
         """
         Private method to get the requested option via EditorConfig.
         
@@ -8086,11 +8102,16 @@
         @param nodefault flag indicating to not get the default value from
             Preferences but return None instead
         @type bool
+        @param config reference to an EditorConfig object or None
+        @type dict
         @return value of requested setting or None if nothing was found and
             nodefault parameter was True
         @rtype any
         """
-        if not self.__editorConfig:
+        if config is None:
+            config = self.__editorConfig
+        
+        if not config:
             if nodefault:
                 return None
             else:
@@ -8098,7 +8119,7 @@
         
         try:
             if option == "EOLMode":
-                value = self.__editorConfig["end_of_line"]
+                value = config["end_of_line"]
                 if value == "lf":
                     value = QsciScintilla.EolUnix
                 elif value == "crlf":
@@ -8108,7 +8129,7 @@
                 else:
                     value = None
             elif option == "DefaultEncoding":
-                value = self.__editorConfig["charset"]
+                value = config["charset"]
         except KeyError:
             value = None
         

eric ide

mercurial