Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.CodeEditor/3.3/ToolTipProvider.cs @ 9333

Last change on this file since 9333 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 6.2 KB
Line 
1// CSharp Editor Example with Code Completion
2// Copyright (c) 2007, Daniel Grunwald
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// - Redistributions of source code must retain the above copyright notice, this list
9//   of conditions and the following disclaimer.
10//
11// - Redistributions in binary form must reproduce the above copyright notice, this list
12//   of conditions and the following disclaimer in the documentation and/or other materials
13//   provided with the distribution.
14//
15// - Neither the name of the ICSharpCode team nor the names of its contributors may be used to
16//   endorse or promote products derived from this software without specific prior written
17//   permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS
20// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28using System.Text;
29using ICSharpCode.SharpDevelop.Dom;
30using ICSharpCode.SharpDevelop.Dom.CSharp;
31using NRefactoryResolver = ICSharpCode.SharpDevelop.Dom.NRefactoryResolver.NRefactoryResolver;
32using TextEditor = ICSharpCode.TextEditor;
33
34namespace HeuristicLab.CodeEditor {
35  sealed class ToolTipProvider {
36    CodeEditor codeEditor;
37    TextEditor.TextEditorControl editor;
38
39    private ToolTipProvider(CodeEditor codeEditor, TextEditor.TextEditorControl editor) {
40      this.codeEditor = codeEditor;
41      this.editor = editor;
42    }
43
44    public static void Attach(CodeEditor codeEditor, TextEditor.TextEditorControl editor) {
45      ToolTipProvider tp = new ToolTipProvider(codeEditor, editor);
46      editor.ActiveTextAreaControl.TextArea.ToolTipRequest += tp.OnToolTipRequest;
47    }
48
49    void OnToolTipRequest(object sender, TextEditor.ToolTipRequestEventArgs e) {
50      if (e.InDocument && !e.ToolTipShown) {
51        IExpressionFinder expressionFinder;
52        expressionFinder = new CSharpExpressionFinder(codeEditor.parseInformation);
53        ExpressionResult expression = expressionFinder.FindFullExpression(
54          editor.Text,
55          editor.Document.PositionToOffset(e.LogicalPosition));
56        if (expression.Region.IsEmpty) {
57          expression.Region = new DomRegion(e.LogicalPosition.Line + 1, e.LogicalPosition.Column + 1);
58        }
59
60        TextEditor.TextArea textArea = editor.ActiveTextAreaControl.TextArea;
61        NRefactoryResolver resolver = new NRefactoryResolver(codeEditor.projectContent.Language);
62        ResolveResult rr = resolver.Resolve(expression,
63                                            codeEditor.parseInformation,
64                                            textArea.MotherTextEditorControl.Text);
65        string toolTipText = GetText(rr);
66        if (toolTipText != null) {
67          e.ShowToolTip(toolTipText);
68        }
69      }
70    }
71
72    static string GetText(ResolveResult result) {
73      if (result == null) {
74        return null;
75      }
76      if (result is MixedResolveResult)
77        return GetText(((MixedResolveResult)result).PrimaryResult);
78      IAmbience ambience = new CSharpAmbience();
79      ambience.ConversionFlags = ConversionFlags.StandardConversionFlags | ConversionFlags.ShowAccessibility;
80      if (result is MemberResolveResult) {
81        return GetMemberText(ambience, ((MemberResolveResult)result).ResolvedMember);
82      } else if (result is LocalResolveResult) {
83        LocalResolveResult rr = (LocalResolveResult)result;
84        ambience.ConversionFlags = ConversionFlags.UseFullyQualifiedTypeNames
85          | ConversionFlags.ShowReturnType;
86        StringBuilder b = new StringBuilder();
87        if (rr.IsParameter)
88          b.Append("parameter ");
89        else
90          b.Append("local variable ");
91        b.Append(ambience.Convert(rr.Field));
92        return b.ToString();
93      } else if (result is NamespaceResolveResult) {
94        return "namespace " + ((NamespaceResolveResult)result).Name;
95      } else if (result is TypeResolveResult) {
96        IClass c = ((TypeResolveResult)result).ResolvedClass;
97        if (c != null)
98          return GetMemberText(ambience, c);
99        else
100          return ambience.Convert(result.ResolvedType);
101      } else if (result is MethodGroupResolveResult) {
102        MethodGroupResolveResult mrr = result as MethodGroupResolveResult;
103        IMethod m = mrr.GetMethodIfSingleOverload();
104        if (m != null)
105          return GetMemberText(ambience, m);
106        else
107          return "Overload of " + ambience.Convert(mrr.ContainingType) + "." + mrr.Name;
108      } else {
109        return null;
110      }
111    }
112
113    static string GetMemberText(IAmbience ambience, IEntity member) {
114      StringBuilder text = new StringBuilder();
115      if (member is IField) {
116        text.Append(ambience.Convert(member as IField));
117      } else if (member is IProperty) {
118        text.Append(ambience.Convert(member as IProperty));
119      } else if (member is IEvent) {
120        text.Append(ambience.Convert(member as IEvent));
121      } else if (member is IMethod) {
122        text.Append(ambience.Convert(member as IMethod));
123      } else if (member is IClass) {
124        text.Append(ambience.Convert(member as IClass));
125      } else {
126        text.Append("unknown member ");
127        text.Append(member.ToString());
128      }
129      string documentation = member.Documentation;
130      if (documentation != null && documentation.Length > 0) {
131        text.Append('\n');
132        text.Append(CodeCompletionData.XmlDocumentationToText(documentation));
133      }
134      return text.ToString();
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.