From a7dd95107eac65e10e2f9380e92da89a661106d5 Mon Sep 17 00:00:00 2001 From: Ned Palacios Date: Sat, 25 Nov 2023 23:28:24 +0800 Subject: [PATCH] fix: inconsistencies in line number issue in output_gen.go --- output_gen.go | 23 ++++++++++++++++------- source.go | 12 ++++++++++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/output_gen.go b/output_gen.go index 1557225..3ffd274 100644 --- a/output_gen.go +++ b/output_gen.go @@ -85,7 +85,7 @@ func (gen *OutputGenerator) Generate(cd *ContextData, explain *ExplainGenerator, startRow = uint32(cd.MainError.ErrorNode.StartPos.Line) } - startLines := doc.LinesAt(int(startRow)-1, int(startRow)+1) + startLines := doc.LinesAt(int(startRow)-1, int(startRow)) endLines := doc.LinesAt(min(int(startRow)+1, doc.TotalLines()), doc.TotalLines()) arrowLength := int(cd.MainError.Nearest.EndByte() - cd.MainError.Nearest.StartByte()) if arrowLength == 0 { @@ -136,9 +136,15 @@ func (gen *OutputGenerator) Generate(cd *ContextData, explain *ExplainGenerator, if len(step.Fixes) != 0 { descriptionBuilder := &strings.Builder{} + + // get the start and end line after applying the diff startLine := step.Fixes[0].StartPosition.Line afterLine := step.Fixes[0].EndPosition.Line + // get the original start and end line + origStartLine := step.Fixes[0].StartPosition.Line + origAfterLine := step.Fixes[0].EndPosition.Line + for fIdx, fix := range step.Fixes { diffPosition = diffPosition.addNoCheck(editedDoc.Apply(Changeset{ NewText: fix.NewText, @@ -146,8 +152,11 @@ func (gen *OutputGenerator) Generate(cd *ContextData, explain *ExplainGenerator, EndPos: fix.EndPosition, }.Add(diffPosition))) - startLine = min(startLine, fix.StartPosition.Line) - afterLine = max(afterLine, fix.EndPosition.Line) + origStartLine = min(origStartLine, fix.StartPosition.Line) + origAfterLine = max(origAfterLine, fix.EndPosition.Line) + + startLine = min(startLine, fix.StartPosition.Line+diffPosition.Line) + afterLine = max(afterLine, fix.EndPosition.Line+diffPosition.Line) if len(fix.Description) != 0 { if fIdx < len(step.Fixes)-1 { @@ -159,21 +168,21 @@ func (gen *OutputGenerator) Generate(cd *ContextData, explain *ExplainGenerator, } gen.writeln("```diff") - gen.writeLines(editedDoc.LinesAt(startLine-2, startLine)...) + gen.writeLines(editedDoc.LinesAt(startLine-2, startLine-1)...) - original := editedDoc.LinesAt(startLine+1, afterLine) + original := editedDoc.LinesAt(origStartLine, origAfterLine) for _, origLine := range original { gen.write("- ") gen.writeln(origLine) } - modified := editedDoc.ModifiedLinesAt(startLine+1, afterLine) + modified := editedDoc.ModifiedLinesAt(startLine, afterLine) for _, modifiedLine := range modified { gen.write("+ ") gen.writeln(modifiedLine) } - gen.writeLines(editedDoc.LinesAt(afterLine+1, min(afterLine+3, editedDoc.TotalLines()))...) + gen.writeLines(editedDoc.LinesAt(origAfterLine+1, min(origAfterLine+2, editedDoc.TotalLines()))...) gen.writeln("```") if descriptionBuilder.Len() != 0 { gen.writeln(descriptionBuilder.String()) diff --git a/source.go b/source.go index 5944c98..61aa8a9 100644 --- a/source.go +++ b/source.go @@ -15,6 +15,10 @@ type Position struct { Index int } +func (pos Position) IsInBetween(loc Location) bool { + return pos.Index >= loc.StartPos.Index && pos.Index <= loc.EndPos.Index +} + func (pos Position) Add(pos2 Position) Position { return Position{ Line: max(pos.Line+pos2.Line, 0), @@ -48,6 +52,10 @@ type Location struct { EndPos Position } +func (loc Location) IsWithin(other Location) bool { + return loc.StartPos.IsInBetween(other) && loc.EndPos.IsInBetween(other) +} + func (loc Location) Point() sitter.Point { return sitter.Point{ Row: uint32(loc.StartPos.Line), @@ -307,10 +315,10 @@ func linesAt(list []string, from int, to int) []string { } else if from > 0 && to == len(list) { return list[from:] } else if from == 0 && to < len(list) { - return list[:to] + return list[:to+1] } - return list[from:to] + return list[from : to+1] } func (doc *EditableDocument) ModifiedLinesAt(from int, to int) []string {