Beispiel #1
0
        public static TooltipInformation CreateTooltipInformation(ICompilation compilation, CSharpUnresolvedFile file, TextEditorData textEditorData, MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy formattingPolicy, IType type, bool smartWrap, bool createFooter = false)
        {
            var tooltipInfo = new TooltipInformation();
            var resolver    = file != null?file.GetResolver(compilation, textEditorData.Caret.Location) : new CSharpResolver(compilation);

            var sig = new SignatureMarkupCreator(resolver, formattingPolicy.CreateOptions());

            sig.BreakLineAfterReturnType = smartWrap;
            try {
                tooltipInfo.SignatureMarkup = sig.GetMarkup(type.IsParameterized ? type.GetDefinition() : type);
            } catch (Exception e) {
                LoggingService.LogError("Got exception while creating markup for :" + type, e);
                return(new TooltipInformation());
            }
            if (type.IsParameterized)
            {
                var typeInfo = new StringBuilder();
                for (int i = 0; i < type.TypeParameterCount; i++)
                {
                    typeInfo.AppendLine(type.GetDefinition().TypeParameters [i].Name + " is " + sig.GetTypeReferenceString(type.TypeArguments [i]));
                }
                tooltipInfo.AddCategory("Type Parameters", typeInfo.ToString());
            }

            var def = type.GetDefinition();

            if (def != null)
            {
                if (createFooter && !string.IsNullOrEmpty(def.ParentAssembly.AssemblyName))
                {
                    tooltipInfo.FooterMarkup = "<small> From " + AmbienceService.EscapeText(def.ParentAssembly.AssemblyName) + "</small>";
                }
                tooltipInfo.SummaryMarkup = AmbienceService.GetSummaryMarkup(def) ?? "";
            }
            return(tooltipInfo);
        }
Beispiel #2
0
        public static TooltipInformation CreateTooltipInformation(ICompilation compilation, CSharpUnresolvedFile file, CSharpResolver resolver, TextEditorData textEditorData, MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy formattingPolicy, IEntity entity, bool smartWrap, bool createFooter = false)
        {
            var tooltipInfo = new TooltipInformation();

            if (resolver == null)
            {
                resolver = file != null?file.GetResolver(compilation, textEditorData.Caret.Location) : new CSharpResolver(compilation);
            }
            var sig = new SignatureMarkupCreator(resolver, formattingPolicy.CreateOptions());

            sig.BreakLineAfterReturnType = smartWrap;
            try {
                tooltipInfo.SignatureMarkup = sig.GetMarkup(entity);
            } catch (Exception e) {
                LoggingService.LogError("Got exception while creating markup for :" + entity, e);
                return(new TooltipInformation());
            }
            tooltipInfo.SummaryMarkup = AmbienceService.GetSummaryMarkup(entity) ?? "";

            if (entity is IMember)
            {
                var evt = (IMember)entity;
                if (evt.ReturnType.Kind == TypeKind.Delegate)
                {
                    tooltipInfo.AddCategory(GettextCatalog.GetString("Delegate Info"), sig.GetDelegateInfo(evt.ReturnType));
                }
            }
            if (entity is IMethod)
            {
                var method = (IMethod)entity;
                if (method.IsExtensionMethod)
                {
                    tooltipInfo.AddCategory(GettextCatalog.GetString("Extension Method from"), method.DeclaringTypeDefinition.FullName);
                }
            }
            if (createFooter)
            {
                if (entity is IType)
                {
                    var type = entity as IType;
                    var def  = type.GetDefinition();
                    if (def != null)
                    {
                        if (!string.IsNullOrEmpty(def.ParentAssembly.AssemblyName))
                        {
                            var project = def.GetSourceProject();
                            if (project != null)
                            {
                                var relPath = FileService.AbsoluteToRelativePath(project.BaseDirectory, def.Region.FileName);
                                tooltipInfo.FooterMarkup = "<small>" + GettextCatalog.GetString("Project:\t{0}", AmbienceService.EscapeText(def.ParentAssembly.AssemblyName)) + "</small>" + Environment.NewLine +
                                                           "<small>" + GettextCatalog.GetString("File:\t\t{0} (line {1})", AmbienceService.EscapeText(relPath), def.Region.Begin.Line) + "</small>";
                            }
                        }
                    }
                }
                else if (entity.DeclaringTypeDefinition != null)
                {
                    var project = entity.DeclaringTypeDefinition.GetSourceProject();
                    if (project != null)
                    {
                        var relPath = FileService.AbsoluteToRelativePath(project.BaseDirectory, entity.Region.FileName);
                        tooltipInfo.FooterMarkup =
                            "<small>" + GettextCatalog.GetString("Project:\t{0}", AmbienceService.EscapeText(project.Name)) + "</small>" + Environment.NewLine +
                            "<small>" + GettextCatalog.GetString("From:\t{0}", AmbienceService.EscapeText(entity.DeclaringType.FullName)) + "</small>" + Environment.NewLine +
                            "<small>" + GettextCatalog.GetString("File:\t\t{0} (line {1})", AmbienceService.EscapeText(relPath), entity.Region.Begin.Line) + "</small>";
                    }
                }
            }
            return(tooltipInfo);
        }
