|
1 | | -import os, re, sublime, subprocess |
| 1 | +import difflib, os, re, sublime, subprocess |
2 | 2 | from . import cs_common, cs_parser |
3 | 3 |
|
4 | 4 | def format_string(view, text): |
@@ -30,14 +30,25 @@ def indent_lines(view, selections, edit): |
30 | 30 | for region in regions: |
31 | 31 | text = view.substr(region) |
32 | 32 | if text_formatted := format_string(view, text): |
33 | | - replacements.append((region, text_formatted)) |
34 | | - |
| 33 | + pos = region.begin() |
| 34 | + diff = difflib.ndiff(text.splitlines(keepends=True), text_formatted.splitlines(keepends=True)) |
| 35 | + for line in diff: |
| 36 | + if line[:2] == '- ': |
| 37 | + replacements.append((sublime.Region(pos, pos + len(line) - 2), '')) |
| 38 | + pos = pos + len(line) - 2 |
| 39 | + elif line[:2] == '+ ': |
| 40 | + replacements.append((sublime.Region(pos, pos), line[2:])) |
| 41 | + elif line[:2] == ' ': |
| 42 | + pos = pos + len(line) - 2 |
| 43 | + elif line[:2] == '? ': |
| 44 | + pass |
35 | 45 | if replacements: |
36 | 46 | selections = [(view.rowcol(r.a), view.rowcol(r.b)) for r in selections] |
37 | | - change_id_sel = view.change_id() |
| 47 | + delta = 0 |
38 | 48 | for region, string in replacements: |
39 | | - transformed_region = view.transform_region_from(region, change_id_sel) |
| 49 | + transformed_region = sublime.Region(region.a + delta, region.b + delta) |
40 | 50 | view.replace(edit, transformed_region, string) |
| 51 | + delta = delta - region.size() + len(string) |
41 | 52 |
|
42 | 53 | selections.clear() |
43 | 54 | for ((rowa, cola), (rowb, colb)) in selections: |
|
0 commit comments