[gedit] Fixed preserving headers and placing tool properties in correct location



commit ecc717c33c09cd867fe3c3c80ba1aa872fb4b579
Author: Jesse van den Kieboom <jesse icecrew nl>
Date:   Thu May 21 22:17:04 2009 +0200

    Fixed preserving headers and placing tool properties in correct location
    
    This handles shebang and modeline headers better. It also fixes the problem of tools
    not being properly saved after they are created (fixes bug #559121)
---
 plugins/externaltools/tools/library.py |   59 ++++++++++++++++---------------
 1 files changed, 30 insertions(+), 29 deletions(-)

diff --git a/plugins/externaltools/tools/library.py b/plugins/externaltools/tools/library.py
index 977dd51..5026547 100644
--- a/plugins/externaltools/tools/library.py
+++ b/plugins/externaltools/tools/library.py
@@ -356,35 +356,36 @@ class Tool(object):
         filename = self.library.get_full_path(self.filename, 'w')
         fp = open(filename, 'w', 1)
 
-        try:
-            script = iter(script)
-            line = script.next()
-
-            # Shebang (should be always present)
-            if line.startswith('#!'):
-                fp.write(line)
-                line = script.next()
-            # Emacs modeline (might or might not be there)
-            if '-*-' in line:
-                fp.write(line)
-                line = script.next()
-            # Vim modeline
-            if 'ex:' in line or 'vi:' in line or 'vim:' in line:
-                fp.write(line)
-                line = script.next()
-            # We put a white line before the info block if there is one available
-            if line.strip() == '':
-                fp.write(line)
-                line = script.next()
-            # Write the info block
-            fp.write(self._dump_properties())
-            fp.write("\n")
-            # And write the remaining part of the script
-            while True:
-                fp.write(line)
-                line = script.next()
-        except StopIteration:
-            pass
+        # Make sure to first print header (shebang, modeline), then
+        # properties, and then actual content
+        header = []
+        content = []
+        inheader = True
+
+        # Parse
+        for line in script:
+            line = line.rstrip("\n")
+            
+            if not inheader:
+                content.append(line)
+            elif line.startswith('#!'):
+                # Shebang (should be always present)
+                header.append(line)
+            elif line.strip().startswith('#') and ('-*-' in line or 'ex:' in line or 'vi:' in line or 'vim:' in line):
+                header.append(line)
+            else:
+                content.append(line)
+                inheader = False
+
+        # Write out header
+        for line in header:
+            fp.write(line + "\n")
+        
+        fp.write(self._dump_properties())
+        fp.write("\n")
+        
+        for line in content:
+            fp.write(line + "\n")
 
         fp.close()
         os.chmod(filename, 0750)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]