~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/HiddenDefinition/HiddenDefinitionRenderer.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Windows.Controls.Primitives;
 
6
using ICSharpCode.Core.Presentation;
 
7
using ICSharpCode.SharpDevelop.Editor;
 
8
using ICSharpCode.SharpDevelop.Gui;
 
9
 
 
10
namespace ICSharpCode.AvalonEdit.AddIn.HiddenDefinition
 
11
{
 
12
        public class HiddenDefinitionRenderer : IDisposable
 
13
        {
 
14
                private CodeEditorView editor;
 
15
                private ExtendedPopup popup = new ExtendedPopup();
 
16
                private HiddenDefinitionControl control;
 
17
                
 
18
                public HiddenDefinitionRenderer(CodeEditorView editorView)
 
19
                {
 
20
                        this.editor = editorView;
 
21
                        control = new HiddenDefinitionControl();
 
22
                        WorkbenchSingleton.Workbench.ActiveContentChanged += WorkbenchSingleton_Workbench_ActiveContentChanged;
 
23
                }
 
24
 
 
25
                public BracketSearchResult BracketSearchResult { get; set; }
 
26
                
 
27
                public void Dispose()
 
28
                {
 
29
                        WorkbenchSingleton.Workbench.ActiveContentChanged -= WorkbenchSingleton_Workbench_ActiveContentChanged;
 
30
                        ClosePopup();
 
31
                        popup = null;
 
32
                }
 
33
                
 
34
                public void ClosePopup()
 
35
                {
 
36
                        if (popup.IsOpen)
 
37
                                popup.IsOpen = false;
 
38
                }
 
39
                
 
40
                public void Show()
 
41
                {
 
42
                        ClosePopup();
 
43
                        
 
44
                        if (BracketSearchResult == null) return;
 
45
                        
 
46
                        // verify if we have a open bracket
 
47
                        if (this.editor.Document.GetCharAt(BracketSearchResult.OpeningBracketOffset) != '{')
 
48
                                return;
 
49
                        
 
50
                        var line = GetLineText(BracketSearchResult.OpeningBracketOffset);
 
51
                        if(line == null) return;
 
52
                        
 
53
                        control.DefinitionText = line;
 
54
                        popup.Child = control;
 
55
                        popup.HorizontalOffset = 70;
 
56
                        popup.Placement = PlacementMode.Relative;
 
57
                        popup.PlacementTarget = editor.TextArea;
 
58
                        popup.IsOpen = true;
 
59
                }
 
60
                
 
61
                /// <summary>
 
62
                /// Gets the line text near the offset.
 
63
                /// </summary>
 
64
                /// <param name="offset">Offset.</param>
 
65
                /// <returns></returns>
 
66
                private string GetLineText(int offset)
 
67
                {
 
68
                        if (!editor.TextArea.TextView.VisualLinesValid)
 
69
                                return null;
 
70
                        
 
71
                        // get line
 
72
                        var documentLine = editor.Document.GetLineByOffset(offset);
 
73
                        string documentText = editor.Document.Text;
 
74
                        string lineText = string.Empty;
 
75
                        int off, length;
 
76
                        
 
77
                        do {
 
78
                                if (documentLine == null || documentLine.IsDeleted) return null;
 
79
                                off = documentLine.Offset;
 
80
                                length = documentLine.Length;
 
81
                                lineText = documentText.Substring(off, length).Trim();
 
82
                                
 
83
                                documentLine = documentLine.PreviousLine;
 
84
                        }
 
85
                        while (lineText == "{" || string.IsNullOrEmpty(lineText) ||
 
86
                               lineText.StartsWith("//") || lineText.StartsWith("/*") ||
 
87
                               lineText.StartsWith("*") || lineText.StartsWith("'"));
 
88
                        
 
89
                        // check whether the line is visible
 
90
                        if (editor.TextArea.TextView.VisualLines[0].StartOffset > off) {
 
91
                                return this.editor.TextArea.TextView.Document.GetText(off, length);
 
92
                        }
 
93
                        
 
94
                        return null;
 
95
                }
 
96
                
 
97
                private void WorkbenchSingleton_Workbench_ActiveContentChanged(object sender, EventArgs e)
 
98
                {
 
99
                        ClosePopup();
 
100
                }
 
101
        }
 
102
}
 
 
b'\\ No newline at end of file'