1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +02:00

Merge pull request #1912 from Ryooooooga/feature/edit-hunk

This commit is contained in:
Jesse Duffield 2022-05-07 11:01:26 +10:00 committed by GitHub
commit 9956a5bfb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 83 additions and 3 deletions

View File

@ -192,6 +192,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
<kbd>v</kbd>: toggle drag select
<kbd>V</kbd>: toggle drag select
<kbd>a</kbd>: toggle select hunk
<kbd>E</kbd>: edit hunk
</pre>
## Reflog

View File

@ -191,6 +191,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
<kbd>f</kbd>: fast-forward this branch from its upstream
<kbd>g</kbd>: view reset options
<kbd>R</kbd>: ブランチ名を変更
<kbd>u</kbd>: set/unset upstream
<kbd>enter</kbd>: コミットを閲覧
<kbd>f</kbd>: リモートをfetch
<kbd>n</kbd>: リモートを新規追加
@ -254,6 +255,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
<kbd>v</kbd>: 範囲選択を切り替え
<kbd>V</kbd>: 範囲選択を切り替え
<kbd>a</kbd>: hunk選択を切り替え
<kbd>E</kbd>: edit hunk
</pre>
## リモートブランチ

View File

@ -230,6 +230,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
<kbd>v</kbd>: toggle drag selecteer
<kbd>V</kbd>: toggle drag selecteer
<kbd>a</kbd>: toggle selecteer hunk
<kbd>E</kbd>: edit hunk
</pre>
## Stash

View File

@ -169,6 +169,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
<kbd>v</kbd>: toggle drag select
<kbd>V</kbd>: toggle drag select
<kbd>a</kbd>: toggle select hunk
<kbd>E</kbd>: edit hunk
</pre>
## Reflog

View File

@ -239,6 +239,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
<kbd>v</kbd>: 切换拖动选择
<kbd>V</kbd>: 切换拖动选择
<kbd>a</kbd>: 切换选择区块
<kbd>E</kbd>: edit hunk
</pre>
## 正常

View File

@ -255,12 +255,15 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain
}
func (self *WorkingTreeCommands) ApplyPatch(patch string, flags ...string) error {
filepath := filepath.Join(self.os.GetTempDir(), utils.GetCurrentRepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch")
self.Log.Infof("saving temporary patch to %s", filepath)
if err := self.os.CreateFileWithContent(filepath, patch); err != nil {
filepath, err := self.SaveTemporaryPatch(patch)
if err != nil {
return err
}
return self.ApplyPatchFile(filepath, flags...)
}
func (self *WorkingTreeCommands) ApplyPatchFile(filepath string, flags ...string) error {
flagStr := ""
for _, flag := range flags {
flagStr += " --" + flag
@ -269,6 +272,15 @@ func (self *WorkingTreeCommands) ApplyPatch(patch string, flags ...string) error
return self.cmd.New(fmt.Sprintf("git apply%s %s", flagStr, self.cmd.Quote(filepath))).Run()
}
func (self *WorkingTreeCommands) SaveTemporaryPatch(patch string) (string, error) {
filepath := filepath.Join(self.os.GetTempDir(), utils.GetCurrentRepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch")
self.Log.Infof("saving temporary patch to %s", filepath)
if err := self.os.CreateFileWithContent(filepath, patch); err != nil {
return "", err
}
return filepath, nil
}
// ShowFileDiff get the diff of specified from and to. Typically this will be used for a single commit so it'll be 123abc^..123abc
// but when we're in diff mode it could be any 'from' to any 'to'. The reverse flag is also here thanks to diff mode.
func (self *WorkingTreeCommands) ShowFileDiff(from string, to string, reverse bool, fileName string, plain bool) (string, error) {

View File

@ -269,6 +269,7 @@ type KeybindingMainConfig struct {
ToggleDragSelectAlt string `yaml:"toggleDragSelect-alt"`
ToggleSelectHunk string `yaml:"toggleSelectHunk"`
PickBothHunks string `yaml:"pickBothHunks"`
EditSelectHunk string `yaml:"editSelectHunk"`
}
type KeybindingSubmodulesConfig struct {
@ -536,6 +537,7 @@ func GetDefaultConfig() *UserConfig {
ToggleDragSelectAlt: "V",
ToggleSelectHunk: "a",
PickBothHunks: "b",
EditSelectHunk: "E",
},
Submodules: KeybindingSubmodulesConfig{
Init: "i",

View File

@ -543,6 +543,13 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
Handler: self.handleToggleSelectHunk,
Description: self.c.Tr.ToggleSelectHunk,
},
{
ViewName: "main",
Contexts: []string{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)},

View File

@ -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
}

View File

@ -167,6 +167,7 @@ type TranslationSet struct {
ToggleDragSelect string
ToggleSelectHunk string
ToggleSelectionForPatch string
EditHunk string
TogglePanel string
ReturnToFilesPanel string
FastForward string
@ -777,6 +778,7 @@ func EnglishTranslationSet() TranslationSet {
ToggleDragSelect: `toggle drag select`,
ToggleSelectHunk: `toggle select hunk`,
ToggleSelectionForPatch: `add/remove line(s) to patch`,
EditHunk: `edit hunk`,
TogglePanel: `switch to other panel`,
ReturnToFilesPanel: `return to files panel`,
FastForward: `fast-forward this branch from its upstream`,