Exemple #1
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 bool WalkObject(ref CodeObject target, CodeObject parent, int indent)
        {
            Type type = target.GetType();
            IEnumerable<PropertyInfo> properties;

            if (!_properties.TryGetValue(type, out properties))
            {
                PropertyInfo[] allProperties = type.GetProperties();
                List<PropertyInfo> someProperties = new List<PropertyInfo>(allProperties.Length);

                foreach (PropertyInfo property in allProperties)
                {
                    if (property.GetIndexParameters().Length == 0 &&
                        property.GetCustomAttributes(typeof (ObsoleteAttribute), false).Length == 0 &&
                        property.CanRead &&
                        ((property.CanWrite && typeof (CodeObject).IsAssignableFrom(property.PropertyType)) ||
                         typeof (IList).IsAssignableFrom(property.PropertyType)))
                    {
                        someProperties.Add(property);
                    }
                }

                properties = someProperties;
                _properties.Add(type, properties);
            }

            foreach (PropertyInfo property in properties)
            {
                object propertyValue = property.GetValue(target, null);

                if (propertyValue != null)
                {
                    if (propertyValue is CodeObject)
                    {
                        CodeObject childObj = (CodeObject) propertyValue;

                        if (WalkObject(ref childObj, target, indent))
                        {
                            property.SetValue(target, childObj, null);
                        }
                    }
                    else
                    {
                        bool isSwitchDefaultStmts = (property == _switchDefaultStmtsProperty);

                        if (isSwitchDefaultStmts)
                        {
                            indent++;
                        }

                        WalkList((IList) propertyValue, target, indent + 1);

                        if (isSwitchDefaultStmts)
                        {
                            indent--;
                        }
                    }
                }
            }

            CodeObject newTarget = target;
            _action(ref newTarget, parent, indent);
            bool changed = (newTarget != target);

            if (changed)
            {
                target = newTarget;
            }

            return changed;
        }
        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(), ""); */
        }