Ejemplo n.º 1
0
        void ExecuteMacro(MacroOutput output, CodeConstruct sourceFileCode, string sourceFilePath, string defaultNamespace, int orderNo)
        {
            NamespaceDeclaration ns = output.CodeConstruct.CompilationUnit.PushChild(
                new NamespaceDeclaration(sourceFileCode.CompilationUnit.Down().OfType <NamespaceDeclaration>().First().Name));

            ns.AddUsing("System");

            List <string> genericTypeNameList = new List <string>();
            List <string> paramNameList       = new List <string>();

            for (int i = 1; i <= MAX_PARAMS; i++)
            {
                genericTypeNameList.Add("T" + i);
                paramNameList.Add("param" + i);
            }

            for (int paramCount = 1; paramCount <= MAX_PARAMS; paramCount++)
            {
                Func <string, int, bool> where = (name, index) => index < paramCount;
                IEnumerable <string> genericTypeNames = genericTypeNameList.Where(where);
                string[]             paramNames       = paramNameList.Where(where).ToArray();

                CodeConstruct sourceFileCodeClone = sourceFileCode.Clone();
                ns.AddChild(MakeGenericCopy(sourceFileCodeClone, "WeakAction", genericTypeNames, paramNames));
                ns.AddChild(MakeGenericCopy(sourceFileCodeClone, "WeakFunc", genericTypeNames, paramNames));
            }
        }
        public static TypeDeclaration add_Type(this NamespaceDeclaration namespaceDeclaration, IClass iClass)
        {
            // move to method IClass.typeDeclaration();
            var typeName = iClass.Name;

            var newType = namespaceDeclaration.type(typeName);          // check if already exists and if it does return it

            if (newType != null)
            {
                return(newType);
            }

            const Modifiers modifiers = Modifiers.None | Modifiers.Public;

            newType = new TypeDeclaration(modifiers, new List <AttributeSection>())
            {
                Name = typeName
            };

            foreach (var baseType in iClass.BaseTypes)
            {
                if (baseType.FullyQualifiedName != "System.Object")  // no need to include this one
                {
                    newType.BaseTypes.Add(new TypeReference(baseType.FullyQualifiedName));
                }
            }

            namespaceDeclaration.AddChild(newType);

            return(newType);

            //return namespaceDeclaration.add_Type(iClass.Name);
        }
        public override object VisitCompilationUnit(CompilationUnit compilationUnit, object data)
        {
            base.VisitCompilationUnit(compilationUnit, data);
            if (!string.IsNullOrEmpty(NamespacePrefixToAdd))
            {
                for (int i = 0; i < compilationUnit.Children.Count; i++)
                {
                    NamespaceDeclaration ns = compilationUnit.Children[i] as NamespaceDeclaration;
                    if (ns != null)
                    {
                        ns.Name = NamespacePrefixToAdd + "." + ns.Name;
                    }
                    if (compilationUnit.Children[i] is TypeDeclaration || compilationUnit.Children[i] is DelegateDeclaration)
                    {
                        ns = new NamespaceDeclaration(NamespacePrefixToAdd);
                        ns.AddChild(compilationUnit.Children[i]);
                        compilationUnit.Children[i] = ns;
                    }
                }
            }

            ToCSharpConvertVisitor v = new ToCSharpConvertVisitor();

            compilationUnit.AcceptVisitor(v, data);
            if (projectContent != null && projectContent.DefaultImports != null)
            {
                int index = 0;
                foreach (string u in projectContent.DefaultImports.Usings)
                {
                    compilationUnit.Children.Insert(index++, new UsingDeclaration(u));
                }
            }
            return(null);
        }
Ejemplo n.º 4
0
        public static TypeDeclaration AddType(this NamespaceDeclaration namespaceDeclaration, string typeName)
        {
            var typeDeclaration = new TypeDeclaration(Modifiers.Public, null);

            typeDeclaration.Name = typeName;
            namespaceDeclaration.AddChild(typeDeclaration);

            return(typeDeclaration);
        }
