Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Don't show folding region tooltip when the mouse hovers behind the co…
Browse files Browse the repository at this point in the history
…llapsed section (after the end of line).
  • Loading branch information
dgrunwald committed Sep 26, 2011
1 parent 092b913 commit aa83bf1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
74 changes: 68 additions & 6 deletions src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CodeEditorView.cs
Expand Up @@ -8,27 +8,27 @@
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Controls.Primitives; using System.Windows.Controls.Primitives;
using System.Windows.Documents; using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Threading; using System.Windows.Threading;

using ICSharpCode.AvalonEdit.AddIn.Options; using ICSharpCode.AvalonEdit.AddIn.Options;
using ICSharpCode.AvalonEdit.AddIn.Snippets; using ICSharpCode.AvalonEdit.AddIn.Snippets;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Editing; using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Folding; using ICSharpCode.AvalonEdit.Folding;
using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Rendering; using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.NRefactory;
using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Editor; using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Editor.AvalonEdit; using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
using ICSharpCode.SharpDevelop.Editor.Commands; using ICSharpCode.SharpDevelop.Editor.Commands;
using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Refactoring;
using Ast = ICSharpCode.NRefactory.Ast;


namespace ICSharpCode.AvalonEdit.AddIn namespace ICSharpCode.AvalonEdit.AddIn
{ {
Expand Down Expand Up @@ -233,7 +233,7 @@ void TextEditorMouseHover(object sender, MouseEventArgs e)
{ {
Debug.Assert(sender == this); Debug.Assert(sender == this);
ToolTipRequestEventArgs args = new ToolTipRequestEventArgs(this.Adapter); 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; args.InDocument = pos.HasValue;
if (pos.HasValue) { if (pos.HasValue) {
args.LogicalPosition = AvalonEditDocumentAdapter.ToLocation(pos.Value); args.LogicalPosition = AvalonEditDocumentAdapter.ToLocation(pos.Value);
Expand All @@ -244,11 +244,11 @@ void TextEditorMouseHover(object sender, MouseEventArgs e)


FoldingManager foldings = this.Adapter.GetService(typeof(FoldingManager)) as FoldingManager; FoldingManager foldings = this.Adapter.GetService(typeof(FoldingManager)) as FoldingManager;
if (foldings != null) { if (foldings != null) {
var foldingsAtOffset = foldings.GetFoldingsContaining(offset); var foldingsAtOffset = foldings.GetFoldingsAt(offset);
FoldingSection collapsedSection = foldingsAtOffset.FirstOrDefault(section => section.IsFolded); FoldingSection collapsedSection = foldingsAtOffset.FirstOrDefault(section => section.IsFolded);


if (collapsedSection != null) { if (collapsedSection != null) {
args.SetToolTip(collapsedSection.TooltipText); args.SetToolTip(GetTooltipTextForCollapsedSection(collapsedSection));
} }
} }


Expand Down Expand Up @@ -396,6 +396,68 @@ void PopupClosed(object sender, EventArgs e)
{ {
popup = null; 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 #endregion


#region Ctrl+Click Go To Definition #region Ctrl+Click Go To Definition
Expand Down
Expand Up @@ -84,6 +84,7 @@ internal CollapsedLineSection CollapseSection(TextView textView)
/// <summary> /// <summary>
/// Gets the content of the collapsed lines as tooltip text. /// Gets the content of the collapsed lines as tooltip text.
/// </summary> /// </summary>
[Obsolete]
public string TooltipText { public string TooltipText {
get { get {
// This fixes SD-1394: // This fixes SD-1394:
Expand Down
Expand Up @@ -1612,6 +1612,7 @@ public Point GetVisualPosition(TextViewPosition position, VisualYPosition yPosit


/// <summary> /// <summary>
/// Gets the text view position from the specified visual position. /// 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> /// </summary>
/// <param name="visualPosition">The position in WPF device-independent pixels relative /// <param name="visualPosition">The position in WPF device-independent pixels relative
/// to the top left corner of the document.</param> /// to the top left corner of the document.</param>
Expand All @@ -1628,6 +1629,26 @@ public Point GetVisualPosition(TextViewPosition position, VisualYPosition yPosit
int documentOffset = line.GetRelativeOffset(visualColumn) + line.FirstDocumentLine.Offset; int documentOffset = line.GetRelativeOffset(visualColumn) + line.FirstDocumentLine.Offset;
return new TextViewPosition(document.GetLocation(documentOffset), visualColumn); 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 #endregion


#region Service Provider #region Service Provider
Expand Down

0 comments on commit aa83bf1

Please sign in to comment.