From 1c12c2b714ca657c912e081a10422700a63c8cf1 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Fri, 27 Sep 2024 14:18:48 +0200 Subject: [PATCH] Fix codemirror fold for empty lines (#22130) --- src/resources/codemirror.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/resources/codemirror.ts b/src/resources/codemirror.ts index 3a9fb811df..59396f23f3 100644 --- a/src/resources/codemirror.ts +++ b/src/resources/codemirror.ts @@ -277,8 +277,17 @@ export const haSyntaxHighlighting = syntaxHighlighting(haHighlightStyle); // A folding service for indent-based languages such as YAML. export const foldingOnIndent = foldService.of((state, from, to) => { const line = state.doc.lineAt(from); + + // empty lines continue their indentation from surrounding lines + if (!line.length || !line.text.trim().length) { + return null; + } + + let onlyEmptyNext = true; + const lineCount = state.doc.lines; const indent = line.text.search(/\S|$/); // Indent level of the first line + let foldStart = from; // Start of the fold let foldEnd = to; // End of the fold @@ -291,7 +300,15 @@ export const foldingOnIndent = foldService.of((state, from, to) => { const nextIndent = nextLine.text.search(/\S|$/); // Indent level of the next line // If the next line is on a deeper indent level, add it to the fold - if (nextIndent > indent) { + // empty lines continue their indentation from surrounding lines + if ( + !nextLine.length || + !nextLine.text.trim().length || + nextIndent > indent + ) { + if (onlyEmptyNext) { + onlyEmptyNext = nextLine.text.trim().length === 0; + } // include this line in the fold and continue foldEnd = nextLine.to; } else { @@ -301,7 +318,10 @@ export const foldingOnIndent = foldService.of((state, from, to) => { } // Don't create fold if it's a single line - if (state.doc.lineAt(foldStart).number === state.doc.lineAt(foldEnd).number) { + if ( + onlyEmptyNext || + state.doc.lineAt(foldStart).number === state.doc.lineAt(foldEnd).number + ) { return null; }