diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-08-22 15:56:30 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-08-23 14:29:18 +1000 |
commit | 5874529f4310c6b4b0abdfcef714ff6f6cc8afd5 (patch) | |
tree | 3a225e081659c52cd548cace154f2f2c64f2894b /pkg/gui | |
parent | e290710f6741c046ee7e52e622af813e8955639b (diff) |
deal with the fact that a nil wrapped in an interface is not equal to nil
Diffstat (limited to 'pkg/gui')
-rw-r--r-- | pkg/gui/branches_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/cherry_picking.go | 23 | ||||
-rw-r--r-- | pkg/gui/context.go | 30 | ||||
-rw-r--r-- | pkg/gui/diffing.go | 10 | ||||
-rw-r--r-- | pkg/gui/global_handlers.go | 6 | ||||
-rw-r--r-- | pkg/gui/list_context.go | 82 |
6 files changed, 91 insertions, 64 deletions
diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 441f69b3f..2a87d364c 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -463,8 +463,8 @@ func (gui *Gui) currentBranch() *commands.Branch { func (gui *Gui) handleNewBranchOffCurrentItem() error { context := gui.currentSideContext() - item := context.GetSelectedItem() - if item == nil { + item, ok := context.GetSelectedItem() + if !ok { return nil } diff --git a/pkg/gui/cherry_picking.go b/pkg/gui/cherry_picking.go index e683d0f3f..1165a43a1 100644 --- a/pkg/gui/cherry_picking.go +++ b/pkg/gui/cherry_picking.go @@ -35,11 +35,12 @@ func (gui *Gui) handleCopyCommit() error { return err } - commit, ok := context.SelectedItem().(*commands.Commit) + item, ok := context.SelectedItem() if !ok { - gui.Log.Error("type cast failed for handling copy commit") + return nil } - if commit == nil { + commit, ok := item.(*commands.Commit) + if !ok { return nil } @@ -114,26 +115,24 @@ func (gui *Gui) handleCopyCommitRange() error { return err } - commit, ok := context.SelectedItem().(*commands.Commit) - if !ok { - gui.Log.Error("type cast failed for handling copy commit") - } - if commit == nil { + commitShaMap := gui.cherryPickedCommitShaMap() + commitsList := gui.commitsListForContext() + selectedLineIdx := context.GetPanelState().GetSelectedLineIdx() + + if selectedLineIdx > len(commitsList)-1 { return nil } - commitShaMap := gui.cherryPickedCommitShaMap() - // find the last commit that is copied that's above our position // if there are none, startIndex = 0 startIndex := 0 - for index, commit := range gui.commitsListForContext()[0:context.GetPanelState().GetSelectedLineIdx()] { + for index, commit := range commitsList[0:selectedLineIdx] { if commitShaMap[commit.Sha] { startIndex = index } } - for index := startIndex; index <= context.GetPanelState().GetSelectedLineIdx(); index++ { + for index := startIndex; index <= selectedLineIdx; index++ { gui.addCommitToCherryPickedCommits(index) } diff --git a/pkg/gui/context.go b/pkg/gui/context.go index e3a90871b..77527f466 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -45,8 +45,6 @@ type Context interface { GetWindowName() string SetWindowName(string) GetKey() string - GetSelectedItemId() string - GetSelectedItem() ListItem SetParentContext(Context) GetParentContext() Context } @@ -64,10 +62,6 @@ func (c BasicContext) SetWindowName(windowName string) { panic("can't set window name on basic context") } -func (c BasicContext) GetSelectedItem() ListItem { - return nil -} - func (c BasicContext) GetWindowName() string { // TODO: fix this up return c.GetViewName() @@ -81,11 +75,6 @@ func (c BasicContext) GetParentContext() Context { panic("can't get parent context on basic context") } -// TODO: think about whether we need this on the Context interface or if it should just be on the ListContext struct -func (c BasicContext) GetSelectedItemId() string { - return "" -} - func (c BasicContext) HandleRender() error { if c.OnRender != nil { return c.OnRender() @@ -534,7 +523,12 @@ func (gui *Gui) currentSideContext() *ListContext { context := stack[len(stack)-1-i] if context.GetKind() == SIDE_CONTEXT { - return context.(*ListContext) + // not all side contexts are list contexts (e.g. the status panel) + listContext, ok := context.(*ListContext) + if !ok { + return nil + } + return listContext } } @@ -694,11 +688,17 @@ func (gui *Gui) getCurrentSideView() *gocui.View { return view } -func (gui *Gui) getSideContextSelectedItem() ListItem { +func (gui *Gui) getSideContextSelectedItemId() string { currentSideContext := gui.currentSideContext() if currentSideContext == nil { - return nil + return "" + } + + item, ok := currentSideContext.GetSelectedItem() + + if ok { + return item.ID() } - return currentSideContext.GetSelectedItem() + return "" } diff --git a/pkg/gui/diffing.go b/pkg/gui/diffing.go index b334fe768..06d19959c 100644 --- a/pkg/gui/diffing.go +++ b/pkg/gui/diffing.go @@ -60,8 +60,8 @@ func (gui *Gui) currentDiffTerminals() []string { if context == nil { return nil } - item := context.GetSelectedItem() - if item == nil { + item, ok := context.GetSelectedItem() + if !ok { return nil } return []string{item.ID()} @@ -79,11 +79,7 @@ func (gui *Gui) currentDiffTerminal() string { func (gui *Gui) currentlySelectedFilename() string { switch gui.currentContextKey() { case FILES_CONTEXT_KEY, COMMIT_FILES_CONTEXT_KEY: - item := gui.getSideContextSelectedItem() - if item == nil { - return "" - } - return item.ID() + return gui.getSideContextSelectedItemId() default: return "" } diff --git a/pkg/gui/global_handlers.go b/pkg/gui/global_handlers.go index c2ae9cae8..574efdc6d 100644 --- a/pkg/gui/global_handlers.go +++ b/pkg/gui/global_handlers.go @@ -181,11 +181,11 @@ func (gui *Gui) fetch(canPromptForCredentials bool) (err error) { func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error { // important to note that this assumes we've selected an item in a side context - item := gui.getSideContextSelectedItem() + itemId := gui.getSideContextSelectedItemId() - if item == nil { + if itemId == "" { return nil } - return gui.OSCommand.CopyToClipboard(item.ID()) + return gui.OSCommand.CopyToClipboard(itemId) } diff --git a/pkg/gui/list_context.go b/pkg/gui/list_context.go index 199f0b5bb..4751da483 100644 --- a/pkg/gui/list_context.go +++ b/pkg/gui/list_context.go @@ -21,8 +21,10 @@ type ListContext struct { OnFocus func() error OnFocusLost func() error OnClickSelectedItem func() error - SelectedItem func() ListItem - GetPanelState func() IListPanelState + + // have to return whether item is nil because you can't work it out on the calling end once the pointer is wrapped in an interface (unless you want to use reflection) + SelectedItem func() (ListItem, bool) + GetPanelState func() IListPanelState Gui *Gui RendersToMainView bool @@ -40,7 +42,7 @@ type ListItem interface { Description() string } -func (lc *ListContext) GetSelectedItem() ListItem { +func (lc *ListContext) GetSelectedItem() (ListItem, bool) { return lc.SelectedItem() } @@ -72,9 +74,9 @@ func (lc *ListContext) GetParentContext() Context { } func (lc *ListContext) GetSelectedItemId() string { - item := lc.SelectedItem() + item, ok := lc.SelectedItem() - if item == nil { + if !ok { return "" } @@ -277,8 +279,11 @@ func (gui *Gui) filesListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetFileListDisplayStrings(gui.State.Files, gui.State.Modes.Diffing.Ref) }, - Contains: CONTAINS_NOTHING, - SelectedItem: func() ListItem { return gui.getSelectedFile() }, + Contains: CONTAINS_NOTHING, + SelectedItem: func() (ListItem, bool) { + item := gui.getSelectedFile() + return item, item != nil + }, } } @@ -295,8 +300,11 @@ func (gui *Gui) branchesListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetBranchListDisplayStrings(gui.State.Branches, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Modes.Diffing.Ref) }, - Contains: CONTAINS_COMMITS, - SelectedItem: func() ListItem { return gui.getSelectedBranch() }, + Contains: CONTAINS_COMMITS, + SelectedItem: func() (ListItem, bool) { + item := gui.getSelectedBranch() + return item, item != nil + }, } } @@ -314,8 +322,11 @@ func (gui *Gui) remotesListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetRemoteListDisplayStrings(gui.State.Remotes, gui.State.Modes.Diffing.Ref) }, - Contains: CONTAINS_BRANCHES, - SelectedItem: func() ListItem { return gui.getSelectedRemote() }, + Contains: CONTAINS_BRANCHES, + SelectedItem: func() (ListItem, bool) { + item := gui.getSelectedRemote() + return item, item != nil + }, } } @@ -332,8 +343,11 @@ func (gui *Gui) remoteBranchesListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetRemoteBranchListDisplayStrings(gui.State.RemoteBranches, gui.State.Modes.Diffing.Ref) }, - Contains: CONTAINS_COMMITS, - SelectedItem: func() ListItem { return gui.getSelectedRemoteBranch() }, + Contains: CONTAINS_COMMITS, + SelectedItem: func() (ListItem, bool) { + item := gui.getSelectedRemoteBranch() + return item, item != nil + }, } } @@ -350,8 +364,11 @@ func (gui *Gui) tagsListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetTagListDisplayStrings(gui.State.Tags, gui.State.Modes.Diffing.Ref) }, - Contains: CONTAINS_COMMITS, - SelectedItem: func() ListItem { return gui.getSelectedTag() }, + Contains: CONTAINS_COMMITS, + SelectedItem: func() (ListItem, bool) { + item := gui.getSelectedTag() + return item, item != nil + }, } } @@ -369,8 +386,11 @@ func (gui *Gui) branchCommitsListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref) }, - Contains: CONTAINS_FILES, - SelectedItem: func() ListItem { return gui.getSelectedLocalCommit() }, + Contains: CONTAINS_FILES, + SelectedItem: func() (ListItem, bool) { + item := gui.getSelectedLocalCommit() + return item, item != nil + }, } } @@ -387,8 +407,11 @@ func (gui *Gui) reflogCommitsListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetReflogCommitListDisplayStrings(gui.State.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref) }, - Contains: CONTAINS_FILES, - SelectedItem: func() ListItem { return gui.getSelectedReflogCommit() }, + Contains: CONTAINS_FILES, + SelectedItem: func() (ListItem, bool) { + item := gui.getSelectedReflogCommit() + return item, item != nil + }, } } @@ -406,8 +429,11 @@ func (gui *Gui) subCommitsListContext() *ListContext { gui.Log.Warn("getting display strings for sub commits") return presentation.GetCommitListDisplayStrings(gui.State.SubCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref) }, - Contains: CONTAINS_COMMITS, - SelectedItem: func() ListItem { return gui.getSelectedSubCommit() }, + Contains: CONTAINS_COMMITS, + SelectedItem: func() (ListItem, bool) { + item := gui.getSelectedSubCommit() + return item, item != nil + }, } } @@ -424,8 +450,11 @@ func (gui *Gui) stashListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetStashEntryListDisplayStrings(gui.State.StashEntries, gui.State.Modes.Diffing.Ref) }, - Contains: CONTAINS_FILES, - SelectedItem: func() ListItem { return gui.getSelectedStashEntry() }, + Contains: CONTAINS_FILES, + SelectedItem: func() (ListItem, bool) { + item := gui.getSelectedStashEntry() + return item, item != nil + }, } } @@ -443,8 +472,11 @@ func (gui *Gui) commitFilesListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetCommitFileListDisplayStrings(gui.State.CommitFiles, gui.State.Modes.Diffing.Ref) }, - Contains: CONTAINS_NOTHING, - SelectedItem: func() ListItem { return gui.getSelectedCommitFile() }, + Contains: CONTAINS_NOTHING, + SelectedItem: func() (ListItem, bool) { + item := gui.getSelectedCommitFile() + return item, item != nil + }, } } |