diff --git a/ILSpy/ContextMenuEntry.cs b/ILSpy/ContextMenuEntry.cs index df64b4046..09fa91560 100644 --- a/ILSpy/ContextMenuEntry.cs +++ b/ILSpy/ContextMenuEntry.cs @@ -21,6 +21,8 @@ using System.ComponentModel.Composition; using System.Linq; using System.Windows; using System.Windows.Controls; + +using ICSharpCode.AvalonEdit; using ICSharpCode.ILSpy.TextView; using ICSharpCode.TreeView; @@ -59,15 +61,23 @@ namespace ICSharpCode.ILSpy /// public ReferenceSegment Reference { get; private set; } + /// + /// Returns the position in TextView the mouse cursor is currently hovering above. + /// Returns null, if TextView returns null; + /// + public TextViewPosition? Position { get; private set; } + public static TextViewContext Create(SharpTreeView treeView = null, DecompilerTextView textView = null) { var reference = textView != null ? textView.GetReferenceSegmentAtMousePosition() : null; + var position = textView != null ? textView.GetPositionFromMousePosition() : null; var selectedTreeNodes = treeView != null ? treeView.GetTopLevelSelection().ToArray() : null; return new TextViewContext { TreeView = treeView, SelectedTreeNodes = selectedTreeNodes, TextView = textView, - Reference = reference + Reference = reference, + Position = position }; } } diff --git a/ILSpy/TextView/DecompilerTextView.cs b/ILSpy/TextView/DecompilerTextView.cs index 7f3ce1879..b13e15db4 100644 --- a/ILSpy/TextView/DecompilerTextView.cs +++ b/ILSpy/TextView/DecompilerTextView.cs @@ -763,19 +763,18 @@ namespace ICSharpCode.ILSpy.TextView internal ReferenceSegment GetReferenceSegmentAtMousePosition() { - TextViewPosition? position = textEditor.TextArea.TextView.GetPosition(Mouse.GetPosition(textEditor.TextArea.TextView) + textEditor.TextArea.TextView.ScrollOffset); + TextViewPosition? position = GetPositionFromMousePosition(); if (position == null) return null; int offset = textEditor.Document.GetOffset(position.Value.Location); return referenceElementGenerator.References.FindSegmentsContaining(offset).FirstOrDefault(); } - public int CurrentOffset { - get { - return textEditor.CaretOffset; - } + internal TextViewPosition? GetPositionFromMousePosition() + { + return textEditor.TextArea.TextView.GetPosition(Mouse.GetPosition(textEditor.TextArea.TextView) + textEditor.TextArea.TextView.ScrollOffset); } - + public DecompilerTextViewState GetState() { if (decompiledNodes == null) diff --git a/ILSpy/TextView/FoldingCommands.cs b/ILSpy/TextView/FoldingCommands.cs index c14cee2f6..314a1412b 100644 --- a/ILSpy/TextView/FoldingCommands.cs +++ b/ILSpy/TextView/FoldingCommands.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; using System.Linq; +using ICSharpCode.AvalonEdit; using ICSharpCode.AvalonEdit.Folding; namespace ICSharpCode.ILSpy.TextView @@ -62,16 +63,23 @@ namespace ICSharpCode.ILSpy.TextView public void Execute(TextViewContext context) { - if (null == context.TextView) - return; + var textView = context.TextView; + if (null == textView) + return; + var editor = textView.textEditor; FoldingManager foldingManager = context.TextView.FoldingManager; if (null == foldingManager) return; - int offset = context.TextView.CurrentOffset; - FoldingSection folding = foldingManager.GetNextFolding(offset); - if (folding == null || folding.StartOffset != offset) { - // no folding found on current line: find innermost folding containing the caret - folding = foldingManager.GetFoldingsContaining(offset).LastOrDefault(); + // 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;