[gnome-builder] python: copy previous line indentation if it was non-zero-width space



commit 491fb6ce54e7ac9a75556f1fe7cc205460f8f462
Author: Christian Hergert <christian hergert me>
Date:   Sat Oct 3 18:34:27 2015 -0700

    python: copy previous line indentation if it was non-zero-width space
    
    Just a precaution to avoid annoying users with what we think the current
    ranking indentation should be.

 plugins/python-pack/python_indenter.py |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
---
diff --git a/plugins/python-pack/python_indenter.py b/plugins/python-pack/python_indenter.py
index 94386cd..648d0b1 100644
--- a/plugins/python-pack/python_indenter.py
+++ b/plugins/python-pack/python_indenter.py
@@ -480,6 +480,11 @@ class PythonIndenter(GObject.Object): #, Ide.Indenter):
 
         nearest = discoveries.nearest
 
+        # If the previous line is non-zero-width whitespace, we should
+        # just copy that line instead of annoying the user.
+        if line_is_space(begin):
+            return self.copy_indent(view, iter)
+
         # If we are in a comment, continue the comment on the new line,
         # possibly adding the prefixing "# ".
         if nearest.rank == Rank.COMMENT:
@@ -567,6 +572,16 @@ def copy_line(iter):
         end.forward_to_line_end()
     return begin.get_slice(end)
 
+def line_is_space(iter):
+    iter = iter.copy()
+    iter.set_line_offset(0)
+    while not iter.ends_line():
+        if iter.get_char().isspace():
+            iter.forward_char()
+            continue
+        return False
+    return iter.get_line_offset() != 0
+
 def forward_to_nonspace(iter):
     """
     Moves forward but stays on the same line.


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