|
| 1 | +""" |
| 2 | +Extracted from: https://github.com/mofosyne/sexp_prettify |
| 3 | +""" |
| 4 | + |
| 5 | +def sexp_prettify(source, |
| 6 | + compact_save=False, |
| 7 | + indent_char='\t', |
| 8 | + indent_size=1, |
| 9 | + consecutive_token_wrap_threshold=72, |
| 10 | + compact_list_prefixes={"pts"}, |
| 11 | + compact_list_column_limit=99, |
| 12 | + shortform_prefixes={"font", "stroke", "fill", "offset", "rotate", "scale", "teardrop"}): |
| 13 | + """ |
| 14 | + Reformats KiCad-like S-expressions to match a specific formatting style. |
| 15 | +
|
| 16 | + Args: |
| 17 | + source (str): The source S-expression string. |
| 18 | + compact_save (bool): If True, enables compact mode formatting. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + str: The formatted S-expression. |
| 22 | + """ |
| 23 | + |
| 24 | + # State tracking |
| 25 | + formatted = [] |
| 26 | + list_depth = 0 |
| 27 | + column = 0 |
| 28 | + previous_non_space_output = '' |
| 29 | + in_quote = False |
| 30 | + escape_next_char = False |
| 31 | + singular_element = False |
| 32 | + space_pending = False |
| 33 | + wrapped_list = False |
| 34 | + scanning_for_prefix = False |
| 35 | + prefix_token = '' |
| 36 | + compact_list_mode = False |
| 37 | + compact_list_indent = 0 |
| 38 | + shortform_mode = False |
| 39 | + shortform_indent = 0 |
| 40 | + |
| 41 | + for c in source: |
| 42 | + |
| 43 | + # Parse quoted strings |
| 44 | + if c == '"' or in_quote: |
| 45 | + if space_pending: |
| 46 | + formatted.append(' ') |
| 47 | + column += 1 |
| 48 | + space_pending = False |
| 49 | + |
| 50 | + if escape_next_char: |
| 51 | + escape_next_char = False |
| 52 | + elif c == '\\': |
| 53 | + escape_next_char = True |
| 54 | + elif c == '"': |
| 55 | + in_quote = not in_quote |
| 56 | + |
| 57 | + formatted.append(c) |
| 58 | + column += 1 |
| 59 | + previous_non_space_output = c |
| 60 | + continue |
| 61 | + |
| 62 | + # Parse spaces and newlines |
| 63 | + if c.isspace(): |
| 64 | + space_pending = True |
| 65 | + |
| 66 | + if scanning_for_prefix: |
| 67 | + if prefix_token in compact_list_prefixes: |
| 68 | + compact_list_mode = True |
| 69 | + compact_list_indent = list_depth |
| 70 | + elif compact_save and prefix_token in shortform_prefixes: |
| 71 | + shortform_mode = True |
| 72 | + shortform_indent = list_depth |
| 73 | + |
| 74 | + scanning_for_prefix = False |
| 75 | + continue |
| 76 | + |
| 77 | + # Parse opening parentheses |
| 78 | + if c == '(': |
| 79 | + space_pending = False |
| 80 | + if compact_list_mode: |
| 81 | + # In fixed listDepth, visually compact mode |
| 82 | + if column < compact_list_column_limit and previous_non_space_output == ')' or compact_list_column_limit == 0: |
| 83 | + # Is a consecutive list and still within column limit (or column limit disabled) |
| 84 | + formatted.append(' ') |
| 85 | + column += 1 |
| 86 | + else: |
| 87 | + # List is either beyond column limit or not after another list move this list to the next line |
| 88 | + formatted.append('\n') |
| 89 | + formatted.append(indent_char * compact_list_indent * indent_size) |
| 90 | + column = compact_list_indent * indent_size |
| 91 | + elif shortform_mode: |
| 92 | + # In one liner mode |
| 93 | + formatted.append(' ') |
| 94 | + column += 1 |
| 95 | + else: |
| 96 | + # Start scanning for prefix for special list handling |
| 97 | + scanning_for_prefix = True |
| 98 | + prefix_token = '' |
| 99 | + if list_depth > 0: |
| 100 | + # Print next line depth |
| 101 | + formatted.append('\n') |
| 102 | + formatted.append(indent_char * list_depth * indent_size) |
| 103 | + column = list_depth * indent_size |
| 104 | + |
| 105 | + singular_element = True |
| 106 | + list_depth += 1 |
| 107 | + |
| 108 | + formatted.append('(') |
| 109 | + column += 1 |
| 110 | + |
| 111 | + previous_non_space_output = '(' |
| 112 | + continue |
| 113 | + |
| 114 | + # Parse closing parentheses |
| 115 | + if c == ')': |
| 116 | + current_shortform_mode = shortform_mode |
| 117 | + space_pending = False |
| 118 | + scanning_for_prefix = False |
| 119 | + |
| 120 | + if list_depth > 0: |
| 121 | + list_depth -= 1 |
| 122 | + |
| 123 | + if compact_list_mode and list_depth < compact_list_indent: |
| 124 | + compact_list_mode = False |
| 125 | + if shortform_mode and list_depth < shortform_indent: |
| 126 | + shortform_mode = False |
| 127 | + |
| 128 | + if wrapped_list: |
| 129 | + # This was a list with wrapped tokens so is already indented |
| 130 | + formatted.append('\n') |
| 131 | + formatted.append(indent_char * list_depth * indent_size) |
| 132 | + column = list_depth * indent_size |
| 133 | + if singular_element: |
| 134 | + singular_element = False |
| 135 | + wrapped_list = False |
| 136 | + else: |
| 137 | + # Normal List |
| 138 | + if singular_element: |
| 139 | + singular_element = False |
| 140 | + elif not current_shortform_mode: |
| 141 | + formatted.append('\n') |
| 142 | + formatted.append(indent_char * list_depth * indent_size) |
| 143 | + column = list_depth * indent_size |
| 144 | + |
| 145 | + formatted.append(')') |
| 146 | + column += 1 |
| 147 | + |
| 148 | + if list_depth <= 0: |
| 149 | + formatted.append('\n') |
| 150 | + column = 0 |
| 151 | + |
| 152 | + previous_non_space_output = ')' |
| 153 | + continue |
| 154 | + |
| 155 | + # Parse characters |
| 156 | + if c != '\0': |
| 157 | + if previous_non_space_output == ')' and not shortform_mode: |
| 158 | + # Is Bare token after a list that should be on next line |
| 159 | + # Dev Note: In KiCAD this may indicate a flag bug |
| 160 | + formatted.append('\n') |
| 161 | + formatted.append(indent_char * list_depth * indent_size) |
| 162 | + column = list_depth * indent_size |
| 163 | + space_pending = False |
| 164 | + elif space_pending and not shortform_mode and not compact_list_mode and column >= consecutive_token_wrap_threshold: |
| 165 | + # Token is above wrap threshold. Move token to next line (If token wrap threshold is zero then this feature is disabled) |
| 166 | + wrapped_list = True |
| 167 | + formatted.append('\n') |
| 168 | + formatted.append(indent_char * list_depth * indent_size) |
| 169 | + column = list_depth * indent_size |
| 170 | + space_pending = False |
| 171 | + elif space_pending and previous_non_space_output != '(': |
| 172 | + formatted.append(' ') |
| 173 | + column += 1 |
| 174 | + space_pending = False |
| 175 | + |
| 176 | + # Add to prefix scanning buffer if scanning for special list handling detection |
| 177 | + if scanning_for_prefix: |
| 178 | + prefix_token += c |
| 179 | + |
| 180 | + # Add character to list |
| 181 | + formatted.append(c) |
| 182 | + column += 1 |
| 183 | + previous_non_space_output = c |
| 184 | + continue |
| 185 | + |
| 186 | + return ''.join(formatted) |
0 commit comments