Skip to content

Commit

Permalink
Get rid of a lot of error return values (#3890)
Browse files Browse the repository at this point in the history
- **PR Description**

Change many functions in the gui package (and some in gocui) to no
longer return errors.

There might be more that could be changed in this way, but I feel these
are the main ones.

Fixes #3887.
  • Loading branch information
stefanhaller authored Sep 6, 2024
2 parents 753b16b + 064fae4 commit 4c6c915
Show file tree
Hide file tree
Showing 121 changed files with 1,167 additions and 712 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/integrii/flaggy v1.4.0
github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68
github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d
github.com/jesseduffield/gocui v0.3.1-0.20240824154427-0fc91d5098e4
github.com/jesseduffield/gocui v0.3.1-0.20240906064314-bfab49c720d7
github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10
github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5
github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e
Expand Down Expand Up @@ -75,8 +75,8 @@ require (
github.com/xanzy/ssh-agent v0.2.1 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68 h1:EQP2Tv8T
github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68/go.mod h1:+LLj9/WUPAP8LqCchs7P+7X0R98HiFujVFANdNaxhGk=
github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d h1:bO+OmbreIv91rCe8NmscRwhFSqkDJtzWCPV4Y+SQuXE=
github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d/go.mod h1:nGNEErzf+NRznT+N2SWqmHnDnF9aLgANB1CUNEan09o=
github.com/jesseduffield/gocui v0.3.1-0.20240824154427-0fc91d5098e4 h1:2su9wjacqT/WxvNrzzdvA6rBJa6n/yZ/jvaS1r60HfM=
github.com/jesseduffield/gocui v0.3.1-0.20240824154427-0fc91d5098e4/go.mod h1:XtEbqCbn45keRXEu+OMZkjN5gw6AEob59afsgHjokZ8=
github.com/jesseduffield/gocui v0.3.1-0.20240906064314-bfab49c720d7 h1:QeLCKRAt4T6sBg5tSrOc4OojCuAcPxUA+4vNMPY4aH4=
github.com/jesseduffield/gocui v0.3.1-0.20240906064314-bfab49c720d7/go.mod h1:XtEbqCbn45keRXEu+OMZkjN5gw6AEob59afsgHjokZ8=
github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 h1:jmpr7KpX2+2GRiE91zTgfq49QvgiqB0nbmlwZ8UnOx0=
github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10/go.mod h1:aA97kHeNA+sj2Hbki0pvLslmE4CbDyhBeSSTUUnOuVo=
github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5 h1:CDuQmfOjAtb1Gms6a1p5L2P8RhbLUq5t8aL7PiQd2uY=
Expand Down Expand Up @@ -475,14 +475,14 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -493,8 +493,8 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (self *BackgroundRoutineMgr) startBackgroundFetch() {
}
err := self.backgroundFetch()
if err != nil && strings.Contains(err.Error(), "exit status 128") && isNew {
_ = self.gui.c.Alert(self.gui.c.Tr.NoAutomaticGitFetchTitle, self.gui.c.Tr.NoAutomaticGitFetchBody)
self.gui.c.Alert(self.gui.c.Tr.NoAutomaticGitFetchTitle, self.gui.c.Tr.NoAutomaticGitFetchBody)
} else {
self.goEvery(time.Second*time.Duration(userConfig.Refresher.FetchInterval), self.gui.stopChan, func() error {
err := self.backgroundFetch()
Expand Down
55 changes: 20 additions & 35 deletions pkg/gui/context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gui

import (
"errors"
"sync"

"github.com/jesseduffield/lazygit/pkg/gui/context"
Expand Down Expand Up @@ -37,9 +36,9 @@ func NewContextMgr(

// use when you don't want to return to the original context upon
// hitting escape: you want to go that context's parent instead.
func (self *ContextMgr) Replace(c types.Context) error {
func (self *ContextMgr) Replace(c types.Context) {
if !c.IsFocusable() {
return nil
return
}

self.Lock()
Expand All @@ -51,14 +50,14 @@ func (self *ContextMgr) Replace(c types.Context) error {
self.ContextStack = append(self.ContextStack[0:len(self.ContextStack)-1], c)
}

defer self.Unlock()
self.Unlock()

return self.Activate(c, types.OnFocusOpts{})
self.Activate(c, types.OnFocusOpts{})
}

func (self *ContextMgr) Push(c types.Context, opts ...types.OnFocusOpts) error {
func (self *ContextMgr) Push(c types.Context, opts ...types.OnFocusOpts) {
if len(opts) > 1 {
return errors.New("cannot pass multiple opts to Push")
panic("cannot pass multiple opts to Push")
}

singleOpts := types.OnFocusOpts{}
Expand All @@ -68,22 +67,18 @@ func (self *ContextMgr) Push(c types.Context, opts ...types.OnFocusOpts) error {
}

if !c.IsFocusable() {
return nil
return
}

contextsToDeactivate, contextToActivate := self.pushToContextStack(c)

for _, contextToDeactivate := range contextsToDeactivate {
if err := self.deactivate(contextToDeactivate, types.OnFocusLostOpts{NewContextKey: c.GetKey()}); err != nil {
return err
}
self.deactivate(contextToDeactivate, types.OnFocusLostOpts{NewContextKey: c.GetKey()})
}

if contextToActivate == nil {
return nil
if contextToActivate != nil {
self.Activate(contextToActivate, singleOpts)
}

return self.Activate(contextToActivate, singleOpts)
}

// Adjusts the context stack based on the context that's being pushed and
Expand Down Expand Up @@ -144,13 +139,13 @@ func (self *ContextMgr) pushToContextStack(c types.Context) ([]types.Context, ty
return contextsToDeactivate, c
}

func (self *ContextMgr) Pop() error {
func (self *ContextMgr) Pop() {
self.Lock()

if len(self.ContextStack) == 1 {
// cannot escape from bottommost context
self.Unlock()
return nil
return
}

var currentContext types.Context
Expand All @@ -160,14 +155,12 @@ func (self *ContextMgr) Pop() error {

self.Unlock()

if err := self.deactivate(currentContext, types.OnFocusLostOpts{NewContextKey: newContext.GetKey()}); err != nil {
return err
}
self.deactivate(currentContext, types.OnFocusLostOpts{NewContextKey: newContext.GetKey()})

return self.Activate(newContext, types.OnFocusOpts{})
self.Activate(newContext, types.OnFocusOpts{})
}

func (self *ContextMgr) deactivate(c types.Context, opts types.OnFocusLostOpts) error {
func (self *ContextMgr) deactivate(c types.Context, opts types.OnFocusLostOpts) {
view, _ := self.gui.c.GocuiGui().View(c.GetViewName())

if opts.NewContextKey != context.SEARCH_CONTEXT_KEY {
Expand All @@ -183,18 +176,14 @@ func (self *ContextMgr) deactivate(c types.Context, opts types.OnFocusLostOpts)
view.Visible = false
}

if err := c.HandleFocusLost(opts); err != nil {
return err
}

return nil
c.HandleFocusLost(opts)
}

func (self *ContextMgr) Activate(c types.Context, opts types.OnFocusOpts) error {
func (self *ContextMgr) Activate(c types.Context, opts types.OnFocusOpts) {
viewName := c.GetViewName()
v, err := self.gui.c.GocuiGui().View(viewName)
if err != nil {
return err
panic(err)
}

self.gui.helpers.Window.SetWindowContext(c)
Expand All @@ -205,7 +194,7 @@ func (self *ContextMgr) Activate(c types.Context, opts types.OnFocusOpts) error
oldView.HighlightInactive = true
}
if _, err := self.gui.c.GocuiGui().SetCurrentView(viewName); err != nil {
return err
panic(err)
}

self.gui.helpers.Search.RenderSearchStatus(c)
Expand All @@ -219,11 +208,7 @@ func (self *ContextMgr) Activate(c types.Context, opts types.OnFocusOpts) error

self.gui.c.GocuiGui().Cursor = v.Editable

if err := c.HandleFocus(opts); err != nil {
return err
}

return nil
c.HandleFocus(opts)
}

func (self *ContextMgr) Current() types.Context {
Expand Down
10 changes: 5 additions & 5 deletions pkg/gui/context/base_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type BaseContext struct {
keybindingsFns []types.KeybindingsFn
mouseKeybindingsFns []types.MouseKeybindingsFn
onClickFn func() error
onRenderToMainFn func() error
onRenderToMainFn func()
onFocusFn onFocusFn
onFocusLostFn onFocusLostFn

Expand All @@ -31,8 +31,8 @@ type BaseContext struct {
}

type (
onFocusFn = func(types.OnFocusOpts) error
onFocusLostFn = func(types.OnFocusLostOpts) error
onFocusFn = func(types.OnFocusOpts)
onFocusLostFn = func(types.OnFocusLostOpts)
)

var _ types.IBaseContext = &BaseContext{}
Expand Down Expand Up @@ -148,13 +148,13 @@ func (self *BaseContext) GetOnClick() func() error {
return self.onClickFn
}

func (self *BaseContext) AddOnRenderToMainFn(fn func() error) {
func (self *BaseContext) AddOnRenderToMainFn(fn func()) {
if fn != nil {
self.onRenderToMainFn = fn
}
}

func (self *BaseContext) GetOnRenderToMain() func() error {
func (self *BaseContext) GetOnRenderToMain() func() {
return self.onRenderToMainFn
}

Expand Down
17 changes: 8 additions & 9 deletions pkg/gui/context/list_context_trait.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (self *ListContextTrait) FocusLine() {
} else if self.renderOnlyVisibleLines {
newOrigin, _ := self.GetViewTrait().ViewPortYBounds()
if oldOrigin != newOrigin {
return self.HandleRender()
self.HandleRender()
}
}
return nil
Expand All @@ -72,26 +72,26 @@ func formatListFooter(selectedLineIdx int, length int) string {
return fmt.Sprintf("%d of %d", selectedLineIdx+1, length)
}

func (self *ListContextTrait) HandleFocus(opts types.OnFocusOpts) error {
func (self *ListContextTrait) HandleFocus(opts types.OnFocusOpts) {
self.FocusLine()

self.GetViewTrait().SetHighlight(self.list.Len() > 0)

return self.Context.HandleFocus(opts)
self.Context.HandleFocus(opts)
}

func (self *ListContextTrait) HandleFocusLost(opts types.OnFocusLostOpts) error {
func (self *ListContextTrait) HandleFocusLost(opts types.OnFocusLostOpts) {
self.GetViewTrait().SetOriginX(0)

if self.refreshViewportOnChange {
self.refreshViewport()
}

return self.Context.HandleFocusLost(opts)
self.Context.HandleFocusLost(opts)
}

// OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view
func (self *ListContextTrait) HandleRender() error {
func (self *ListContextTrait) HandleRender() {
self.list.ClampSelection()
if self.renderOnlyVisibleLines {
// Rendering only the visible area can save a lot of cell memory for
Expand All @@ -110,13 +110,12 @@ func (self *ListContextTrait) HandleRender() error {
}
self.c.Render()
self.setFooter()

return nil
}

func (self *ListContextTrait) OnSearchSelect(selectedLineIdx int) error {
self.GetList().SetSelection(self.ViewIndexToModelIndex(selectedLineIdx))
return self.HandleFocus(types.OnFocusOpts{})
self.HandleFocus(types.OnFocusOpts{})
return nil
}

func (self *ListContextTrait) IsItemVisible(item types.HasUrn) bool {
Expand Down
4 changes: 1 addition & 3 deletions pkg/gui/context/menu_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error {
return nil
}

if err := self.c.Context().Pop(); err != nil {
return err
}
self.c.Context().Pop()

if selectedItem == nil {
return nil
Expand Down
6 changes: 2 additions & 4 deletions pkg/gui/context/merge_conflicts_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,11 @@ func (self *MergeConflictsContext) IsUserScrolling() bool {
return self.viewModel.userVerticalScrolling
}

func (self *MergeConflictsContext) RenderAndFocus() error {
func (self *MergeConflictsContext) RenderAndFocus() {
self.setContent()
self.FocusSelection()

self.c.Render()

return nil
}

func (self *MergeConflictsContext) Render() error {
Expand All @@ -99,7 +97,7 @@ func (self *MergeConflictsContext) setContent() {

func (self *MergeConflictsContext) FocusSelection() {
if !self.IsUserScrolling() {
_ = self.GetView().SetOriginY(self.GetOriginY())
self.GetView().SetOriginY(self.GetOriginY())
}

self.SetSelectedLineRange()
Expand Down
Loading

0 comments on commit 4c6c915

Please sign in to comment.