예제 #1
0
        /// <summary>
        /// Merge both CodeCompileUnit. The main type (class) will come from designerCompileUnit
        /// </summary>
        /// <param name="compileUnit"></param>
        /// <param name="designerCompileUnit"></param>
        /// <returns></returns>
        internal static XMergedCodeCompileUnit MergeCodeCompileUnit(XCodeCompileUnit compileUnit, XCodeCompileUnit designerCompileUnit)
        {
            // Create the merged CodeCompileUnit
            var mergedCodeCompileUnit = new XMergedCodeCompileUnit();

            CodeNamespace       designerNamespace;
            CodeTypeDeclaration designerClass = FindDesignerClass(designerCompileUnit, out designerNamespace);

            if (designerClass != null)
            {
                // Do the same with the form
                CodeNamespace       nameSpace;
                CodeTypeDeclaration formClass;
                HasPartialClass(compileUnit, out nameSpace, out formClass);
                // and merge only if ...
                if ((string.Compare(designerNamespace.Name, nameSpace.Name, true) == 0) &&
                    (string.Compare(designerClass.Name, formClass.Name, true) == 0))
                {
                    // Ok, same Namespace & same Class : Merge !

                    // So, the "main" class is...
                    XCodeTypeDeclaration mergedType = new XCodeTypeDeclaration(formClass.Name);
                    // And does inherit from
                    mergedType.BaseTypes.AddRange(formClass.BaseTypes);
                    mergedType.IsPartial      = true;
                    mergedType.TypeAttributes = formClass.TypeAttributes;
                    // Now, read members from each side, and put a stamp on each
                    foreach (CodeTypeMember member in designerClass.Members)
                    {
                        member.SetFromDesigner(true);
                        mergedType.Members.Add(member);
                    }
                    foreach (CodeTypeMember member in formClass.Members)
                    {
                        member.SetFromDesigner(false);
                        mergedType.Members.Add(member);
                    }
                    // A class is always in a NameSpace
                    XCodeNamespace mergedNamespace = new XCodeNamespace(nameSpace.Name);
                    mergedNamespace.Types.Add(mergedType);
                    // Now, add it to the CompileUnit
                    mergedCodeCompileUnit.Namespaces.Clear();
                    mergedCodeCompileUnit.Namespaces.Add(mergedNamespace);
                    //
                }
                else
                {
                    // Something went wrong, return the designer CodeCompileUnit
                    mergedCodeCompileUnit = new XMergedCodeCompileUnit(designerCompileUnit);
                }
            }
            mergedCodeCompileUnit.FormUnit     = compileUnit;
            mergedCodeCompileUnit.FileName     = compileUnit.FileName;
            mergedCodeCompileUnit.DesignerUnit = designerCompileUnit;
            return(mergedCodeCompileUnit);
        }
예제 #2
0
        public override void EnterNamespace_(XSharpParser.Namespace_Context context)
        {
            string newNamespaceName = context.Name.GetCleanText();

            // We already have something in Stack
            // so we are nesting Namespaces, get the previous name prefix
            if (this.NamespaceStack.Count > 0 && !string.IsNullOrEmpty(CurrentNamespace.Name))
            {
                newNamespaceName = this.CurrentNamespace.Name + "." + newNamespaceName;
            }
            XCodeNamespace newNamespace = new XCodeNamespace(newNamespaceName);

            writeTrivia(newNamespace, context);
            this.NamespaceStack.Push(this.CurrentNamespace);
            this.CurrentNamespace = newNamespace;
        }
예제 #3
0
        private static XCodeTypeDeclaration FindFirstClass(CodeCompileUnit ccu, out XCodeNamespace namespaceName)
        {
            namespaceName = null;
            XCodeTypeDeclaration rstClass = null;

            if (ccu != null)
            {
                foreach (XCodeNamespace namespace2 in ccu.Namespaces)
                {
                    foreach (XCodeTypeDeclaration declaration in namespace2.Types)
                    {
                        //  The first Type == The first Class declaration
                        if (declaration.IsClass)
                        {
                            namespaceName = namespace2;
                            rstClass      = declaration;
                            break;
                        }
                    }
                }
            }
            return(rstClass);
        }