import sublime
import sublime_plugin


class ReformatArgs(sublime_plugin.TextCommand):
    def run(self, edit):
        # self.view.insert(edit, 0, "Hello, World!")
        sels = self.view.sel()

        for sel in sels:
            selected_str = self.view.substr(sel)

            if ',' not in selected_str:
                continue

            line = self.view.substr(self.view.line(sel))
            num_spaces = len(line) - len(line.lstrip())

            restringed = self.restring(selected_str, num_spaces)
            self.view.replace(edit, sel, restringed)
            # print(restringed)

    def restring(self, text, num_spaces):
        spaces = num_spaces * ' '
        padding = 4 * ' ' + spaces

        depth = 0
        out_str = "\n" + padding
        prev_char = ''
        for char_num, character in enumerate(text):
            chars_left = len(text) - 1 - char_num

            # Depth is how many parenthizations deep we are
            if character in '([{':
                depth += 1

            if character in '}])':
                depth -= 1

            # Scrub spaces if we're at the base depth
            if ((prev_char != ',') or (character != ' ')) or depth > 0:
                out_str += character
                prev_char = character

            # : If there's a comma
            # : And we're not inside of a parenthization
            # : And this isn't a comma at the end of the string
            # --> Then add a newline
            if character == ',' and depth == 0 and chars_left > 0:
                out_str += '\n' + padding

        out_str += '\n' + spaces
        return out_str

9f8b936dbadcac36d6198411ec9f69630f0bcfd0
<mask token>


class ReformatArgs(sublime_plugin.TextCommand):
    <mask token>
    <mask token>

<mask token>


class ReformatArgs(sublime_plugin.TextCommand):

    def run(self, edit):
        sels = self.view.sel()
        for sel in sels:
            selected_str = self.view.substr(sel)
            if ',' not in selected_str:
                continue
            line = self.view.substr(self.view.line(sel))
            num_spaces = len(line) - len(line.lstrip())
            restringed = self.restring(selected_str, num_spaces)
            self.view.replace(edit, sel, restringed)
    <mask token>

<mask token>


class ReformatArgs(sublime_plugin.TextCommand):

    def run(self, edit):
        sels = self.view.sel()
        for sel in sels:
            selected_str = self.view.substr(sel)
            if ',' not in selected_str:
                continue
            line = self.view.substr(self.view.line(sel))
            num_spaces = len(line) - len(line.lstrip())
            restringed = self.restring(selected_str, num_spaces)
            self.view.replace(edit, sel, restringed)

    def restring(self, text, num_spaces):
        spaces = num_spaces * ' '
        padding = 4 * ' ' + spaces
        depth = 0
        out_str = '\n' + padding
        prev_char = ''
        for char_num, character in enumerate(text):
            chars_left = len(text) - 1 - char_num
            if character in '([{':
                depth += 1
            if character in '}])':
                depth -= 1
            if (prev_char != ',' or character != ' ') or depth > 0:
                out_str += character
                prev_char = character
            if character == ',' and depth == 0 and chars_left > 0:
                out_str += '\n' + padding
        out_str += '\n' + spaces
        return out_str

import sublime
import sublime_plugin


class ReformatArgs(sublime_plugin.TextCommand):

    def run(self, edit):
        sels = self.view.sel()
        for sel in sels:
            selected_str = self.view.substr(sel)
            if ',' not in selected_str:
                continue
            line = self.view.substr(self.view.line(sel))
            num_spaces = len(line) - len(line.lstrip())
            restringed = self.restring(selected_str, num_spaces)
            self.view.replace(edit, sel, restringed)

    def restring(self, text, num_spaces):
        spaces = num_spaces * ' '
        padding = 4 * ' ' + spaces
        depth = 0
        out_str = '\n' + padding
        prev_char = ''
        for char_num, character in enumerate(text):
            chars_left = len(text) - 1 - char_num
            if character in '([{':
                depth += 1
            if character in '}])':
                depth -= 1
            if (prev_char != ',' or character != ' ') or depth > 0:
                out_str += character
                prev_char = character
            if character == ',' and depth == 0 and chars_left > 0:
                out_str += '\n' + padding
        out_str += '\n' + spaces
        return out_str

import sublime
import sublime_plugin


class ReformatArgs(sublime_plugin.TextCommand):
    def run(self, edit):
        # self.view.insert(edit, 0, "Hello, World!")
        sels = self.view.sel()

        for sel in sels:
            selected_str = self.view.substr(sel)

            if ',' not in selected_str:
                continue

            line = self.view.substr(self.view.line(sel))
            num_spaces = len(line) - len(line.lstrip())

            restringed = self.restring(selected_str, num_spaces)
            self.view.replace(edit, sel, restringed)
            # print(restringed)

    def restring(self, text, num_spaces):
        spaces = num_spaces * ' '
        padding = 4 * ' ' + spaces

        depth = 0
        out_str = "\n" + padding
        prev_char = ''
        for char_num, character in enumerate(text):
            chars_left = len(text) - 1 - char_num

            # Depth is how many parenthizations deep we are
            if character in '([{':
                depth += 1

            if character in '}])':
                depth -= 1

            # Scrub spaces if we're at the base depth
            if ((prev_char != ',') or (character != ' ')) or depth > 0:
                out_str += character
                prev_char = character

            # : If there's a comma
            # : And we're not inside of a parenthization
            # : And this isn't a comma at the end of the string
            # --> Then add a newline
            if character == ',' and depth == 0 and chars_left > 0:
                out_str += '\n' + padding

        out_str += '\n' + spaces
        return out_str
