Repository: goplus/gop
Pull request #1038 (2021-12-08 06:51:54)

x/format/_testdata/gopsyntax


Patch:
diff --git a/x/format/format.go b/x/format/format.go
index 846a3b9e..15833ed6 100644
--- a/x/format/format.go
+++ b/x/format/format.go
@@ -17,6 +17,8 @@
 package format
 
 import (
+	"go/token"
+
 	"github.com/goplus/gop/ast"
 )
 
@@ -46,7 +48,9 @@ func fmtToBuiltin(ctx *importCtx, sel *ast.Ident, ref *ast.Expr) bool {
 }
 
 func commandStyleFirst(v *ast.CallExpr) {
-	v.NoParenEnd = v.Rparen
+	if v.NoParenEnd == token.NoPos {
+		v.NoParenEnd = v.Rparen
+	}
 }
 
 func fncallStartingLowerCase(v *ast.CallExpr) {
diff --git a/x/format/stmt_expr_or_type.go b/x/format/stmt_expr_or_type.go
index ad78fed7..36ea11f6 100644
--- a/x/format/stmt_expr_or_type.go
+++ b/x/format/stmt_expr_or_type.go
@@ -91,6 +91,8 @@ func formatExpr(ctx *formatCtx, expr ast.Expr, ref *ast.Expr) {
 	case *ast.IndexExpr:
 		formatExpr(ctx, v.X, &v.X)
 		formatExpr(ctx, v.Index, &v.Index)
+	case *ast.SliceLit:
+		formatExprs(ctx, v.Elts)
 	case *ast.CompositeLit:
 		formatType(ctx, v.Type, &v.Type)
 		formatExprs(ctx, v.Elts)
@@ -105,6 +107,17 @@ func formatExpr(ctx *formatCtx, expr ast.Expr, ref *ast.Expr) {
 	case *ast.TypeAssertExpr:
 		formatExpr(ctx, v.X, &v.X)
 		formatType(ctx, v.Type, &v.Type)
+	case *ast.LambdaExpr:
+		formatExprs(ctx, v.Rhs)
+	case *ast.LambdaExpr2:
+		formatBlockStmt(ctx, v.Body)
+	case *ast.RangeExpr:
+		formatRangeExpr(ctx, v)
+	case *ast.ComprehensionExpr:
+		formatComprehensionExpr(ctx, v)
+	case *ast.ErrWrapExpr:
+		formatExpr(ctx, v.X, &v.X)
+		formatExpr(ctx, v.Default, &v.Default)
 	case *ast.ParenExpr:
 		formatExpr(ctx, v.X, &v.X)
 	case *ast.Ellipsis:
@@ -114,6 +127,29 @@ func formatExpr(ctx *formatCtx, expr ast.Expr, ref *ast.Expr) {
 	}
 }
 
+func formatRangeExpr(ctx *formatCtx, v *ast.RangeExpr) {
+	formatExpr(ctx, v.First, &v.First)
+	formatExpr(ctx, v.Last, &v.Last)
+	formatExpr(ctx, v.Expr3, &v.Expr3)
+}
+
+func formatComprehensionExpr(ctx *formatCtx, v *ast.ComprehensionExpr) {
+	formatForPhrases(ctx, v.Fors)
+	formatExpr(ctx, v.Elt, &v.Elt)
+}
+
+func formatForPhrases(ctx *formatCtx, fors []*ast.ForPhrase) {
+	for _, f := range fors {
+		formatForPhrase(ctx, f)
+	}
+}
+
+func formatForPhrase(ctx *formatCtx, v *ast.ForPhrase) {
+	formatExpr(ctx, v.X, &v.X)
+	formatStmt(ctx, v.Init)
+	formatExpr(ctx, v.Cond, &v.Cond)
+}
+
 func formatSliceExpr(ctx *formatCtx, v *ast.SliceExpr) {
 	formatExpr(ctx, v.X, &v.X)
 	formatExpr(ctx, v.Low, &v.Low)
@@ -167,6 +203,9 @@ func formatStmt(ctx *formatCtx, stmt ast.Stmt) {
 		formatForStmt(ctx, v)
 	case *ast.RangeStmt:
 		formatRangeStmt(ctx, v)
+	case *ast.ForPhraseStmt:
+		formatForPhrase(ctx, v.ForPhrase)
+		formatBlockStmt(ctx, v.Body)
 	case *ast.IfStmt:
 		formatIfStmt(ctx, v)
 	case *ast.CaseClause:


Test patch:
diff --git a/x/format/_testdata/gopsyntax/format.expect b/x/format/_testdata/gopsyntax/format.expect
new file mode 100644
index 00000000..b783df28
--- /dev/null
+++ b/x/format/_testdata/gopsyntax/format.expect
@@ -0,0 +1,9 @@
+a := [1, 2]
+plot x => x * x
+onStart x => {
+	println x
+}
+for i <- :10 {
+}
+println [x*x for x <- :10]
+print f()?:10
diff --git a/x/format/_testdata/gopsyntax/index.gop b/x/format/_testdata/gopsyntax/index.gop
new file mode 100644
index 00000000..b783df28
--- /dev/null
+++ b/x/format/_testdata/gopsyntax/index.gop
@@ -0,0 +1,9 @@
+a := [1, 2]
+plot x => x * x
+onStart x => {
+	println x
+}
+for i <- :10 {
+}
+println [x*x for x <- :10]
+print f()?:10