Ejemplo n.º 5
0
			public override void Visit(ModuleContainer mc)
			{
				bool first = true;
				foreach (var container in mc.Containers) {
					var nspace = container as NamespaceContainer;
					if (nspace == null) {
						container.Accept(this);
						continue;
					}
					NamespaceDeclaration nDecl = null;
					var loc = LocationsBag.GetLocations(nspace);
					
					if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
						nDecl = new NamespaceDeclaration ();
						if (loc != null) {
							nDecl.AddChild(new CSharpTokenNode (Convert(loc [0])), Roles.NamespaceKeyword);
						}
						ConvertNamespaceName(nspace.RealMemberName, nDecl);
						if (loc != null && loc.Count > 1) {
							nDecl.AddChild(new CSharpTokenNode (Convert(loc [1])), Roles.LBrace);
						}
						AddToNamespace(nDecl);
						namespaceStack.Push(nDecl);
					}
					
					if (nspace.Usings != null) {
						foreach (var us in nspace.Usings) {
							us.Accept(this);
						}
					}
					
					if (first) {
						first = false;
						AddAttributeSection(Unit, mc);
					}
					
					if (nspace.Containers != null) {
						foreach (var subContainer in nspace.Containers) {
							subContainer.Accept(this);
						}
					}
					if (nDecl != null) {
						AddAttributeSection (nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
						if (loc != null && loc.Count > 2)
							nDecl.AddChild (new CSharpTokenNode (Convert (loc [2])), Roles.RBrace);
						if (loc != null && loc.Count > 3)
							nDecl.AddChild (new CSharpTokenNode (Convert (loc [3])), Roles.Semicolon);
						
						namespaceStack.Pop ();
					} else {
						AddAttributeSection (unit, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
					}
				}
				AddAttributeSection (unit, mc.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
			}
Ejemplo n.º 6
0
        public void Setup()
        {
            _mocks        = new MockRepository();
            _typeResolver = _mocks.DynamicMock <ITypeResolver>();
            _visitor      = new TypeInspectionVisitor(_typeResolver);

            _type      = new TypeDeclaration(Modifiers.Public, new List <AttributeSection>());
            _type.Name = "SomeType";
            _namespace = new NamespaceDeclaration("SomeNamespace");
            _namespace.AddChild(_type);
            _type.Parent = _namespace;
        }
Ejemplo n.º 7
0
			public override void Visit (UsingsBag.Namespace nspace)
			{
				NamespaceDeclaration nDecl = null;
				if (nspace.Name != null) {
					nDecl = new NamespaceDeclaration ();
					nDecl.AddChild (new CSharpTokenNode (Convert (nspace.NamespaceLocation), "namespace".Length), NamespaceDeclaration.Roles.Keyword);
					nDecl.AddChild (new Identifier (nspace.Name.Name, Convert (nspace.Name.Location)), NamespaceDeclaration.Roles.Identifier);
					nDecl.AddChild (new CSharpTokenNode (Convert (nspace.OpenBrace), 1), NamespaceDeclaration.Roles.LBrace);
					AddToNamespace (nDecl);
					namespaceStack.Push (nDecl);
					
				}
				VisitNamespaceUsings (nspace);
				VisitNamespaceBody (nspace);
				
				if (nDecl != null) {
					nDecl.AddChild (new CSharpTokenNode (Convert (nspace.CloseBrace), 1), NamespaceDeclaration.Roles.RBrace);
					if (!nspace.OptSemicolon.IsNull)
						nDecl.AddChild (new CSharpTokenNode (Convert (nspace.OptSemicolon), 1), NamespaceDeclaration.Roles.Semicolon);
				
					namespaceStack.Pop ();
				}
			}
        // should be merged with the one using CompilationUnit
        public static TypeDeclaration add_Type(this NamespaceDeclaration namespaceDeclaration, string typeName)
        {
            var newType = namespaceDeclaration.type(typeName);          // check if already exists and if it does return it

            if (newType != null)
            {
                return(newType);
            }

            const Modifiers modifiers = Modifiers.None | Modifiers.Public;

            newType      = new TypeDeclaration(modifiers, new List <AttributeSection>());
            newType.Name = typeName;
            namespaceDeclaration.AddChild(newType);
            return(newType);
        }
        private void CreateTypeDeclarations(string jarFile)
        {
            LoadClasses(Path.GetFullPath(jarFile));

            foreach (string package in classes.Keys)
            {
                if (!IsIncluded(package))
                {
                    continue;
                }
                Console.WriteLine(package);
                IDictionary types          = new Hashtable();
                IList       packageClasses = (IList)classes[package];
                foreach (ClassFile cf in packageClasses)
                {
                    if (cf.IsPublic)
                    {
                        TypeDeclaration type = GetTypeDeclaration(cf);
                        types.Add(type.Name, type);
                    }
                }

                foreach (TypeDeclaration type in types.Values)
                {
                    if (type.Name.IndexOf("$") == -1)
                    {
                        NamespaceDeclaration nameSpace = new NamespaceDeclaration(package);
                        type.Parent = nameSpace;
                        nameSpace.AddChild(type);

                        CompilationUnit cu = new CompilationUnit();
                        cu.Children.Add(nameSpace);
                        compilationUnits.Add(cu);
                    }
                    else
                    {
                        string          parentName = type.Name.Substring(0, type.Name.IndexOf("$"));
                        TypeDeclaration parent     = (TypeDeclaration)types[GetTypeName(parentName)];
                        if (parent != null)
                        {
                            type.Name = GetTypeName(type.Name);
                            parent.AddChild(type);
                        }
                    }
                }
            }
        }
Ejemplo n.º 10
0
        private void CreateInterfaceFieldsClass(TypeDeclaration typeDeclaration)
        {
            List <INode> fields = AstUtil.GetChildrenWithType(typeDeclaration, typeof(FieldDeclaration));

            if (fields.Count > 0)
            {
                TypeDeclaration fieldsClass = new TypeDeclaration(Modifiers.Public, null);
                fieldsClass.Type = ClassType.Class;
                fieldsClass.Name = typeDeclaration.Name + fieldsClassSuffix;
                fieldsClass.Children.AddRange(fields);
                fieldsClass.Parent = typeDeclaration.Parent;
                ApplyModifiers(fields);

                NamespaceDeclaration nsDeclaration = (NamespaceDeclaration)AstUtil.GetParentOfType(typeDeclaration, typeof(NamespaceDeclaration));
                nsDeclaration.AddChild(fieldsClass);

                string fullName = GetFullName(fieldsClass);

                CodeBase.Types[fullName] = fieldsClass;
                string typeName = GetFullName(typeDeclaration);

                CodeBase.References[typeName] = fullName;
            }
        }
Ejemplo n.º 11
0
			public override void Visit(NamespaceContainer ns)
			{
				NamespaceDeclaration nDecl = null;
				var loc = LocationsBag.GetLocations(ns);
				// <invalid> is caused by the parser - see Bug 12383 - [AST] Non existing namespaces generated
				if (ns.NS != null && !string.IsNullOrEmpty(ns.NS.Name) && !ns.NS.Name.EndsWith("<invalid>", StringComparison.Ordinal)) {
					nDecl = new NamespaceDeclaration();
					if (loc != null) {
						nDecl.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.NamespaceKeyword), Roles.NamespaceKeyword);
					}
					nDecl.AddChild(ConvertNamespaceName(ns.RealMemberName), NamespaceDeclaration.NamespaceNameRole);
					if (loc != null && loc.Count > 1) {
						nDecl.AddChild(new CSharpTokenNode(Convert(loc [1]), Roles.LBrace), Roles.LBrace);
					}

					AddToNamespace(nDecl);
					namespaceStack.Push(nDecl);
				}
				
				if (ns.Usings != null) {
					foreach (var us in ns.Usings) {
						us.Accept(this);
					}
				}
				
				if (ns.Containers != null) {
					foreach (var container in ns.Containers) {
						container.Accept(this);
					}
				}
				
				if (nDecl != null) {
					AddAttributeSection(nDecl, ns.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
					if (loc != null && loc.Count > 2)
						nDecl.AddChild(new CSharpTokenNode(Convert(loc [2]), Roles.RBrace), Roles.RBrace);
					if (loc != null && loc.Count > 3)
						nDecl.AddChild(new CSharpTokenNode(Convert(loc [3]), Roles.Semicolon), Roles.Semicolon);
					
					namespaceStack.Pop();
				}
			}
Ejemplo n.º 12
0
            public override void Visit(NamespaceContainer nspace)
            {
                NamespaceDeclaration nDecl = null;
                var loc = LocationsBag.GetLocations(nspace);

                if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
                    nDecl = new NamespaceDeclaration ();
                    if (loc != null) {
                        nDecl.AddChild(new CSharpTokenNode (Convert(loc [0]), Roles.NamespaceKeyword), Roles.NamespaceKeyword);
                    }
                    ConvertNamespaceName(nspace.RealMemberName, nDecl);
                    if (loc != null && loc.Count > 1) {
                        nDecl.AddChild(new CSharpTokenNode (Convert(loc [1]), Roles.LBrace), Roles.LBrace);
                    }
                    AddToNamespace(nDecl);
                    namespaceStack.Push(nDecl);
                }

                if (nspace.Usings != null) {
                    foreach (var us in nspace.Usings) {
                        us.Accept(this);
                    }
                }

                if (nspace.Containers != null) {
                    foreach (var container in nspace.Containers) {
                        container.Accept(this);
                    }
                }

                if (nDecl != null) {
                    AddAttributeSection(nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
                    if (loc != null && loc.Count > 2)
                        nDecl.AddChild (new CSharpTokenNode (Convert (loc [2]), Roles.RBrace), Roles.RBrace);
                    if (loc != null && loc.Count > 3)
                        nDecl.AddChild (new CSharpTokenNode (Convert (loc [3]), Roles.Semicolon), Roles.Semicolon);

                    namespaceStack.Pop ();
                }
            }
        // Steps to load the designer:
        // - Parse main file
        // - Find other files containing parts of the form
        // - Parse all files and look for fields (for controls) and InitializeComponents method
        // - Create CodeDom objects for fields and InitializeComponents statements
        // - If debug build and Ctrl pressed, output CodeDom to console
        // - Return CodeDom objects to the .NET designer
        protected override CodeCompileUnit Parse()
        {
            LoggingService.Debug("NRefactoryDesignerLoader.Parse()");

            lastTextContent = this.Generator.ViewContent.DesignerCodeFileContent;

            ParseInformation parseInfo = ParserService.GetParseInformation(this.Generator.ViewContent.DesignerCodeFile.FileName);

            IClass         formClass;
            bool           isFirstClassInFile;
            IList <IClass> parts = FindFormClassParts(parseInfo, out formClass, out isFirstClassInFile);

            const string missingReferenceMessage = "Your project is missing a reference to '${Name}' - please add it using 'Project > Add Reference'.";

            if (formClass.ProjectContent.GetClass("System.Drawing.Point", 0) == null)
            {
                throw new FormsDesignerLoadException(
                          StringParser.Parse(
                              missingReferenceMessage, new string[, ] {
                    { "Name", "System.Drawing" }
                }
                              ));
            }
            if (formClass.ProjectContent.GetClass("System.Windows.Forms.Form", 0) == null)
            {
                throw new FormsDesignerLoadException(
                          StringParser.Parse(
                              missingReferenceMessage, new string[, ] {
                    { "Name", "System.Windows.Forms" }
                }
                              ));
            }

            List <KeyValuePair <string, CompilationUnit> > compilationUnits = new List <KeyValuePair <string, CompilationUnit> >();
            bool foundInitMethod = false;

            foreach (IClass part in parts)
            {
                string fileName = part.CompilationUnit.FileName;
                if (fileName == null)
                {
                    continue;
                }
                bool found = false;
                foreach (KeyValuePair <string, CompilationUnit> entry in compilationUnits)
                {
                    if (FileUtility.IsEqualFileName(fileName, entry.Key))
                    {
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    continue;
                }

                ITextBuffer fileContent;
                if (FileUtility.IsEqualFileName(fileName, this.Generator.ViewContent.PrimaryFileName))
                {
                    fileContent = this.Generator.ViewContent.PrimaryFileContent;
                }
                else if (FileUtility.IsEqualFileName(fileName, this.Generator.ViewContent.DesignerCodeFile.FileName))
                {
                    fileContent = new StringTextBuffer(this.Generator.ViewContent.DesignerCodeFileContent);
                }
                else
                {
                    fileContent = ParserService.GetParseableFileContent(fileName);
                }

                ICSharpCode.NRefactory.IParser p = ICSharpCode.NRefactory.ParserFactory.CreateParser(language, fileContent.CreateReader());
                p.Parse();
                if (p.Errors.Count > 0)
                {
                    throw new FormsDesignerLoadException("Syntax errors in " + fileName + ":\r\n" + p.Errors.ErrorOutput);
                }

                // Try to fix the type names to fully qualified ones
                FixTypeNames(p.CompilationUnit, part.CompilationUnit, ref foundInitMethod);
                compilationUnits.Add(new KeyValuePair <string, CompilationUnit>(fileName, p.CompilationUnit));
            }

            if (!foundInitMethod)
            {
                throw new FormsDesignerLoadException("The InitializeComponent method was not found. Designer cannot be loaded.");
            }

            CompilationUnit      combinedCu = new CompilationUnit();
            NamespaceDeclaration nsDecl     = new NamespaceDeclaration(formClass.Namespace);

            combinedCu.AddChild(nsDecl);
            TypeDeclaration formDecl = new TypeDeclaration(Modifiers.Public, null);

            nsDecl.AddChild(formDecl);
            formDecl.Name = formClass.Name;
            foreach (KeyValuePair <string, CompilationUnit> entry in compilationUnits)
            {
                foreach (object o in entry.Value.Children)
                {
                    TypeDeclaration td = o as TypeDeclaration;
                    if (td != null && td.Name == formDecl.Name)
                    {
                        foreach (INode node in td.Children)
                        {
                            formDecl.AddChild(node);
                        }
                        formDecl.BaseTypes.AddRange(td.BaseTypes);
                    }
                    if (o is NamespaceDeclaration)
                    {
                        foreach (object o2 in ((NamespaceDeclaration)o).Children)
                        {
                            td = o2 as TypeDeclaration;
                            if (td != null && td.Name == formDecl.Name)
                            {
                                foreach (INode node in td.Children)
                                {
                                    formDecl.AddChild(node);
                                }
                                formDecl.BaseTypes.AddRange(td.BaseTypes);
                            }
                        }
                    }
                }
            }

            CodeDomVisitor visitor = new CodeDomVisitor();

            visitor.EnvironmentInformationProvider = new ICSharpCode.SharpDevelop.Dom.NRefactoryResolver.NRefactoryInformationProvider(formClass.ProjectContent);
            visitor.VisitCompilationUnit(combinedCu, null);

            // output generated CodeDOM to the console :
                        #if DEBUG
            if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
            {
                CodeDomVerboseOutputGenerator outputGenerator = new CodeDomVerboseOutputGenerator();
                outputGenerator.GenerateCodeFromMember(visitor.codeCompileUnit.Namespaces[0].Types[0], Console.Out, null);
                this.CodeDomProvider.GenerateCodeFromCompileUnit(visitor.codeCompileUnit, Console.Out, null);
            }
                        #endif

            LoggingService.Debug("NRefactoryDesignerLoader.Parse() finished");

            if (!isFirstClassInFile)
            {
                MessageService.ShowWarning("The form must be the first class in the file in order for form resources be compiled correctly.\n" +
                                           "Please move other classes below the form class definition or move them to other files.");
            }

            return(visitor.codeCompileUnit);
        }