Browse Source

#702: indents cleanup tool

Rafał Pitoń 8 years ago
parent
commit
597c946440

+ 39 - 31
extras/fixdictsformatting.py

@@ -28,12 +28,13 @@ def fix_formatting(filesource):
 def walk_tree(node, children):
     for item in children:
         if item.type == syms.dictsetmaker:
-            walk_dict_tree(item, item.children)
+            indent = item.parent.children[-1].column
+            walk_dict_tree(item, item.children, indent)
         else:
             walk_tree(item, item.children)
 
 
-def walk_dict_tree(node, children):
+def walk_dict_tree(node, children, indent):
     for item in children:
         prev = item.prev_sibling
         if isinstance(prev, Leaf) and prev.value == ':':
@@ -47,10 +48,10 @@ def walk_dict_tree(node, children):
             elif six.text_type(item).strip()[0] in ('[', '{'):
                 walk_tree(item, item.children)
             else:
-                walk_dedent_tree(item, item.children)
+                walk_dedent_tree(item, item.children, indent)
 
 
-def walk_dedent_tree(node, children):
+def walk_dedent_tree(node, children, indent):
     force_split_next = False
     for item in children:
         prev = item.prev_sibling
@@ -74,60 +75,54 @@ def walk_dedent_tree(node, children):
                     # different stringformat tactic
                     force_split_next = True
         elif isinstance(item, Node):
-            for subitem in item.children[1:]:
-                walk_dedent_tree_node(subitem, subitem.children, force_split_next)
-                force_split_next = False
+            if node.type == syms.power:
+                for subitem in item.children[1:]:
+                    walk_dedent_power_node(subitem, subitem.children, indent)
+            else:
+                for subitem in item.children[1:]:
+                    walk_dedent_tree_node(subitem, subitem.children, indent, force_split_next)
+                    force_split_next = False
 
 
-def walk_dedent_tree_node(node, children, force_split_next=False):
+def walk_dedent_tree_node(node, children, indent, force_split_next=False):
     if six.text_type(node).startswith("\n"):
         if isinstance(node, Leaf):
             prev = node.prev_sibling
             next = node.next_sibling
 
-            if next and six.text_type(next).strip() == ':':
-                return # excape hatch for misidentification of single nested dict item
-            if six.text_type(node).strip() == '}':
-                return # generally yapf does good job positioning closing curlybraces
-
             is_followup = prev and prev.type == token.STRING and node.type == token.STRING
             if is_followup:
                 new_value = node.value
-                new_prefix = "\n%s" % (' ' * (len(prev.prefix.lstrip("\n")) / 4 * 4))
 
                 # insert linebreak after last string in braces, so its closing brace moves to new line
                 if not node.next_sibling:
                     closing_bracket = node.parent.parent.children[-1]
                     if not six.text_type(closing_bracket).startswith("\n"):
-                        new_value = "%s\n%s" % (node.value, (' ' * ((len(prev.prefix.lstrip("\n")) / 4 - 1) * 4)))
+                        new_value = "%s\n%s" % (node.value, (' ' * (indent + 4)))
 
                 node.replace(Leaf(
                     node.type,
                     new_value,
-                    prefix=new_prefix,
+                    prefix="\n%s" % (' ' * (indent + 8)),
                 ))
             else:
+                if six.text_type(node).strip() in (')', '}'):
+                    new_prefix = "\n%s" % (' ' * (indent + 4))
+                else:
+                    new_prefix = "\n%s" % (' ' * (indent + 8))
+
                 node.replace(Leaf(
                     node.type,
                     node.value,
-                    prefix=node.prefix[:-4],
+                    prefix=new_prefix
                 ))
         else:
             for item in children:
-                walk_dedent_tree_node(item, item.children)
+                walk_dedent_tree_node(item, item.children, indent)
     elif isinstance(node, Leaf):
         if node.type == token.STRING:
             strings_tuple = node.parent.parent
 
-            # compute indent
-            if force_split_next:
-                container = strings_tuple.parent.children[0]
-            else:
-                container = strings_tuple.parent.parent.children[0]
-            while isinstance(container, Node):
-                container = container.children[0]
-            indent = container.column + 4
-
             prev = node.prev_sibling
             next = node.next_sibling
 
@@ -138,15 +133,28 @@ def walk_dedent_tree_node(node, children, force_split_next=False):
                 node.replace(Leaf(
                     node.type,
                     node.value,
-                    prefix="\n%s" % (' ' * indent),
+                    prefix="\n%s" % (' ' * (indent + 8)),
                 ))
             elif force_split_next:
                 node.replace(Leaf(
                     node.type,
-                    "%s\n%s" % (node.value, (' ' * (indent - 4))),
-                    prefix="\n%s" % (' ' * indent),
+                    "%s\n%s" % (node.value, (' ' * (indent + 4))),
+                    prefix="\n%s" % (' ' * (indent + 8)),
                 ))
     else:
         for item in children:
