예제 #1
0
		///-------------------------------------------------------------------------------------------------------------
		/// <summary>
		///	 Cleans up Generate member state.
		/// </summary>
		///-------------------------------------------------------------------------------------------------------------
		protected virtual void DisposeGenerateState()
		{
			try
			{
				_itemCode = null;
				_itemDesigner = null;
				_ccu = null;
				_ctd = null;
				_create = false;
				_codeFields = null;
				_designerFields = null;
				_className_Full = null;
				_className_Namespace = null;
				_className_Name = null;

				if (_codeDomProvider != null)
				{
					_codeDomProvider.Dispose();
					_codeDomProvider = null;
				}
			}
			catch
			{
			}
		}
예제 #2
0
		///-------------------------------------------------------------------------------------------------------------
		/// <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();
				}
			}
		}
예제 #3
0
		///-------------------------------------------------------------------------------------------------------------
		/// <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;
		}
예제 #4
0
		///-------------------------------------------------------------------------------------------------------------
		/// <summary>
		///	 
		/// </summary>
		///-------------------------------------------------------------------------------------------------------------
		void IVsCodeBehindCodeGenerator.Generate()
		{
			DocData ddDesigner = null;
			DocDataTextWriter designerWriter = null;

			try
			{
				if (_itemCode != null && _codeDomProvider != null)
				{

					// Generate the code
					StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
					_codeDomProvider.GenerateCodeFromCompileUnit(_ccu, stringWriter, _codeGeneratorOptions);
					string generatedCode = stringWriter.ToString();

					// Create designer file if requested
					if (_itemDesigner == null && _create)
					{
						_itemDesigner = GetDesignerItem(_itemCode, true);
					}

					// See if generated code changed
					string designerContents = _itemDesigner.GetDocumentText();
					if (!BufferEquals(designerContents, generatedCode))  // Would be nice to just compare lengths but the buffer gets formatted after insertion
					{
						ddDesigner = new LockedDocData(_serviceProvider, _itemDesigner.FullPath());

						// Try to check out designer file (this throws)
						ddDesigner.CheckoutFile(_serviceProvider);

						// Write out the new code
						designerWriter = new DocDataTextWriter(ddDesigner);
						designerWriter.Write(generatedCode);
						designerWriter.Flush();
						designerWriter.Close();
					}
				}
			}
			finally
			{
				if (designerWriter != null)
				{
					designerWriter.Dispose();
				}
				if (ddDesigner != null)
				{
					ddDesigner.Dispose();
				}
			}
		}
예제 #5
0
		///-------------------------------------------------------------------------------------------------------------
		/// <summary>
		///	 Returns field names in the specified class using code model.
		///	 If publicOnly is true only public fields are returned.
		/// </summary>
		///-------------------------------------------------------------------------------------------------------------
		protected FieldDataDictionary GetFieldNames(VsHierarchyItem itemCode, string className, bool caseSensitive, bool onlyBaseClasses, int maxDepth)
		{
			FieldDataDictionary fields = null;

			if (itemCode != null)
			{
				CodeClass codeClass = FindClass(itemCode, className);
				if (codeClass != null)
				{
					GetFieldNames(codeClass, caseSensitive, onlyBaseClasses, 0, maxDepth, ref fields);
				}
			}

			return fields;
		}
예제 #6
0
		///-------------------------------------------------------------------------------------------------------------
		/// <summary>
		///	 Locates the code model CodeClass 
		/// </summary>
		///-------------------------------------------------------------------------------------------------------------
		protected virtual CodeClass FindClass(VsHierarchyItem item, string className)
		{
			if (item != null)
			{
				try
				{
					ProjectItem projectItem = item.ProjectItem();
					if (projectItem != null)
					{
						FileCodeModel fileCodeModel = projectItem.FileCodeModel;
						if (fileCodeModel != null)
						{
							return FindClass(fileCodeModel.CodeElements, className);
						}
					}
				}
				catch
				{
				}
			}
			return null;
		}
예제 #7
0
        //--------------------------------------------------------------------------------------------
        /// <summary>
        ///     Locates the item in the provided hierarchy using the provided moniker
        ///     and return a VsHierarchyItem for it
        /// </summary>
        //--------------------------------------------------------------------------------------------
        internal static VsHierarchyItem CreateFromMoniker(string moniker, IVsHierarchy hier)
        {
            VsHierarchyItem item = null;

            if (!string.IsNullOrEmpty(moniker) && hier != null)
            {
                IVsProject proj = hier as IVsProject;
                if (proj != null)
                {
                    int  hr;
                    int  isFound = 0;
                    uint itemid  = VSConstants.VSITEMID_NIL;
                    VSDOCUMENTPRIORITY[] priority = new VSDOCUMENTPRIORITY[1];
                    hr = proj.IsDocumentInProject(moniker, out isFound, priority, out itemid);
                    if (ErrorHandler.Succeeded(hr) && isFound != 0 && itemid != VSConstants.VSITEMID_NIL)
                    {
                        item = new VsHierarchyItem(itemid, hier);
                    }
                }
            }

            return(item);
        }
예제 #8
0
		///-------------------------------------------------------------------------------------------------------------
		/// <summary>
		///	 Gets a VshierarchyItem for the designer file if possible.
		///	 Will create new file if specified and not existing.
		/// </summary>
		///-------------------------------------------------------------------------------------------------------------
		protected 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;
		}