mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-07 22:02:56 +03:00
Adjust line number for working copy when editing a line
There are two ways to jump to the editor on a specific line: pressing `e` in the staging or patch building panels, or clicking on a hyperlink in a delta diff. In both cases, this works perfectly in the unstaged changes view, but in other views (either staged changes, or an older commit) it can often jump to the wrong line; this happens when there are further changes to the file being viewed in later commits or in unstaged changes. This commit fixes this so that you end up on the right line in these cases.
This commit is contained in:
@@ -80,6 +80,14 @@ func (self *BranchesContext) GetDiffTerminals() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *BranchesContext) RefForAdjustingLineNumberInDiff() string {
|
||||
branch := self.GetSelected()
|
||||
if branch != nil {
|
||||
return branch.ID()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *BranchesContext) ShowBranchHeadsInSubCommits() bool {
|
||||
return true
|
||||
}
|
||||
|
@@ -77,6 +77,13 @@ func (self *CommitFilesContext) GetDiffTerminals() []string {
|
||||
return []string{self.GetRef().RefName()}
|
||||
}
|
||||
|
||||
func (self *CommitFilesContext) RefForAdjustingLineNumberInDiff() string {
|
||||
if refs := self.GetRefRange(); refs != nil {
|
||||
return refs.To.RefName()
|
||||
}
|
||||
return self.GetRef().RefName()
|
||||
}
|
||||
|
||||
func (self *CommitFilesContext) GetFromAndToForDiff() (string, string) {
|
||||
if refs := self.GetRefRange(); refs != nil {
|
||||
return refs.From.ParentRefName(), refs.To.RefName()
|
||||
|
@@ -170,6 +170,14 @@ func (self *LocalCommitsContext) GetDiffTerminals() []string {
|
||||
return []string{itemId}
|
||||
}
|
||||
|
||||
func (self *LocalCommitsContext) RefForAdjustingLineNumberInDiff() string {
|
||||
commits, _, _ := self.GetSelectedItems()
|
||||
if commits == nil {
|
||||
return ""
|
||||
}
|
||||
return commits[0].Hash
|
||||
}
|
||||
|
||||
func (self *LocalCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
|
||||
return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr)
|
||||
}
|
||||
|
@@ -86,6 +86,10 @@ func (self *ReflogCommitsContext) GetDiffTerminals() []string {
|
||||
return []string{itemId}
|
||||
}
|
||||
|
||||
func (self *ReflogCommitsContext) RefForAdjustingLineNumberInDiff() string {
|
||||
return self.GetSelectedItemId()
|
||||
}
|
||||
|
||||
func (self *ReflogCommitsContext) ShowBranchHeadsInSubCommits() bool {
|
||||
return false
|
||||
}
|
||||
|
@@ -78,6 +78,10 @@ func (self *RemoteBranchesContext) GetDiffTerminals() []string {
|
||||
return []string{itemId}
|
||||
}
|
||||
|
||||
func (self *RemoteBranchesContext) RefForAdjustingLineNumberInDiff() string {
|
||||
return self.GetSelectedItemId()
|
||||
}
|
||||
|
||||
func (self *RemoteBranchesContext) ShowBranchHeadsInSubCommits() bool {
|
||||
return true
|
||||
}
|
||||
|
@@ -53,3 +53,7 @@ func (self *RemotesContext) GetDiffTerminals() []string {
|
||||
|
||||
return []string{itemId}
|
||||
}
|
||||
|
||||
func (self *RemotesContext) RefForAdjustingLineNumberInDiff() string {
|
||||
return ""
|
||||
}
|
||||
|
@@ -71,3 +71,7 @@ func (self *StashContext) GetDiffTerminals() []string {
|
||||
|
||||
return []string{itemId}
|
||||
}
|
||||
|
||||
func (self *StashContext) RefForAdjustingLineNumberInDiff() string {
|
||||
return self.GetSelectedItemId()
|
||||
}
|
||||
|
@@ -217,6 +217,14 @@ func (self *SubCommitsContext) GetDiffTerminals() []string {
|
||||
return []string{itemId}
|
||||
}
|
||||
|
||||
func (self *SubCommitsContext) RefForAdjustingLineNumberInDiff() string {
|
||||
commits, _, _ := self.GetSelectedItems()
|
||||
if commits == nil {
|
||||
return ""
|
||||
}
|
||||
return commits[0].Hash
|
||||
}
|
||||
|
||||
func (self *SubCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
|
||||
return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr)
|
||||
}
|
||||
|
@@ -66,6 +66,10 @@ func (self *TagsContext) GetDiffTerminals() []string {
|
||||
return []string{itemId}
|
||||
}
|
||||
|
||||
func (self *TagsContext) RefForAdjustingLineNumberInDiff() string {
|
||||
return self.GetSelectedItemId()
|
||||
}
|
||||
|
||||
func (self *TagsContext) ShowBranchHeadsInSubCommits() bool {
|
||||
return true
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/patch"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/modes/diffing"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
||||
@@ -164,3 +165,45 @@ func (self *DiffHelper) OpenDiffToolForRef(selectedRef types.Ref) error {
|
||||
}))
|
||||
return err
|
||||
}
|
||||
|
||||
// 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
|
||||
// diff, or when pressing `e` in the staging or patch building panels. It works
|
||||
// by getting a diff of what's being viewed in the main view against the working
|
||||
// copy, and then using that diff to adjust the line number.
|
||||
// path is the file path of the file being viewed
|
||||
// linenumber is the line number to adjust (one-based)
|
||||
// viewname is the name of the view that shows the diff. We need to pass it
|
||||
// because the diff adjustment is slightly different depending on which view is
|
||||
// showing the diff.
|
||||
func (self *DiffHelper) AdjustLineNumber(path string, linenumber int, viewname string) int {
|
||||
switch viewname {
|
||||
|
||||
case "main", "patchBuilding":
|
||||
if diffableContext, ok := self.c.Context().CurrentSide().(types.DiffableContext); ok {
|
||||
ref := diffableContext.RefForAdjustingLineNumberInDiff()
|
||||
if len(ref) != 0 {
|
||||
return self.adjustLineNumber(linenumber, ref, "--", path)
|
||||
}
|
||||
}
|
||||
// if the type cast to DiffableContext returns false, we are in the
|
||||
// unstaged changes view of the Files panel; no need to adjust line
|
||||
// numbers in this case
|
||||
|
||||
case "secondary", "stagingSecondary":
|
||||
return self.adjustLineNumber(linenumber, "--", path)
|
||||
}
|
||||
|
||||
return linenumber
|
||||
}
|
||||
|
||||
func (self *DiffHelper) adjustLineNumber(linenumber int, diffArgs ...string) int {
|
||||
args := append([]string{"--unified=0"}, diffArgs...)
|
||||
diff, err := self.c.Git().Diff.GetDiff(false, args...)
|
||||
if err != nil {
|
||||
return linenumber
|
||||
}
|
||||
patch := patch.Parse(diff)
|
||||
return patch.AdjustLineNumber(linenumber)
|
||||
}
|
||||
|
@@ -107,6 +107,7 @@ func (self *PatchBuildingController) EditFile() error {
|
||||
}
|
||||
|
||||
lineNumber := self.context().GetState().CurrentLineNumber()
|
||||
lineNumber = self.c.Helpers().Diff.AdjustLineNumber(path, lineNumber, self.context().GetViewName())
|
||||
return self.c.Helpers().Files.EditFileAtLine(path, lineNumber)
|
||||
}
|
||||
|
||||
|
@@ -161,6 +161,7 @@ func (self *StagingController) EditFile() error {
|
||||
}
|
||||
|
||||
lineNumber := self.context.GetState().CurrentLineNumber()
|
||||
lineNumber = self.c.Helpers().Diff.AdjustLineNumber(path, lineNumber, self.context.GetViewName())
|
||||
return self.c.Helpers().Files.EditFileAtLine(path, lineNumber)
|
||||
}
|
||||
|
||||
|
@@ -368,6 +368,7 @@ 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.Files.EditFiles([]string{filepath})
|
||||
|
@@ -152,6 +152,14 @@ type DiffableContext interface {
|
||||
// which becomes an option when you bring up the diff menu, but when you're just
|
||||
// flicking through branches it will be using the local branch name.
|
||||
GetDiffTerminals() []string
|
||||
|
||||
// Returns the ref that should be used for creating a diff of what's
|
||||
// currently shown in the main view against the working directory, in order
|
||||
// to adjust line numbers in the diff to match the current state of the
|
||||
// shown file. For example, if the main view shows a range diff of commits,
|
||||
// we need to pass the first commit of the range. This is used by
|
||||
// DiffHelper.AdjustLineNumber.
|
||||
RefForAdjustingLineNumberInDiff() string
|
||||
}
|
||||
|
||||
type IListContext interface {
|
||||
|
Reference in New Issue
Block a user