{"record":{"id":"b899145e94b9f0d3","repo":"ajaxorg/ace","slug":"trying-to-add-fold-to-foldrow-that-doesn-t-have-a","errorCode":null,"errorMessage":"Trying to add fold to FoldRow that doesn't have a matching row","messagePattern":"Trying to add fold to FoldRow that doesn't have a matching row","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/edit_session/fold_line.js","lineNumber":75,"sourceCode":"                return -a.range.compareEnd(b.start.row, b.start.column);\n            });\n            if (this.range.compareEnd(fold.start.row, fold.start.column) > 0) {\n                this.end.row = fold.end.row;\n                this.end.column =  fold.end.column;\n            } else if (this.range.compareStart(fold.end.row, fold.end.column) < 0) {\n                this.start.row = fold.start.row;\n                this.start.column = fold.start.column;\n            }\n        } else if (fold.start.row == this.end.row) {\n            this.folds.push(fold);\n            this.end.row = fold.end.row;\n            this.end.column = fold.end.column;\n        } else if (fold.end.row == this.start.row) {\n            this.folds.unshift(fold);\n            this.start.row = fold.start.row;\n            this.start.column = fold.start.column;\n        } else {\n            throw new Error(\"Trying to add fold to FoldRow that doesn't have a matching row\");\n        }\n        fold.foldLine = this;\n    }\n\n    /**\n     * @param {number} row\n     */\n    containsRow(row) {\n        return row >= this.start.row && row <= this.end.row;\n    }\n\n    /**\n     * @param {Function} callback\n     * @param {number} endRow\n     * @param {number} endColumn\n     */\n    walk(callback, endRow, endColumn) {\n        var lastEnd = 0,","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/ajaxorg/ace/blob/2c1eddc392eb92cf222a4478f1893d2b2c354fb0/src/edit_session/fold_line.js#L57-L93","documentation":"FoldLine.addFold() places a fold either at the end of the line (start.row == endRow), at the start (end.row == startRow), or as sameRow. A fold whose rows match none of these cannot belong to this FoldLine, so Ace throws to protect fold integrity.","triggerScenarios":"Adding a fold whose row range partially overlaps a fold line (e.g. fold from row 3 to row 7 on a FoldLine covering rows 2-4); manually constructing folds across multiple FoldLines.","commonSituations":"Custom folding plugins computing ranges incorrectly; folding operations applied after text changed line numbers without updating fold state.","solutions":["Ensure fold ranges align with FoldLine boundaries (single row, or start/end matching the FoldLine's rows)","Use editSession.addFold / unfold APIs rather than manipulating FoldLine directly","Unfold and re-fold the region if line numbers shifted after edits"],"exampleFix":"// before\nfoldLine.addFold(new Fold(3, 0, 7, 5)); // spans beyond this fold line\n// after\nsession.addFold('...', new AceRange(3, 0, 7, 5));","handlingStrategy":"try-catch","validationCode":"if (!(fold.end.row === foldLine.start.row || fold.start.row === foldLine.endRow || (fold.start.row === fold.end.row && fold.start.row >= foldLine.startRow && fold.end.row <= foldLine.endRow))) {\n  throw new Error('fold rows do not align with FoldLine');\n}","typeGuard":"function foldFits(foldLine, fold) { return fold.start.row >= foldLine.startRow && fold.end.row <= foldLine.endRow; }","tryCatchPattern":"try { session.addFold('...', range); } catch (e) { if (/matching row/.test(e.message)) { session.unfold(range); session.addFold('...', range); } else throw e; }","preventionTips":["Keep fold ranges on single rows or aligned to existing fold boundaries","Use session-level folding APIs exclusively","Refresh fold state after programmatic document edits"],"tags":["folding","internal-state"],"backgroundTag":"invalid-fold-operation","analyzedSha":"2c1eddc392eb92cf222a4478f1893d2b2c354fb0","analyzedAt":"2026-08-30T00:39:52.222Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}