Пример #1
0
        protected override void OnParsedDocumentUpdated()
        {
            base.OnParsedDocumentUpdated();
            aspDoc = CU as AspNetParsedDocument;

            var newProj = (AspNetAppProject)base.Document.Project;

            if (newProj == null)
            {
                throw new InvalidOperationException("Document has no project");
            }

            if (project != newProj)
            {
                project = newProj;
                refman  = new DocumentReferenceManager(project);
            }

            if (HasDoc)
            {
                refman.Doc = aspDoc;
            }

            documentBuilder = HasDoc? LanguageCompletionBuilderService.GetBuilder(aspDoc.Info.Language) : null;

            if (documentBuilder != null)
            {
                var usings = refman.GetUsings();
                documentInfo = new DocumentInfo(aspDoc, usings, refman.GetDoms());
                documentInfo.ParsedDocument  = documentBuilder.BuildDocument(documentInfo, TextEditorData);
                documentInfo.CodeBesideClass = CreateCodeBesideClass(documentInfo, refman);
            }
        }
Пример #2
0
 void GetHtmlFoldingRegions(List <FoldingRegion> foldingRegions)
 {
     if (htmlParsedDocument != null)
     {
         var d = new AspNetParsedDocument(null, WebSubtype.Html, null, htmlParsedDocument);
         foldingRegions.AddRange(d.Foldings);
     }
 }
Пример #3
0
 public DocumentInfo(ICompilation dom, AspNetParsedDocument aspNetParsedDocument, IEnumerable <string> imports,
                     IList <ICompilation> references)
 {
     this.Dom            = dom;
     this.AspNetDocument = aspNetParsedDocument;
     this.Imports        = imports;
     this.References     = references;
     BuildExpressionAndScriptsLists();
 }
 public DocumentInfo(AspNetParsedDocument aspNetParsedDocument, IEnumerable <string> imports,
                     IList <ProjectDom> references)
 {
     this.AspNetDocument = aspNetParsedDocument;
     this.Imports        = imports;
     this.References     = references;
     ScriptBlocks        = new List <TagNode> ();
     Expressions         = new List <ExpressionNode> ();
     aspNetParsedDocument.RootNode.AcceptVisit(new ExpressionCollector(this));
 }
Пример #5
0
        /// <summary>
        /// Parses a given string
        /// </summary>
        /// <param name='doc'>
        /// ASP.NET document
        /// </param>
        /// <param name='fileName'>
        /// File name.
        /// </param>
        AspNetParsedDocument Parse(string doc, string fileName)
        {
            AspNetParsedDocument parsedDoc = null;
            AspNetParser         parser    = new AspNetParser();

            using (StringReader strRd = new StringReader(doc)) {
                parsedDoc = parser.Parse(true, fileName, strRd, textEditor.Project) as AspNetParsedDocument;
            }
            return(parsedDoc);
        }
Пример #6
0
        /// <summary>
        /// Updates a control's tag in the source code editor.
        /// </summary>
        /// <param name='component'>
        /// The changed component.
        /// </param>
        /// <param name='memberDesc'>
        /// Member desc of the property that was changed.
        /// </param>
        /// <param name='newVal'>
        /// The new value of the changed property.
        /// </param>
        public void UpdateTag(IComponent component, MemberDescriptor memberDesc, object newVal)
        {
            string key               = String.Empty;
            string value             = String.Empty;
            bool   removeOnly        = false;
            AspNetParsedDocument doc = host.RootDocument.Parse();
            XElement             el  = GetControlTag(doc.XDocument.RootElement, component.Site.Name);

            if (memberDesc is PropertyDescriptor)
            {
                var propDesc = memberDesc as PropertyDescriptor;
                // check if the value is the default for the property of the component
                // remove the attribute if it's the default
                if (propDesc.Attributes.Contains(new DefaultValueAttribute(newVal)))
                {
                    removeOnly = true;
                }
                else
                {
                    key   = memberDesc.Name;
                    value = propDesc.Converter.ConvertToString(newVal);
                }
            }
            else if (memberDesc is EventDescriptor)
            {
                //var eventDesc = memberDesc as EventDescriptor;
                //key = "On" + eventDesc.Name;
                // TODO: get the handler method name
                //value = newVal.ToString ();
            }
            else
            {
                // well, well, well! what do we have here!
            }

            if (removeOnly)
            {
                RemoveAttribute(el, memberDesc.Name);
            }
            else
            {
                SetAttribtue(el, key, value);
            }
        }
