@@ -1376,15 +1376,24 @@ protected Task HandleEvaluateRequest(
1376
1376
1377
1377
#region Event Handlers
1378
1378
1379
- private FoldingRange [ ] Fold (
1380
- string documentUri )
1379
+ private FoldingRange [ ] Fold ( string documentUri )
1381
1380
{
1382
1381
// TODO Should be using dynamic registrations
1383
1382
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
+
1384
1392
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 )
1388
1397
{
1389
1398
result . Add ( new FoldingRange {
1390
1399
EndCharacter = fold . EndCharacter ,
@@ -1394,6 +1403,7 @@ private FoldingRange[] Fold(
1394
1403
StartLine = fold . StartLine
1395
1404
} ) ;
1396
1405
}
1406
+
1397
1407
return result . ToArray ( ) ;
1398
1408
}
1399
1409
0 commit comments