@@ -1009,9 +1009,8 @@ func (f *File) SetRequireSeparateIndirect(req []*Require) {
10091009 // We may insert new requirements into the last uncommented
10101010 // direct-only and indirect-only blocks. We may also move requirements
10111011 // to the opposite block if their indirect markings change.
1012- lastDirectBlock , lastIndirectBlock * LineBlock
1013- lastDirectBlockIndex = - 1
1014- lastIndirectBlockIndex = - 1
1012+ lastDirectIndex = - 1
1013+ lastIndirectIndex = - 1
10151014
10161015 // If there are no direct-only or indirect-only blocks, a new block may
10171016 // be inserted after the last require line or block.
@@ -1033,6 +1032,13 @@ func (f *File) SetRequireSeparateIndirect(req []*Require) {
10331032 }
10341033 lastRequireIndex = i
10351034 requireLineOrBlockCount ++
1035+ if ! hasComments (stmt .Comments ) {
1036+ if isIndirect (stmt ) {
1037+ lastIndirectIndex = i
1038+ } else {
1039+ lastDirectIndex = i
1040+ }
1041+ }
10361042
10371043 case * LineBlock :
10381044 if len (stmt .Token ) == 0 || stmt .Token [0 ] != "require" {
@@ -1054,46 +1060,67 @@ func (f *File) SetRequireSeparateIndirect(req []*Require) {
10541060 }
10551061 }
10561062 if allDirect {
1057- lastDirectBlock = stmt
1058- lastDirectBlockIndex = i
1063+ lastDirectIndex = i
10591064 }
10601065 if allIndirect {
1061- lastIndirectBlock = stmt
1062- lastIndirectBlockIndex = i
1066+ lastIndirectIndex = i
10631067 }
10641068 }
10651069 }
10661070
10671071 oneFlatUncommentedBlock := requireLineOrBlockCount == 1 &&
10681072 ! hasComments (* f .Syntax .Stmt [lastRequireIndex ].Comment ())
10691073
1070- // Create direct and indirect blocks if needed. It's okay if they're empty.
1071- // Cleanup will remove them and convert one-line blocks to lines.
1072- if lastDirectBlock == nil {
1073- lastDirectBlock = & LineBlock {Token : []string {"require" }}
1074- i := len (f .Syntax .Stmt )
1075- if lastIndirectBlockIndex >= 0 {
1076- i = lastIndirectBlockIndex
1077- lastIndirectBlockIndex ++
1078- } else if lastRequireIndex >= 0 {
1079- i = lastRequireIndex + 1
1080- }
1074+ // Create direct and indirect blocks if needed. Convert lines into blocks
1075+ // if needed. If we end up with an empty block or a one-line block,
1076+ // Cleanup will delete it or convert it to a line later.
1077+ insertBlock := func (i int ) * LineBlock {
1078+ block := & LineBlock {Token : []string {"require" }}
10811079 f .Syntax .Stmt = append (f .Syntax .Stmt , nil )
10821080 copy (f .Syntax .Stmt [i + 1 :], f .Syntax .Stmt [i :])
1083- f .Syntax .Stmt [i ] = lastDirectBlock
1084- lastDirectBlockIndex = i
1081+ f .Syntax .Stmt [i ] = block
1082+ return block
10851083 }
10861084
1087- if lastIndirectBlock == nil {
1088- lastIndirectBlock = & LineBlock {Token : []string {"require" }}
1089- i := len (f .Syntax .Stmt )
1090- if lastDirectBlockIndex >= 0 {
1091- i = lastDirectBlockIndex + 1
1085+ ensureBlock := func (i int ) * LineBlock {
1086+ switch stmt := f .Syntax .Stmt [i ].(type ) {
1087+ case * LineBlock :
1088+ return stmt
1089+ case * Line :
1090+ block := & LineBlock {
1091+ Token : []string {"require" },
1092+ Line : []* Line {stmt },
1093+ }
1094+ stmt .Token = stmt .Token [1 :] // remove "require"
1095+ stmt .InBlock = true
1096+ f .Syntax .Stmt [i ] = block
1097+ return block
1098+ default :
1099+ panic (fmt .Sprintf ("unexpected statement: %v" , stmt ))
10921100 }
1093- f .Syntax .Stmt = append (f .Syntax .Stmt , nil )
1094- copy (f .Syntax .Stmt [i + 1 :], f .Syntax .Stmt [i :])
1095- f .Syntax .Stmt [i ] = lastIndirectBlock
1096- lastIndirectBlockIndex = i
1101+ }
1102+
1103+ var lastDirectBlock * LineBlock
1104+ if lastDirectIndex < 0 {
1105+ if lastIndirectIndex >= 0 {
1106+ lastDirectIndex = lastIndirectIndex
1107+ lastIndirectIndex ++
1108+ } else if lastRequireIndex >= 0 {
1109+ lastDirectIndex = lastRequireIndex + 1
1110+ } else {
1111+ lastDirectIndex = len (f .Syntax .Stmt )
1112+ }
1113+ lastDirectBlock = insertBlock (lastDirectIndex )
1114+ } else {
1115+ lastDirectBlock = ensureBlock (lastDirectIndex )
1116+ }
1117+
1118+ var lastIndirectBlock * LineBlock
1119+ if lastIndirectIndex < 0 {
1120+ lastIndirectIndex = lastDirectIndex + 1
1121+ lastIndirectBlock = insertBlock (lastIndirectIndex )
1122+ } else {
1123+ lastIndirectBlock = ensureBlock (lastIndirectIndex )
10971124 }
10981125
10991126 // Delete requirements we don't want anymore.
0 commit comments