Пример #7
0
        protected override void OnParsedDocumentUpdated()
        {
            base.OnParsedDocumentUpdated();
            aspDoc = CU as AspNetParsedDocument;

            var newProj = base.Document.Project as AspNetAppProject;

            if (newProj == null)
            {
                return;
            }
            //throw new InvalidOperationException ("Document has no project");

            if (project != newProj)
            {
                project = newProj;
                refman  = new DocumentReferenceManager(project);
            }

            if (HasDoc)
            {
                refman.Doc = aspDoc;
            }

            documentBuilder = HasDoc ? LanguageCompletionBuilderService.GetBuilder(aspDoc.Info.Language) : null;

            if (documentBuilder != null)
            {
                var usings = refman.GetUsings();
                documentInfo = new DocumentInfo(document.Compilation, aspDoc, usings, refman.GetDoms());
                documentInfo.ParsedDocument  = documentBuilder.BuildDocument(documentInfo, Editor);
                documentInfo.CodeBesideClass = CreateCodeBesideClass(documentInfo, refman);

/*				var domWrapper = new AspProjectDomWrapper (documentInfo);
 *                              if (localDocumentInfo != null)
 *                                      localDocumentInfo.HiddenDocument.HiddenContext = domWrapper;*/
            }
        }
Пример #8
0
        public static BuildResult GenerateCodeBehind(
            AspNetAppProject project,
            string filename,
            AspNetParsedDocument document,
            out CodeCompileUnit ccu)
        {
            ccu = null;
            var    result    = new BuildResult();
            string className = document.Info.InheritedClass;

            AddErrorsToResult(result, filename, document.Errors);
            if (result.ErrorCount > 0)
            {
                return(result);
            }

            if (string.IsNullOrEmpty(className))
            {
                return(result);
            }

            var refman = new DocumentReferenceManager(project)
            {
                Doc = document
            };
            var memberList = new MemberListBuilder(refman, document.XDocument);

            memberList.Build();

            AddErrorsToResult(result, filename, memberList.Errors);
            if (result.ErrorCount > 0)
            {
                return(result);
            }

            //initialise the generated type
            ccu = new CodeCompileUnit();
            var namespac = new CodeNamespace();

            ccu.Namespaces.Add(namespac);
            var typeDecl = new CodeTypeDeclaration {
                IsClass   = true,
                IsPartial = true,
            };

            namespac.Types.Add(typeDecl);

            //name the class and namespace
            int namespaceSplit = className.LastIndexOf('.');

            if (namespaceSplit > -1)
            {
                namespac.Name = project.StripImplicitNamespace(className.Substring(0, namespaceSplit));
                typeDecl.Name = className.Substring(namespaceSplit + 1);
            }
            else
            {
                typeDecl.Name = className;
            }

            string masterTypeName = null;

            if (!String.IsNullOrEmpty(document.Info.MasterPageTypeName))
            {
                masterTypeName = document.Info.MasterPageTypeName;
            }
            else if (!String.IsNullOrEmpty(document.Info.MasterPageTypeVPath))
            {
                try {
                    ProjectFile          resolvedMaster       = project.ResolveVirtualPath(document.Info.MasterPageTypeVPath, document.FileName);
                    AspNetParsedDocument masterParsedDocument = null;
                    if (resolvedMaster != null)
                    {
                        masterParsedDocument = TypeSystemService.ParseFile(project, resolvedMaster.FilePath) as AspNetParsedDocument;
                    }
                    if (masterParsedDocument != null && !String.IsNullOrEmpty(masterParsedDocument.Info.InheritedClass))
                    {
                        masterTypeName = masterParsedDocument.Info.InheritedClass;
                    }
                } catch (Exception ex) {
                    LoggingService.LogWarning("Error resolving master page type", ex);
                }
                if (string.IsNullOrEmpty(masterTypeName))
                {
                    var msg = string.Format("Could not find type for master '{0}'", document.Info.MasterPageTypeVPath);
                    result.AddError(filename, msg);
                    return(result);
                }
            }

            if (masterTypeName != null)
            {
                var masterProp = new CodeMemberProperty {
                    Name       = "Master",
                    Type       = new CodeTypeReference(masterTypeName),
                    HasGet     = true,
                    HasSet     = false,
                    Attributes = MemberAttributes.Public | MemberAttributes.New | MemberAttributes.Final,
                };
                masterProp.GetStatements.Add(new CodeMethodReturnStatement(
                                                 new CodeCastExpression(masterTypeName,
                                                                        new CodePropertyReferenceExpression(
                                                                            new CodeBaseReferenceExpression(), "Master"))));
                typeDecl.Members.Add(masterProp);
            }

            //shortcut building the existing members type map
            if (memberList.Members.Count == 0)
            {
                return(result);
            }

            var dom     = refman.TypeCtx.Compilation;
            var cls     = ReflectionHelper.ParseReflectionName(className).Resolve(dom);
            var members = GetDesignerMembers(memberList.Members.Values, cls, filename);

            //add fields for each control in the page

            foreach (var member in members)
            {
                var type = new CodeTypeReference(member.Type.FullName);
                typeDecl.Members.Add(new CodeMemberField(type, member.Name)
                {
                    Attributes = MemberAttributes.Family
                });
            }
            return(result);
        }
