diff options
author | Ryooooooga <eial5q265e5@gmail.com> | 2022-05-04 21:20:45 +0900 |
---|---|---|
committer | Ryooooooga <eial5q265e5@gmail.com> | 2022-05-06 21:53:00 +0900 |
commit | d458e78d95132a894d0577a31b3af643f070b3e6 (patch) | |
tree | e04239847d4b029c7caed877b4e48f13d7ba8e76 /pkg/gui | |
parent | 0940e0182b7d9ecba1a5f222b77f0d678753e9d5 (diff) |
feat: add ability to edit hunk
Diffstat (limited to 'pkg/gui')
-rw-r--r-- | pkg/gui/keybindings.go | 7 | ||||
-rw-r--r-- | pkg/gui/staging_panel.go | 51 |
2 files changed, 58 insertions, 0 deletions
diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 772aa7b6e..a334d7f7a 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -544,6 +544,13 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi Description: self.c.Tr.ToggleSelectHunk, }, { + ViewName: "main", + Contexts: []string{string(context.MAIN_PATCH_BUILDING_CONTEXT_KEY), string(context.MAIN_STAGING_CONTEXT_KEY)}, + Key: opts.GetKey(opts.Config.Main.EditSelectHunk), + Handler: self.handleEditHunk, + Description: self.c.Tr.EditHunk, + }, + { ViewName: "main", Contexts: []string{string(context.MAIN_PATCH_BUILDING_CONTEXT_KEY), string(context.MAIN_STAGING_CONTEXT_KEY)}, Key: gocui.MouseLeft, diff --git a/pkg/gui/staging_panel.go b/pkg/gui/staging_panel.go index d17b1d2d9..01ccdb290 100644 --- a/pkg/gui/staging_panel.go +++ b/pkg/gui/staging_panel.go @@ -174,3 +174,54 @@ func (gui *Gui) HandleOpenFile() error { return gui.helpers.Files.OpenFile(file.GetPath()) } + +func (gui *Gui) handleEditHunk() error { + return gui.withLBLActiveCheck(func(state *LblPanelState) error { + return gui.editHunk(state.SecondaryFocused, state) + }) +} + +func (gui *Gui) editHunk(reverse bool, state *LblPanelState) error { + file := gui.getSelectedFile() + if file == nil { + return nil + } + + hunk := state.CurrentHunk() + patchText := patch.ModifiedPatchForRange(gui.Log, file.Name, state.GetDiff(), hunk.FirstLineIdx, hunk.LastLineIdx(), reverse, false) + patchFilepath, err := gui.git.WorkingTree.SaveTemporaryPatch(patchText) + if err != nil { + return err + } + + lineOffset := 3 + lineIdxInHunk := state.GetSelectedLineIdx() - hunk.FirstLineIdx + if err := gui.helpers.Files.EditFileAtLine(patchFilepath, lineIdxInHunk+lineOffset); err != nil { + return err + } + + editedPatchText, err := gui.git.File.Cat(patchFilepath) + if err != nil { + return err + } + + applyFlags := []string{} + if !reverse || state.SecondaryFocused { + applyFlags = append(applyFlags, "cached") + } + gui.c.LogAction(gui.c.Tr.Actions.ApplyPatch) + + lineCount := strings.Count(editedPatchText, "\n") + 1 + newPatchText := patch.ModifiedPatchForRange(gui.Log, file.Name, editedPatchText, 0, lineCount, false, false) + if err := gui.git.WorkingTree.ApplyPatch(newPatchText, applyFlags...); err != nil { + return gui.c.Error(err) + } + + if err := gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.FILES}}); err != nil { + return err + } + if err := gui.refreshStagingPanel(false, -1); err != nil { + return err + } + return nil +} |