Skip to content

Commit

Permalink
WIP Click in commits view to enter patch building for clicked line
Browse files Browse the repository at this point in the history
This involves first switching to the commit files view, and then entering the
clicked file from there.
  • Loading branch information
stefanhaller committed Oct 11, 2024
1 parent e72efae commit 5e84f7e
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/gui/controllers/switch_to_diff_files_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package controllers

import (
"path/filepath"

"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)

Expand Down Expand Up @@ -47,6 +51,49 @@ func (self *SwitchToDiffFilesController) GetKeybindings(opts types.KeybindingsOp
return bindings
}

func (self *SwitchToDiffFilesController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
return []*gocui.ViewMouseBinding{
{
ViewName: "main",
Key: gocui.MouseLeft,
Handler: self.onClickMain,
FocusedView: self.context.GetViewName(),
},
}
}

func (self *SwitchToDiffFilesController) onClickMain(opts gocui.ViewMouseBindingOpts) error {
clickedFile, line, ok := self.c.Helpers().Staging.GetFileAndLineForClickedDiffLine("main", opts.Y)
if !ok {
return nil
}

if err := self.enter(); err != nil {
return err
}

context := self.c.Contexts().CommitFiles
var node *filetree.CommitFileNode

relativePath, err := filepath.Rel(self.c.Git().RepoPaths.RepoPath(), clickedFile)
if err != nil {
return err
}
context.CommitFileTreeViewModel.ExpandToPath(relativePath)
self.c.PostRefreshUpdate(context)

idx, ok := context.CommitFileTreeViewModel.GetIndexForPath(relativePath)
if !ok {
return nil
}

context.SetSelectedLineIdx(idx)
context.GetViewTrait().FocusPoint(
context.ModelIndexToViewIndex(idx))
node = context.GetSelected()
return self.c.Helpers().CommitFiles.EnterCommitFile(node, types.OnFocusOpts{ClickedWindowName: "main", ClickedViewLineIdx: opts.Y, ClickedViewRealLineIdx: line})
}

func (self *SwitchToDiffFilesController) Context() types.Context {
return self.context
}
Expand Down

0 comments on commit 5e84f7e

Please sign in to comment.