Esempio n. 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);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public CodeCompileUnit Parse(string source)
        {
            XCodeCompileUnit ccu = new XCodeCompileUnit();

            //
            try
            {
                var          reporter = new ErrorIgnorer();
                ITokenStream tokenStream;
                LanguageService.CodeAnalysis.XSharp.SyntaxParser.XSharpParserRuleContext xtree;
                bool ok = XSharp.Parser.VsParser.Parse(source, this.FileName, _projectNode.ParseOptions, reporter, out tokenStream, out xtree, out _);

                // We need to d 2 steps here:
                // 1 - Scan for the fields , so we know the difference between fields and properties when we perform step 2
                // 2 - Scan for the rest. We pass the list of fields to the tree discover code so it "knows" about all fields

                var discoverFields = new XSharpFieldsDiscover(_projectNode, typeInMainFile);
                discoverFields.SourceCode  = source;
                discoverFields.CurrentFile = this.FileName;

                var walker = new LanguageService.SyntaxTree.Tree.ParseTreeWalker();
                walker.Walk(discoverFields, xtree);
                // now the discoverFields class should contain a Dictionary with <context, FieldList>
                var discover = new XSharpClassDiscover(_projectNode, typeInMainFile);
                discover.FieldList   = discoverFields.FieldList;
                discover.SourceCode  = source;
                discover.CurrentFile = this.FileName;
                //
                walker.Walk(discover, xtree);
                //
                ccu = discover.CodeCompileUnit;
                var firstType = ccu.GetFirstClass();
                if (firstType != null)
                {
                    // save a copy of the member list to the CCU
                    ccu.Members = firstType.Members;
                }
                // save file name & original source
                ccu.FileName = this.FileName;
                ccu.Source   = source;
            }
            catch (Exception ex)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    Debug.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
                }
            }
            //
            return(ccu);
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public CodeCompileUnit Parse(string source)
        {
            XCodeCompileUnit ccu = new XCodeCompileUnit();

            //
            try
            {
                // Tab replace, in order to have the good position of Members (Line/col)
                String TabSpace = new String(' ', TabSize);
                source = source.Replace("\t", TabSpace);
                //
                var          reporter = new ErrorIgnorer();
                ITokenStream tokenStream;
                LanguageService.CodeAnalysis.XSharp.SyntaxParser.XSharpParserRuleContext xtree;
                bool ok = XSharp.Parser.VsParser.Parse(source, this.FileName, _projectNode.ParseOptions, reporter, out tokenStream, out xtree);

                // We need to d 2 steps here:
                // 1 - Scan for the fields , so we know the difference between fields and properties when we perform step 2
                // 2 - Scan for the rest. We pass the list of fields to the tree discover code so it "knows" about all fields

                var discoverFields = new XSharpFieldsDiscover(_projectNode);
                discoverFields.SourceCode  = source;
                discoverFields.CurrentFile = this.FileName;

                var walker = new LanguageService.SyntaxTree.Tree.ParseTreeWalker();
                walker.Walk(discoverFields, xtree);
                // now the discoverFields class should contain a Dictionary with <context, FieldList>
                var discover = new XSharpClassDiscover(_projectNode);
                discover.FieldList   = discoverFields.FieldList;
                discover.SourceCode  = source;
                discover.CurrentFile = this.FileName;
                //
                walker.Walk(discover, xtree);
                //
                ccu = discover.CodeCompileUnit;
                ccu.UserData[XSharpCodeConstants.USERDATA_FILENAME] = this.FileName;
                ccu.UserData[XSharpCodeConstants.USERDATA_CODE]     = source;
            }
            catch (Exception ex)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    Debug.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
                }
            }
            //
            return(ccu);
        }
Esempio n. 4
0
        public override CodeCompileUnit Parse(TextReader codeStream)
        {
            CodeCompileUnit ccu;

            if (codeStream == null)
            {
                ccu = new XCodeCompileUnit();
            }
            else
            {
                string codeFile;
                codeFile = codeStream.ReadToEnd();
                var field = codeStream.GetType().GetField("ClassName");
                if (field != null)
                {
                    typeInMainFile = field.GetValue(codeStream) as CodeTypeDeclaration;
                }
                ccu = Parse(codeFile);
            }
            return(ccu);
        }