Beispiel #3
0
        void AddAspAttributeValueCompletionData(CompletionDataList list, S.XName tagName, S.XName attName, string id)
        {
            Debug.Assert(tagName.IsValid && tagName.HasPrefix);
            Debug.Assert(attName.IsValid && !attName.HasPrefix);

            IType controlClass = HasDoc ? refman.GetControlType(tagName.Prefix, tagName.Name) : null;

            if (controlClass == null)
            {
                LoggingService.LogWarning("Could not obtain IType for {0}", tagName.FullName);

                var database = WebTypeContext.GetSystemWebDom(project);
                controlClass = ReflectionHelper.ParseReflectionName("System.Web.UI.WebControls.WebControl").Resolve(database);

                if (controlClass == null)
                {
                    LoggingService.LogWarning("Could not obtain IType for System.Web.UI.WebControls.WebControl");
                    return;
                }
            }

            //find the codebehind class
            IType        codeBehindClass;
            ICompilation projectDatabase;

            GetCodeBehind(out codeBehindClass, out projectDatabase);

            //if it's an event, suggest compatible methods
            if (codeBehindClass != null && attName.Name.StartsWith("On"))
            {
                string eventName = attName.Name.Substring(2);

                foreach (IEvent ev in controlClass.GetEvents())
                {
                    if (ev.Name == eventName)
                    {
                        var domMethod = BindingService.MDDomToCodeDomMethod(ev);
                        if (domMethod == null)
                        {
                            return;
                        }

                        foreach (IMethod meth
                                 in BindingService.GetCompatibleMethodsInClass(codeBehindClass, ev))
                        {
                            list.Add(meth.Name, "md-method",
                                     GettextCatalog.GetString("A compatible method in the CodeBehind class"));
                        }

                        string suggestedIdentifier = ev.Name;
                        if (id != null)
                        {
                            suggestedIdentifier = id + "_" + suggestedIdentifier;
                        }
                        else
                        {
                            suggestedIdentifier = tagName.Name + "_" + suggestedIdentifier;
                        }

                        domMethod.Name = BindingService.GenerateIdentifierUniqueInClass
                                             (codeBehindClass, suggestedIdentifier);
                        domMethod.Attributes = (domMethod.Attributes & ~System.CodeDom.MemberAttributes.AccessMask)
                                               | System.CodeDom.MemberAttributes.Family;

                        list.Add(
                            new SuggestedHandlerCompletionData(project, domMethod, codeBehindClass,
                                                               MonoDevelop.DesignerSupport.CodeBehind.GetNonDesignerClass(codeBehindClass))
                            );
                        return;
                    }
                }
            }

            if (projectDatabase == null)
            {
                projectDatabase = WebTypeContext.GetSystemWebDom(project);

                if (projectDatabase == null)
                {
                    LoggingService.LogWarning("Could not obtain type database in AddAspAttributeCompletionData");
                    return;
                }
            }

            //if it's a property and is an enum or bool, suggest valid values
            foreach (IProperty prop in controlClass.GetProperties())
            {
                if (prop.Name != attName.Name)
                {
                    continue;
                }

                //boolean completion
                if (prop.ReturnType.Equals(projectDatabase.FindType(KnownTypeCode.Boolean)))
                {
                    AddBooleanCompletionData(list);
                    return;
                }
                //color completion
                if (prop.ReturnType.Equals(projectDatabase.FindType(typeof(System.Drawing.Color))))
                {
                    System.Drawing.ColorConverter conv = new System.Drawing.ColorConverter();
                    foreach (System.Drawing.Color c in conv.GetStandardValues(null))
                    {
                        if (c.IsSystemColor)
                        {
                            continue;
                        }
                        string hexcol = string.Format("#{0:x2}{1:x2}{2:x2}", c.R, c.G, c.B);
                        list.Add(c.Name, hexcol);
                    }
                    return;
                }

                //enum completion
                IType retCls = prop.ReturnType;
                if (retCls != null && retCls.Kind == TypeKind.Enum)
                {
                    foreach (var enumVal in retCls.GetFields())
                    {
                        if (enumVal.IsPublic && enumVal.IsStatic)
                        {
                            list.Add(enumVal.Name, "md-literal", AmbienceService.GetSummaryMarkup(enumVal));
                        }
                    }
                    return;
                }
            }
        }
