Permalink
Browse files

Don't show folding region tooltip when the mouse hovers behind the co…

…llapsed section (after the end of line).
  • Loading branch information...
1 parent 092b913 commit aa83bf1c446161837ef5f24cafb0b3d28cbfd68f @dgrunwald dgrunwald committed Sep 26, 2011
@@ -8,27 +8,27 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
+using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Threading;
+
using ICSharpCode.AvalonEdit.AddIn.Options;
using ICSharpCode.AvalonEdit.AddIn.Snippets;
+using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Folding;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Rendering;
-using ICSharpCode.NRefactory;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
using ICSharpCode.SharpDevelop.Editor.Commands;
using ICSharpCode.SharpDevelop.Gui;
-using ICSharpCode.SharpDevelop.Refactoring;
-using Ast = ICSharpCode.NRefactory.Ast;
namespace ICSharpCode.AvalonEdit.AddIn
{
@@ -233,7 +233,7 @@ void TextEditorMouseHover(object sender, MouseEventArgs e)
{
Debug.Assert(sender == this);
ToolTipRequestEventArgs args = new ToolTipRequestEventArgs(this.Adapter);
- var pos = GetPositionFromPoint(e.GetPosition(this));
+ var pos = this.TextArea.TextView.GetPositionFloor(e.GetPosition(this.TextArea.TextView) + this.TextArea.TextView.ScrollOffset);
args.InDocument = pos.HasValue;
if (pos.HasValue) {
args.LogicalPosition = AvalonEditDocumentAdapter.ToLocation(pos.Value);
@@ -244,11 +244,11 @@ void TextEditorMouseHover(object sender, MouseEventArgs e)
FoldingManager foldings = this.Adapter.GetService(typeof(FoldingManager)) as FoldingManager;
if (foldings != null) {
- var foldingsAtOffset = foldings.GetFoldingsContaining(offset);
+ var foldingsAtOffset = foldings.GetFoldingsAt(offset);
FoldingSection collapsedSection = foldingsAtOffset.FirstOrDefault(section => section.IsFolded);
if (collapsedSection != null) {
- args.SetToolTip(collapsedSection.TooltipText);
+ args.SetToolTip(GetTooltipTextForCollapsedSection(collapsedSection));
}
}
@@ -396,6 +396,68 @@ void PopupClosed(object sender, EventArgs e)
{
popup = null;
}
+
+ #region GetTooltipTextForCollapsedSection
+ string GetTooltipTextForCollapsedSection(FoldingSection foldingSection)
+ {
+ // This fixes SD-1394:
+ // Each line is checked for leading indentation whitespaces. If
+ // a line has the same or more indentation than the first line,
+ // it is reduced. If a line is less indented than the first line
+ // the indentation is removed completely.
+ //
+ // See the following example:
+ // line 1
+ // line 2
+ // line 3
+ // line 4
+ //
+ // is reduced to:
+ // line 1
+ // line 2
+ // line 3
+ // line 4
+
+ const int maxLineCount = 15;
+
+ TextDocument document = this.Document;
+ int startOffset = foldingSection.StartOffset;
+ int endOffset = foldingSection.EndOffset;
+
+ DocumentLine startLine = document.GetLineByOffset(startOffset);
+ DocumentLine endLine = document.GetLineByOffset(endOffset);
+ StringBuilder builder = new StringBuilder();
+
+ DocumentLine current = startLine;
+ ISegment startIndent = TextUtilities.GetLeadingWhitespace(document, startLine);
+ int lineCount = 0;
+ while (current != endLine.NextLine && lineCount < maxLineCount) {
+ ISegment currentIndent = TextUtilities.GetLeadingWhitespace(document, current);
+
+ if (current == startLine && current == endLine)
+ builder.Append(document.GetText(startOffset, endOffset - startOffset));
+ else if (current == startLine) {
+ if (current.EndOffset - startOffset > 0)
+ builder.AppendLine(document.GetText(startOffset, current.EndOffset - startOffset).TrimStart());
+ } else if (current == endLine) {
+ if (startIndent.Length <= currentIndent.Length)
+ builder.Append(document.GetText(current.Offset + startIndent.Length, endOffset - current.Offset - startIndent.Length));
+ else
+ builder.Append(document.GetText(current.Offset + currentIndent.Length, endOffset - current.Offset - currentIndent.Length));
+ } else {
+ if (startIndent.Length <= currentIndent.Length)
+ builder.AppendLine(document.GetText(current.Offset + startIndent.Length, current.Length - startIndent.Length));
+ else
+ builder.AppendLine(document.GetText(current.Offset + currentIndent.Length, current.Length - currentIndent.Length));
+ }
+
+ current = current.NextLine;
+ lineCount++;
+ }
+
+ return builder.ToString();
+ }
+ #endregion
#endregion
#region Ctrl+Click Go To Definition
@@ -84,6 +84,7 @@ internal CollapsedLineSection CollapseSection(TextView textView)
/// <summary>
/// Gets the content of the collapsed lines as tooltip text.
/// </summary>
+ [Obsolete]
public string TooltipText {
get {
// This fixes SD-1394:
@@ -1612,6 +1612,7 @@ public Point GetVisualPosition(TextViewPosition position, VisualYPosition yPosit
/// <summary>
/// Gets the text view position from the specified visual position.
+ /// If the position is within a character, it is rounded to the next character boundary.
/// </summary>
/// <param name="visualPosition">The position in WPF device-independent pixels relative
/// to the top left corner of the document.</param>
@@ -1628,6 +1629,26 @@ public Point GetVisualPosition(TextViewPosition position, VisualYPosition yPosit
int documentOffset = line.GetRelativeOffset(visualColumn) + line.FirstDocumentLine.Offset;
return new TextViewPosition(document.GetLocation(documentOffset), visualColumn);
}
+
+ /// <summary>
+ /// Gets the text view position from the specified visual position.
+ /// If the position is inside a character, the position in front of the character is returned.
+ /// </summary>
+ /// <param name="visualPosition">The position in WPF device-independent pixels relative
+ /// to the top left corner of the document.</param>
+ /// <returns>The logical position, or null if the position is outside the document.</returns>
+ public TextViewPosition? GetPositionFloor(Point visualPosition)
+ {
+ VerifyAccess();
+ if (this.Document == null)
+ throw ThrowUtil.NoDocumentAssigned();
+ VisualLine line = GetVisualLineFromVisualTop(visualPosition.Y);
+ if (line == null)
+ return null;
+ int visualColumn = line.GetVisualColumnFloor(visualPosition);
+ int documentOffset = line.GetRelativeOffset(visualColumn) + line.FirstDocumentLine.Offset;
+ return new TextViewPosition(document.GetLocation(documentOffset), visualColumn);
+ }
#endregion
#region Service Provider

0 comments on commit aa83bf1

Please sign in to comment.