Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
more structural edit fixes
Dane Schneiderdcdb24dd3617
diff --git a/app/server/syntax/structural_edits.go b/app/server/syntax/structural_edits.goindex 88bff097..d22659ee 100644--- a/app/server/syntax/structural_edits.go+++ b/app/server/syntax/structural_edits.go@@ -19,7 +19,7 @@ type Anchor struct {type TreeSitterSection []*tree_sitter.Node-const verboseLogging = true+const verboseLogging = falsefunc ApplyReferences(ctx context.Context, original, proposed string, references []Reference, parser *tree_sitter.Parser) (string, error) {var b strings.Builder@@ -67,13 +67,17 @@ func ApplyReferences(ctx context.Context, original, proposed string, referencescontinue}- // fmt.Printf("line: %s, idx: %d\n", line, idx)+ // if verboseLogging {+ // fmt.Printf("line: %s, idx: %d\n", line, idx)+ // }oNode := originalNodesByLine[idx]- // fmt.Println("node:")- // fmt.Println(node.Type())- // fmt.Println(node.Content(originalBytes))+ // if verboseLogging {+ // fmt.Println("node:")+ // fmt.Println(oNode.Type())+ // fmt.Println(oNode.Content(originalBytes))+ // }stringMatch := line == snodeMatch := oNode != nil && oNode.IsNamed() && nodesMatch(oNode, pNode, originalBytes, proposedBytes)@@ -85,8 +89,9 @@ func ApplyReferences(ctx context.Context, original, proposed string, references}if verboseLogging {fmt.Printf("found match: num: %d, endLineNum: %d\n", idx+1, endLineNum)+ // fmt.Println(oNode.Content(originalBytes))}- // fmt.Println(oNode.Content(originalBytes))+return &Anchor{Open: idx + 1, Close: endLineNum}}}@@ -175,14 +180,30 @@ func ApplyReferences(ctx context.Context, original, proposed string, referencesvar fullRef []stringif eof {- fullRef = originalLines[refStart-1:]+ fullRef = originalLines[refStart:]+ if verboseLogging {+ fmt.Println("eof")+ fmt.Printf("fullRef refStart: %d\n", refStart)+ fmt.Printf("originalLines[refStart]: %s\n", originalLines[refStart])+ fmt.Printf("writing fullRef: %s\n", strings.Join(fullRef, "\n"))+ fmt.Printf("depth: %d\n", depth)+ }} else {- fullRef = originalLines[refStart-1 : oLineNum-1]- }- if verboseLogging {- fmt.Printf("writing fullRef: %s\n", strings.Join(fullRef, "\n"))- fmt.Printf("depth: %d\n", depth)+ start := refStart - 1+ end := oLineNum - 1+ if verboseLogging {+ fmt.Printf("start: %d, end: %d\n", start, end)+ }+ fullRef = originalLines[start:end]+ if verboseLogging {+ fmt.Printf("fullRef refStart: %d, oLineNum-1: %d\n", refStart, oLineNum-1)+ fmt.Printf("originalLines[refStart-1]: %s\n", originalLines[refStart-1])+ fmt.Printf("originalLines[oLineNum-1]: %s\n", originalLines[oLineNum-1])+ fmt.Printf("writing fullRef: %s\n", strings.Join(fullRef, "\n"))+ fmt.Printf("depth: %d\n", depth)+ }}+write(strings.Join(fullRef, "\n"), depth > 0)postRefContent := postRefBuffers[0].String()@@ -197,11 +218,10 @@ func ApplyReferences(ctx context.Context, original, proposed string, referencesif verboseLogging {fmt.Printf("numRefs > 1, refOriginalParent: %s\n", refOriginalParent.Type())fmt.Printf("refOriginalParent.Content(originalBytes):\n%s\n", refOriginalParent.Content(originalBytes))+ fmt.Println("original parent type:", refOriginalParent.Type())+ fmt.Printf("numRefs: %d, oLineNum: %d\n", numRefs, oLineNum)}- fmt.Println("original parent type:", refOriginalParent.Type())- fmt.Printf("numRefs: %d, oLineNum: %d\n", numRefs, oLineNum)-sections := getSections(refOriginalParent, originalBytes, numRefs, oLineNum)for i, section := range sections {@@ -217,8 +237,6 @@ func ApplyReferences(ctx context.Context, original, proposed string, references}}- reachedContent := false-for idx, pLine := range proposedLines {if verboseLogging {fmt.Printf("\n\ni: %d, pLine: %s, refOpen: %v\n", idx, pLine, refOpen)@@ -264,19 +282,15 @@ func ApplyReferences(ctx context.Context, original, proposed string, referencesif !refOpen {if strings.TrimSpace(pLine) == "" {- if reachedContent {- write(pLine, true)+ write(pLine, true)+ if verboseLogging {fmt.Printf("newline, incrementing oLineNum: %d\n", oLineNum)- setOLineNum(oLineNum + 1)}+ setOLineNum(oLineNum + 1)continue}}- if strings.TrimSpace(pLine) != "" {- reachedContent = true- }-var matching boolisClosingAnchor := closingLinesByPLineNum[pLineNum] != 0@@ -292,8 +306,10 @@ func ApplyReferences(ctx context.Context, original, proposed string, referencespNode := proposedNodesByLine[idx]anchor := findNextAnchor(pLine, pNode, oLineNum-1)if anchor != nil {- fmt.Println("anchor found")- fmt.Printf("anchor.Close: %d, anchor.Open: %d\n", anchor.Close, anchor.Open)+ if verboseLogging {+ fmt.Println("anchor found")+ fmt.Printf("anchor.Close: %d, anchor.Open: %d\n", anchor.Close, anchor.Open)+ }matching = truesetOLineNum(anchor.Open)@@ -301,16 +317,22 @@ func ApplyReferences(ctx context.Context, original, proposed string, referencesoriginalClosingLine := originalLines[anchor.Close-1]- fmt.Printf("originalClosingLine: %s\n", originalClosingLine)+ if verboseLogging {+ fmt.Printf("originalClosingLine: %s\n", originalClosingLine)+ }if proposedUpdatesHaveLine(originalClosingLine, idx) {- // fmt.Printf("proposedUpdatesHaveLine: %v\n", proposedUpdatesHaveLine(originalClosingLine, anchor.Open))+ // if verboseLogging {+ // fmt.Printf("proposedUpdatesHaveLine: %v\n", proposedUpdatesHaveLine(originalClosingLine, anchor.Open))+ // }closingPLineNum := int(pNode.EndPoint().Row) + 1closingLinesByPLineNum[closingPLineNum] = anchor.ClosenoMatchUntilStructureClose = originalClosingLineincDepth()} else {- fmt.Println("proposed updates do not have originalClosingLine")+ if verboseLogging {+ fmt.Println("proposed updates do not have originalClosingLine")+ }}}}@@ -327,11 +349,12 @@ func ApplyReferences(ctx context.Context, original, proposed string, referencesfmt.Printf("closing ref, oLineNum: %d\n", oLineNum)}refOpen = false- writeRefs(finalLine)+ writeRefs(false)write(pLine, !finalLine)- setOLineNum(oLineNum + 1)if isClosingAnchor {decDepth()+ } else {+ setOLineNum(oLineNum + 1)}// reset buffers@@ -355,7 +378,9 @@ func ApplyReferences(ctx context.Context, original, proposed string, referencesfmt.Printf("writing pLine: %s\n", pLine)}write(pLine, !finalLine)- setOLineNum(oLineNum + 1)+ if matching && !isClosingAnchor {+ setOLineNum(oLineNum + 1)+ }}}@@ -371,14 +396,20 @@ func ApplyReferences(ctx context.Context, original, proposed string, references}func nodesMatch(n1, n2 *tree_sitter.Node, source1, source2 []byte) bool {- // fmt.Println("check nodesMatch")+ // if verboseLogging {+ // fmt.Println("check nodesMatch")+ // }if n1 == nil || n2 == nil || !n1.IsNamed() || !n2.IsNamed() {- // fmt.Println("nodes not named")+ // if verboseLogging {+ // fmt.Println("nodes not named")+ // }return false}if n1.Type() != n2.Type() {- // fmt.Println("n1 type != n2 type")+ // if verboseLogging {+ // fmt.Println("n1 type != n2 type")+ // }return false}@@ -419,7 +450,9 @@ func nodesMatch(n1, n2 *tree_sitter.Node, source1, source2 []byte) bool {return false}- // fmt.Printf("found declaration nodes of type: %s and %s\n", decl1.Type(), decl2.Type())+ // if verboseLogging {+ // fmt.Printf("found declaration nodes of type: %s and %s\n", decl1.Type(), decl2.Type())+ // }// Get names from first named childname1Node := decl1.NamedChild(0)@@ -433,7 +466,9 @@ func nodesMatch(n1, n2 *tree_sitter.Node, source1, source2 []byte) bool {name1 := name1Node.Content(source1)name2 := name2Node.Content(source2)- // fmt.Printf("name1: %s, name2: %s\n", name1, name2)+ // if verboseLogging {+ // fmt.Printf("name1: %s, name2: %s\n", name1, name2)+ // }return name1 == name2}@@ -586,8 +621,10 @@ func buildNodeIndex(tree *tree_sitter.Tree) map[int]*tree_sitter.Node {// First pass - index direct nodesvar indexNodes func(node *tree_sitter.Node, depth int)indexNodes = func(node *tree_sitter.Node, depth int) {- // fmt.Printf("node: %s, depth: %d, childCount: %d\n", node.Type(), depth, node.ChildCount())- // spew.Dump(node.StartPoint())+ // if verboseLogging {+ // fmt.Printf("node: %s, depth: %d, childCount: %d\n", node.Type(), depth, node.ChildCount())+ // // spew.Dump(node.StartPoint())+ // }if node.Type() != root.Type() {line := int(node.StartPoint().Row)diff --git a/app/server/syntax/structural_edits_test.go b/app/server/syntax/structural_edits_test.goindex 2d341e93..a3008491 100644--- a/app/server/syntax/structural_edits_test.go+++ b/app/server/syntax/structural_edits_test.go@@ -2,6 +2,7 @@ package syntaximport ("context"+ "log""testing"tree_sitter "github.com/smacker/go-tree-sitter"@@ -17,131 +18,131 @@ func TestStructuralReplacements(t *testing.T) {want stringparser *tree_sitter.Parser}{- // {- // name: "single reference in function",- // original: `- // func processUser(id int) error {- // validate(id)- // startTx()- // updateUser(id)- // commit()- // return nil- // }`,- // proposed: `- // func processUser(id int) error {- // // ... existing code ...- // log.Info("processing user")- // return nil- // }`,- // references: []Reference{- // 3,- // },- // want: `- // func processUser(id int) error {- // validate(id)- // startTx()- // updateUser(id)- // commit()- // log.Info("processing user")- // return nil- // }`,- // parser: getParserForLanguage("go"),- // },- // {- // name: "modified function signature, ref at end of function",- // original: `- // func processUser(id int) error {- // validate(id)- // validateAgain(id)- // prepForUpdate(id)- // return update(id)- // }`,- // proposed: `- // func processUser(id int, force bool) (error, bool) {- // if force {- // log.Warn("will force update")- // }- // // ... existing code ...- // }`,- // references: []Reference{- // 6,- // },- // want: `- // func processUser(id int, force bool) (error, bool) {- // if force {- // log.Warn("will force update")- // }- // validate(id)- // validateAgain(id)- // prepForUpdate(id)- // return update(id)- // }`,- // parser: getParserForLanguage("go"),- // },- // {- // name: "modified function signature, ref at beginning of function",- // original: `- // func processUser(id int) error {- // validate(id)- // validateAgain(id)- // prepForUpdate(id)- // return update(id)- // }`,- // proposed: `- // func processUser(id int, force bool) (error, bool) {- // // ... existing code ...- // if force {- // log.Warn("will force update")- // }- // return update(id)- // }`,- // references: []Reference{- // 3,- // },- // want: `- // func processUser(id int, force bool) (error, bool) {- // validate(id)- // validateAgain(id)- // prepForUpdate(id)- // if force {- // log.Warn("will force update")- // }- // return update(id)- // }`,- // parser: getParserForLanguage("go"),- // },- // {- // name: "bad formatting",- // original: `- // func processUser(id int) error {- // validate(id)- // validateAgain(id)- // prepForUpdate(id)- // return update(id)- // }`,- // proposed: `- // func processUser(id int, force bool) (error, bool) {- // // ... existing code ...- // if force {- // log.Warn("will force update")- // }- // return update(id)- // }`,- // references: []Reference{- // 3,- // },- // want: `- // func processUser(id int, force bool) (error, bool) {- // validate(id)- // validateAgain(id)- // prepForUpdate(id)- // if force {- // log.Warn("will force update")- // }- // return update(id)- // }`,- // parser: getParserForLanguage("go"),- // },+ {+ name: "single reference in function",+ original: `+ func processUser(id int) error {+ validate(id)+ startTx()+ updateUser(id)+ commit()+ return nil+ }`,+ proposed: `+ func processUser(id int) error {+ // ... existing code ...+ log.Info("processing user")+ return nil+ }`,+ references: []Reference{+ 3,+ },+ want: `+ func processUser(id int) error {+ validate(id)+ startTx()+ updateUser(id)+ commit()+ log.Info("processing user")+ return nil+ }`,+ parser: getParserForLanguage("go"),+ },+ {+ name: "modified function signature, ref at end of function",+ original: `+ func processUser(id int) error {+ validate(id)+ validateAgain(id)+ prepForUpdate(id)+ return update(id)+ }`,+ proposed: `+ func processUser(id int, force bool) (error, bool) {+ if force {+ log.Warn("will force update")+ }+ // ... existing code ...+ }`,+ references: []Reference{+ 6,+ },+ want: `+ func processUser(id int, force bool) (error, bool) {+ if force {+ log.Warn("will force update")+ }+ validate(id)+ validateAgain(id)+ prepForUpdate(id)+ return update(id)+ }`,+ parser: getParserForLanguage("go"),+ },+ {+ name: "modified function signature, ref at beginning of function",+ original: `+ func processUser(id int) error {+ validate(id)+ validateAgain(id)+ prepForUpdate(id)+ return update(id)+ }`,+ proposed: `+ func processUser(id int, force bool) (error, bool) {+ // ... existing code ...+ if force {+ log.Warn("will force update")+ }+ return update(id)+ }`,+ references: []Reference{+ 3,+ },+ want: `+ func processUser(id int, force bool) (error, bool) {+ validate(id)+ validateAgain(id)+ prepForUpdate(id)+ if force {+ log.Warn("will force update")+ }+ return update(id)+ }`,+ parser: getParserForLanguage("go"),+ },+ {+ name: "bad formatting",+ original: `+ func processUser(id int) error {+ validate(id)+ validateAgain(id)+ prepForUpdate(id)+ return update(id)+ }`,+ proposed: `+ func processUser(id int, force bool) (error, bool) {+ // ... existing code ...+ if force {+ log.Warn("will force update")+ }+ return update(id)+ }`,+ references: []Reference{+ 3,+ },+ want: `+ func processUser(id int, force bool) (error, bool) {+ validate(id)+ validateAgain(id)+ prepForUpdate(id)+ if force {+ log.Warn("will force update")+ }+ return update(id)+ }`,+ parser: getParserForLanguage("go"),+ },{name: "multiple refs in class/nested structures",original: `@@ -233,11 +234,14 @@ func TestStructuralReplacements(t *testing.T) {},}- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- got, err := ApplyReferences(context.Background(), tt.original, tt.proposed, tt.references, tt.parser)- assert.NoError(t, err)- assert.Equal(t, tt.want, got)- })+ for i, tt := range tests {+ if i >= 0 {+ t.Run(tt.name, func(t *testing.T) {+ got, err := ApplyReferences(context.Background(), tt.original, tt.proposed, tt.references, tt.parser)+ assert.NoError(t, err)+ log.Println(tt.name)+ assert.Equal(t, tt.want, got)+ })+ }}}