Skip to content

Commit b6c5724

Browse files
committed
GitAddSelectedHunkCommand: add ability to edit patch
1 parent bbfabfd commit b6c5724

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Default.sublime-commands

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@
261261
"caption": "Git: Add Selected Hunk",
262262
"command": "git_add_selected_hunk"
263263
}
264+
,{
265+
"caption": "Git: Add Selected Hunk (Edit)",
266+
"command": "git_add_selected_hunk", "args": { "edit_patch": "True" }
267+
}
264268
,{
265269
"caption": "Git: Commit Selected Hunk",
266270
"command": "git_commit_selected_hunk"

Main.sublime-menu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
,{ "caption": "-" }
2121
,{ "caption": "Add", "command": "git_raw", "args": { "command": "git add", "append_current_file": true } }
2222
,{ "caption": "Add Selected Hunk", "command": "git_add_selected_hunk" }
23+
,{ "caption": "Add Selected Hunk (Edit)", "command": "git_add_selected_hunk", "args": { "edit_patch": "True" } }
2324
,{ "caption": "-" }
2425
,{ "caption": "Move/Rename...", "command": "git_mv"}
2526
,{ "caption": "Remove/Delete", "command": "git_raw", "args": { "command": "git rm", "append_current_file": true } }

git/add.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ def rerun(self, result):
4545

4646

4747
class GitAddSelectedHunkCommand(GitTextCommand):
48-
def run(self, edit):
49-
self.run_command(['git', 'diff', '--no-color', '-U1', self.get_file_name()], self.cull_diff)
48+
def run(self, edit, edit_patch=False):
49+
self.run_command(['git', 'diff', '--no-color', '-U1', self.get_file_name()], lambda result: self.cull_diff(result, edit_patch))
5050

51-
def cull_diff(self, result):
51+
def cull_diff(self, result, edit_patch=False):
5252
selection = []
5353
for sel in self.view.sel():
5454
selection.append({
@@ -85,10 +85,22 @@ def cull_diff(self, result):
8585
selection_is_hunky = True
8686

8787
if selection_is_hunky:
88-
self.run_command(['git', 'apply', '--cached'], stdin=diffs)
88+
if edit_patch: # open an input panel to modify the patch
89+
patch_view = self.get_window().show_input_panel(
90+
"Message", diffs,
91+
lambda edited_patch: self.on_input(edited_patch), None, None
92+
)
93+
s = sublime.load_settings("Git.sublime-settings")
94+
syntax = s.get("diff_syntax", "Packages/Diff/Diff.tmLanguage")
95+
patch_view.set_syntax_file(syntax)
96+
patch_view.settings().set('word_wrap', False)
97+
else:
98+
self.on_input(diffs)
8999
else:
90100
sublime.status_message("No selected hunk")
91101

102+
def on_input(self, patch):
103+
self.run_command(['git', 'apply', '--cached'], stdin=patch)
92104

93105
# Also, sometimes we want to undo adds
94106

0 commit comments

Comments
 (0)