@@ -77,13 +77,17 @@ let replaceCharAt = (str: string, col: int, char: string): string => {
7777 *
7878 * Before: | content | (pipe at wrong column)
7979 * After: | content | (pipe at correct column)
80+ *
81+ * Note: position.row is 1-indexed (from error messages), convert to 0-indexed for array access
8082 */
8183let fixMisalignedPipe = (text : string , error : ErrorTypes .t ): option <(string , fixedIssue )> => {
8284 switch error .code {
8385 | MisalignedPipe ({position , expectedCol , actualCol }) => {
8486 let lines = splitLines (text )
87+ // Convert 1-indexed row to 0-indexed for array access
88+ let rowIndex = position .row - 1
8589
86- switch getLine (lines , position . row ) {
90+ switch getLine (lines , rowIndex ) {
8791 | None => None
8892 | Some (line ) => {
8993 // Calculate how many spaces to add or remove
@@ -94,7 +98,7 @@ let fixMisalignedPipe = (text: string, error: ErrorTypes.t): option<(string, fix
9498 insertAt (line , actualCol , String .repeat (" " , diff ))
9599 } else {
96100 // Need to remove spaces before the pipe
97- let removeCount = Js . Math .abs_int (diff )
101+ let removeCount = Math .Int . abs (diff )
98102 // Make sure we're removing spaces, not content
99103 let beforePipe = line -> String .slice (~start = actualCol + diff , ~end = actualCol )
100104 if beforePipe -> String .trim === "" {
@@ -105,15 +109,15 @@ let fixMisalignedPipe = (text: string, error: ErrorTypes.t): option<(string, fix
105109 }
106110
107111 if newLine !== line {
108- let newLines = replaceLine (lines , position . row , newLine )
112+ let newLines = replaceLine (lines , rowIndex , newLine )
109113 let fixedText = joinLines (newLines )
110114
111115 Some ((
112116 fixedText ,
113117 {
114118 original : error ,
115- description : ` Aligned pipe at line ${Int.toString(position.row + 1 )} to column ${Int.toString(expectedCol + 1)}` ,
116- line : position .row + 1 ,
119+ description : ` Aligned pipe at line ${Int.toString(position.row)} to column ${Int.toString(expectedCol + 1)}` ,
120+ line : position .row ,
117121 column : expectedCol + 1 ,
118122 },
119123 ))
@@ -129,13 +133,17 @@ let fixMisalignedPipe = (text: string, error: ErrorTypes.t): option<(string, fix
129133
130134/**
131135 * Fix MisalignedClosingBorder - adjust closing '|' position
136+ *
137+ * Note: position.row is 1-indexed (from error messages), convert to 0-indexed for array access
132138 */
133139let fixMisalignedClosingBorder = (text : string , error : ErrorTypes .t ): option <(string , fixedIssue )> => {
134140 switch error .code {
135141 | MisalignedClosingBorder ({position , expectedCol , actualCol }) => {
136142 let lines = splitLines (text )
143+ // Convert 1-indexed row to 0-indexed for array access
144+ let rowIndex = position .row - 1
137145
138- switch getLine (lines , position . row ) {
146+ switch getLine (lines , rowIndex ) {
139147 | None => None
140148 | Some (line ) => {
141149 let diff = expectedCol - actualCol
@@ -145,7 +153,7 @@ let fixMisalignedClosingBorder = (text: string, error: ErrorTypes.t): option<(st
145153 insertAt (line , actualCol , String .repeat (" " , diff ))
146154 } else {
147155 // Need to remove spaces before the closing pipe
148- let removeCount = Js . Math .abs_int (diff )
156+ let removeCount = Math .Int . abs (diff )
149157 let beforePipe = line -> String .slice (~start = actualCol + diff , ~end = actualCol )
150158 if beforePipe -> String .trim === "" {
151159 removeAt (line , actualCol + diff , removeCount )
@@ -155,15 +163,15 @@ let fixMisalignedClosingBorder = (text: string, error: ErrorTypes.t): option<(st
155163 }
156164
157165 if newLine !== line {
158- let newLines = replaceLine (lines , position . row , newLine )
166+ let newLines = replaceLine (lines , rowIndex , newLine )
159167 let fixedText = joinLines (newLines )
160168
161169 Some ((
162170 fixedText ,
163171 {
164172 original : error ,
165- description : ` Aligned closing border at line ${Int.toString(position.row + 1 )} to column ${Int.toString(expectedCol + 1)}` ,
166- line : position .row + 1 ,
173+ description : ` Aligned closing border at line ${Int.toString(position.row)} to column ${Int.toString(expectedCol + 1)}` ,
174+ line : position .row ,
167175 column : expectedCol + 1 ,
168176 },
169177 ))
@@ -179,30 +187,34 @@ let fixMisalignedClosingBorder = (text: string, error: ErrorTypes.t): option<(st
179187
180188/**
181189 * Fix UnusualSpacing - replace tabs with spaces
190+ *
191+ * Note: position.row is 1-indexed (from error messages), convert to 0-indexed for array access
182192 */
183193let fixUnusualSpacing = (text : string , error : ErrorTypes .t ): option <(string , fixedIssue )> => {
184194 switch error .code {
185195 | UnusualSpacing ({position , issue }) => {
186196 // Check if the issue is about tabs
187197 if issue -> String .includes ("tab" ) || issue -> String .includes ("Tab" ) {
188198 let lines = splitLines (text )
199+ // Convert 1-indexed row to 0-indexed for array access
200+ let rowIndex = position .row - 1
189201
190- switch getLine (lines , position . row ) {
202+ switch getLine (lines , rowIndex ) {
191203 | None => None
192204 | Some (line ) => {
193205 // Replace tabs with 2 spaces (common convention)
194206 let newLine = line -> String .replaceAll ("\t " , " " )
195207
196208 if newLine !== line {
197- let newLines = replaceLine (lines , position . row , newLine )
209+ let newLines = replaceLine (lines , rowIndex , newLine )
198210 let fixedText = joinLines (newLines )
199211
200212 Some ((
201213 fixedText ,
202214 {
203215 original : error ,
204- description : ` Replaced tabs with spaces at line ${Int.toString(position.row + 1 )}` ,
205- line : position .row + 1 ,
216+ description : ` Replaced tabs with spaces at line ${Int.toString(position.row)}` ,
217+ line : position .row ,
206218 column : position .col + 1 ,
207219 },
208220 ))
@@ -221,13 +233,17 @@ let fixUnusualSpacing = (text: string, error: ErrorTypes.t): option<(string, fix
221233
222234/**
223235 * Fix UnclosedBracket - add closing ']' at end of line
236+ *
237+ * Note: opening.row is 1-indexed (from error messages), convert to 0-indexed for array access
224238 */
225239let fixUnclosedBracket = (text : string , error : ErrorTypes .t ): option <(string , fixedIssue )> => {
226240 switch error .code {
227241 | UnclosedBracket ({opening }) => {
228242 let lines = splitLines (text )
243+ // Convert 1-indexed row to 0-indexed for array access
244+ let rowIndex = opening .row - 1
229245
230- switch getLine (lines , opening . row ) {
246+ switch getLine (lines , rowIndex ) {
231247 | None => None
232248 | Some (line ) => {
233249 // Find the content after '[' and close it
@@ -237,15 +253,15 @@ let fixUnclosedBracket = (text: string, error: ErrorTypes.t): option<(string, fi
237253 if ! (trimmedLine -> String .endsWith ("]" )) {
238254 // Add ' ]' with proper spacing
239255 let newLine = trimmedLine ++ " ]"
240- let newLines = replaceLine (lines , opening . row , newLine )
256+ let newLines = replaceLine (lines , rowIndex , newLine )
241257 let fixedText = joinLines (newLines )
242258
243259 Some ((
244260 fixedText ,
245261 {
246262 original : error ,
247- description : ` Added closing bracket at line ${Int.toString(opening.row + 1 )}` ,
248- line : opening .row + 1 ,
263+ description : ` Added closing bracket at line ${Int.toString(opening.row)}` ,
264+ line : opening .row ,
249265 column : String .length (trimmedLine ) + 2 ,
250266 },
251267 ))
@@ -261,18 +277,23 @@ let fixUnclosedBracket = (text: string, error: ErrorTypes.t): option<(string, fi
261277
262278/**
263279 * Fix MismatchedWidth - extend the shorter border to match the longer one
280+ *
281+ * Note: topLeft.row is 1-indexed (from error messages), convert to 0-indexed for array access
264282 */
265283let fixMismatchedWidth = (text : string , error : ErrorTypes .t ): option <(string , fixedIssue )> => {
266284 switch error .code {
267285 | MismatchedWidth ({topLeft , topWidth , bottomWidth }) => {
268286 let lines = splitLines (text )
269287 let diff = topWidth - bottomWidth
288+ // Convert 1-indexed row to 0-indexed for array access
289+ let topLeftRowIndex = topLeft .row - 1
270290
271291 if diff === 0 {
272292 None
273293 } else {
274294 // Find the bottom border line
275295 // We need to trace down from topLeft to find the bottom
296+ // Note: row here is 0-indexed array index
276297 let rec findBottomRow = (row : int ): option <int > => {
277298 if row >= Array .length (lines ) {
278299 None
@@ -284,7 +305,7 @@ let fixMismatchedWidth = (text: string, error: ErrorTypes.t): option<(string, fi
284305 let col = topLeft .col
285306 if col < String .length (line ) {
286307 let char = line -> String .charAt (col )
287- if char === "+" && row > topLeft . row {
308+ if char === "+" && row > topLeftRowIndex {
288309 Some (row )
289310 } else {
290311 findBottomRow (row + 1 )
@@ -297,10 +318,10 @@ let fixMismatchedWidth = (text: string, error: ErrorTypes.t): option<(string, fi
297318 }
298319 }
299320
300- switch findBottomRow (topLeft . row + 1 ) {
321+ switch findBottomRow (topLeftRowIndex + 1 ) {
301322 | None => None
302- | Some (bottomRow ) => {
303- switch getLine (lines , bottomRow ) {
323+ | Some (bottomRowIndex ) => {
324+ switch getLine (lines , bottomRowIndex ) {
304325 | None => None
305326 | Some (bottomLine ) => {
306327 if diff > 0 {
@@ -313,15 +334,15 @@ let fixMismatchedWidth = (text: string, error: ErrorTypes.t): option<(string, fi
313334 let dashes = String .repeat ("-" , diff )
314335 let newLine = before ++ dashes ++ after
315336
316- let newLines = replaceLine (lines , bottomRow , newLine )
337+ let newLines = replaceLine (lines , bottomRowIndex , newLine )
317338 let fixedText = joinLines (newLines )
318339
319340 Some ((
320341 fixedText ,
321342 {
322343 original : error ,
323- description : ` Extended bottom border at line ${Int.toString(bottomRow + 1)} by ${Int.toString(diff)} characters` ,
324- line : bottomRow + 1 ,
344+ description : ` Extended bottom border at line ${Int.toString(bottomRowIndex + 1)} by ${Int.toString(diff)} characters` ,
345+ line : bottomRowIndex + 1 ,
325346 column : closingPlusCol + 1 ,
326347 },
327348 ))
@@ -332,25 +353,25 @@ let fixMismatchedWidth = (text: string, error: ErrorTypes.t): option<(string, fi
332353 // Top is shorter, need to extend it
333354 // This is trickier as it affects content alignment
334355 // For now, we extend the top border
335- switch getLine (lines , topLeft . row ) {
356+ switch getLine (lines , topLeftRowIndex ) {
336357 | None => None
337358 | Some (topLine ) => {
338359 let closingPlusCol = topLeft .col + topWidth - 1
339360 if closingPlusCol >= 0 && closingPlusCol < String .length (topLine ) {
340361 let before = topLine -> String .slice (~start = 0 , ~end = closingPlusCol )
341362 let after = topLine -> String .sliceToEnd (~start = closingPlusCol )
342- let dashes = String .repeat ("-" , Js . Math .abs_int (diff ))
363+ let dashes = String .repeat ("-" , Math .Int . abs (diff ))
343364 let newLine = before ++ dashes ++ after
344365
345- let newLines = replaceLine (lines , topLeft . row , newLine )
366+ let newLines = replaceLine (lines , topLeftRowIndex , newLine )
346367 let fixedText = joinLines (newLines )
347368
348369 Some ((
349370 fixedText ,
350371 {
351372 original : error ,
352- description : ` Extended top border at line ${Int.toString(topLeft.row + 1 )} by ${Int.toString(Js. Math.abs_int (diff))} characters` ,
353- line : topLeft .row + 1 ,
373+ description : ` Extended top border at line ${Int.toString(topLeft.row)} by ${Int.toString(Math.Int.abs (diff))} characters` ,
374+ line : topLeft .row ,
354375 column : closingPlusCol + 1 ,
355376 },
356377 ))
0 commit comments