void AppendParameterList(StringBuilder result, CodeGenerationOptions options, IList <IParameter> parameters)
        {
            for (int i = 0; i < parameters.Count; i++)
            {
                if (i > 0)
                {
                    result.Append(", ");
                }

                var p = parameters[i];
                if (p.IsOut)
                {
                    result.Append("out ");
                }
                if (p.IsRef)
                {
                    result.Append("ref ");
                }
                if (p.IsParams)
                {
                    result.Append("params ");
                }
                AppendReturnType(result, options, p.Type);
                result.Append(" ");
                result.Append(CSharpAmbience.FilterName(p.Name));
            }
        }
        ////		public override string CreateFieldEncapsulation (IUnresolvedTypeDefinition implementingType, IField field, string propertyName, Accessibility modifiers, bool readOnly)
        ////		{
        ////			SetIndentTo (implementingType);
        ////			StringBuilder result = new StringBuilder ();
        ////			AppendIndent (result);
        ////
        //////			if (modifiers != MonoDevelop.Projects.Dom.Modifiers.None) {
        //////				switch (modifiers) {
        //////				}
        //////				result.Append (ambience.GetString (modifiers));
        //////				result.Append (" ");
        //////			}
        ////			var options = new CodeGenerationOptions () {
        ////				ImplementingType = field.DeclaringTypeDefinition,
        ////				Part = implementingType
        ////			};
        ////			result.Append ("public ");
        ////			AppendReturnType (result, options, field.ReturnType);
        ////			result.Append (" ");
        ////			result.Append (propertyName);
        ////			AppendBraceStart (result, Policy.PropertyBraceStyle);
        ////			AppendIndent (result);
        ////
        ////			result.Append ("get");
        ////			AppendBraceStart (result, Policy.PropertyGetBraceStyle);
        ////			AppendIndent (result);
        ////			result.Append ("return this.");
        ////			result.Append (CSharpAmbience.FilterName (field.Name));
        ////			result.Append (";");
        ////			AppendLine (result);
        ////			AppendBraceEnd (result, Policy.PropertyGetBraceStyle);
        ////			AppendLine (result);
        ////
        ////			if (!readOnly) {
        ////				AppendIndent (result);
        ////				result.Append ("set");
        ////				AppendBraceStart (result, Policy.PropertyGetBraceStyle);
        ////				AppendIndent (result);
        ////				result.Append (CSharpAmbience.FilterName (field.Name));
        ////				result.Append (" = value;");
        ////				AppendLine (result);
        ////				AppendBraceEnd (result, Policy.PropertyGetBraceStyle);
        ////				AppendLine (result);
        ////			}
        ////
        ////			AppendBraceEnd (result, Policy.PropertyBraceStyle);
        ////			return result.ToString ();
        ////		}
        //
        //		int CountBlankLines (IReadonlyTextDocument doc, int startLine)
        //		{
        //			int result = 0;
        //			IDocumentLine line;
        //			while ((line = doc.GetLine (startLine + result)) != null && doc.GetLineIndent (line).Length == line.Length) {
        //				result++;
        //			}
        //
        //			return result;
        //		}
        //
        //		static bool InsertUsingAfter (AstNode node)
        //		{
        //			return node is NewLineNode && IsCommentOrUsing (node.GetNextSibling (s => !(s is NewLineNode))) ||
        //				IsCommentOrUsing (node) || (node is PreProcessorDirective);
        //		}
        //
        //		static bool IsCommentOrUsing (AstNode node)
        //		{
        //			return node is ICSharpCode.NRefactory.CSharp.Comment ||
        //				node is UsingDeclaration ||
        //				node is UsingAliasDeclaration;
        //		}
        //

        //
        //		static string OutputNode (TextEditor editor, DocumentContext context, AstNode node)
        //		{
        //			using (var stringWriter = new System.IO.StringWriter ()) {
        ////				formatter.Indentation = indentLevel;
        //				var formatter = new TextWriterTokenWriter (stringWriter);
        //				stringWriter.NewLine = editor.EolMarker;
        //
        //				var visitor = new CSharpOutputVisitor (formatter, null /* TODO: BROKEN DUE ROSLYN PORT (note: that code should be unused) */ );
        //				node.AcceptVisitor (visitor);
        //				return stringWriter.ToString ();
        //			}
        //		}
        //
        //
        //		public AstType CreateShortType (ICompilation compilation, CSharpUnresolvedFile parsedFile, TextLocation loc, IType fullType)
        //		{
        //			var csResolver = parsedFile.GetResolver (compilation, loc);
        //			var builder = new ICSharpCode.NRefactory.CSharp.Refactoring.TypeSystemAstBuilder (csResolver);
        //			return builder.ConvertType (fullType);
        //		}
        //
        //		public override void CompleteStatement (MonoDevelop.Ide.Gui.Document doc)
        //		{
        //			//  TODO: BROKEN DUE ROSLYN PORT - needs to be ported to NR6
        ////			var fixer = new ConstructFixer (doc.GetFormattingOptions (), doc.Editor.CreateNRefactoryTextEditorOptions ());
        ////			int newOffset;
        ////			if (fixer.TryFix (new DocumentWrapper (doc.Editor), doc.Editor.CaretOffset, out newOffset)) {
        ////				doc.Editor.CaretOffset = newOffset;
        ////			}
        //		}



        static CodeGeneratorMemberResult GenerateProtocolCode(IMethodSymbol method, CodeGenerationOptions options)
        {
            int bodyStartOffset = -1, bodyEndOffset = -1;
            var result          = new StringBuilder();
            var exportAttribute = method.GetAttributes().FirstOrDefault(attr => attr.AttributeClass.Name == "ExportAttribute");

            if (exportAttribute != null)
            {
                result.Append("[Export(\"");
                result.Append(exportAttribute.ConstructorArguments.First().Value.ToString());
                result.Append("\")]");
                result.AppendLine();
            }
            AppendModifiers(result, options, method);

            AppendReturnType(result, options, method.ReturnType);
            result.Append(" ");
            if (options.ExplicitDeclaration)
            {
                AppendReturnType(result, options, method.ContainingType);
                result.Append(".");
            }

            result.Append(CSharpAmbience.FilterName(method.Name));
            if (method.TypeParameters.Length > 0)
            {
                result.Append("<");
                for (int i = 0; i < method.TypeParameters.Length; i++)
                {
                    if (i > 0)
                    {
                        result.Append(", ");
                    }
                    var p = method.TypeParameters[i];
                    result.Append(CSharpAmbience.FilterName(p.Name));
                }
                result.Append(">");
            }
            result.Append("(");
            AppendParameterList(result, options, method.Parameters, true);
            result.Append(")");

            var typeParameters = method.TypeParameters;

            result.Append("{");
            AppendIndent(result);
            bodyStartOffset = result.Length;
            result.Append("throw new System.NotImplementedException ();");
            bodyEndOffset = result.Length;
            AppendLine(result);
            result.Append("}");
            return(new CodeGeneratorMemberResult(result.ToString(), bodyStartOffset, bodyEndOffset));
        }
        public override string CreateFieldEncapsulation(IUnresolvedTypeDefinition implementingType, IField field, string propertyName, Accessibility modifiers, bool readOnly)
        {
            SetIndentTo(implementingType);
            StringBuilder result = new StringBuilder();

            AppendIndent(result);

//			if (modifiers != MonoDevelop.Projects.Dom.Modifiers.None) {
//				switch (modifiers) {
//				}
//				result.Append (ambience.GetString (modifiers));
//				result.Append (" ");
//			}
            var options = new CodeGenerationOptions()
            {
                ImplementingType = field.DeclaringTypeDefinition,
                Part             = implementingType
            };

            result.Append("public ");
            AppendReturnType(result, options, field.ReturnType);
            result.Append(" ");
            result.Append(propertyName);
            AppendBraceStart(result, Policy.PropertyBraceStyle);
            AppendIndent(result);

            result.Append("get");
            AppendBraceStart(result, Policy.PropertyGetBraceStyle);
            AppendIndent(result);
            result.Append("return this.");
            result.Append(CSharpAmbience.FilterName(field.Name));
            result.Append(";");
            AppendLine(result);
            AppendBraceEnd(result, Policy.PropertyGetBraceStyle);
            AppendLine(result);

            if (!readOnly)
            {
                AppendIndent(result);
                result.Append("set");
                AppendBraceStart(result, Policy.PropertyGetBraceStyle);
                AppendIndent(result);
                result.Append(CSharpAmbience.FilterName(field.Name));
                result.Append(" = value;");
                AppendLine(result);
                AppendBraceEnd(result, Policy.PropertyGetBraceStyle);
                AppendLine(result);
            }

            AppendBraceEnd(result, Policy.PropertyBraceStyle);
            return(result.ToString());
        }
        /*
         * void ResolveReturnTypes ()
         * {
         *      returnType = member.ReturnType;
         *      foreach (IUsing u in unit.Usings) {
         *              foreach (KeyValuePair<string, IReturnType> alias in u.Aliases) {
         *                      if (alias.Key == member.ReturnType.FullName) {
         *                              returnType = alias.Value;
         *                              return;
         *                      }
         *              }
         *      }
         * }*/


        CodeGeneratorMemberResult GenerateCode(IField field, CodeGenerationOptions options)
        {
            StringBuilder result = new StringBuilder();

            AppendIndent(result);
            AppendModifiers(result, options, field);
            result.Append(" ");
            AppendReturnType(result, options, field.ReturnType);
            result.Append(" ");
            result.Append(CSharpAmbience.FilterName(field.Name));
            result.Append(";");
            return(new CodeGeneratorMemberResult(result.ToString(), -1, -1));
        }
        public override string GetHeading(int overload, string[] parameterMarkup, int currentParameter)
        {
            var flags = OutputFlags.ClassBrowserEntries | OutputFlags.IncludeMarkup | OutputFlags.IncludeGenerics;

            if (staticResolve)
            {
                flags |= OutputFlags.StaticUsage;
            }

            var m = methods [overload];

            string name       = m.EntityType == EntityType.Constructor || m.EntityType == EntityType.Destructor ? m.DeclaringType.Name : m.Name;
            var    parameters = new StringBuilder();
            int    curLen     = 0;
            string prefix     = GetPrefix(m);

            foreach (string parameter in parameterMarkup)
            {
                if (parameters.Length > 0)
                {
                    parameters.Append(", ");
                }
                string         text;
                Pango.AttrList attrs;
                char           ch;
                Pango.Global.ParseMarkup(parameter, '_', out attrs, out text, out ch);
                if (text != null)
                {
                    curLen += text.Length + 2;
                }
                parameters.Append(parameter);
            }
            var sb = new StringBuilder();

            if (m.IsExtensionMethod)
            {
                sb.Append(GettextCatalog.GetString("(Extension) "));
            }
            sb.Append(prefix);
            sb.Append("<b>");
            sb.Append(CSharpAmbience.FilterName(name));
            sb.Append("</b> (");
            sb.Append(parameters.ToString());
            sb.Append(")");

            return(sb.ToString());
        }
        CodeGeneratorMemberResult GenerateCode(IEvent evt, CodeGenerationOptions options)
        {
            StringBuilder result = new StringBuilder();

            AppendObsoleteAttribute(result, options, evt);
            AppendModifiers(result, options, evt);

            result.Append("event ");
            AppendReturnType(result, options, evt.ReturnType);
            result.Append(" ");
            if (options.ExplicitDeclaration)
            {
                result.Append(ambience.GetString(evt.DeclaringTypeDefinition, OutputFlags.IncludeGenerics));
                result.Append(".");
            }
            result.Append(CSharpAmbience.FilterName(evt.Name));
            if (options.ExplicitDeclaration)
            {
                AppendBraceStart(result, Policy.EventBraceStyle);
                AppendIndent(result);
                result.Append("add");
                AppendBraceStart(result, Policy.EventAddBraceStyle);
                AppendIndent(result);
                result.Append("// TODO");
                AppendLine(result);
                AppendBraceEnd(result, Policy.EventAddBraceStyle);

                AppendIndent(result);
                result.Append("remove");
                AppendBraceStart(result, Policy.EventRemoveBraceStyle);
                AppendIndent(result);
                result.Append("// TODO");
                AppendLine(result);

                AppendBraceEnd(result, Policy.EventRemoveBraceStyle);
                AppendBraceEnd(result, Policy.EventBraceStyle);
            }
            else
            {
                result.Append(";");
            }
            return(new CodeGeneratorMemberResult(result.ToString()));
        }
        static CodeGeneratorMemberResult GeneratePartialCode(IMethodSymbol method, CodeGenerationOptions options)
        {
            int bodyStartOffset = -1, bodyEndOffset = -1;
            var result = new StringBuilder();

            AppendObsoleteAttribute(result, options, method);
            result.Append("partial ");
            AppendReturnType(result, options, method.ReturnType);
            result.Append(" ");
            if (options.ExplicitDeclaration)
            {
                AppendReturnType(result, options, method.ContainingType);
                result.Append(".");
            }

            result.Append(CSharpAmbience.FilterName(method.Name));
            if (method.TypeParameters.Length > 0)
            {
                result.Append("<");
                for (int i = 0; i < method.TypeParameters.Length; i++)
                {
                    if (i > 0)
                    {
                        result.Append(", ");
                    }
                    var p = method.TypeParameters[i];
                    result.Append(CSharpAmbience.FilterName(p.Name));
                }
                result.Append(">");
            }
            result.Append("(");
            AppendParameterList(result, options, method.Parameters, true);
            result.Append(")");

            var typeParameters = method.TypeParameters;

            result.AppendLine("{");
            bodyStartOffset = result.Length;
            AppendLine(result);
            bodyEndOffset = result.Length;
            result.AppendLine("}");
            return(new CodeGeneratorMemberResult(result.ToString(), bodyStartOffset, bodyEndOffset));
        }
        static CodeGeneratorMemberResult GenerateCode(IEventSymbol evt, CodeGenerationOptions options)
        {
            StringBuilder result = new StringBuilder();

            AppendObsoleteAttribute(result, options, evt);
            AppendModifiers(result, options, evt);

            result.Append("event ");
            AppendReturnType(result, options, evt.Type);
            result.Append(" ");
            if (options.ExplicitDeclaration)
            {
                AppendReturnType(result, options, evt.ContainingType);
                result.Append(".");
            }

            result.Append(CSharpAmbience.FilterName(evt.Name));
            if (options.ExplicitDeclaration)
            {
                result.Append("{");
                AppendIndent(result);
                result.Append("add {");
                AppendIndent(result);
                result.Append("// TODO");
                AppendLine(result);
                result.Append("}");

                AppendIndent(result);
                result.Append("remove {");
                AppendIndent(result);
                result.Append("// TODO");
                AppendLine(result);
                result.Append("}}");
            }
            else
            {
                result.Append(";");
            }
            return(new CodeGeneratorMemberResult(result.ToString()));
        }
