// 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);
        }
예제 #2
0
		// 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()
		{
			SD.Log.Debug("CSharpDesignerLoader.Parse()");
			
			lastTextContentVersion = context.DesignerCodeFileDocument.Version;
			var primaryParseInfo = context.GetPrimaryFileParseInformation();
			var compilation = context.GetCompilation();
			
			// Find designer class
			ITypeDefinition designerClass = FormsDesignerSecondaryDisplayBinding.GetDesignableClass(primaryParseInfo.UnresolvedFile, compilation, out primaryPart);
			IMethod initializeComponents = FormsDesignerSecondaryDisplayBinding.GetInitializeComponents(designerClass);
			
			if (initializeComponents == null) {
				throw new FormsDesignerLoadException("The InitializeComponent method was not found. Designer cannot be loaded.");
			}
			Debug.Assert(primaryPart != null);
			Debug.Assert(designerClass != null);
			
			bool isFirstClassInFile = primaryParseInfo.UnresolvedFile.TopLevelTypeDefinitions[0] == primaryPart;
			
			// TODO: translate
			const string missingReferenceMessage = "Your project is missing a reference to '${Name}' - please add it using 'Project > Add Reference'.";
			if (compilation.FindType(typeof(System.Drawing.Point)).Kind == TypeKind.Unknown) {
				throw new FormsDesignerLoadException(StringParser.Parse(missingReferenceMessage, new StringTagPair("Name", "System.Drawing")));
			}
			if (compilation.FindType(typeof(System.Windows.Forms.Form)).Kind == TypeKind.Unknown) {
				throw new FormsDesignerLoadException(StringParser.Parse(missingReferenceMessage, new StringTagPair("Name" , "System.Windows.Forms")));
			}
			
			CodeDomConvertVisitor cv = new CodeDomConvertVisitor();
			cv.UseFullyQualifiedTypeNames = true;
			
			CSharpFullParseInformation designerParseInfo;
			MethodDeclaration initializeComponentsDeclaration = initializeComponents.GetDeclaration(out designerParseInfo) as MethodDeclaration;
			if (initializeComponentsDeclaration == null)
				throw new FormsDesignerLoadException("Could not find source code for InitializeComponents");
			var resolver = designerParseInfo.GetResolver(compilation);
			var codeMethod = (CodeMemberMethod) cv.Convert(initializeComponentsDeclaration, resolver);
			var codeClass = new CodeTypeDeclaration(designerClass.Name);
			codeClass.Attributes = MemberAttributes.Public;
			codeClass.BaseTypes.AddRange(designerClass.DirectBaseTypes.Select(cv.Convert).ToArray());
			codeClass.Members.Add(codeMethod);
			
			foreach (var field in designerClass.Fields) {
				var codeField = new CodeMemberField(cv.Convert(field.Type), field.Name);
				codeClass.Members.Add(codeField);
			}
			var codeNamespace = new CodeNamespace(designerClass.Namespace);
			codeNamespace.Types.Add(codeClass);
			var codeUnit = new CodeCompileUnit();
			codeUnit.Namespaces.Add(codeNamespace);
			
			// output generated CodeDOM to the console :
			#if DEBUG
			if ((Control.ModifierKeys & Keys.Control) == Keys.Control) {
				CodeDomVerboseOutputGenerator outputGenerator = new CodeDomVerboseOutputGenerator();
				outputGenerator.GenerateCodeFromMember(codeMethod, Console.Out, null);
				this.CodeDomProvider.GenerateCodeFromCompileUnit(codeUnit, 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 to be compiled correctly.\n" +
				                           "Please move other classes below the form class definition or move them to other files.");
			}
			
			return codeUnit;
		}