Пример #9
0
        public static System.CodeDom.CodeCompileUnit GenerateCodeBehind(AspNetAppProject project,
                                                                        string filename,
                                                                        AspNetParsedDocument document,
                                                                        List <CodeBehindWarning> errors)
        {
            string className = document.Info.InheritedClass;

            if (document.HasErrors)
            {
                AddFail(errors, document, document.Errors.Where(x => x.ErrorType == ErrorType.Error).First());
                return(null);
            }

            if (string.IsNullOrEmpty(className))
            {
                return(null);
            }

            var refman = new DocumentReferenceManager(project)
            {
                Doc = document
            };
            var memberList = new MemberListVisitor(document, refman);

            document.RootNode.AcceptVisit(memberList);

            var err = memberList.Errors.Where(x => x.ErrorType == ErrorType.Error).FirstOrDefault();

            if (err != null)
            {
                AddFail(errors, document, err);
                return(null);
            }

            //initialise the generated type
            var ccu      = new CodeCompileUnit();
            var namespac = new CodeNamespace();

            ccu.Namespaces.Add(namespac);
            var typeDecl = new System.CodeDom.CodeTypeDeclaration()
            {
                IsClass   = true,
                IsPartial = true,
            };

            namespac.Types.Add(typeDecl);

            //name the class and namespace
            int    namespaceSplit = className.LastIndexOf('.');
            string namespaceName  = null;

            if (namespaceSplit > -1)
            {
                namespac.Name = project.StripImplicitNamespace(className.Substring(0, namespaceSplit));
                typeDecl.Name = className.Substring(namespaceSplit + 1);
            }
            else
            {
                typeDecl.Name = className;
            }

            string masterTypeName = null;

            if (!String.IsNullOrEmpty(document.Info.MasterPageTypeName))
            {
                masterTypeName = document.Info.MasterPageTypeName;
            }
            else if (!String.IsNullOrEmpty(document.Info.MasterPageTypeVPath))
            {
                try {
                    ProjectFile          resolvedMaster       = project.ResolveVirtualPath(document.Info.MasterPageTypeVPath, document.FileName);
                    AspNetParsedDocument masterParsedDocument = null;
                    if (resolvedMaster != null)
                    {
                        masterParsedDocument = ProjectDomService.Parse(project, resolvedMaster.FilePath) as AspNetParsedDocument;
                    }
                    if (masterParsedDocument != null && !String.IsNullOrEmpty(masterParsedDocument.Info.InheritedClass))
                    {
                        masterTypeName = masterParsedDocument.Info.InheritedClass;
                    }
                    else
                    {
                        errors.Add(new CodeBehindWarning(String.Format("Could not find type for master '{0}'",
                                                                       document.Info.MasterPageTypeVPath),
                                                         document.FileName));
                    }
                } catch (Exception ex) {
                    errors.Add(new CodeBehindWarning(String.Format("Could not find type for master '{0}'",
                                                                   document.Info.MasterPageTypeVPath),
                                                     document.FileName));
                    LoggingService.LogWarning("Error resolving master page type", ex);
                }
            }

            if (masterTypeName != null)
            {
                var masterProp = new CodeMemberProperty()
                {
                    Name       = "Master",
                    Type       = new CodeTypeReference(masterTypeName),
                    HasGet     = true,
                    HasSet     = false,
                    Attributes = System.CodeDom.MemberAttributes.Public | System.CodeDom.MemberAttributes.New
                                 | System.CodeDom.MemberAttributes.Final,
                };
                masterProp.GetStatements.Add(new System.CodeDom.CodeMethodReturnStatement(
                                                 new System.CodeDom.CodeCastExpression(masterTypeName,
                                                                                       new System.CodeDom.CodePropertyReferenceExpression(
                                                                                           new System.CodeDom.CodeBaseReferenceExpression(), "Master"))));
                typeDecl.Members.Add(masterProp);
            }

            //shortcut building the existing members type map
            if (memberList.Members.Count == 0)
            {
                return(ccu);
            }

            var dom     = refman.TypeCtx.ProjectDom;
            var cls     = dom.GetType(className);
            var members = GetDesignerMembers(memberList.Members.Values, cls, filename, dom, dom);

            //add fields for each control in the page

            foreach (var member in members)
            {
                var type = new CodeTypeReference(member.Type.FullName);
                typeDecl.Members.Add(new CodeMemberField(type, member.Name)
                {
                    Attributes = MemberAttributes.Family
                });
            }
            return(ccu);
        }
