@@ -11,7 +11,7 @@ import {
1111 resolveVariables ,
1212} from "./utils/vscode" ;
1313import { initLogging } from "./utils/logging" ;
14- import { TemplateFoldingProvider , type TemplateRange } from "./utils/folding" ;
14+ import { TemplateFoldingProvider } from "./utils/folding" ;
1515import JudgeViewProvider from "./providers/JudgeViewProvider" ;
1616import StressViewProvider from "./providers/StressViewProvider" ;
1717import PanelViewProvider from "./providers/PanelViewProvider" ;
@@ -26,7 +26,7 @@ let templateFoldingProvider: TemplateFoldingProvider;
2626type Dependencies = Record < string , string [ ] > ;
2727
2828// Track inserted template ranges by document URI for folding
29- const templateRangesByUri = new Map < string , TemplateRange > ( ) ;
29+ const templateRangesByUri = new Map < string , vscode . FoldingRange > ( ) ;
3030
3131async function getTemplateContent (
3232 relativeFile : string ,
@@ -281,8 +281,9 @@ function registerCommands(context: vscode.ExtensionContext): void {
281281 return ;
282282 }
283283
284- const startLine = editor . selection . active . line ;
285- const templateLineCount = content . split ( "\n" ) . length - 1 ;
284+ const start = editor . selection . active . line ;
285+ const newlineCount = ( content . match ( / \n / g) || [ ] ) . length ;
286+ const originalSelection = editor . selection ;
286287
287288 const inserted = await editor . edit ( ( edit : vscode . TextEditorEdit ) => {
288289 edit . insert ( editor . selection . active , content ) ;
@@ -295,16 +296,25 @@ function registerCommands(context: vscode.ExtensionContext): void {
295296 const foldTemplate = config . get < boolean > ( "foldFileTemplate" ) ! ;
296297 if ( foldTemplate ) {
297298 // Track the template range for folding
298- const endLine = startLine + templateLineCount ;
299+ // newlineCount is the number of newline characters; the range spans from start to start + newlineCount
300+ const end = start + newlineCount ;
299301 const documentUri = editor . document . uri . toString ( ) ;
300- templateRangesByUri . set ( documentUri , { startLine , endLine } ) ;
302+ templateRangesByUri . set ( documentUri , new vscode . FoldingRange ( start , end ) ) ;
301303
302304 // Notify the folding provider that ranges have changed
303305 templateFoldingProvider . notifyFoldingRangesChanged ( ) ;
304306
305307 // Wait for VS Code's internal folding debounce (~200ms), then fold
306308 setTimeout ( async ( ) => {
309+ // Move cursor to the start of the template so fold operates on the template region
310+ const startPos = new vscode . Position ( start , 0 ) ;
311+ editor . selection = new vscode . Selection ( startPos , startPos ) ;
312+
313+ // Fold at the cursor position
307314 await vscode . commands . executeCommand ( "editor.fold" , { levels : 1 , direction : "down" } ) ;
315+
316+ // Restore the original selection
317+ editor . selection = originalSelection ;
308318 templateRangesByUri . delete ( documentUri ) ;
309319 } , 200 ) ;
310320 }
0 commit comments