@@ -1376,15 +1376,24 @@ protected Task HandleEvaluateRequest(
13761376
13771377 #region Event Handlers
13781378
1379- private FoldingRange [ ] Fold (
1380- string documentUri )
1379+ private FoldingRange [ ] Fold ( string documentUri )
13811380 {
13821381 // TODO Should be using dynamic registrations
13831382 if ( ! this . currentSettings . CodeFolding . Enable ) { return null ; }
1383+
1384+ // Avoid crash when using untitled: scheme or any other scheme where the document doesn't
1385+ // have a backing file. https://github.com/PowerShell/vscode-powershell/issues/1676
1386+ // Perhaps a better option would be to parse the contents of the document as a string
1387+ // as opposed to reading a file but the senario of "no backing file" probably doesn't
1388+ // warrant the extra effort.
1389+ ScriptFile scriptFile ;
1390+ if ( ! editorSession . Workspace . TryGetFile ( documentUri , out scriptFile ) ) { return null ; }
1391+
13841392 var result = new List < FoldingRange > ( ) ;
1385- foreach ( FoldingReference fold in TokenOperations . FoldableRegions (
1386- editorSession . Workspace . GetFile ( documentUri ) . ScriptTokens ,
1387- this . currentSettings . CodeFolding . ShowLastLine ) )
1393+ FoldingReference [ ] foldableRegions =
1394+ TokenOperations . FoldableRegions ( scriptFile . ScriptTokens , this . currentSettings . CodeFolding . ShowLastLine ) ;
1395+
1396+ foreach ( FoldingReference fold in foldableRegions )
13881397 {
13891398 result . Add ( new FoldingRange {
13901399 EndCharacter = fold . EndCharacter ,
@@ -1394,6 +1403,7 @@ private FoldingRange[] Fold(
13941403 StartLine = fold . StartLine
13951404 } ) ;
13961405 }
1406+
13971407 return result . ToArray ( ) ;
13981408 }
13991409
0 commit comments