///------------------------------------------------------------------------------------------------------------- /// <summary> /// /// </summary> ///------------------------------------------------------------------------------------------------------------- void IVsCodeBehindCodeGenerator.BeginGenerate(string document, string codeBehindFile, string className_Full, bool create) { DisposeGenerateState(); _itemCode = VsHierarchyItem.CreateFromMoniker(codeBehindFile, _hierarchy); _itemDesigner = GetDesignerItem(_itemCode, false); _create = create; _className_Full = className_Full; if (_itemCode != null) { // Get the CodeDomProvider for the language (MergedCodeDomProvider C#/VB) _codeDomProvider = CreateCodeDomProvider(_itemCode.VsItemID); if (_codeDomProvider != null) { // Get the field names so we can preserve location and access bool caseSensitive = IsCaseSensitive(_codeDomProvider); _codeFields = GetFieldNames(_itemCode, _className_Full, caseSensitive, false, 30); _designerFields = GetFieldNames(_itemDesigner, _className_Full, caseSensitive, false, 0); // Generate the class string designerContents = _itemDesigner.GetDocumentText(); TextReader reader = new StringReader(designerContents); _ccu = _codeDomProvider.Parse(reader); GenerateClass(); } } }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// /// </summary> ///------------------------------------------------------------------------------------------------------------- bool IVsCodeBehindCodeGenerator.IsGenerateAllowed(string document, string codeBehindFile, bool create) { VsHierarchyItem itemCode = VsHierarchyItem.CreateFromMoniker(codeBehindFile, _hierarchy); VsHierarchyItem itemDesigner = GetDesignerItem(itemCode, false); if ((itemDesigner != null && itemDesigner.IsBuildActionCompile()) || (itemDesigner == null && create)) { return(true); } return(false); }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// Gets a VshierarchyItem for the designer file if possible. /// Will create new file if specified and not existing. /// </summary> ///------------------------------------------------------------------------------------------------------------- public /*protected, but public for FSharp.Project.dll*/ virtual VsHierarchyItem GetDesignerItem(VsHierarchyItem itemCode, bool create) { VsHierarchyItem itemDesigner = null; if (itemCode != null) { // Calculate codebehind and designer file paths string codeBehindFile = itemCode.FullPath(); string designerFile = null; if (_isPartialClassDisabled) { designerFile = codeBehindFile; } else if (!string.IsNullOrEmpty(codeBehindFile)) { designerFile = codeBehindFile.Insert(codeBehindFile.LastIndexOf("."), ".designer"); } // Try to locate existing designer file if (!string.IsNullOrEmpty(designerFile)) { itemDesigner = VsHierarchyItem.CreateFromMoniker(designerFile, _hierarchy); if (itemDesigner != null) { return(itemDesigner); } } // Create empty designer file if requested if (create && !string.IsNullOrEmpty(designerFile)) { ProjectItem projectItemCode = itemCode.ProjectItem(); if (projectItemCode != null) { ProjectItems projectItems = projectItemCode.Collection; if (projectItems != null) { try { using (StreamWriter sw = File.CreateText(designerFile)) { sw.WriteLine(" "); } projectItems.AddFromFileCopy(designerFile); } catch { } itemDesigner = VsHierarchyItem.CreateFromMoniker(designerFile, _hierarchy); if (itemDesigner != null) { return(itemDesigner); } } } } } return(itemDesigner); }