public static IFoldSegment CreateFoldSegment(TextEditor editor, int offset, int length, bool isCollapsed = false, string collapsedText = "...", FoldingType foldingType = FoldingType.Unknown) { if (editor == null) { throw new ArgumentNullException("editor"); } var result = editor.CreateFoldSegment(offset, length, isCollapsed); result.CollapsedText = collapsedText; result.FoldingType = foldingType; return(result); }
async Task UpdateFoldings(ParsedDocument parsedDocument, DocumentLocation caretLocation, bool firstTime = false, CancellationToken token = default(CancellationToken)) { if (parsedDocument == null || !textEditor.Options.ShowFoldMargin || isDisposed) { return; } // don't update parsed documents that contain errors - the foldings from there may be invalid. if (parsedDocument.HasErrors) { return; } try { var foldSegments = new List <IFoldSegment> (); foreach (FoldingRegion region in await parsedDocument.GetFoldingsAsync(token)) { if (token.IsCancellationRequested) { return; } var type = FoldingType.Unknown; bool setFolded = false; bool folded = false; //decide whether the regions should be folded by default switch (region.Type) { case FoldType.Member: type = FoldingType.TypeMember; break; case FoldType.Type: type = FoldingType.TypeDefinition; break; case FoldType.UserRegion: type = FoldingType.Region; setFolded = DefaultSourceEditorOptions.Instance.DefaultRegionsFolding; folded = true; break; case FoldType.Comment: type = FoldingType.Comment; setFolded = DefaultSourceEditorOptions.Instance.DefaultCommentFolding; folded = true; break; case FoldType.CommentInsideMember: type = FoldingType.Comment; setFolded = DefaultSourceEditorOptions.Instance.DefaultCommentFolding; folded = false; break; case FoldType.Undefined: setFolded = true; folded = region.IsFoldedByDefault; break; } var start = textEditor.LocationToOffset(region.Region.Begin); var end = textEditor.LocationToOffset(region.Region.End); var marker = textEditor.CreateFoldSegment(start, end - start); foldSegments.Add(marker); marker.CollapsedText = region.Name; marker.FoldingType = type; //and, if necessary, set its fold state if (marker != null && setFolded && firstTime) { // only fold on document open, later added folds are NOT folded by default. marker.IsCollapsed = folded; continue; } if (marker != null && region.Region.Contains(caretLocation.Line, caretLocation.Column)) { marker.IsCollapsed = false; } } if (firstTime) { textEditor.SetFoldings(foldSegments); } else { Application.Invoke(delegate { if (!token.IsCancellationRequested) { textEditor.SetFoldings(foldSegments); } }); } } catch (Exception ex) { LoggingService.LogError("Unhandled exception in ParseInformationUpdaterWorkerThread", ex); } }