Ejemplo n.º 1
0
        public static CompletionItem Build(CodeObject m)
        {
            if(m is CodeMemberMethodEx) {
                var method = m as CodeMemberMethodEx;
                return new CompletionItemMethod(method.Name, string.Format("Method {0}({1})\n{2}", method.Name, method.GetParamInfo(), GetDocumentCommentString(method.Comments)));
            }else if(m is CodeTypeDeclaration && ((CodeTypeDeclaration)m).IsClass) {
                var classdecl = ((CodeTypeDeclaration)m);
                return new CompletionItemClass(classdecl.Name, string.Format("class {0}\n{1}", classdecl.Name, GetDocumentCommentString(classdecl.Comments)));
            }else if(m is CodeMemberProperty) {
                var prop = ((CodeMemberProperty)m);
                if((prop.Attributes & MemberAttributes.Static) == MemberAttributes.Static)
                    return new CompletionItemPropertyStatic(prop.Name, string.Format("Property {0}\n{1}", prop.Name, GetDocumentCommentString(prop.Comments)));
                else
                    return new CompletionItemProperty(prop.Name, string.Format("Property {0}\n{1}", prop.Name, GetDocumentCommentString(prop.Comments)));

            }else if(m is CodeMemberField) {
                var prop = ((CodeMemberField)m);
                return new CompletionItemField(prop.Name, string.Format("Field {0}\n{1}", prop.Name, GetDocumentCommentString(prop.Comments)));
            } else if(m is CodeParameterDeclarationExpression) {
                var argument = m as CodeParameterDeclarationExpression;
                return new CompletionItemField(argument.Name, string.Format("Argument {0}", argument.Name));
            } else
                throw new NotSupportedException("Cant handle obj type: " + m.GetType().ToString());

            //return /* new CompletionItem(m.ToString(), ""); */
        }
