Skip to content

Commit

Permalink
Use parse_diff_in_view fns instead of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste committed Jul 1, 2019
1 parent e5bbc2a commit b60c4c3
Showing 1 changed file with 4 additions and 39 deletions.
43 changes: 4 additions & 39 deletions core/commands/diff.py
Expand Up @@ -510,25 +510,13 @@ def run(self, edit, reset=False):

# Filter out any cursors that are larger than a single point.
cursor_pts = tuple(cursor.a for cursor in self.view.sel() if cursor.a == cursor.b)
diff = parse_diff_in_view(self.view)

self.header_starts = tuple(region.a for region in self.view.find_all("^diff"))
self.header_ends = tuple(region.b for region in self.view.find_all(r"^\+\+\+.+\n(?=@@)"))
self.hunk_starts = tuple(region.a for region in self.view.find_all("^@@"))
self.hunk_ends = sorted(list(
# Hunks end when the next diff starts.
set(self.header_starts[1:]) |
# Hunks end when the next hunk starts, except for hunks
# immediately following diff headers.
(set(self.hunk_starts) - set(self.header_ends)) |
# The last hunk ends at the end of the file.
# It should include the last line (`+ 1`).
set((self.view.size() + 1, ))
))

extract = partial(extract_content, self.view)
flatten = chain.from_iterable

patches = unique(flatten(filter_(self.head_and_hunk_for_pt(pt) for pt in cursor_pts)))
patch = ''.join(patches)
patches = unique(flatten(filter_(head_and_hunk_for_pt(diff, pt) for pt in cursor_pts)))
patch = ''.join(map(extract, patches))

if patch:
self.apply_patch(patch, cursor_pts, reset)
Expand Down Expand Up @@ -576,29 +564,6 @@ def apply_patch(self, patch, pts, reset):

self.view.run_command("gs_diff_refresh")

def head_and_hunk_for_pt(self, pt):
"""
Given a cursor position, find and return the diff header and the
diff for the selected hunk/file.
"""

for hunk_start, hunk_end in zip(self.hunk_starts, self.hunk_ends):
if hunk_start <= pt < hunk_end:
break
else:
return None

header_start, header_end = max(
(header_start, header_end)
for header_start, header_end in zip(self.header_starts, self.header_ends)
if (header_start, header_end) < (hunk_start, hunk_end)
)

header = self.view.substr(sublime.Region(header_start, header_end))
diff = self.view.substr(sublime.Region(hunk_start, hunk_end))

return header, diff


class GsDiffOpenFileAtHunkCommand(TextCommand, GitCommand):

Expand Down

0 comments on commit b60c4c3

Please sign in to comment.