Skip to content

Commit 2828fb9

Browse files
committed
Rewrap patch when view width changes
This makes it so that when the staging view is resized, we keep the same patch line selected (as opposed to the same view line, which may correspond to a different patch line after resizing). It doesn't seem like a terribly important feature for resizing the window, but it is essential when initially entering the staging view: we select the first line of the first hunk in this case, but we do that before layout runs. At layout time the view is then split into unstaged/staged changes, and if this split is horizontal, the view gets narrower and may be wrapped in a different way. With this commit we ensure that the first line of the first hunk is still selected after that.
1 parent 5213a9d commit 2828fb9

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

pkg/gui/context/patch_explorer_context.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ func NewPatchExplorerContext(
3939
mutex: &deadlock.Mutex{},
4040
getIncludedLineIndices: getIncludedLineIndices,
4141
SimpleContext: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
42-
View: view,
43-
WindowName: windowName,
44-
Key: key,
45-
Kind: types.MAIN_CONTEXT,
46-
Focusable: true,
47-
HighlightOnFocus: true,
42+
View: view,
43+
WindowName: windowName,
44+
Key: key,
45+
Kind: types.MAIN_CONTEXT,
46+
Focusable: true,
47+
HighlightOnFocus: true,
48+
NeedsRerenderOnWidthChange: types.NEEDS_RERENDER_ON_WIDTH_CHANGE_WHEN_WIDTH_CHANGES,
4849
})),
4950
SearchTrait: NewSearchTrait(c),
5051
}
@@ -58,6 +59,8 @@ func NewPatchExplorerContext(
5859
}),
5960
)
6061

62+
ctx.SetHandleRenderFunc(ctx.OnViewWidthChanged)
63+
6164
return ctx
6265
}
6366

@@ -140,3 +143,11 @@ func (self *PatchExplorerContext) GetMutex() *deadlock.Mutex {
140143
func (self *PatchExplorerContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
141144
return nil
142145
}
146+
147+
func (self *PatchExplorerContext) OnViewWidthChanged() {
148+
if state := self.GetState(); state != nil {
149+
state.OnViewWidthChanged(self.GetView())
150+
self.setContent()
151+
self.RenderAndFocus()
152+
}
153+
}

pkg/gui/context/simple_context.go

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
type SimpleContext struct {
99
*BaseContext
10+
handleRenderFunc func()
1011
}
1112

1213
func NewSimpleContext(baseContext *BaseContext) *SimpleContext {
@@ -54,6 +55,13 @@ func (self *SimpleContext) HandleFocusLost(opts types.OnFocusLostOpts) {
5455
}
5556

5657
func (self *SimpleContext) HandleRender() {
58+
if self.handleRenderFunc != nil {
59+
self.handleRenderFunc()
60+
}
61+
}
62+
63+
func (self *SimpleContext) SetHandleRenderFunc(f func()) {
64+
self.handleRenderFunc = f
5765
}
5866

5967
func (self *SimpleContext) HandleRenderToMain() {

pkg/gui/patch_exploring/state.go

+17
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *Stat
9090
}
9191
}
9292

93+
func (s *State) OnViewWidthChanged(view *gocui.View) {
94+
if !view.Wrap {
95+
return
96+
}
97+
98+
selectedPatchLineIdx := s.patchLineIndices[s.selectedLineIdx]
99+
var rangeStartPatchLineIdx int
100+
if s.selectMode == RANGE {
101+
rangeStartPatchLineIdx = s.patchLineIndices[s.rangeStartLineIdx]
102+
}
103+
s.viewLineIndices, s.patchLineIndices = wrapPatchLines(s.diff, view)
104+
s.selectedLineIdx = s.viewLineIndices[selectedPatchLineIdx]
105+
if s.selectMode == RANGE {
106+
s.rangeStartLineIdx = s.viewLineIndices[rangeStartPatchLineIdx]
107+
}
108+
}
109+
93110
func (s *State) GetSelectedPatchLineIdx() int {
94111
return s.patchLineIndices[s.selectedLineIdx]
95112
}

0 commit comments

Comments
 (0)