/// <summary> /// Helper method to create a stub script file and then call FoldableRegions /// </summary> private static FoldingReference[] GetRegions(string text) { ScriptFile scriptFile = new( // Use any absolute path. Even if it doesn't exist. DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")), text, Version.Parse("5.0")); FoldingReference[] result = TokenOperations.FoldableReferences(scriptFile.ScriptTokens).ToArray(); // The foldable regions need to be deterministic for testing so sort the array. Array.Sort(result); return(result); }
public Task <Container <FoldingRange> > Handle(FoldingRangeRequestParam request, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { _logger.LogDebug("FoldingRange request canceled for file: {0}", request.TextDocument.Uri); return(Task.FromResult(new Container <FoldingRange>())); } // TODO Should be using dynamic registrations if (!_configurationService.CurrentSettings.CodeFolding.Enable) { return(null); } // Avoid crash when using untitled: scheme or any other scheme where the document doesn't // have a backing file. https://github.com/PowerShell/vscode-powershell/issues/1676 // Perhaps a better option would be to parse the contents of the document as a string // as opposed to reading a file but the scenario of "no backing file" probably doesn't // warrant the extra effort. if (!_workspaceService.TryGetFile(request.TextDocument.Uri, out ScriptFile scriptFile)) { return(null); } var result = new List <FoldingRange>(); // If we're showing the last line, decrement the Endline of all regions by one. int endLineOffset = _configurationService.CurrentSettings.CodeFolding.ShowLastLine ? -1 : 0; foreach (FoldingReference fold in TokenOperations.FoldableReferences(scriptFile.ScriptTokens).References) { result.Add(new FoldingRange { EndCharacter = fold.EndCharacter, EndLine = fold.EndLine + endLineOffset, Kind = fold.Kind, StartCharacter = fold.StartCharacter, StartLine = fold.StartLine }); } return(Task.FromResult(new Container <FoldingRange>(result))); }