Example #9
0
        public string GetHeading(int overload, string[] parameterMarkup, int currentParameter)
        {
            var flags = OutputFlags.ClassBrowserEntries | OutputFlags.IncludeMarkup | OutputFlags.IncludeGenerics;

            string name       = delegateType.Name;
            var    parameters = new StringBuilder();
            int    curLen     = 0;
            string prefix     = !delegateMethod.IsConstructor ? ambience.GetString(delegateMethod.ReturnType, flags) + " " : "";

            foreach (string parameter in parameterMarkup)
            {
                if (parameters.Length > 0)
                {
                    parameters.Append(", ");
                }
                string         text;
                Pango.AttrList attrs;
                char           ch;
                Pango.Global.ParseMarkup(parameter, '_', out attrs, out text, out ch);
                if (curLen > 80)
                {
                    parameters.AppendLine();
                    parameters.Append(new string (' ', (prefix != null ? prefix.Length : 0) + name.Length + 4));
                    curLen = 0;
                }
                curLen += text.Length + 2;
                parameters.Append(parameter);
            }
            var sb = new StringBuilder();

            sb.Append(prefix);
            sb.Append("<b>");
            sb.Append(CSharpAmbience.FilterName(name));
            sb.Append("</b> (");
            sb.Append(parameters.ToString());
            sb.Append(")");
            return(sb.ToString());
        }
        CodeGeneratorMemberResult GenerateCode(IProperty property, CodeGenerationOptions options)
        {
            var regions = new List <CodeGeneratorBodyRegion> ();
            var result  = new StringBuilder();

            AppendObsoleteAttribute(result, options, property);
            AppendModifiers(result, options, property);
            AppendReturnType(result, options, property.ReturnType);
            result.Append(" ");
            if (property.IsIndexer)
            {
                result.Append("this[");
                AppendParameterList(result, options, property.Parameters);
                result.Append("]");
            }
            else
            {
                if (options.ExplicitDeclaration)
                {
                    result.Append(ambience.GetString(property.DeclaringType, OutputFlags.IncludeGenerics));
                    result.Append(".");
                }
                result.Append(CSharpAmbience.FilterName(property.Name));
            }
            AppendBraceStart(result, Policy.PropertyBraceStyle);
            if (property.CanGet)
            {
                int bodyStartOffset, bodyEndOffset;
                AppendIndent(result);
                result.Append("get");
                if (options.ImplementingType.Kind == TypeKind.Interface)
                {
                    result.AppendLine(";");
                }
                else
                {
                    AppendBraceStart(result, Policy.PropertyGetBraceStyle);
                    if (IsMonoTouchModelMember(property))
                    {
                        AppendMonoTouchTodo(result, options, out bodyStartOffset, out bodyEndOffset);
                    }
                    else if (property.IsAbstract || property.DeclaringTypeDefinition.Kind == TypeKind.Interface)
                    {
                        AppendNotImplementedException(result, options, out bodyStartOffset, out bodyEndOffset);
                    }
                    else
                    {
                        AppendIndent(result);
                        bodyStartOffset = result.Length;
                        if (property.SymbolKind == SymbolKind.Indexer)
                        {
                            result.Append("return base[");
                            if (property.Parameters.Count > 0)
                            {
                                result.Append(CSharpAmbience.FilterName(property.Parameters.First().Name));
                            }
                            result.Append("];");
                        }
                        else
                        {
                            result.Append("return base.");
                            result.Append(CSharpAmbience.FilterName(property.Name));
                            result.Append(";");
                        }
                        bodyEndOffset = result.Length;
                        AppendLine(result);
                    }
                    AppendBraceEnd(result, Policy.PropertyGetBraceStyle);
                    AppendLine(result);
                    regions.Add(new CodeGeneratorBodyRegion(bodyStartOffset, bodyEndOffset));
                }
            }

            if (property.CanSet)
            {
                int bodyStartOffset, bodyEndOffset;
                AppendIndent(result);
                result.Append("set");
                if (options.ImplementingType.Kind == TypeKind.Interface)
                {
                    result.AppendLine(";");
                }
                else
                {
                    AppendBraceStart(result, Policy.PropertyGetBraceStyle);
                    if (IsMonoTouchModelMember(property))
                    {
                        AppendMonoTouchTodo(result, options, out bodyStartOffset, out bodyEndOffset);
                    }
                    else if (property.IsAbstract || property.DeclaringTypeDefinition.Kind == TypeKind.Interface)
                    {
                        AppendNotImplementedException(result, options, out bodyStartOffset, out bodyEndOffset);
                    }
                    else
                    {
                        AppendIndent(result);
                        bodyStartOffset = result.Length;
                        if (property.SymbolKind == SymbolKind.Indexer)
                        {
                            result.Append("base[");
                            if (property.Parameters.Count > 0)
                            {
                                result.Append(CSharpAmbience.FilterName(property.Parameters.First().Name));
                            }
                            result.Append("] = value;");
                        }
                        else
                        {
                            result.Append("base.");
                            result.Append(CSharpAmbience.FilterName(property.Name));
                            result.Append(" = value;");
                        }
                        bodyEndOffset = result.Length;
                        AppendLine(result);
                    }
                    AppendBraceEnd(result, Policy.PropertyGetBraceStyle);
                    AppendLine(result);
                    regions.Add(new CodeGeneratorBodyRegion(bodyStartOffset, bodyEndOffset));
                }
            }
            AppendBraceEnd(result, Policy.PropertyBraceStyle);
            return(new CodeGeneratorMemberResult(result.ToString(), regions));
        }
        void AppendParameterList(StringBuilder result, CodeGenerationOptions options, IList <IParameter> parameters)
        {
            for (int i = 0; i < parameters.Count; i++)
            {
                if (i > 0)
                {
                    result.Append(", ");
                }

                var p = parameters[i];
                if (p.IsOut)
                {
                    result.Append("out ");
                }
                if (p.IsRef)
                {
                    result.Append("ref ");
                }
                if (p.IsParams)
                {
                    result.Append("params ");
                }
                AppendReturnType(result, options, p.Type);
                result.Append(" ");
                result.Append(CSharpAmbience.FilterName(p.Name));
                if (p.ConstantValue != null)
                {
                    result.Append(" = ");
                    if (p.Type.Kind == TypeKind.Enum)
                    {
                        bool found = false;
                        foreach (var literal in GetEnumLiterals(p.Type))
                        {
                            if (literal.ConstantValue.Equals(p.ConstantValue))
                            {
                                AppendReturnType(result, options, p.Type);
                                result.Append("." + literal.Name);
                                found = true;
                                break;
                            }
                        }
                        if (!found)
                        {
                            result.Append("(");
                            AppendReturnType(result, options, p.Type);
                            result.Append(")" + p.ConstantValue);
                        }
                    }
                    else if (p.ConstantValue is char)
                    {
                        result.Append("'" + p.ConstantValue + "'");
                    }
                    else if (p.ConstantValue is string)
                    {
                        result.Append("\"" + CSharpTextEditorIndentation.ConvertToStringLiteral((string)p.ConstantValue) + "\"");
                    }
                    else if (p.ConstantValue is bool)
                    {
                        result.Append((bool)p.ConstantValue ? "true" : "false");
                    }
                    else
                    {
                        result.Append(p.ConstantValue);
                    }
                }
            }
        }
        CodeGeneratorMemberResult GenerateCode(IMethod method, CodeGenerationOptions options)
        {
            int           bodyStartOffset = -1, bodyEndOffset = -1;
            StringBuilder result = new StringBuilder();

            AppendObsoleteAttribute(result, options, method);
            AppendModifiers(result, options, method);
            if (method.IsPartial)
            {
                result.Append("partial ");
            }
            AppendReturnType(result, options, method.ReturnType);
            result.Append(" ");
            if (options.ExplicitDeclaration)
            {
                AppendReturnType(result, options, method.DeclaringType);
                result.Append(".");
            }

            result.Append(CSharpAmbience.FilterName(method.Name));
            if (method.TypeParameters.Count > 0)
            {
                result.Append("<");
                for (int i = 0; i < method.TypeParameters.Count; i++)
                {
                    if (i > 0)
                    {
                        result.Append(", ");
                    }
                    var p = method.TypeParameters [i];
                    result.Append(CSharpAmbience.FilterName(p.Name));
                }
                result.Append(">");
            }
            if (Policy.BeforeMethodDeclarationParentheses)
            {
                result.Append(" ");
            }
            result.Append("(");
            AppendParameterList(result, options, method.Parameters);
            result.Append(")");

            var typeParameters = method.TypeParameters;

            // This should also check the types are in the correct mscorlib
            Func <IType, bool> validBaseType = t => t.FullName != "System.Object" && t.FullName != "System.ValueType";

            bool isFromInterface = method.DeclaringType != null && method.DeclaringTypeDefinition.Kind == TypeKind.Interface;

            if (!options.ExplicitDeclaration && isFromInterface && typeParameters.Any(p => p.HasDefaultConstructorConstraint || p.HasReferenceTypeConstraint || p.HasValueTypeConstraint || p.DirectBaseTypes.Any(validBaseType)))
            {
                result.Append(" where ");
                int typeParameterCount = 0;
                foreach (var p in typeParameters)
                {
                    if (typeParameterCount != 0)
                    {
                        result.Append(", ");
                    }

                    typeParameterCount++;
                    result.Append(CSharpAmbience.FilterName(p.Name));
                    result.Append(" : ");
                    int constraintCount = 0;

                    if (p.HasDefaultConstructorConstraint)
                    {
                        result.Append("new ()");
                        constraintCount++;
                    }

                    if (p.HasValueTypeConstraint)
                    {
                        if (constraintCount != 0)
                        {
                            result.Append(", ");
                        }
                        result.Append("struct");
                        constraintCount++;
                    }

                    if (p.HasReferenceTypeConstraint)
                    {
                        if (constraintCount != 0)
                        {
                            result.Append(", ");
                        }
                        result.Append("class");
                        constraintCount++;
                    }
                    //					bool hadInterfaces = false;
                    foreach (var c in p.DirectBaseTypes.Where(validBaseType))
                    {
                        if (constraintCount != 0)
                        {
                            result.Append(", ");
                        }
                        constraintCount++;
                        AppendReturnType(result, options, c);
                        //						if (c.Kind == TypeKind.Interface)
                        //							hadInterfaces = true;
                    }
                }
            }

            if (options.ImplementingType.Kind == TypeKind.Interface)
            {
                result.Append(";");
            }
            else
            {
                AppendBraceStart(result, Policy.MethodBraceStyle);
                if (method.Name == "ToString" && (method.Parameters == null || method.Parameters.Count == 0) && method.ReturnType != null /* && method.ReturnType.FullName == "System.String"*/)
                {
                    AppendIndent(result);
                    bodyStartOffset = result.Length;
                    result.Append("return string.Format");
                    if (Policy.BeforeMethodDeclarationParentheses)
                    {
                        result.Append(" ");
                    }
                    result.Append("(\"[");
                    result.Append(options.ImplementingType.Name);
                    if (options.ImplementingType.Properties.Any())
                    {
                        result.Append(": ");
                    }
                    int i          = 0;
                    var properties = new List <IProperty> ();

                    foreach (IProperty property in options.ImplementingType.Properties)
                    {
                        if (properties.Any(p => p.Name == property.Name))
                        {
                            continue;
                        }
                        properties.Add(property);
                    }

                    foreach (IProperty property in properties)
                    {
                        if (property.IsStatic || !property.IsPublic)
                        {
                            continue;
                        }
                        if (i > 0)
                        {
                            result.Append(", ");
                        }
                        result.Append(property.Name);
                        result.Append("={");
                        result.Append(i++);
                        result.Append("}");
                    }
                    result.Append("]\"");
                    foreach (IProperty property in properties)
                    {
                        if (property.IsStatic || !property.IsPublic)
                        {
                            continue;
                        }
                        result.Append(", ");
                        result.Append(property.Name);
                    }
                    result.Append(");");
                    bodyEndOffset = result.Length;
                    AppendLine(result);
                }
                else if (IsMonoTouchModelMember(method))
                {
                    AppendMonoTouchTodo(result, options, out bodyStartOffset, out bodyEndOffset);
                }
                else if (method.IsAbstract || !(method.IsVirtual || method.IsOverride) || method.DeclaringTypeDefinition.Kind == TypeKind.Interface)
                {
                    AppendNotImplementedException(result, options, out bodyStartOffset, out bodyEndOffset);
                }
                else
                {
                    bool skipBody = false;
                    // Analyze if the body consists just of a single throw instruction
                    // See: Bug 1373 - overriding [Model] class methods shouldn't insert base.Methods
                    // TODO: Extend this to user defined code.
                    try {
                        if (method.Region.FileName == null)
                        {
                            var asm = AssemblyDefinition.ReadAssembly(method.ParentAssembly.UnresolvedAssembly.Location);
                            foreach (var type in asm.MainModule.Types)
                            {
                                if (type.FullName != method.DeclaringType.FullName)
                                {
                                    continue;
                                }
                                foreach (var m  in type.Resolve().Methods)
                                {
                                    if (m.HasBody && m.Name == method.Name)
                                    {
                                        var context = new DecompilerContext(asm.MainModule);

                                        context.CurrentType = type;

                                        context.Settings = new DecompilerSettings()
                                        {
                                            AnonymousMethods    = true,
                                            AutomaticEvents     = true,
                                            AutomaticProperties = true,
                                            ForEachStatement    = true,
                                            LockStatement       = true
                                        };

                                        var astBuilder = new AstBuilder(context);
                                        astBuilder.AddMethod(m);

                                        astBuilder.RunTransformations(o => false);

                                        var visitor = new ThrowsExceptionVisitor();
                                        astBuilder.SyntaxTree.AcceptVisitor(visitor);
                                        skipBody = visitor.Throws;
                                        if (skipBody)
                                        {
                                            break;
                                        }
                                    }
                                }
                                if (skipBody)
                                {
                                    break;
                                }
                            }
                        }
                    } catch (Exception) {
                    }
                    AppendIndent(result);
                    bodyStartOffset = result.Length;
                    if (!skipBody)
                    {
                        if (method.ReturnType.ReflectionName != typeof(void).FullName)
                        {
                            result.Append("return ");
                        }
                        result.Append("base.");
                        result.Append(CSharpAmbience.FilterName(method.Name));
                        if (Policy.BeforeMethodCallParentheses)
                        {
                            result.Append(" ");
                        }
                        result.Append("(");
                        for (int i = 0; i < method.Parameters.Count; i++)
                        {
                            if (i > 0)
                            {
                                result.Append(", ");
                            }

                            var p = method.Parameters [i];
                            if (p.IsOut)
                            {
                                result.Append("out ");
                            }
                            if (p.IsRef)
                            {
                                result.Append("ref ");
                            }
                            result.Append(CSharpAmbience.FilterName(p.Name));
                        }
                        result.Append(");");
                    }
                    else
                    {
                        result.Append("throw new System.NotImplementedException ();");
                    }
                    bodyEndOffset = result.Length;
                    AppendLine(result);
                }
                AppendBraceEnd(result, Policy.MethodBraceStyle);
            }
            return(new CodeGeneratorMemberResult(result.ToString(), bodyStartOffset, bodyEndOffset));
        }
        //		class ThrowsExceptionVisitor : DepthFirstAstVisitor
        //		{
        //			public bool Throws = false;
        //
        //			public override void VisitBlockStatement (BlockStatement blockStatement)
        //			{
        //				if (blockStatement.Statements.Count == 1 && blockStatement.Statements.First () is ThrowStatement)
        //					Throws = true;
        //			}
        //		}

        static void AppendParameterList(StringBuilder result, CodeGenerationOptions options, IList <IParameterSymbol> parameters, bool asParameterList)
        {
            for (int i = 0; i < parameters.Count; i++)
            {
                if (i > 0)
                {
                    result.Append(", ");
                }

                var p = parameters [i];
                if (p.RefKind == RefKind.Out)
                {
                    result.Append("out ");
                }
                if (p.RefKind == RefKind.Ref)
                {
                    result.Append("ref ");
                }
                if (asParameterList)
                {
                    if (p.IsParams)
                    {
                        result.Append("params ");
                    }
                    AppendReturnType(result, options, p.Type);
                    result.Append(" ");
                }
                result.Append(CSharpAmbience.FilterName(p.Name));
                if (p.HasExplicitDefaultValue)
                {
                    result.Append(" = ");
                    if (p.ExplicitDefaultValue is Enum)
                    {
                        var name = Enum.GetName(p.ExplicitDefaultValue.GetType(), p.ExplicitDefaultValue);
                        if (name != null)
                        {
                            AppendReturnType(result, options, p.Type);
                            result.Append("." + name);
                        }
                        else
                        {
                            result.Append("(");
                            AppendReturnType(result, options, p.Type);
                            result.Append(")" + p.ExplicitDefaultValue);
                        }
                    }
                    else if (p.ExplicitDefaultValue is char)
                    {
                        result.Append("'" + p.ExplicitDefaultValue + "'");
                    }
                    else if (p.ExplicitDefaultValue is string)
                    {
                        result.Append("\"" + CSharpTextEditorIndentation.ConvertToStringLiteral((string)p.ExplicitDefaultValue) + "\"");
                    }
                    else if (p.ExplicitDefaultValue is bool)
                    {
                        result.Append((bool)p.ExplicitDefaultValue ? "true" : "false");
                    }
                    else
                    {
                        result.Append(p.ExplicitDefaultValue);
                    }
                }
            }
        }