-            walk_dedent_tree_node(item, item.children)
+            walk_dedent_tree_node(item, item.children, indent)
+
+
+def walk_dedent_power_node(node, children, indent):
+    if isinstance(node, Leaf):
+        if six.text_type(node).startswith("\n"):
+            node.replace(Leaf(
+                node.type,
+                node.value,
+                prefix=node.prefix[:-4],
+            ))
+    else:
+        for item in children:
+            walk_dedent_power_node(item, item.children, indent)
 

+ 2 - 7
misago/core/migrations/0002_basic_settings.py

@@ -33,10 +33,7 @@ def create_basic_settings_group(apps, schema_editor):
                 {
                     'setting': 'forum_index_title',
                     'name': _("Index title"),
-                    'description': _(
-                        "You may set custon title on "
-                        "forum index by typing it here."
-                    ),
+                    'description': _("You may set custon title on forum index by typing it here."),
                     'legend': _("Forum index"),
                     'field_extra': {
                         'max_length': 255
@@ -74,9 +71,7 @@ def create_basic_settings_group(apps, schema_editor):
                 {
                     'setting': 'email_footer',
                     'name': _("E-mails footer"),
-                    'description': _(
-                        "Optional short message included at the end of e-mails sent by forum."
-                    ),
+                    'description': _("Optional short message included at the end of e-mails sent by forum."),
                     'legend': _("Forum e-mails"),
                     'field_extra': {
                         'max_length': 255

+ 3 - 9
misago/legal/migrations/0001_initial.py

@@ -14,9 +14,7 @@ def create_legal_settings_group(apps, schema_editor):
         apps, {
             'key': 'legal',
             'name': _("Legal information"),
-            'description': _(
-                "Those settings allow you to set forum terms of service and privacy policy."
-            ),
+            'description': _("Those settings allow you to set forum terms of service and privacy policy."),
             'settings': [
                 {
                     'setting': 'terms_of_service_title',
@@ -33,9 +31,7 @@ def create_legal_settings_group(apps, schema_editor):
                 {
                     'setting': 'terms_of_service_link',
                     'name': _("Terms link"),
-                    'description': _(
-                        "If terms of service are located on other page, enter there its link."
-                    ),
+                    'description': _("If terms of service are located on other page, enter there its link."),
                     'value': "",
                     'field_extra': {
                         'max_length': 255,
@@ -77,9 +73,7 @@ def create_legal_settings_group(apps, schema_editor):
                 {
                     'setting': 'privacy_policy_link',
                     'name': _("Policy link"),
-                    'description': _(
-                        "If privacy policy is located on other page, enter there its link."
-                    ),
+                    'description': _("If privacy policy is located on other page, enter there its link."),
                     'value': "",
                     'field_extra': {
                         'max_length': 255,

+ 1 - 3
misago/threads/forms.py

@@ -58,9 +58,7 @@ class AttachmentTypeForm(forms.ModelForm):
             'limit_downloads_to': _("Limit downloads to"),
         }
         help_texts = {
-            'extensions': _(
-                "List of comma separated file extensions associated with this attachment type."
-            ),
+            'extensions': _("List of comma separated file extensions associated with this attachment type."),
             'mimetypes': _(
                 "Optional list of comma separated mime types associated with this attachment type."
             ),

+ 1 - 3
misago/threads/viewmodels/threads.py

@@ -35,9 +35,7 @@ LIST_DENIED_MESSAGES = {
     'new': ugettext_lazy("You have to sign in to see list of threads you haven't read."),
     'unread': ugettext_lazy("You have to sign in to see list of threads with new replies."),
     'subscribed': ugettext_lazy("You have to sign in to see list of threads you are subscribing."),
-    'unapproved': ugettext_lazy(
-        "You have to sign in to see list of threads with unapproved posts."
-    ),
+    'unapproved': ugettext_lazy("You have to sign in to see list of threads with unapproved posts."),
 }
 
 

+ 1 - 1
misago/users/djangoadmin.py

@@ -82,7 +82,7 @@ class UserAdmin(admin.ModelAdmin):
             {
                 'fields': (
                     'username', 'email', 'rank', 'last_login', 'joined_on', 'is_staff',
-                'is_superuser', 'edit_from_misago_link',
+                    'is_superuser', 'edit_from_misago_link',
                 )
             },
         ],

+ 2 - 6
misago/users/migrations/0002_users_settings.py

@@ -186,9 +186,7 @@ def create_users_settings_group(apps, schema_editor):
         apps, {
             'key': 'captcha',
             'name': _("CAPTCHA"),
-            'description': _(
-                "Those settings allow you to combat automatic registrations on your forum."
-            ),
+            'description': _("Those settings allow you to combat automatic registrations on your forum."),
             'settings': [
                 {
                     'setting': 'captcha_type',
@@ -247,9 +245,7 @@ def create_users_settings_group(apps, schema_editor):
                 {
                     'setting': 'qa_answers',
                     'name': _("Valid answers"),
-                    'description': _(
-                        "Enter each answer in new line. Answers are case-insensitive."
-                    ),
+                    'description': _("Enter each answer in new line. Answers are case-insensitive."),
                     'value': '',
                     'form_field': 'textarea',
                     'field_extra': {