1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-13 00:07:59 +02:00

use array of ints instead of range

This commit is contained in:
Jesse Duffield 2019-11-04 11:46:28 +11:00
parent cc039d1f9b
commit a3c84296bf
3 changed files with 40 additions and 15 deletions

@ -6,6 +6,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -29,7 +30,7 @@ func newHunk(header string, body string, firstLineIdx int) *PatchHunk {
} }
} }
func (hunk *PatchHunk) updatedLinesForRange(firstLineIdx int, lastLineIdx int, reverse bool) []string { func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool) []string {
skippedNewlineMessageIndex := -1 skippedNewlineMessageIndex := -1
newLines := []string{} newLines := []string{}
@ -39,12 +40,12 @@ func (hunk *PatchHunk) updatedLinesForRange(firstLineIdx int, lastLineIdx int, r
if line == "" { if line == "" {
break break
} }
isLineInsideRange := (firstLineIdx <= lineIdx && lineIdx <= lastLineIdx) isLineSelected := utils.IncludesInt(lineIndices, lineIdx)
firstChar, content := line[:1], line[1:] firstChar, content := line[:1], line[1:]
transformedFirstChar := transformedFirstChar(firstChar, reverse, isLineInsideRange) transformedFirstChar := transformedFirstChar(firstChar, reverse, isLineSelected)
if isLineInsideRange || (transformedFirstChar == "\\" && skippedNewlineMessageIndex != lineIdx) || transformedFirstChar == " " { if isLineSelected || (transformedFirstChar == "\\" && skippedNewlineMessageIndex != lineIdx) || transformedFirstChar == " " {
newLines = append(newLines, transformedFirstChar+content) newLines = append(newLines, transformedFirstChar+content)
continue continue
} }
@ -58,9 +59,9 @@ func (hunk *PatchHunk) updatedLinesForRange(firstLineIdx int, lastLineIdx int, r
return newLines return newLines
} }
func transformedFirstChar(firstChar string, reverse bool, isLineInsideRange bool) string { func transformedFirstChar(firstChar string, reverse bool, isLineSelected bool) string {
if reverse { if reverse {
if !isLineInsideRange && firstChar == "+" { if !isLineSelected && firstChar == "+" {
return " " return " "
} else if firstChar == "-" { } else if firstChar == "-" {
return "+" return "+"
@ -71,7 +72,7 @@ func transformedFirstChar(firstChar string, reverse bool, isLineInsideRange bool
} }
} }
if !isLineInsideRange && firstChar == "-" { if !isLineSelected && firstChar == "-" {
return " " return " "
} }
@ -82,8 +83,8 @@ func (hunk *PatchHunk) formatHeader(oldStart int, oldLength int, newStart int, n
return fmt.Sprintf("@@ -%d,%d +%d,%d @@%s\n", oldStart, oldLength, newStart, newLength, heading) return fmt.Sprintf("@@ -%d,%d +%d,%d @@%s\n", oldStart, oldLength, newStart, newLength, heading)
} }
func (hunk *PatchHunk) formatWithChanges(firstLineIdx int, lastLineIdx int, reverse bool, startOffset int) (int, string) { func (hunk *PatchHunk) formatWithChanges(lineIndices []int, reverse bool, startOffset int) (int, string) {
bodyLines := hunk.updatedLinesForRange(firstLineIdx, lastLineIdx, reverse) bodyLines := hunk.updatedLines(lineIndices, reverse)
startOffset, header, ok := hunk.updatedHeader(bodyLines, startOffset, reverse) startOffset, header, ok := hunk.updatedHeader(bodyLines, startOffset, reverse)
if !ok { if !ok {
return startOffset, "" return startOffset, ""
@ -184,12 +185,17 @@ func NewPatchModifier(log *logrus.Entry, filename string, diffText string) *Patc
} }
} }
func (d *PatchModifier) ModifiedPatchForRange(firstLineIdx int, lastLineIdx int, reverse bool) string { func (d *PatchModifier) ModifiedPatchForLines(lineIndices []int, reverse bool) string {
// step one is getting only those hunks which we care about // step one is getting only those hunks which we care about
hunksInRange := []*PatchHunk{} hunksInRange := []*PatchHunk{}
outer:
for _, hunk := range d.hunks { for _, hunk := range d.hunks {
if hunk.LastLineIdx >= firstLineIdx && hunk.FirstLineIdx <= lastLineIdx { // if there is any line in our lineIndices array that the hunk contains, we append it
hunksInRange = append(hunksInRange, hunk) for _, lineIdx := range lineIndices {
if lineIdx >= hunk.FirstLineIdx && lineIdx <= hunk.LastLineIdx {
hunksInRange = append(hunksInRange, hunk)
continue outer
}
} }
} }
@ -198,7 +204,7 @@ func (d *PatchModifier) ModifiedPatchForRange(firstLineIdx int, lastLineIdx int,
formattedHunks := "" formattedHunks := ""
var formattedHunk string var formattedHunk string
for _, hunk := range hunksInRange { for _, hunk := range hunksInRange {
startOffset, formattedHunk = hunk.formatWithChanges(firstLineIdx, lastLineIdx, reverse, startOffset) startOffset, formattedHunk = hunk.formatWithChanges(lineIndices, reverse, startOffset)
formattedHunks += formattedHunk formattedHunks += formattedHunk
} }
@ -211,7 +217,16 @@ func (d *PatchModifier) ModifiedPatchForRange(firstLineIdx int, lastLineIdx int,
return fileHeader + formattedHunks return fileHeader + formattedHunks
} }
func ModifiedPatch(log *logrus.Entry, filename string, diffText string, firstLineIdx int, lastLineIdx int, reverse bool) string { func (d *PatchModifier) ModifiedPatchForRange(firstLineIdx int, lastLineIdx int, reverse bool) string {
// generate array of consecutive line indices from our range
selectedLines := []int{}
for i := firstLineIdx; i <= lastLineIdx; i++ {
selectedLines = append(selectedLines, i)
}
return d.ModifiedPatchForLines(selectedLines, reverse)
}
func ModifiedPatchForRange(log *logrus.Entry, filename string, diffText string, firstLineIdx int, lastLineIdx int, reverse bool) string {
p := NewPatchModifier(log, filename, diffText) p := NewPatchModifier(log, filename, diffText)
return p.ModifiedPatchForRange(firstLineIdx, lastLineIdx, reverse) return p.ModifiedPatchForRange(firstLineIdx, lastLineIdx, reverse)
} }

@ -502,7 +502,7 @@ func TestModifyPatchForRange(t *testing.T) {
for _, s := range scenarios { for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
result := ModifiedPatch(nil, s.filename, s.diffText, s.firstLineIndex, s.lastLineIndex, s.reverse) result := ModifiedPatchForRange(nil, s.filename, s.diffText, s.firstLineIndex, s.lastLineIndex, s.reverse)
if !assert.Equal(t, s.expected, result) { if !assert.Equal(t, s.expected, result) {
fmt.Println(result) fmt.Println(result)
} }

@ -226,6 +226,16 @@ func IncludesString(list []string, a string) bool {
return false return false
} }
// IncludesInt if the list contains the Int
func IncludesInt(list []int, a int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// NextIndex returns the index of the element that comes after the given number // NextIndex returns the index of the element that comes after the given number
func NextIndex(numbers []int, currentNumber int) int { func NextIndex(numbers []int, currentNumber int) int {
for index, number := range numbers { for index, number := range numbers {