Ejemplo n.º 2
0
 internal void ValidateIdentifiers(CodeObject e) {
     if (e is CodeCompileUnit) {
         ValidateCodeCompileUnit((CodeCompileUnit)e);
     } 
     else if (e is CodeComment) {
         ValidateComment((CodeComment)e);
     } 
     else if (e is CodeExpression) {
         ValidateExpression((CodeExpression)e);
     }
     else if (e is CodeNamespace) {
         ValidateNamespace((CodeNamespace)e);
     }
     else if (e is CodeNamespaceImport) {
         ValidateNamespaceImport((CodeNamespaceImport)e);
     }
     else if (e is CodeStatement) {
         ValidateStatement((CodeStatement)e);
     }
     else if (e is CodeTypeMember) {
         ValidateTypeMember((CodeTypeMember)e);
     }
     else if (e is CodeTypeReference) {
         ValidateTypeReference((CodeTypeReference)e);
     }
     else if (e is CodeDirective) {
         ValidateCodeDirective((CodeDirective) e);
     }
     else {
         throw new ArgumentException(SR.GetString(SR.InvalidElementType, e.GetType().FullName), "e");
     }
 }
        private void Process(ref CodeObject target, CodeObject parent, int indent)
        {
            CodeMethodInvokeExpression methodInvokeExpr = target as CodeMethodInvokeExpression;

            if (methodInvokeExpr != null &&
                methodInvokeExpr.Method.TargetObject == null &&
                StringUtils.CaseInsensitiveEquals(methodInvokeExpr.Method.MethodName, "CreateObject") &&
                methodInvokeExpr.Parameters.Count == 1)
            {
                CodePrimitiveExpression primitiveExpr = methodInvokeExpr.Parameters[0] as CodePrimitiveExpression;

                if (primitiveExpr != null)
                {
                    string progId = primitiveExpr.Value as string;

                    if (progId != null)
                    {
                        Type type = (_importer == null
                                         ? Type.GetTypeFromProgID(progId)
                                         : _importer.ImportProgId(progId));

                        if (type != null)
                        {
                            target = new CodeObjectCreateExpression(Utils.CreateTypeReference(type));
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public override void Generate(CodeObject codeObject, Entity entity)
        {
            var contentType = (ContentType) entity;
            var type = (CodeTypeDeclaration) codeObject;

            AddStructure(type, contentType);
        }
Ejemplo n.º 5
0
 public CodeDomWalker(CodeObject rootObject)
 {
     _rootObjects = new object[]
         {
             rootObject
         };
 }
Ejemplo n.º 6
0
        static string Dump(CodeObject co)
        {
            var cse = co as CodeSnippetExpression;
            if(cse != null)
                return cse.Value;

            throw new NotImplementedException();
        }
 internal static void CloneUserData(CodeObject original, CodeObject result)
 {
     foreach (object obj2 in original.UserData.Keys)
     {
         object key = CloneObject(obj2);
         object obj4 = CloneObject(original.UserData[obj2]);
         result.UserData.Add(key, obj4);
     }
 }
        public override void Generate(CodeObject codeObject, Entity entity)
        {
            var type = (CodeTypeDeclaration)codeObject;
            var contentType = (ContentType) entity;
            var info = contentType.Info;

            AddFieldIfNotEmpty(type, "icon", info.Icon);
            AddFieldIfNotEmpty(type, "thumbnail", info.Thumbnail);
            AddFieldIfTrue(type, "allowAtRoot", info.AllowAtRoot);
        }
        public override void Generate(CodeObject codeObject, Entity entity)
        {
            var description = (EntityDescription) entity;
            var type = (CodeTypeMember)codeObject;

            ValidateAlias(description);
            SetName(type, description);
            AddDisplayNameIfDifferent(type, description);
            AddDescription(type, description);
        }
        public override void Generate(CodeObject codeObject, Entity entity)
        {
            var propNode = (CodeMemberProperty) codeObject;
            var property = (GenericProperty) entity;

            var contentRef = new CodePropertyReferenceExpression(null, "Content");
            var getPropertyValueMethod = new CodeMethodReferenceExpression(contentRef, "GetPropertyValue", propNode.Type);
            var getPropertyValueCall = new CodeMethodInvokeExpression(getPropertyValueMethod, new CodePrimitiveExpression(property.Alias));
            propNode.GetStatements.Add(new CodeMethodReturnStatement(getPropertyValueCall));
        }
        public override void Generate(CodeObject codeObject, Entity entity)
        {
            var contentType = (ContentType) entity;
            var info = (DocumentTypeInfo) contentType.Info;
            var type = (CodeTypeDeclaration) codeObject;

            base.Generate(type, contentType);

            AddFieldIfNotEmpty(type, "defaultTemplate", info.DefaultTemplate);
            AddAllowedTemplates(type, info);
        }
Ejemplo n.º 12
0
        private void WalkList(IList list, CodeObject parent, int indent)
        {
            for (int i = 0; i < list.Count; i++)
            {
                CodeObject obj = list[i] as CodeObject;

                if (obj != null && WalkObject(ref obj, parent, indent))
                {
                    list[i] = obj;
                }
            }
        }
Ejemplo n.º 13
0
        public override void Generate(CodeObject codeObject, Entity entity)
        {
            var compileUnit = (CodeCompileUnit) codeObject;

            if (String.IsNullOrWhiteSpace(Config.Namespace))
                throw new Exception("ContentType namespace not configured.");

            var ns = new CodeNamespace(Config.Namespace);
            compileUnit.Namespaces.Add(ns);

            foreach(var generator in memberGenerators)
                generator.Generate(ns, entity);
        }
Ejemplo n.º 14
0
        public override void Generate(CodeObject codeObject, Entity entity)
        {
            var type = (CodeTypeDeclaration) codeObject;
            var contentType = (ContentType) entity;

            foreach (var property in contentType.GenericProperties)
            {
                var propNode = new CodeMemberProperty();
                foreach(var generator in propertyGenerators)
                    generator.Generate(propNode, property);
                type.Members.Add(propNode);
            }
        }
        public override void Generate(CodeObject codeObject, Entity entity)
        {
            var property = (GenericProperty)entity;
            var propNode = (CodeMemberProperty) codeObject;
            
            SetType(propNode, property);

            foreach (var generator in memberGenerators)
                generator.Generate(codeObject, property);

            AddDataType(propNode, property);
            AddCategory(propNode, property);
            AddRequired(propNode, property);
            AddValidation(propNode, property);
        }
Ejemplo n.º 16
0
        public override void Generate(CodeObject codeObject, Entity entity)
        {
            var contentType = (ContentType) entity;
            var info = contentType.Info;

            var ns = (CodeNamespace)codeObject;
            var type = new CodeTypeDeclaration();
            ns.Types.Add(type);

            entityDescriptionGenerator.Generate(type, info);
            SetBaseClass(type, info);

            if (memberGenerators != null)
                foreach(var generator in memberGenerators)
                    generator.Generate(type, contentType);
        }
Ejemplo n.º 17
0
 public override void Generate(CodeObject codeObject, Entity entity)
 {
     var type = (CodeTypeDeclaration) codeObject;
     var ctor = new CodeConstructor
     {
         Attributes = MemberAttributes.Public
     };
     ctor.Parameters.Add(
         new CodeParameterDeclarationExpression(
             "IPublishedContent",
             "content"
             )
         );
     ctor.BaseConstructorArgs.Add(
         new CodeVariableReferenceExpression("content")
         );
     type.Members.Add(ctor);
 }
Ejemplo n.º 18
0
        private void Localize(ref CodeObject target, CodeObject parent, int indent)
        {
            CodeMethodInvokeExpression methodInvokeExpr = target as CodeMethodInvokeExpression;

            if (methodInvokeExpr != null && methodInvokeExpr.Parameters.Count == 1 && methodInvokeExpr.Method.TargetObject != null)
            {
                CodeObjectSource source = CodeObjectMetaData.GetExpressionSource(methodInvokeExpr.Method.TargetObject);

                if (source != null && source.Target == typeof (ITranslator) && source.ArrayRanks.Length == 0)
                {
                    CodePrimitiveExpression primitiveExpr = methodInvokeExpr.Parameters[0] as CodePrimitiveExpression;

                    if (primitiveExpr != null)
                    {
                        string value = primitiveExpr.Value as string;

                        if (value != null)
                        {
                            string baseName = GenerateResourceName(value);
                            int counter = 0;
                            string name = baseName;
                            string existingValue;

                            while (_localizedStrings.TryGetValue(name, out existingValue) && value != existingValue)
                            {
                                name = baseName + (++counter);
                            }

                            target = new CodePropertyReferenceExpression(
                                new CodeTypeReferenceExpression(
                                    new CodeTypeReference(
                                        _namespaceName + (string.IsNullOrEmpty(_namespaceName) ? string.Empty : ".") + "Properties.Resources")),
                                name);

                            if (existingValue == null)
                            {
                                _localizedStrings.Add(name, value);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 19
0
        public void Convert(CodeObject code)
        {
            if (code is CodeCompileUnit)
            {
                foreach (CodeTypeMember member in ((CodeCompileUnit)code).Namespaces[0].Types[0].Members)
                {
                    if (member is CodeEntryPointMethod)
                        EmitStatements(((CodeEntryPointMethod)member).Statements);
                    else
                        EmitMethod((CodeMemberMethod)member);
                }
            }
            else if (code is CodeEntryPointMethod || code is CodeMemberMethod)
                EmitMethod((CodeMemberMethod)code);
            else if (code is CodeStatement)
                EmitStatement((CodeStatement)code);

            writer.WriteLine();
        }
Ejemplo n.º 20
0
        public static string GenerateCode(CodeObject codeObj)
        {
            var provider = new CSharpCodeProvider();

            using (var stream = new MemoryStream())
            using (var writer = new StreamWriter(stream))
            {
                if (codeObj is CodeCompileUnit)
                {
                    provider.GenerateCodeFromCompileUnit(codeObj as CodeCompileUnit, writer, null);
                }
                else if (codeObj is CodeExpression)
                {
                    provider.GenerateCodeFromExpression(codeObj as CodeExpression, writer, null);
                }
                else if (codeObj is CodeStatement)
                {
                    provider.GenerateCodeFromStatement(codeObj as CodeStatement, writer, null);
                }
                else if (codeObj is CodeTypeMember)
                {
                    provider.GenerateCodeFromMember(codeObj as CodeTypeMember, writer, null);
                }
                else if (codeObj is CodeNamespace)
                {
                    provider.GenerateCodeFromNamespace(codeObj as CodeNamespace, writer, null);
                }
                else
                {
                    throw new InvalidOperationException();
                }

                writer.Flush();
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
Ejemplo n.º 21
0
        private void Factorize(ref CodeObject target, CodeObject parent, int indent)
        {
            CodeTypeReference typeRef = target as CodeTypeReference;

            if (typeRef != null)
            {
                CodeObjectSource source = Utils.GetTypeReferenceSource(typeRef);

                if (source != null)
                {
                    Type type = source.Target as Type;

                    if (type != null &&
                        type != typeof (void) &&
                        type != typeof (object) &&
                        type != typeof (string) &&
                        type != typeof (decimal) &&
                        !type.IsPrimitive)
                    {
                        string baseType = typeRef.BaseType;
                        int pos = baseType.LastIndexOf('.');

                        if (pos >= 0)
                        {
                            string name = baseType.Substring(0, pos);

                            if (name != _currentNameSpaceName)
                            {
                                _namespaceNames.Add(name);
                            }

                            typeRef.BaseType = baseType.Substring(pos + 1);
                        }
                    }
                }
            }
        }
        private void Gather(ref CodeObject target, CodeObject parent, int depth)
        {
            CodeTypeReference typeRef = target as CodeTypeReference;

            if (typeRef != null)
            {
                CodeObjectSource source = Utils.GetTypeReferenceSource(typeRef);

                if (source != null)
                {
                    Type type = source.Target as Type;

                    if (type != null)
                    {
                        Assembly assembly = type.Assembly;

                        if (assembly != _mscorlibAssembly && assembly != _thisAssembly && !_references.ContainsKey(assembly.FullName))
                        {
                            _references.Add(assembly.FullName, (assembly.GlobalAssemblyCache ? null : assembly.Location));
                        }
                    }
                }
            }
        }
        private void Correct(ref CodeObject target, CodeObject parent, int indent)
        {
            if (target is CodeVariableReferenceExpression)
            {
                CodeVariableReferenceExpression variableExpr = (CodeVariableReferenceExpression) target;

                if (StringUtils.CaseInsensitiveEquals(variableExpr.VariableName, _setValueName))
                {
                    variableExpr.VariableName = _propertyName;
                }
            }
            else if (target is CodeMethodInvokeExpression)
            {
                CodeMethodInvokeExpression methodInvokeExpr = (CodeMethodInvokeExpression) target;

                if (methodInvokeExpr.Method.TargetObject == null && StringUtils.CaseInsensitiveEquals(methodInvokeExpr.Method.MethodName, _setValueName))
                {
                    CodeIndexerExpression indexerExpr = new CodeIndexerExpression(
                        new CodeVariableReferenceExpression(_propertyName));
                    indexerExpr.Indices.AddRange(methodInvokeExpr.Parameters);
                    target = indexerExpr;
                }
            }
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Set the user data with key 'MethodMemberInfo' of the specified CodeObject to a method 
 /// from the <see cref="InfoApe"/> class. The MethodInfo resolution is done
 /// through a <see cref="MethodInfo.GetMethod(string)"/> signature.
 /// </summary>
 /// <param name="codeTypeMember">The code type member which gets the <see cref="MethodInfo.UserData"/> 'MethodMemberInfo' set.</param>
 /// <param name="methodSignature">The signature of the desired method.</param>
 private static MethodInfo SetUserData(CodeObject codeTypeMember, string methodSignature)
 {
     MethodInfo methodInfo = typeof(InfoApe).GetMethod(methodSignature);
     codeTypeMember.UserData[NStubConstants.TestMemberMethodInfoKey] = methodInfo;
     return methodInfo;
 }
Ejemplo n.º 25
0
 public override void Generate(CodeObject codeObject, Entity entity)
 {
     var ns = (CodeNamespace) codeObject;
     AddImports(ns);
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Set the parameter index on the CodeExpression using
        /// EvaluationData.
        /// </summary>
        /// <param name="toSet">CodeExpression on which to set the
        /// parameter index.</param>
        /// <param name="parameterIndex">Index to set.</param>
        public static void SetParameterIndex(CodeObject toSet, int parameterIndex)
        {
            EvaluationData data = toSet.UserData[typeof(EvaluationData)] as EvaluationData;

            if (data == null)
            {
                data = new EvaluationData();
                toSet.UserData.Add(typeof(EvaluationData), data);
            }

            data.ParameterIndex = parameterIndex;
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Get the CodeExpression object's corresponding index
        /// into the IExpressionEvaluator's parameter list using
        /// EvaluationData.
        /// </summary>
        /// <param name="toGet">Expression for which to get the 
        /// parameter.</param>
        /// <param name="throwIfNotExist">Whether to throw an
        /// exception if the parameter cannot be found.  True to
        /// throw.</param>
        /// <returns>The index into the parameter list for this
        /// expression's replacement value.</returns>
        public static int GetParameterIndex(CodeObject toGet, bool throwIfNotExist)
        {
            EvaluationData data = toGet.UserData[typeof(EvaluationData)] as EvaluationData;

            if (data == null)
            {
                if (throwIfNotExist)
                {
                    throw new ApplicationException("No EvaluationData from which to extract the parameter.");
                }

                return -1;
            }
            else
            {
                return data.ParameterIndex;
            }
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Helper method used for getting the type of
        /// a CodeExpression through use of EvaluationData.
        /// </summary>
        /// <param name="toGet">CodeExpression which is adorned
        /// with type information.</param>
        /// <param name="throwIfNotExist">Whether to throw an
        /// exception if the type data is not available.  True to
        /// throw.</param>
        /// <returns>The type of the result of the CodeExpression.</returns>
        public static Type GetType(CodeObject toGet, bool throwIfNotExist)
        {
            EvaluationData data = toGet.UserData[typeof(EvaluationData)] as EvaluationData;

            if (data == null)
            {
                if (throwIfNotExist)
                {
                    throw new ApplicationException("The expression did not contain an EvaluationData object.");
                }

                return null;
            }
            else
            {
                return data.Type;
            }
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Helper method used for setting the type of
        /// a CodeExpression through use of EvaluationData.
        /// </summary>
        /// <param name="toSet">CodeExpression to adorn with
        /// type information.</param>
        /// <param name="newType">Type of the CodeExpression's result.</param>
        public static void SetType(CodeObject toSet, Type newType)
        {
            EvaluationData data = toSet.UserData[typeof(EvaluationData)] as EvaluationData;

            if (data == null)
            {
                data = new EvaluationData();
                toSet.UserData.Add(typeof(EvaluationData), data);
            }

            data.Type = newType;
        }
Ejemplo n.º 30
0
 private bool GetUserData(CodeObject e, string property, bool defaultValue)
 {
     object obj2 = e.UserData[property];
     if ((obj2 != null) && (obj2 is bool))
     {
         return (bool)obj2;
     }
     return defaultValue;
 }