Пример #10
0
 static void AddFail(List <CodeBehindWarning> errors, AspNetParsedDocument document, Error err)
 {
     errors.Add(new CodeBehindWarning(GettextCatalog.GetString(
                                          "Parser failed with error {0}. CodeBehind members for this file will not be added.", err.Message),
                                      document.FileName, err.Region.Start.Line, err.Region.Start.Column));
 }
        public override void ModifyTags(MonoDevelop.Projects.SolutionItem policyParent, MonoDevelop.Projects.Project project, string language, string identifier, string fileName, ref Dictionary <string, string> tags)
        {
            base.ModifyTags(policyParent, project, language, identifier, fileName, ref tags);
            if (fileName == null)
            {
                return;
            }

            tags["AspNetMaster"]        = "";
            tags["AspNetMasterContent"] = "";

            AspNetAppProject aspProj = project as AspNetAppProject;

            if (aspProj == null)
            {
                throw new InvalidOperationException("MasterContentFileDescriptionTemplate is only valid for ASP.NET projects");
            }

            ProjectFile masterPage    = null;
            string      masterContent = "";

            var dialog = new MonoDevelop.Ide.Projects.ProjectFileSelectorDialog(aspProj, null, "*.master");

            try {
                dialog.Title = GettextCatalog.GetString("Select a Master Page...");
                int response = MonoDevelop.Ide.MessageService.RunCustomDialog(dialog);
                if (response == (int)Gtk.ResponseType.Ok)
                {
                    masterPage = dialog.SelectedFile;
                }
            }
            finally {
                dialog.Destroy();
            }
            if (masterPage == null)
            {
                return;
            }

            tags["AspNetMaster"] = aspProj.LocalToVirtualPath(masterPage);

            try {
                AspNetParsedDocument pd = ProjectDomService.GetParsedDocument(ProjectDomService.GetProjectDom(project), masterPage.FilePath)
                                          as AspNetParsedDocument;
                if (pd == null)
                {
                    return;
                }

                ContentPlaceHolderVisitor visitor = new ContentPlaceHolderVisitor();
                pd.RootNode.AcceptVisit(visitor);

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (string id in visitor.PlaceHolders)
                {
                    sb.Append("<asp:Content ContentPlaceHolderID=\"");
                    sb.Append(id);
                    sb.Append("\" ID=\"");
                    sb.Append(id);
                    sb.Append("Content\" runat=\"server\">\n</asp:Content>\n");
                }

                tags["AspNetMasterContent"] = sb.ToString();
            }
            catch (Exception ex) {
                //no big loss if we just insert blank space
                //it's just a template for the user to start editing
            }
        }