~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/AspNet/MonoDevelop.AspNet/MonoDevelop.AspNet/CodeBehind.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
 
47
47
namespace MonoDevelop.AspNet
48
48
{
49
 
        
50
 
        
51
49
        public static class CodeBehind
52
50
        {
53
51
                public static string GetCodeBehindClassName (ProjectFile file)
57
55
                                return null;
58
56
                        return proj.GetCodebehindTypeName (file.Name);
59
57
                }
60
 
                
61
 
                static void AddFail (List<CodeBehindWarning> errors, AspNetParsedDocument document, Error err)
62
 
                {
63
 
                        errors.Add (new CodeBehindWarning (GettextCatalog.GetString (
64
 
                                        "Parser failed with error {0}. CodeBehind members for this file will not be added.", err.Message),
65
 
                                        document.FileName, err.Region.BeginLine, err.Region.BeginColumn));
66
 
                }
67
 
                
68
 
                public static System.CodeDom.CodeCompileUnit GenerateCodeBehind (AspNetAppProject project,
69
 
                                                                                 string filename,
70
 
                                                                                 AspNetParsedDocument document, 
71
 
                                                                                 List<CodeBehindWarning> errors)
72
 
                {
 
58
 
 
59
                public static ProjectFile GetDesignerFile (ProjectFile file)
 
60
                {
 
61
                        var project = file.Project as AspNetAppProject;
 
62
 
 
63
                        var type = AspNetAppProject.DetermineWebSubtype (file.FilePath);
 
64
                        if (type != WebSubtype.WebForm && type != WebSubtype.WebControl && type != WebSubtype.MasterPage)
 
65
                                return null;
 
66
 
 
67
                        var dfName = project.LanguageBinding.GetFileName (file.FilePath + ".designer");
 
68
                        return project.Files.GetFile (dfName);
 
69
                }
 
70
 
 
71
                public static BuildResult UpdateDesignerFile (
 
72
                        CodeBehindWriter writer,
 
73
                        AspNetAppProject project,
 
74
                        ProjectFile file, ProjectFile designerFile
 
75
                )
 
76
                {
 
77
                        var result = new BuildResult ();
 
78
 
 
79
                        //parse the ASP.NET file
 
80
                        var parsedDocument = TypeSystemService.ParseFile (project, file.FilePath) as AspNetParsedDocument;
 
81
                        if (parsedDocument == null) {
 
82
                                result.AddError (string.Format ("Failed to parse file '{0}'", file.Name));
 
83
                                return result;
 
84
                        }
 
85
 
 
86
                        //TODO: ensure type system is up to date
 
87
 
 
88
                        CodeCompileUnit ccu;
 
89
                        result.Append (GenerateCodeBehind (project, designerFile.FilePath, parsedDocument, out ccu));
 
90
                        if (ccu != null) {
 
91
                                writer.WriteFile (designerFile.FilePath, ccu);
 
92
                        }
 
93
 
 
94
                        return result;
 
95
                }
 
96
                
 
97
                public static BuildResult GenerateCodeBehind (
 
98
                        AspNetAppProject project,
 
99
                        string filename,
 
100
                        AspNetParsedDocument document,
 
101
                        out CodeCompileUnit ccu)
 
102
                {
 
103
                        ccu = null;
 
104
                        var result = new BuildResult ();
73
105
                        string className = document.Info.InheritedClass;
74
 
                        
75
 
                        if (document.HasErrors) {
76
 
                                AddFail (errors, document, document.Errors.Where (x => x.ErrorType == ErrorType.Error).First ());
77
 
                                return null;
78
 
                        }
 
106
 
 
107
                        foreach (var err in document.Errors)
 
108
                                result.AddError (filename, err.Region.BeginLine, err.Region.BeginColumn, null, err.Message);
 
109
                        if (result.ErrorCount > 0)
 
110
                                return result;
79
111
                        
80
112
                        if (string.IsNullOrEmpty (className))
81
 
                                return null;
 
113
                                return result;
82
114
                        
83
115
                        var refman = new DocumentReferenceManager (project) { Doc = document };
84
 
                        var memberList = new MemberListVisitor (refman);
85
 
                        document.RootNode.AcceptVisit (memberList);
86
 
                        
87
 
                        var err = memberList.Errors.Where (x => x.ErrorType == ErrorType.Error).FirstOrDefault ();
88
 
                        if (err != null) {
89
 
                                AddFail (errors, document, err);
90
 
                                return null;
91
 
                        }
 
116
                        var memberList = new MemberListBuilder (refman, document.XDocument);
 
117
                        memberList.Build ();
 
118
 
 
119
                        foreach (var err in memberList.Errors)
 
120
                                result.AddError (filename, err.Region.BeginLine, err.Region.BeginColumn, null, err.Message);
 
121
                        if (result.ErrorCount > 0)
 
122
                                return result;
92
123
                        
93
124
                        //initialise the generated type
94
 
                        var ccu = new CodeCompileUnit ();
 
125
                        ccu = new CodeCompileUnit ();
95
126
                        var namespac = new CodeNamespace ();
96
127
                        ccu.Namespaces.Add (namespac); 
97
 
                        var typeDecl = new System.CodeDom.CodeTypeDeclaration () {
 
128
                        var typeDecl = new CodeTypeDeclaration {
98
129
                                IsClass = true,
99
130
                                IsPartial = true,
100
131
                        };
118
149
                                        AspNetParsedDocument masterParsedDocument = null;
119
150
                                        if (resolvedMaster != null)
120
151
                                                masterParsedDocument = TypeSystemService.ParseFile (project, resolvedMaster.FilePath) as AspNetParsedDocument;
121
 
                                        if (masterParsedDocument != null && !String.IsNullOrEmpty (masterParsedDocument.Info.InheritedClass)) {
 
152
                                        if (masterParsedDocument != null && !String.IsNullOrEmpty (masterParsedDocument.Info.InheritedClass))
122
153
                                                masterTypeName = masterParsedDocument.Info.InheritedClass;
123
 
                                        } else {
124
 
                                                errors.Add (new CodeBehindWarning (String.Format ("Could not find type for master '{0}'",
125
 
                                                                                                  document.Info.MasterPageTypeVPath),
126
 
                                                                                   document.FileName));
127
 
                                        }
128
154
                                } catch (Exception ex) {
129
 
                                        errors.Add (new CodeBehindWarning (String.Format ("Could not find type for master '{0}'",
130
 
                                                                                          document.Info.MasterPageTypeVPath),
131
 
                                                                           document.FileName));
132
155
                                        LoggingService.LogWarning ("Error resolving master page type", ex);
133
156
                                }
 
157
                                if (string.IsNullOrEmpty (masterTypeName)) {
 
158
                                        var msg = string.Format ("Could not find type for master '{0}'", document.Info.MasterPageTypeVPath);
 
159
                                        result.AddError (filename, msg);
 
160
                                        return result;
 
161
                                }
134
162
                        }
135
163
                        
136
164
                        if (masterTypeName != null) {
137
 
                                var masterProp = new CodeMemberProperty () {
 
165
                                var masterProp = new CodeMemberProperty {
138
166
                                        Name = "Master",
139
167
                                        Type = new CodeTypeReference (masterTypeName),
140
168
                                        HasGet = true,
141
169
                                        HasSet = false,
142
 
                                        Attributes = System.CodeDom.MemberAttributes.Public | System.CodeDom.MemberAttributes.New 
143
 
                                                | System.CodeDom.MemberAttributes.Final,
 
170
                                        Attributes = MemberAttributes.Public | MemberAttributes.New | MemberAttributes.Final,
144
171
                                };
145
 
                                masterProp.GetStatements.Add (new System.CodeDom.CodeMethodReturnStatement (
146
 
                                                new System.CodeDom.CodeCastExpression (masterTypeName, 
147
 
                                                        new System.CodeDom.CodePropertyReferenceExpression (
148
 
                                                                new System.CodeDom.CodeBaseReferenceExpression (), "Master"))));
 
172
                                masterProp.GetStatements.Add (new CodeMethodReturnStatement (
 
173
                                                new CodeCastExpression (masterTypeName, 
 
174
                                                        new CodePropertyReferenceExpression (
 
175
                                                                new CodeBaseReferenceExpression (), "Master"))));
149
176
                                typeDecl.Members.Add (masterProp);
150
177
                        }
151
178
                        
152
179
                        //shortcut building the existing members type map
153
180
                        if (memberList.Members.Count == 0)
154
 
                                return ccu;
 
181
                                return result;
155
182
                        
156
183
                        var dom = refman.TypeCtx.Compilation;
157
 
                        var cls = dom.LookupType (className);
 
184
                        var cls = ReflectionHelper.ParseReflectionName (className).Resolve (dom);
158
185
                        var members = GetDesignerMembers (memberList.Members.Values, cls, filename);
159
186
                        
160
187
                        //add fields for each control in the page
163
190
                                var type = new CodeTypeReference (member.Type.FullName);
164
191
                                typeDecl.Members.Add (new CodeMemberField (type, member.Name) { Attributes = MemberAttributes.Family });
165
192
                        }
166
 
                        return ccu;
 
193
                        return result;
167
194
                }
168
195
                
169
196
                /// <summary>Filters out members whose names conflict with existing accessible members</summary>