public override void Run() { ITextEditor editor = SD.GetActiveViewContentService <ITextEditor>(); FoldingManager foldingManager = editor.GetService(typeof(FoldingManager)) as FoldingManager; if (foldingManager != null) { // look for folding on this line: FoldingSection folding = foldingManager.GetNextFolding(editor.Document.PositionToOffset(editor.Caret.Line, 1)); if (folding == null || editor.Document.GetLineForOffset(folding.StartOffset).LineNumber != editor.Caret.Line) { // no folding found on current line: find innermost folding containing the caret folding = foldingManager.GetFoldingsContaining(editor.Caret.Offset).LastOrDefault(); } if (folding != null) { folding.IsFolded = !folding.IsFolded; } } }
/// <summary> /// Toggles the folding at the caret position. /// </summary> public void ToggleCurrentFolding() { if (FoldingManager == null) { return; } // Get next folding on the current line. var folding = FoldingManager.GetNextFolding(CaretOffset); if (folding == null || Document.GetLocation(folding.StartOffset).Line != Document.GetLocation(CaretOffset).Line) { // No folding after caret on the same line. --> Search for innermost folding that contains // the caret. folding = FoldingManager.GetFoldingsContaining(CaretOffset).LastOrDefault(); } // Toggle folding if a folding was found. if (folding != null) { folding.IsFolded = !folding.IsFolded; } }
public void Execute(TextViewContext context) { var textView = context.TextView; if (null == textView) { return; } var editor = textView.textEditor; FoldingManager foldingManager = context.TextView.FoldingManager; if (null == foldingManager) { return; } // TODO: or use Caret if position is not given? var posBox = context.Position; if (null == posBox) { return; } TextViewPosition pos = posBox.Value; // look for folding on this line: FoldingSection folding = foldingManager.GetNextFolding(editor.Document.GetOffset(pos.Line, 1)); if (folding == null || editor.Document.GetLineByOffset(folding.StartOffset).LineNumber != pos.Line) { // no folding found on current line: find innermost folding containing the mouse position folding = foldingManager.GetFoldingsContaining(editor.Document.GetOffset(pos.Line, pos.Column)).LastOrDefault(); } if (folding != null) { folding.IsFolded = !folding.IsFolded; } }