mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-10-12 02:16:50 +00:00
Route the editing of a diff's files through DiffHelper
Three places open a file of a diff in the editor: `e` and alt-click in the main view, the lazygit-edit:// hyperlinks of a diff renderer, and `e` in the commit files panel. The first two also each carry a line of the diff forward to the file's current line before opening it. Give them one pair of methods to call. The next commit changes where these files are opened from, and can then do so in one place. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
283bd590f3
commit
64fd7d8686
@@ -65,7 +65,8 @@ func (gui *Gui) resetHelpersAndControllers() {
|
||||
searchHelper,
|
||||
)
|
||||
diffLineHelper := helpers.NewDiffLineHelper(helperCommon)
|
||||
diffHelper := helpers.NewDiffHelper(helperCommon, diffLineHelper)
|
||||
filesHelper := helpers.NewFilesHelper(helperCommon)
|
||||
diffHelper := helpers.NewDiffHelper(helperCommon, diffLineHelper, filesHelper)
|
||||
cherryPickHelper := helpers.NewCherryPickHelper(
|
||||
helperCommon,
|
||||
rebaseHelper,
|
||||
@@ -92,7 +93,7 @@ func (gui *Gui) resetHelpersAndControllers() {
|
||||
CustomPatch: customPatchHelper,
|
||||
Bisect: bisectHelper,
|
||||
Suggestions: suggestionsHelper,
|
||||
Files: helpers.NewFilesHelper(helperCommon),
|
||||
Files: filesHelper,
|
||||
WorkingTree: helpers.NewWorkingTreeHelper(helperCommon, refsHelper, commitsHelper, gpgHelper, rebaseHelper),
|
||||
Tags: helpers.NewTagsHelper(helperCommon, commitsHelper, gpgHelper),
|
||||
BranchesHelper: helpers.NewBranchesHelper(helperCommon, worktreeHelper),
|
||||
|
||||
@@ -432,7 +432,7 @@ func (self *CommitFilesController) open(node *filetree.CommitFileNode) error {
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) edit(nodes []*filetree.CommitFileNode) error {
|
||||
return self.c.Helpers().Files.EditFiles(lo.FilterMap(nodes,
|
||||
return self.c.Helpers().Diff.EditFilesOfDiff(lo.FilterMap(nodes,
|
||||
func(node *filetree.CommitFileNode, _ int) (string, bool) {
|
||||
return node.GetPath(), node.IsFile()
|
||||
}))
|
||||
|
||||
@@ -16,12 +16,14 @@ import (
|
||||
type DiffHelper struct {
|
||||
c *HelperCommon
|
||||
diffLineHelper *DiffLineHelper
|
||||
filesHelper *FilesHelper
|
||||
}
|
||||
|
||||
func NewDiffHelper(c *HelperCommon, diffLineHelper *DiffLineHelper) *DiffHelper {
|
||||
func NewDiffHelper(c *HelperCommon, diffLineHelper *DiffLineHelper, filesHelper *FilesHelper) *DiffHelper {
|
||||
return &DiffHelper{
|
||||
c: c,
|
||||
diffLineHelper: diffLineHelper,
|
||||
filesHelper: filesHelper,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,6 +226,20 @@ func (self *DiffHelper) OpenDiffToolForRef(selectedRef models.Ref) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// EditFilesOfDiff opens files of the diff that the current side panel shows in the
|
||||
// editor.
|
||||
func (self *DiffHelper) EditFilesOfDiff(paths []string) error {
|
||||
return self.filesHelper.EditFiles(paths)
|
||||
}
|
||||
|
||||
// EditFileOfDiffAtLine opens a file of the diff shown in the given view in the editor,
|
||||
// at a line of that diff. The line is carried forward to where it is in the file now
|
||||
// (see AdjustLineNumber).
|
||||
func (self *DiffHelper) EditFileOfDiffAtLine(path string, lineNumber int, viewName string) error {
|
||||
lineNumber = self.AdjustLineNumber(path, lineNumber, viewName)
|
||||
return self.filesHelper.EditFileAtLine(path, lineNumber)
|
||||
}
|
||||
|
||||
// AdjustLineNumber is used to adjust a line number in the diff that's currently
|
||||
// being viewed, so that it corresponds to the line number in the actual working
|
||||
// copy state of the file. It is used when clicking on a delta hyperlink in a
|
||||
|
||||
@@ -1085,13 +1085,10 @@ func (self *MainViewController) editDiffLine(viewLine int, beforeEdit func()) er
|
||||
// it opens the file without jumping anywhere — as pressing edit on a file in a side
|
||||
// panel does.
|
||||
if info.Type == types.DiffLineFileHeader {
|
||||
return self.c.Helpers().Files.EditFiles([]string{info.Path})
|
||||
return self.c.Helpers().Diff.EditFilesOfDiff([]string{info.Path})
|
||||
}
|
||||
|
||||
// The diff may be of an older commit, whose line numbers aren't the file's current
|
||||
// ones, so they have to be carried forward before we can point an editor at them.
|
||||
lineNumber := self.c.Helpers().Diff.AdjustLineNumber(info.Path, info.NewLine, self.context.GetViewName())
|
||||
return self.c.Helpers().Files.EditFileAtLine(info.Path, lineNumber)
|
||||
return self.c.Helpers().Diff.EditFileOfDiffAtLine(info.Path, info.NewLine, self.context.GetViewName())
|
||||
}
|
||||
|
||||
// openPullRequestAtSelectedLine opens the pull request of the branch whose commit the
|
||||
|
||||
+2
-3
@@ -417,10 +417,9 @@ func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, contextKey types.Context
|
||||
filepath := matches[1]
|
||||
if matches[2] != "" {
|
||||
lineNumber := utils.MustConvertToInt(matches[2])
|
||||
lineNumber = gui.helpers.Diff.AdjustLineNumber(filepath, lineNumber, viewname)
|
||||
return gui.helpers.Files.EditFileAtLine(filepath, lineNumber)
|
||||
return gui.helpers.Diff.EditFileOfDiffAtLine(filepath, lineNumber, viewname)
|
||||
}
|
||||
return gui.helpers.Files.EditFiles([]string{filepath})
|
||||
return gui.helpers.Diff.EditFilesOfDiff([]string{filepath})
|
||||
}
|
||||
|
||||
if entry, ok := strings.CutPrefix(url, helpers.DiffStatLinkScheme); ok {
|
||||
|
||||
Reference in New Issue
Block a user