Beispiel #4
0
        /*public override void RunParameterCompletionCommand ()
         * {
         *      if (localDocumentInfo == null) {
         *              base.RunParameterCompletionCommand ();
         *              return;
         *      }
         *      var doc = document;
         *      document = localDocumentInfo.HiddenDocument;
         *      var cw = CompletionWidget;
         *      CompletionWidget = documentBuilder.CreateCompletionWidget (localDocumentInfo);
         *      try {
         *              base.RunParameterCompletionCommand ();
         *      } finally {
         *              document = doc;
         *              CompletionWidget = cw;
         *      }
         * }*/

        protected override void GetElementCompletions(CompletionDataList list)
        {
            S.XName parentName = GetParentElementName(0);

            //fallback
            if (!HasDoc)
            {
                AddAspBeginExpressions(list);
                string aspPrefix = "asp:";
                var    type      = ReflectionHelper.ParseReflectionName("System.Web.UI.Control").Resolve(TypeSystemService.GetCompilation(project));
                foreach (var cls in WebTypeContext.ListSystemControlClasses(type, project))
                {
                    list.Add(new AspTagCompletionData(aspPrefix, cls));
                }

                base.GetElementCompletions(list);
                return;
            }

            IType controlClass = null;

            if (parentName.HasPrefix)
            {
                controlClass = refman.GetControlType(parentName.Prefix, parentName.Name);
            }
            else
            {
                S.XName grandparentName = GetParentElementName(1);
                if (grandparentName.IsValid && grandparentName.HasPrefix)
                {
                    controlClass = refman.GetControlType(grandparentName.Prefix, grandparentName.Name);
                }
            }

            //we're just in HTML
            if (controlClass == null)
            {
                //root element?
                if (!parentName.IsValid)
                {
                    if (aspDoc.Info.Subtype == WebSubtype.WebControl)
                    {
                        AddHtmlTagCompletionData(list, Schema, new S.XName("div"));
                        AddAspBeginExpressions(list);
                        list.AddRange(refman.GetControlCompletionData());
                        AddMiscBeginTags(list);
                    }
                    else if (!string.IsNullOrEmpty(aspDoc.Info.MasterPageFile))
                    {
                        //FIXME: add the actual region names
                        list.Add(new CompletionData("asp:Content"));
                    }
                }
                else
                {
                    AddAspBeginExpressions(list);
                    list.AddRange(refman.GetControlCompletionData());
                    base.GetElementCompletions(list);
                }
                return;
            }

            string defaultProp;
            bool   childrenAsProperties = AreChildrenAsProperties(controlClass, out defaultProp);

            if (defaultProp != null && defaultProp.Length == 0)
            {
                defaultProp = null;
            }

            //parent permits child controls directly
            if (!childrenAsProperties)
            {
                AddAspBeginExpressions(list);
                list.AddRange(refman.GetControlCompletionData());
                AddMiscBeginTags(list);
                //TODO: get correct parent for Content tags
                AddHtmlTagCompletionData(list, Schema, new S.XName("body"));
                return;
            }

            //children of properties
            if (childrenAsProperties && (!parentName.HasPrefix || defaultProp != null))
            {
                if (controlClass.GetProjectContent() == null)
                {
                    LoggingService.LogWarning("IType {0} does not have a SourceProjectDom", controlClass);
                    return;
                }

                string    propName = defaultProp ?? parentName.Name;
                IProperty property =
                    controlClass.GetProperties()
                    .Where(x => string.Compare(propName, x.Name, StringComparison.OrdinalIgnoreCase) == 0)
                    .FirstOrDefault();

                if (property == null)
                {
                    return;
                }

                //sanity checks on attributes
                switch (GetPersistenceMode(property))
                {
                case System.Web.UI.PersistenceMode.Attribute:
                case System.Web.UI.PersistenceMode.EncodedInnerDefaultProperty:
                    return;

                case System.Web.UI.PersistenceMode.InnerDefaultProperty:
                    if (!parentName.HasPrefix)
                    {
                        return;
                    }
                    break;

                case System.Web.UI.PersistenceMode.InnerProperty:
                    if (parentName.HasPrefix)
                    {
                        return;
                    }
                    break;
                }

                //check if allows freeform ASP/HTML content
                if (property.ReturnType.ToString() == "System.Web.UI.ITemplate")
                {
                    AddAspBeginExpressions(list);
                    AddMiscBeginTags(list);
                    AddHtmlTagCompletionData(list, Schema, new S.XName("body"));
                    list.AddRange(refman.GetControlCompletionData());
                    return;
                }

                //FIXME:unfortunately ASP.NET doesn't seem to have enough type information / attributes
                //to be able to resolve the correct child types here
                //so we assume it's a list and have a quick hack to find arguments of strongly typed ILists

                IType collectionType = property.ReturnType;
                if (collectionType == null)
                {
                    list.AddRange(refman.GetControlCompletionData());
                    return;
                }

                string  addStr = "Add";
                IMethod meth   = collectionType.GetMethods()
                                 .Where(m => m.Parameters.Count == 1 && m.Name == addStr).FirstOrDefault();

                if (meth != null)
                {
                    IType argType = meth.Parameters [0].Type;
                    var   type    = ReflectionHelper.ParseReflectionName("System.Web.UI.Control").Resolve(argType.GetDefinition().Compilation);
                    if (argType != null && argType.IsBaseType(type))
                    {
                        list.AddRange(refman.GetControlCompletionData(argType));
                        return;
                    }
                }

                list.AddRange(refman.GetControlCompletionData());
                return;
            }

            //properties as children of controls
            if (parentName.HasPrefix && childrenAsProperties)
            {
                if (controlClass.GetProjectContent() == null)
                {
                    LoggingService.LogWarning("IType {0} does not have a SourceProjectDom", controlClass);
                }

                foreach (IProperty prop in GetUniqueMembers <IProperty> (controlClass.GetProperties()))
                {
                    if (GetPersistenceMode(prop) != System.Web.UI.PersistenceMode.Attribute)
                    {
                        list.Add(prop.Name, prop.GetStockIcon(), AmbienceService.GetSummaryMarkup(prop));
                    }
                }
                return;
            }
        }
        public string CreateTooltip(MonoDevelop.Ide.Gui.Document doc, int offset, ResolveResult result, string errorInformations, Gdk.ModifierType modifierState)
        {
            try {
                OutputSettings settings = new OutputSettings(OutputFlags.ClassBrowserEntries | OutputFlags.IncludeParameterName | OutputFlags.IncludeKeywords | OutputFlags.IncludeMarkup | OutputFlags.UseFullName);
                // Approximate value for usual case
                StringBuilder s             = new StringBuilder(150);
                string        documentation = null;
                if (result != null)
                {
                    if (result is UnknownIdentifierResolveResult)
                    {
                        s.Append(String.Format(GettextCatalog.GetString("Unresolved identifier '{0}'"), ((UnknownIdentifierResolveResult)result).Identifier));
                    }
                    else if (result.IsError)
                    {
                        s.Append(GettextCatalog.GetString("Resolve error."));
                    }
                    else if (result is LocalResolveResult)
                    {
                        var lr = (LocalResolveResult)result;
                        s.Append("<small><i>");
                        s.Append(lr.IsParameter ? paramStr : localStr);
                        s.Append("</i></small>\n");
                        s.Append(ambience.GetString(lr.Variable.Type, settings));
                        s.Append(" ");
                        s.Append(lr.Variable.Name);
                    }
                    else if (result is MethodGroupResolveResult)
                    {
                        var mrr = (MethodGroupResolveResult)result;
                        s.Append("<small><i>");
                        s.Append(methodStr);
                        s.Append("</i></small>\n");
                        var allMethods = new List <IMethod> (mrr.Methods);
                        foreach (var l in mrr.GetExtensionMethods())
                        {
                            allMethods.AddRange(l);
                        }
                        var method = allMethods.FirstOrDefault();
                        if (method != null)
                        {
                            s.Append(GLib.Markup.EscapeText(CreateAmbience(doc, offset, method.Compilation).ConvertEntity(method)));
                            if (allMethods.Count > 1)
                            {
                                int overloadCount = allMethods.Count - 1;
                                s.Append(string.Format(GettextCatalog.GetPluralString(" (+{0} overload)", " (+{0} overloads)", overloadCount), overloadCount));
                            }
                            documentation = AmbienceService.GetSummaryMarkup(method);
                        }
                    }
                    else if (result is MemberResolveResult)
                    {
                        var member = ((MemberResolveResult)result).Member;
                        s.Append("<small><i>");
                        s.Append(GetString(member));
                        s.Append("</i></small>\n");
                        var field = member as IField;
                        if (field != null && field.IsConst)
                        {
                            s.Append(GLib.Markup.EscapeText(CreateAmbience(doc, offset, field.Compilation).ConvertType(field.Type)));
                            s.Append(" ");
                            s.Append(field.Name);
                            s.Append(" = ");
                            s.Append(GetConst(field.ConstantValue));
                            s.Append(";");
                        }
                        else
                        {
                            s.Append(GLib.Markup.EscapeText(CreateAmbience(doc, offset, member.Compilation).ConvertEntity(member)));
                        }
                        documentation = AmbienceService.GetSummaryMarkup(member);
                    }
                    else if (result is NamespaceResolveResult)
                    {
                        s.Append("<small><i>");
                        s.Append(namespaceStr);
                        s.Append("</i></small>\n");
                        s.Append(ambience.GetString(((NamespaceResolveResult)result).NamespaceName, settings));
                    }
                    else
                    {
                        var tr         = result;
                        var typeString = GetString(tr.Type);
                        if (!string.IsNullOrEmpty(typeString))
                        {
                            s.Append("<small><i>");
                            s.Append(typeString);
                            s.Append("</i></small>\n");
                        }
                        settings.OutputFlags |= OutputFlags.UseFullName;
                        s.Append(ambience.GetString(tr.Type, settings));
                        documentation = AmbienceService.GetSummaryMarkup(tr.Type.GetDefinition());
                    }

                    if (!string.IsNullOrEmpty(documentation))
                    {
                        s.Append("\n<small>");
                        s.Append(documentation);
                        s.Append("</small>");
                    }
                }

                if (!string.IsNullOrEmpty(errorInformations))
                {
                    if (s.Length != 0)
                    {
                        s.Append("\n\n");
                    }
                    s.Append("<small>");
                    s.Append(errorInformations);
                    s.Append("</small>");
                }
                return(s.ToString());
            } catch (Exception e) {
                return(e.ToString());
            }
        }