Skip to content

Commit 4aa35d9

Browse files
committed
when comparing two selections, ignore indent. I might add this to other diff commands in the future
1 parent 5d745a2 commit 4aa35d9

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

file_diffs.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# coding: utf8
2+
import os
3+
import re
24

35
import sublime
46
import sublime_plugin
57
import difflib
68

7-
import os
89
from fnmatch import fnmatch
910
import codecs
1011

@@ -119,8 +120,41 @@ def run(self, edit, **kwargs):
119120
regions = self.view.sel()
120121
current = self.view.substr(regions[0])
121122
diff = self.view.substr(regions[1])
122-
diff = self.run_diff(current, diff)
123-
self.show_diff(diff)
123+
124+
# trim off indent
125+
indent = None
126+
for line in current.splitlines():
127+
new_indent = re.match('[ \t]*', line).group(0)
128+
if new_indent == '':
129+
continue
130+
131+
if indent is None:
132+
indent = new_indent
133+
elif len(new_indent) < len(indent):
134+
indent = new_indent
135+
136+
if not indent:
137+
break
138+
139+
if indent:
140+
current = u"\n".join(line[len(indent):] for line in current.splitlines())
141+
142+
# trim off indent
143+
indent = None
144+
for line in diff.splitlines():
145+
new_indent = re.match('[ \t]*', line).group(0)
146+
if new_indent == '':
147+
continue
148+
149+
if indent is None:
150+
indent = new_indent
151+
elif len(new_indent) < len(indent):
152+
indent = new_indent
153+
154+
if indent:
155+
diff = u"\n".join(line[len(indent):] for line in diff.splitlines())
156+
157+
self.show_diff(self.run_diff(current, diff))
124158

125159

126160
class FileDiffSavedCommand(FileDiffCommand):

0 commit comments

Comments
 (0)