[2659] | 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 |
|
---|
| 28 | using System;
|
---|
| 29 | using System.Text;
|
---|
| 30 | using ICSharpCode.SharpDevelop.Dom;
|
---|
| 31 | using ICSharpCode.SharpDevelop.Dom.CSharp;
|
---|
| 32 | using ICSharpCode.SharpDevelop.Dom.VBNet;
|
---|
| 33 | using TextEditor = ICSharpCode.TextEditor;
|
---|
| 34 | using NRefactoryResolver = ICSharpCode.SharpDevelop.Dom.NRefactoryResolver.NRefactoryResolver;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.CodeEditor {
|
---|
| 37 | sealed class ToolTipProvider {
|
---|
| 38 | CodeEditor codeEditor;
|
---|
| 39 | TextEditor.TextEditorControl editor;
|
---|
| 40 |
|
---|
| 41 | private ToolTipProvider(CodeEditor codeEditor, TextEditor.TextEditorControl editor) {
|
---|
| 42 | this.codeEditor = codeEditor;
|
---|
| 43 | this.editor = editor;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public static void Attach(CodeEditor codeEditor, TextEditor.TextEditorControl editor) {
|
---|
| 47 | ToolTipProvider tp = new ToolTipProvider(codeEditor, editor);
|
---|
| 48 | editor.ActiveTextAreaControl.TextArea.ToolTipRequest += tp.OnToolTipRequest;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void OnToolTipRequest(object sender, TextEditor.ToolTipRequestEventArgs e) {
|
---|
| 52 | if (e.InDocument && !e.ToolTipShown) {
|
---|
| 53 | IExpressionFinder expressionFinder;
|
---|
| 54 | expressionFinder = new CSharpExpressionFinder(codeEditor.parseInformation);
|
---|
| 55 | ExpressionResult expression = expressionFinder.FindFullExpression(
|
---|
| 56 | editor.Text,
|
---|
| 57 | editor.Document.PositionToOffset(e.LogicalPosition));
|
---|
| 58 | if (expression.Region.IsEmpty) {
|
---|
| 59 | expression.Region = new DomRegion(e.LogicalPosition.Line + 1, e.LogicalPosition.Column + 1);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | TextEditor.TextArea textArea = editor.ActiveTextAreaControl.TextArea;
|
---|
| 63 | NRefactoryResolver resolver = new NRefactoryResolver(codeEditor.projectContent.Language);
|
---|
| 64 | ResolveResult rr = resolver.Resolve(expression,
|
---|
| 65 | codeEditor.parseInformation,
|
---|
| 66 | textArea.MotherTextEditorControl.Text);
|
---|
| 67 | string toolTipText = GetText(rr);
|
---|
| 68 | if (toolTipText != null) {
|
---|
| 69 | e.ShowToolTip(toolTipText);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | static string GetText(ResolveResult result) {
|
---|
| 75 | if (result == null) {
|
---|
| 76 | return null;
|
---|
| 77 | }
|
---|
| 78 | if (result is MixedResolveResult)
|
---|
| 79 | return GetText(((MixedResolveResult)result).PrimaryResult);
|
---|
| 80 | IAmbience ambience = new CSharpAmbience();
|
---|
| 81 | ambience.ConversionFlags = ConversionFlags.StandardConversionFlags | ConversionFlags.ShowAccessibility;
|
---|
| 82 | if (result is MemberResolveResult) {
|
---|
| 83 | return GetMemberText(ambience, ((MemberResolveResult)result).ResolvedMember);
|
---|
| 84 | } else if (result is LocalResolveResult) {
|
---|
| 85 | LocalResolveResult rr = (LocalResolveResult)result;
|
---|
| 86 | ambience.ConversionFlags = ConversionFlags.UseFullyQualifiedTypeNames
|
---|
| 87 | | ConversionFlags.ShowReturnType;
|
---|
| 88 | StringBuilder b = new StringBuilder();
|
---|
| 89 | if (rr.IsParameter)
|
---|
| 90 | b.Append("parameter ");
|
---|
| 91 | else
|
---|
| 92 | b.Append("local variable ");
|
---|
| 93 | b.Append(ambience.Convert(rr.Field));
|
---|
| 94 | return b.ToString();
|
---|
| 95 | } else if (result is NamespaceResolveResult) {
|
---|
| 96 | return "namespace " + ((NamespaceResolveResult)result).Name;
|
---|
| 97 | } else if (result is TypeResolveResult) {
|
---|
| 98 | IClass c = ((TypeResolveResult)result).ResolvedClass;
|
---|
| 99 | if (c != null)
|
---|
| 100 | return GetMemberText(ambience, c);
|
---|
| 101 | else
|
---|
| 102 | return ambience.Convert(result.ResolvedType);
|
---|
| 103 | } else if (result is MethodGroupResolveResult) {
|
---|
| 104 | MethodGroupResolveResult mrr = result as MethodGroupResolveResult;
|
---|
| 105 | IMethod m = mrr.GetMethodIfSingleOverload();
|
---|
| 106 | if (m != null)
|
---|
| 107 | return GetMemberText(ambience, m);
|
---|
| 108 | else
|
---|
| 109 | return "Overload of " + ambience.Convert(mrr.ContainingType) + "." + mrr.Name;
|
---|
| 110 | } else {
|
---|
| 111 | return null;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | static string GetMemberText(IAmbience ambience, IEntity member) {
|
---|
| 116 | StringBuilder text = new StringBuilder();
|
---|
| 117 | if (member is IField) {
|
---|
| 118 | text.Append(ambience.Convert(member as IField));
|
---|
| 119 | } else if (member is IProperty) {
|
---|
| 120 | text.Append(ambience.Convert(member as IProperty));
|
---|
| 121 | } else if (member is IEvent) {
|
---|
| 122 | text.Append(ambience.Convert(member as IEvent));
|
---|
| 123 | } else if (member is IMethod) {
|
---|
| 124 | text.Append(ambience.Convert(member as IMethod));
|
---|
| 125 | } else if (member is IClass) {
|
---|
| 126 | text.Append(ambience.Convert(member as IClass));
|
---|
| 127 | } else {
|
---|
| 128 | text.Append("unknown member ");
|
---|
| 129 | text.Append(member.ToString());
|
---|
| 130 | }
|
---|
| 131 | string documentation = member.Documentation;
|
---|
| 132 | if (documentation != null && documentation.Length > 0) {
|
---|
| 133 | text.Append('\n');
|
---|
| 134 | text.Append(CodeCompletionData.XmlDocumentationToText(documentation));
|
---|
| 135 | }
|
---|
| 136 | return text.ToString();
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|