diff options
Diffstat (limited to 'pkg/gui/staging_panel.go')
-rw-r--r-- | pkg/gui/staging_panel.go | 327 |
1 files changed, 220 insertions, 107 deletions
diff --git a/pkg/gui/staging_panel.go b/pkg/gui/staging_panel.go index a24becbd2..93e1d46c5 100644 --- a/pkg/gui/staging_panel.go +++ b/pkg/gui/staging_panel.go @@ -1,12 +1,21 @@ package gui import ( + "strings" + "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/git" - "github.com/jesseduffield/lazygit/pkg/utils" +) + +const ( + LINE = iota + RANGE + HUNK ) func (gui *Gui) refreshStagingPanel() error { + state := gui.State.Panels.Staging + file, err := gui.getSelectedFile(gui.g) if err != nil { if err != gui.Errors.ErrNoFiles { @@ -15,67 +24,109 @@ func (gui *Gui) refreshStagingPanel() error { return gui.handleStagingEscape(gui.g, nil) } - if !file.HasUnstagedChanges { + gui.State.SplitMainPanel = true + + indexFocused := false + if state != nil { + indexFocused = state.IndexFocused + } + + if !file.HasUnstagedChanges && !file.HasStagedChanges { return gui.handleStagingEscape(gui.g, nil) } - // note for custom diffs, we'll need to send a flag here saying not to use the custom diff - diff := gui.GitCommand.Diff(file, true) - colorDiff := gui.GitCommand.Diff(file, false) + if (indexFocused && !file.HasStagedChanges) || (!indexFocused && !file.HasUnstagedChanges) { + indexFocused = !indexFocused + } - if len(diff) < 2 { - return gui.handleStagingEscape(gui.g, nil) + getDiffs := func() (string, string) { + // note for custom diffs, we'll need to send a flag here saying not to use the custom diff + diff := gui.GitCommand.Diff(file, true, indexFocused) + secondaryColorDiff := gui.GitCommand.Diff(file, false, !indexFocused) + return diff, secondaryColorDiff } - // parse the diff and store the line numbers of hunks and stageable lines - // TODO: maybe instantiate this at application start - p, err := git.NewPatchParser(gui.Log) - if err != nil { - return nil + diff, secondaryColorDiff := getDiffs() + + // if we have e.g. a deleted file with nothing else to the diff will have only + // 4-5 lines in which case we'll swap panels + if len(strings.Split(diff, "\n")) < 5 { + if len(strings.Split(secondaryColorDiff, "\n")) < 5 { + return gui.handleStagingEscape(gui.g, nil) + } + indexFocused = !indexFocused + diff, secondaryColorDiff = getDiffs() } - hunkStarts, stageableLines, err := p.ParsePatch(diff) + + patchParser, err := git.NewPatchParser(gui.Log, diff) if err != nil { return nil } - var selectedLine int - if gui.State.Panels.Staging != nil { - end := len(stageableLines) - 1 - if end < gui.State.Panels.Staging.SelectedLine { - selectedLine = end + if len(patchParser.StageableLines) == 0 { + return gui.handleStagingEscape(gui.g, nil) + } + + var selectedLineIdx int + var firstLineIdx int + var lastLineIdx int + selectMode := LINE + if state != nil { + if state.SelectMode == HUNK { + // this is tricky: we need to find out which hunk we just staged based on our old `state.PatchParser` (as opposed to the new `patchParser`) + // we do this by getting the first line index of the original hunk, then + // finding the next stageable line, then getting its containing hunk + // in the new diff + selectMode = HUNK + prevNewHunk := state.PatchParser.GetHunkContainingLine(state.SelectedLineIdx, 0) + selectedLineIdx = patchParser.GetNextStageableLineIndex(prevNewHunk.FirstLineIdx) + newHunk := patchParser.GetHunkContainingLine(selectedLineIdx, 0) + firstLineIdx, lastLineIdx = newHunk.FirstLineIdx, newHunk.LastLineIdx } else { - selectedLine = gui.State.Panels.Staging.SelectedLine + selectedLineIdx = patchParser.GetNextStageableLineIndex(state.SelectedLineIdx) + firstLineIdx, lastLineIdx = selectedLineIdx, selectedLineIdx } } else { - selectedLine = 0 + selectedLineIdx = patchParser.StageableLines[0] + firstLineIdx, lastLineIdx = selectedLineIdx, selectedLineIdx } gui.State.Panels.Staging = &stagingPanelState{ - StageableLines: stageableLines, - HunkStarts: hunkStarts, - SelectedLine: selectedLine, - Diff: diff, + PatchParser: patchParser, + SelectedLineIdx: selectedLineIdx, + SelectMode: selectMode, + FirstLineIdx: firstLineIdx, + LastLineIdx: lastLineIdx, + Diff: diff, + IndexFocused: indexFocused, } - if len(stageableLines) == 0 { - return gui.createErrorPanel(gui.g, "No lines to stage") + if err := gui.refreshView(); err != nil { + return err } - if err := gui.focusLineAndHunk(); err != nil { + if err := gui.focusSelection(selectMode == HUNK); err != nil { return err } - mainView := gui.getMainView() - mainView.Highlight = true - mainView.Wrap = false + secondaryView := gui.getSecondaryView() + secondaryView.Highlight = true + secondaryView.Wrap = false gui.g.Update(func(*gocui.Gui) error { - return gui.setViewContent(gui.g, gui.getMainView(), colorDiff) + return gui.setViewContent(gui.g, gui.getSecondaryView(), secondaryColorDiff) }) return nil } +func (gui *Gui) handleTogglePanel(g *gocui.Gui, v *gocui.View) error { + state := gui.State.Panels.Staging + + state.IndexFocused = !state.IndexFocused + return gui.refreshStagingPanel() +} + func (gui *Gui) handleStagingEscape(g *gocui.Gui, v *gocui.View) error { gui.State.Panels.Staging = nil @@ -83,135 +134,166 @@ func (gui *Gui) handleStagingEscape(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) handleStagingPrevLine(g *gocui.Gui, v *gocui.View) error { - return gui.handleCycleLine(true) + return gui.handleCycleLine(-1) } func (gui *Gui) handleStagingNextLine(g *gocui.Gui, v *gocui.View) error { - return gui.handleCycleLine(false) + return gui.handleCycleLine(1) } func (gui *Gui) handleStagingPrevHunk(g *gocui.Gui, v *gocui.View) error { - return gui.handleCycleHunk(true) + return gui.handleCycleHunk(-1) } func (gui *Gui) handleStagingNextHunk(g *gocui.Gui, v *gocui.View) error { - return gui.handleCycleHunk(false) + return gui.handleCycleHunk(1) } -func (gui *Gui) handleCycleHunk(prev bool) error { +func (gui *Gui) handleCycleHunk(change int) error { state := gui.State.Panels.Staging - lineNumbers := state.StageableLines - currentLine := lineNumbers[state.SelectedLine] - currentHunkIndex := utils.PrevIndex(state.HunkStarts, currentLine) - var newHunkIndex int - if prev { - if currentHunkIndex == 0 { - newHunkIndex = len(state.HunkStarts) - 1 - } else { - newHunkIndex = currentHunkIndex - 1 - } + newHunk := state.PatchParser.GetHunkContainingLine(state.SelectedLineIdx, change) + state.SelectedLineIdx = state.PatchParser.GetNextStageableLineIndex(newHunk.FirstLineIdx) + if state.SelectMode == HUNK { + state.FirstLineIdx, state.LastLineIdx = newHunk.FirstLineIdx, newHunk.LastLineIdx } else { - if currentHunkIndex == len(state.HunkStarts)-1 { - newHunkIndex = 0 - } else { - newHunkIndex = currentHunkIndex + 1 - } + state.FirstLineIdx, state.LastLineIdx = state.SelectedLineIdx, state.SelectedLineIdx } - state.SelectedLine = utils.NextIndex(lineNumbers, state.HunkStarts[newHunkIndex]) + if err := gui.refreshView(); err != nil { + return err + } - return gui.focusLineAndHunk() + return gui.focusSelection(true) } -func (gui *Gui) handleCycleLine(prev bool) error { +func (gui *Gui) handleCycleLine(change int) error { state := gui.State.Panels.Staging - lineNumbers := state.StageableLines - currentLine := lineNumbers[state.SelectedLine] - var newIndex int - if prev { - newIndex = utils.PrevIndex(lineNumbers, currentLine) + + if state.SelectMode == HUNK { + return gui.handleCycleHunk(change) + } + + newSelectedLineIdx := state.SelectedLineIdx + change + if newSelectedLineIdx < 0 { + newSelectedLineIdx = 0 + } else if newSelectedLineIdx > len(state.PatchParser.PatchLines)-1 { + newSelectedLineIdx = len(state.PatchParser.PatchLines) - 1 + } + + state.SelectedLineIdx = newSelectedLineIdx + + if state.SelectMode == RANGE { + if state.SelectedLineIdx < state.FirstLineIdx { + state.FirstLineIdx = state.SelectedLineIdx + } else { + state.LastLineIdx = state.SelectedLineIdx + } } else { - newIndex = utils.NextIndex(lineNumbers, currentLine) + state.LastLineIdx = state.SelectedLineIdx + state.FirstLineIdx = state.SelectedLineIdx + } + + if err := gui.refreshView(); err != nil { + return err } - state.SelectedLine = newIndex - return gui.focusLineAndHunk() + return gui.focusSelection(false) } -// focusLineAndHunk works out the best focus for the staging panel given the +func (gui *Gui) refreshView() error { + state := gui.State.Panels.Staging + + colorDiff := state.PatchParser.Render(state.FirstLineIdx, state.LastLineIdx) + + mainView := gui.getMainView() + mainView.Highlight = true + mainView.Wrap = false + + gui.g.Update(func(*gocui.Gui) error { + return gui.setViewContent(gui.g, gui.getMainView(), colorDiff) + }) + + return nil +} + +// focusSelection works out the best focus for the staging panel given the // selected line and size of the hunk -func (gui *Gui) focusLineAndHunk() error { +func (gui *Gui) focusSelection(includeCurrentHunk bool) error { stagingView := gui.getMainView() state := gui.State.Panels.Staging - lineNumber := state.StageableLines[state.SelectedLine] - - // we want the bottom line of the view buffer to ideally be the bottom line - // of the hunk, but if the hunk is too big we'll just go three lines beyond - // the currently selected line so that the user can see the context - var bottomLine int - nextHunkStartIndex := utils.NextIndex(state.HunkStarts, lineNumber) - if nextHunkStartIndex == 0 { - // for now linesHeight is an efficient means of getting the number of lines - // in the patch. However if we introduce word wrap we'll need to update this - bottomLine = stagingView.LinesHeight() - 1 - } else { - bottomLine = state.HunkStarts[nextHunkStartIndex] - 1 - } + _, viewHeight := stagingView.Size() + bufferHeight := viewHeight - 1 + _, origin := stagingView.Origin() + + firstLineIdx := state.SelectedLineIdx + lastLineIdx := state.SelectedLineIdx - hunkStartIndex := utils.PrevIndex(state.HunkStarts, lineNumber) - hunkStart := state.HunkStarts[hunkStartIndex] - // if it's the first hunk we'll also show the diff header - if hunkStartIndex == 0 { - hunkStart = 0 + if includeCurrentHunk { + hunk := state.PatchParser.GetHunkContainingLine(state.SelectedLineIdx, 0) + firstLineIdx = hunk.FirstLineIdx + lastLineIdx = hunk.LastLineIdx } - _, height := stagingView.Size() - // if this hunk is too big, we will just ensure that the user can at least - // see three lines of context below the cursor - if bottomLine-hunkStart > height { - bottomLine = lineNumber + 3 + margin := 0 // we may want to have a margin in place to show context but right now I'm thinking we keep this at zero + + var newOrigin int + if firstLineIdx-origin < margin { + newOrigin = firstLineIdx - margin + } else if lastLineIdx-origin > bufferHeight-margin { + newOrigin = lastLineIdx - bufferHeight + margin + } else { + newOrigin = origin } - return gui.generalFocusLine(lineNumber, bottomLine, stagingView) + gui.g.Update(func(*gocui.Gui) error { + if err := stagingView.SetOrigin(0, newOrigin); err != nil { + return err + } + + return stagingView.SetCursor(0, state.SelectedLineIdx-newOrigin) + }) + + return nil } -func (gui *Gui) handleStageHunk(g *gocui.Gui, v *gocui.View) error { - return gui.handleStageLineOrHunk(true) +func (gui *Gui) handleStageSelection(g *gocui.Gui, v *gocui.View) error { + return gui.applySelection(false) } -func (gui *Gui) handleStageLine(g *gocui.Gui, v *gocui.View) error { - return gui.handleStageLineOrHunk(false) +func (gui *Gui) handleResetSelection(g *gocui.Gui, v *gocui.View) error { + return gui.applySelection(true) } -func (gui *Gui) handleStageLineOrHunk(hunk bool) error { +func (gui *Gui) applySelection(reverse bool) error { state := gui.State.Panels.Staging - p, err := git.NewPatchModifier(gui.Log) - if err != nil { - return err - } - currentLine := state.StageableLines[state.SelectedLine] - var patch string - if hunk { - patch, err = p.ModifyPatchForHunk(state.Diff, state.HunkStarts, currentLine) - } else { - patch, err = p.ModifyPatchForLine(state.Diff, currentLine) + if !reverse && state.IndexFocused { + return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("CantStageStaged")) } + + file, err := gui.getSelectedFile(gui.g) if err != nil { return err } - // for logging purposes - // ioutil.WriteFile("patch.diff", []byte(patch), 0600) + patch := git.ModifiedPatch(gui.Log, file.Name, state.Diff, state.FirstLineIdx, state.LastLineIdx, reverse) + + if patch == "" { + return nil + } // apply the patch then refresh this panel // create a new temp file with the patch, then call git apply with that patch - _, err = gui.GitCommand.ApplyPatch(patch) + _, err = gui.GitCommand.ApplyPatch(patch, false, !reverse || state.IndexFocused) if err != nil { return err } + if state.SelectMode == RANGE { + state.SelectMode = LINE + } + if err := gui.refreshFiles(); err != nil { return err } @@ -220,3 +302,34 @@ func (gui *Gui) handleStageLineOrHunk(hunk bool) error { } return nil } + +func (gui *Gui) handleToggleSelectRange(g *gocui.Gui, v *gocui.View) error { + state := gui.State.Panels.Staging + if state.SelectMode == RANGE { + state.SelectMode = LINE + } else { + state.SelectMode = RANGE + } + state.FirstLineIdx, state.LastLineIdx = state.SelectedLineIdx, state.SelectedLineIdx + + return gui.refreshView() +} + +func (gui *Gui) handleToggleSelectHunk(g *gocui.Gui, v *gocui.View) error { + state := gui.State.Panels.Staging + + if state.SelectMode == HUNK { + state.SelectMode = LINE + state.FirstLineIdx, state.LastLineIdx = state.SelectedLineIdx, state.SelectedLineIdx + } else { + state.SelectMode = HUNK + selectedHunk := state.PatchParser.GetHunkContainingLine(state.SelectedLineIdx, 0) + state.FirstLineIdx, state.LastLineIdx = selectedHunk.FirstLineIdx, selectedHunk.LastLineIdx + } + + if err := gui.refreshView(); err != nil { + return err + } + + return gui.focusSelection(state.SelectMode == HUNK) +} |