private IVsHierarchy TryGetVsHierarchy() { var synchronizer = _solution.TryGetComponent <ProjectModelSynchronizer>(); if (synchronizer == null) { return(null); } VsHierarchyItem hierarchyItem = synchronizer.TryGetHierarchyItemByProjectItem(_projectFile, false); if (hierarchyItem == null) { return(null); } return(hierarchyItem.Hierarchy); }
private IVsBuildMacroInfo TryGetVsBuildMacroInfo() { var synchronizer = _solution.TryGetComponent <ProjectModelSynchronizer>(); if (synchronizer == null) { return(null); } VsHierarchyItem hierarchyItem = synchronizer.TryGetHierarchyItemByProjectItem(_project, false); if (hierarchyItem == null) { return(null); } return(hierarchyItem.Hierarchy as IVsBuildMacroInfo); }
private static bool IsVsHierarchyItemExpanded(VsHierarchyItem hierarchyItem, IVsUIHierarchyWindow uiWindow) { if (!hierarchyItem.IsExpandable()) { return false; } const uint expandedStateMask = (uint)__VSHIERARCHYITEMSTATE.HIS_Expanded; uint itemState; uiWindow.GetItemState(hierarchyItem.UIHierarchy(), hierarchyItem.VsItemID, expandedStateMask, out itemState); return ((__VSHIERARCHYITEMSTATE)itemState == __VSHIERARCHYITEMSTATE.HIS_Expanded); }
private static void CollapseVsHierarchyItem(VsHierarchyItem vsHierarchyItem, IVsUIHierarchyWindow vsHierarchyWindow) { if (vsHierarchyItem == null || vsHierarchyWindow == null) { return; } vsHierarchyWindow.ExpandItem(vsHierarchyItem.UIHierarchy(), vsHierarchyItem.VsItemID, EXPANDFLAGS.EXPF_CollapseFolder); }
/// <summary> /// Adapts the specified hierarchy item to supported target types. /// </summary> /// <param name="item">The hierarchy item to adapt.</param> /// <returns>The entry point that exposes supported target types.</returns> public static IAdaptable <VsHierarchyItem> Adapt(this VsHierarchyItem item) { return(new Adaptable <VsHierarchyItem>(Adapters.ServiceInstance, item)); }
///------------------------------------------------------------------------------------------------------------- /// <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(); } } }
///------------------------------------------------------------------------------------------------------------- /// <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> /// 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; }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// Locates the code model CodeClass /// </summary> ///------------------------------------------------------------------------------------------------------------- public /*protected, but public for FSharp.Project.dll*/ 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; }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// Returns field names in the specified class using code model. /// If publicOnly is true only public fields are returned. /// </summary> ///------------------------------------------------------------------------------------------------------------- public /*protected, but public for FSharp.Project.dll*/ 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; }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// Cleans up Generate member state. /// </summary> ///------------------------------------------------------------------------------------------------------------- public /*protected, but public for FSharp.Project.dll*/ 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 { } }