Example #14
0
        public string GetMethodMarkup(int overload, string[] parameterMarkup, int currentParameter)
        {
            var flags = OutputFlags.ClassBrowserEntries | OutputFlags.IncludeMarkup | OutputFlags.IncludeGenerics;

            if (staticResolve)
            {
                flags |= OutputFlags.StaticUsage;
            }
            string        name       = (this.delegateName ?? (methods [overload].IsConstructor ? ambience.GetString(methods [overload].DeclaringType, flags) : methods [overload].Name));
            StringBuilder parameters = new StringBuilder();
            int           curLen     = 0;
            string        prefix     = !methods [overload].IsConstructor ? ambience.GetString(methods [overload].ReturnType, flags) + " " : "";

            foreach (string parameter in parameterMarkup)
            {
                if (parameters.Length > 0)
                {
                    parameters.Append(", ");
                }
                string         text;
                Pango.AttrList attrs;
                char           ch;
                Pango.Global.ParseMarkup(parameter, '_', out attrs, out text, out ch);
                if (curLen > 80)
                {
                    parameters.AppendLine();
                    parameters.Append(new string (' ', (prefix != null ? prefix.Length : 0) + name.Length + 4));
                    curLen = 0;
                }
                curLen += text.Length + 2;
                parameters.Append(parameter);
            }
            StringBuilder sb = new StringBuilder();

            if (!staticResolve && methods [overload].WasExtended)
            {
                sb.Append(GettextCatalog.GetString("(Extension) "));
            }
            sb.Append(prefix);
            sb.Append("<b>");
            sb.Append(CSharpAmbience.FilterName(name));
            sb.Append("</b> (");
            sb.Append(parameters.ToString());
            sb.Append(")");

            if (methods [overload].IsObsolete)
            {
                sb.AppendLine();
                sb.Append(GettextCatalog.GetString("[Obsolete]"));
            }
            IParameter curParameter = currentParameter >= 0 && currentParameter < methods [overload].Parameters.Count ? methods [overload].Parameters [currentParameter] : null;

            string docText = AmbienceService.GetDocumentation(methods [overload]);

            if (!string.IsNullOrEmpty(docText))
            {
                string text = docText;
                if (curParameter != null)
                {
                    Regex paramRegex = new Regex("(\\<param\\s+name\\s*=\\s*\"" + curParameter.Name + "\"\\s*\\>.*?\\</param\\>)", RegexOptions.Compiled);
                    Match match      = paramRegex.Match(docText);
                    if (match.Success)
                    {
                        text = match.Groups [1].Value;
                        text = "<summary>" + AmbienceService.GetDocumentationSummary(methods [overload]) + "</summary>" + text;
                    }
                }
                else
                {
                    text = "<summary>" + AmbienceService.GetDocumentationSummary(methods [overload]) + "</summary>";
                }
                sb.AppendLine();
                sb.Append(AmbienceService.GetDocumentationMarkup(text, new AmbienceService.DocumentationFormatOptions {
                    HighlightParameter = curParameter != null ? curParameter.Name : null,
                    Ambience           = ambience,
                    SmallText          = true
                }));
            }

            if (curParameter != null)
            {
                var returnType = curParameter.DeclaringMember.SourceProjectDom.GetType(curParameter.ReturnType);
                if (returnType != null && returnType.ClassType == ClassType.Delegate)
                {
                    sb.AppendLine();
                    sb.AppendLine();
                    sb.Append("<small>");
                    sb.AppendLine(GettextCatalog.GetString("Delegate information"));
                    sb.Append(ambience.GetString(returnType, OutputFlags.ReformatDelegates | OutputFlags.IncludeReturnType | OutputFlags.IncludeParameters | OutputFlags.IncludeParameterName));
                    sb.Append("</small>");
                }
            }
            return(sb.ToString());
        }