MCPcopy
hub / github.com/jesseduffield/lazygit / NewState

Function NewState

pkg/gui/patch_exploring/state.go:48–125  ·  view source on GitHub ↗
(diff string, selectedLineIdx int, view *gocui.View, oldState *State, useHunkModeByDefault bool)

Source from the content-addressed store, hash-verified

46)
47
48func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *State, useHunkModeByDefault bool) *State {
49 if oldState != nil && diff == oldState.diff && selectedLineIdx == -1 {
50 // if we're here then we can return the old state. If selectedLineIdx was not -1
51 // then that would mean we were trying to click and potentially drag a range, which
52 // is why in that case we continue below
53 return oldState
54 }
55
56 patch := patch.Parse(diff)
57
58 if !patch.ContainsChanges() {
59 return nil
60 }
61
62 viewLineIndices, patchLineIndices := wrapPatchLines(diff, view)
63
64 rangeStartLineIdx := 0
65 if oldState != nil {
66 rangeStartLineIdx = oldState.rangeStartLineIdx
67 }
68
69 selectMode := LINE
70 if useHunkModeByDefault && !patch.IsSingleHunkForWholeFile() {
71 selectMode = HUNK
72 }
73
74 userEnabledHunkMode := false
75 if oldState != nil {
76 userEnabledHunkMode = oldState.userEnabledHunkMode
77 }
78
79 // if we have clicked from the outside to focus the main view we'll pass in a non-negative line index so that we can instantly select that line
80 if selectedLineIdx >= 0 {
81 // Clamp to the number of wrapped view lines; index might be out of
82 // bounds if a custom pager is being used which produces more lines
83 selectedLineIdx = min(selectedLineIdx, len(viewLineIndices)-1)
84
85 selectMode = RANGE
86 rangeStartLineIdx = selectedLineIdx
87 } else if oldState != nil {
88 // if we previously had a selectMode of RANGE, we want that to now be line again (or hunk, if that's the default)
89 if oldState.selectMode != RANGE {
90 selectMode = oldState.selectMode
91 }
92 oldPatchLineIdx := oldState.patchLineIndices[oldState.selectedLineIdx]
93 newPatchLineIdx := patch.GetNextChangeIdx(oldPatchLineIdx)
94 // When staging an addition from a consecutive changes block, the unselected deletions get
95 // reordered to appear before the remaining additions in the new diff. This can cause the
96 // cursor to land on a deletion at the same patch line index where the staged addition used
97 // to be. In that case, skip forward past any deletions, then call GetNextChangeIdx from the
98 // first non-deletion position, which correctly lands on the next meaningful change.
99 newLines := patch.Lines()
100 if newPatchLineIdx == oldPatchLineIdx &&
101 oldState.patch.Lines()[oldPatchLineIdx].IsAddition() &&
102 newLines[newPatchLineIdx].IsDeletion() &&
103 patch.HunkOldStartForLine(newPatchLineIdx) == oldState.patch.HunkOldStartForLine(oldPatchLineIdx) {
104 for newPatchLineIdx < len(newLines) && newLines[newPatchLineIdx].IsDeletion() {
105 newPatchLineIdx++

Callers 2

RefreshStagingPanelMethod · 0.92

Calls 9

ParseFunction · 0.92
wrapPatchLinesFunction · 0.85
ContainsChangesMethod · 0.80
GetNextChangeIdxMethod · 0.80
IsAdditionMethod · 0.80
IsDeletionMethod · 0.80
HunkOldStartForLineMethod · 0.80
LinesMethod · 0.45

Tested by

no test coverage detected