Detect indentation style (tabs vs spaces) and unit size from file lines. Args: lines: Lines from the matched file region Returns: Tuple of (style, unit_size) where: - style: 'tabs' or 'spaces' - unit_size: Number of spaces per indent level (or 1 for tab
(lines: List[str])
| 169 | |
| 170 | |
| 171 | def _detect_indent_style(lines: List[str]) -> tuple: |
| 172 | """ |
| 173 | Detect indentation style (tabs vs spaces) and unit size from file lines. |
| 174 | |
| 175 | Args: |
| 176 | lines: Lines from the matched file region |
| 177 | |
| 178 | Returns: |
| 179 | Tuple of (style, unit_size) where: |
| 180 | - style: 'tabs' or 'spaces' |
| 181 | - unit_size: Number of spaces per indent level (or 1 for tabs) |
| 182 | """ |
| 183 | indents = [] |
| 184 | |
| 185 | for line in lines: |
| 186 | if line and line.strip() and line[0] in " \t": |
| 187 | indent = _get_indentation(line) |
| 188 | if indent: |
| 189 | indents.append(indent) |
| 190 | |
| 191 | if not indents: |
| 192 | return ("spaces", 4) # Default fallback |
| 193 | |
| 194 | # Check if using tabs |
| 195 | if any("\t" in indent for indent in indents): |
| 196 | return ("tabs", 1) |
| 197 | |
| 198 | # Detect space unit size by finding common indent differences |
| 199 | indent_sizes = sorted(set(len(indent) for indent in indents)) |
| 200 | |
| 201 | if len(indent_sizes) >= 2: |
| 202 | # Calculate differences between consecutive indent levels |
| 203 | differences = [ |
| 204 | indent_sizes[i + 1] - indent_sizes[i] for i in range(len(indent_sizes) - 1) |
| 205 | ] |
| 206 | |
| 207 | # Use the most common difference as the unit |
| 208 | if differences: |
| 209 | from collections import Counter |
| 210 | |
| 211 | unit = Counter(differences).most_common(1)[0][0] |
| 212 | return ("spaces", unit if unit > 0 else 4) |
| 213 | |
| 214 | # Fallback: assume 4 spaces |
| 215 | return ("spaces", 4) |
| 216 | |
| 217 | |
| 218 | def _get_min_indentation(lines: List[str]) -> str: |
no test coverage detected