Browse Source

Adapt folding commands from SharpDevelop

pull/345/head
Ronny Klier 13 years ago
parent
commit
e68c9c907f
  1. 12
      ILSpy/ContextMenuEntry.cs
  2. 11
      ILSpy/TextView/DecompilerTextView.cs
  3. 22
      ILSpy/TextView/FoldingCommands.cs

12
ILSpy/ContextMenuEntry.cs

@ -21,6 +21,8 @@ using System.ComponentModel.Composition;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using ICSharpCode.AvalonEdit;
using ICSharpCode.ILSpy.TextView; using ICSharpCode.ILSpy.TextView;
using ICSharpCode.TreeView; using ICSharpCode.TreeView;
@ -59,15 +61,23 @@ namespace ICSharpCode.ILSpy
/// </summary> /// </summary>
public ReferenceSegment Reference { get; private set; } public ReferenceSegment Reference { get; private set; }
/// <summary>
/// Returns the position in TextView the mouse cursor is currently hovering above.
/// Returns null, if TextView returns null;
/// </summary>
public TextViewPosition? Position { get; private set; }
public static TextViewContext Create(SharpTreeView treeView = null, DecompilerTextView textView = null) public static TextViewContext Create(SharpTreeView treeView = null, DecompilerTextView textView = null)
{ {
var reference = textView != null ? textView.GetReferenceSegmentAtMousePosition() : null; var reference = textView != null ? textView.GetReferenceSegmentAtMousePosition() : null;
var position = textView != null ? textView.GetPositionFromMousePosition() : null;
var selectedTreeNodes = treeView != null ? treeView.GetTopLevelSelection().ToArray() : null; var selectedTreeNodes = treeView != null ? treeView.GetTopLevelSelection().ToArray() : null;
return new TextViewContext { return new TextViewContext {
TreeView = treeView, TreeView = treeView,
SelectedTreeNodes = selectedTreeNodes, SelectedTreeNodes = selectedTreeNodes,
TextView = textView, TextView = textView,
Reference = reference Reference = reference,
Position = position
}; };
} }
} }

11
ILSpy/TextView/DecompilerTextView.cs

@ -763,19 +763,18 @@ namespace ICSharpCode.ILSpy.TextView
internal ReferenceSegment GetReferenceSegmentAtMousePosition() internal ReferenceSegment GetReferenceSegmentAtMousePosition()
{ {
TextViewPosition? position = textEditor.TextArea.TextView.GetPosition(Mouse.GetPosition(textEditor.TextArea.TextView) + textEditor.TextArea.TextView.ScrollOffset); TextViewPosition? position = GetPositionFromMousePosition();
if (position == null) if (position == null)
return null; return null;
int offset = textEditor.Document.GetOffset(position.Value.Location); int offset = textEditor.Document.GetOffset(position.Value.Location);
return referenceElementGenerator.References.FindSegmentsContaining(offset).FirstOrDefault(); return referenceElementGenerator.References.FindSegmentsContaining(offset).FirstOrDefault();
} }
public int CurrentOffset { internal TextViewPosition? GetPositionFromMousePosition()
get { {
return textEditor.CaretOffset; return textEditor.TextArea.TextView.GetPosition(Mouse.GetPosition(textEditor.TextArea.TextView) + textEditor.TextArea.TextView.ScrollOffset);
}
} }
public DecompilerTextViewState GetState() public DecompilerTextViewState GetState()
{ {
if (decompiledNodes == null) if (decompiledNodes == null)

22
ILSpy/TextView/FoldingCommands.cs

@ -10,6 +10,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Folding; using ICSharpCode.AvalonEdit.Folding;
namespace ICSharpCode.ILSpy.TextView namespace ICSharpCode.ILSpy.TextView
@ -62,16 +63,23 @@ namespace ICSharpCode.ILSpy.TextView
public void Execute(TextViewContext context) public void Execute(TextViewContext context)
{ {
if (null == context.TextView) var textView = context.TextView;
return; if (null == textView)
return;
var editor = textView.textEditor;
FoldingManager foldingManager = context.TextView.FoldingManager; FoldingManager foldingManager = context.TextView.FoldingManager;
if (null == foldingManager) if (null == foldingManager)
return; return;
int offset = context.TextView.CurrentOffset; // TODO: or use Caret if position is not given?
FoldingSection folding = foldingManager.GetNextFolding(offset); var posBox = context.Position;
if (folding == null || folding.StartOffset != offset) { if (null == posBox)
// no folding found on current line: find innermost folding containing the caret return;
folding = foldingManager.GetFoldingsContaining(offset).LastOrDefault(); 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) { if (folding != null) {
folding.IsFolded = !folding.IsFolded; folding.IsFolded = !folding.IsFolded;

Loading…
Cancel
Save