@@ -11,6 +11,7 @@ import {
1111 resolveVariables ,
1212} from "./utils/vscode" ;
1313import { initLogging } from "./utils/logging" ;
14+ import { TemplateFoldingProvider , type TemplateRange } from "./utils/folding" ;
1415import JudgeViewProvider from "./providers/JudgeViewProvider" ;
1516import StressViewProvider from "./providers/StressViewProvider" ;
1617import PanelViewProvider from "./providers/PanelViewProvider" ;
@@ -20,9 +21,13 @@ import { createStatusBarItem } from "./statusBar";
2021let judgeViewProvider : JudgeViewProvider ;
2122let stressViewProvider : StressViewProvider ;
2223let panelViewProvider : PanelViewProvider ;
24+ let templateFoldingProvider : TemplateFoldingProvider ;
2325
2426type Dependencies = Record < string , string [ ] > ;
2527
28+ // Track inserted template ranges by document URI for folding
29+ const templateRangesByUri = new Map < string , TemplateRange > ( ) ;
30+
2631async function getTemplateContent (
2732 relativeFile : string ,
2833 baseDirectory : string ,
@@ -123,10 +128,27 @@ function registerDocumentContentProviders(context: vscode.ExtensionContext): voi
123128 context . subscriptions . push (
124129 vscode . workspace . onDidCloseTextDocument ( ( document ) => {
125130 ReadonlyStringProvider . cleanup ( document . uri ) ;
131+ // Also clean up template ranges for closed documents
132+ templateRangesByUri . delete ( document . uri . toString ( ) ) ;
126133 } )
127134 ) ;
128135}
129136
137+ function registerFoldingProvider ( context : vscode . ExtensionContext ) : void {
138+ // Create the folding provider with a callback to get template ranges
139+ templateFoldingProvider = new TemplateFoldingProvider ( ( documentUri ) => {
140+ return templateRangesByUri . get ( documentUri ) ;
141+ } ) ;
142+
143+ // Register for all file scheme documents
144+ context . subscriptions . push (
145+ vscode . languages . registerFoldingRangeProvider ( { scheme : "file" } , templateFoldingProvider )
146+ ) ;
147+
148+ // Dispose the provider when extension deactivates
149+ context . subscriptions . push ( templateFoldingProvider ) ;
150+ }
151+
130152function registerCommands ( context : vscode . ExtensionContext ) : void {
131153 registerRunSettingsCommands ( context ) ;
132154
@@ -254,14 +276,37 @@ function registerCommands(context: vscode.ExtensionContext): void {
254276 return ;
255277 }
256278
257- const inserted = vscode . window . activeTextEditor ?. edit ( ( edit : vscode . TextEditorEdit ) => {
258- if ( vscode . window . activeTextEditor ) {
259- edit . insert ( vscode . window . activeTextEditor . selection . active , content ) ;
260- }
279+ const editor = vscode . window . activeTextEditor ;
280+ if ( ! editor ) {
281+ return ;
282+ }
283+
284+ const startLine = editor . selection . active . line ;
285+ const templateLineCount = content . split ( "\n" ) . length - 1 ;
286+
287+ const inserted = await editor . edit ( ( edit : vscode . TextEditorEdit ) => {
288+ edit . insert ( editor . selection . active , content ) ;
261289 } ) ;
290+
291+ if ( ! inserted ) {
292+ return ;
293+ }
294+
262295 const foldTemplate = config . get < boolean > ( "foldFileTemplate" ) ! ;
263- if ( inserted && foldTemplate ) {
264- vscode . commands . executeCommand ( "editor.fold" ) ;
296+ if ( foldTemplate ) {
297+ // Track the template range for folding
298+ const endLine = startLine + templateLineCount ;
299+ const documentUri = editor . document . uri . toString ( ) ;
300+ templateRangesByUri . set ( documentUri , { startLine, endLine } ) ;
301+
302+ // Notify the folding provider that ranges have changed
303+ templateFoldingProvider . notifyFoldingRangesChanged ( ) ;
304+
305+ // Wait for VS Code's internal folding debounce (~200ms), then fold
306+ setTimeout ( async ( ) => {
307+ await vscode . commands . executeCommand ( "editor.fold" , { levels : 1 , direction : "down" } ) ;
308+ templateRangesByUri . delete ( documentUri ) ;
309+ } , 200 ) ;
265310 }
266311 } ) ( ) ;
267312 } )
@@ -361,6 +406,7 @@ export function activate(context: vscode.ExtensionContext): void {
361406 registerViewProviders ( context ) ;
362407 registerCommands ( context ) ;
363408 registerDocumentContentProviders ( context ) ;
409+ registerFoldingProvider ( context ) ;
364410
365411 createStatusBarItem ( context ) ;
366412
0 commit comments