/// <summary> /// This is called after the single file generator has been invoked to create or update the code file. /// </summary> /// <param name="fileNode">The node associated to the generator</param> /// <param name="data">data to update the file with</param> /// <param name="size">size of the data</param> /// <param name="fileName">Name of the file to update or create</param> /// <returns>full path of the file</returns> protected virtual string UpdateGeneratedCodeFile(FileNode fileNode, byte[] data, int size, string fileName) { string filePath = Path.Combine(Path.GetDirectoryName(fileNode.GetMkDocument()), fileName); IVsRunningDocumentTable rdt = this.projectMgr.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; // (kberes) Shouldn't this be an InvalidOperationException instead with some not to annoying errormessage to the user? if (rdt == null) { ErrorHandler.ThrowOnFailure(VSConstants.E_FAIL); } IVsHierarchy hier; uint cookie; uint itemid; IntPtr docData = IntPtr.Zero; ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)(_VSRDTFLAGS.RDT_NoLock), filePath, out hier, out itemid, out docData, out cookie)); if (docData != IntPtr.Zero) { Marshal.Release(docData); IVsTextStream srpStream; string inputFileContents = this.GetBufferContents(filePath, out srpStream); if (srpStream != null) { int oldLen = 0; int hr = srpStream.GetSize(out oldLen); if (ErrorHandler.Succeeded(hr)) { IntPtr dest = IntPtr.Zero; try { dest = Marshal.AllocCoTaskMem(data.Length); Marshal.Copy(data, 0, dest, data.Length); ErrorHandler.ThrowOnFailure(srpStream.ReplaceStream(0, oldLen, dest, size / 2)); } finally { if (dest != IntPtr.Zero) { Marshal.Release(dest); } } } } } else { using (FileStream generatedFileStream = File.Open(filePath, FileMode.OpenOrCreate)) { generatedFileStream.Write(data, 0, size); } EnvDTE.ProjectItem projectItem = fileNode.GetAutomationObject() as EnvDTE.ProjectItem; if (projectItem != null && (this.projectMgr.FindChild(fileNode.FileName) == null)) { projectItem.ProjectItems.AddFromFile(filePath); } } return filePath; }
/// <summary> /// Invokes the specified generator /// </summary> /// <param name="fileNode">The node on which to invoke the generator.</param> protected internal virtual void InvokeGenerator(FileNode fileNode) { if (fileNode == null) { throw new ArgumentNullException("node"); } SingleFileGeneratorNodeProperties nodeproperties = fileNode.NodeProperties as SingleFileGeneratorNodeProperties; if (nodeproperties == null) { throw new InvalidOperationException(); } string customToolProgID = nodeproperties.CustomTool; if (String.IsNullOrEmpty(customToolProgID)) { return; } string customToolNamespace = nodeproperties.CustomToolNamespace; try { if (!this.runningGenerator) { //Get the buffer contents for the current node string moniker = fileNode.GetMkDocument(); this.runningGenerator = true; //Get the generator IVsSingleFileGenerator generator; int generateDesignTimeSource; int generateSharedDesignTimeSource; int generateTempPE; SingleFileGeneratorFactory factory = new SingleFileGeneratorFactory(this.projectMgr.ProjectGuid, this.projectMgr.Site); ErrorHandler.ThrowOnFailure(factory.CreateGeneratorInstance(customToolProgID, out generateDesignTimeSource, out generateSharedDesignTimeSource, out generateTempPE, out generator)); //Check to see if the generator supports siting IObjectWithSite objWithSite = generator as IObjectWithSite; if (objWithSite != null) { objWithSite.SetSite(fileNode.OleServiceProvider); } //Determine the namespace if (String.IsNullOrEmpty(customToolNamespace)) { customToolNamespace = this.ComputeNamespace(moniker); } //Run the generator IntPtr[] output = new IntPtr[1]; output[0] = IntPtr.Zero; uint outPutSize; string extension; ErrorHandler.ThrowOnFailure(generator.DefaultExtension(out extension)); //Find if any dependent node exists string dependentNodeName = Path.GetFileNameWithoutExtension(fileNode.FileName) + extension; HierarchyNode dependentNode = fileNode.FirstChild; while (dependentNode != null) { if (String.Compare(dependentNode.ItemNode.GetMetadata(ProjectFileConstants.DependentUpon), fileNode.FileName, StringComparison.OrdinalIgnoreCase) == 0) { dependentNodeName = ((FileNode)dependentNode).FileName; break; } dependentNode = dependentNode.NextSibling; } //If you found a dependent node. if (dependentNode != null) { //Then check out the node and dependent node from SCC if (!this.CanEditFile(dependentNode.GetMkDocument())) { throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED); } } else //It is a new node to be added to the project { // Check out the project file if necessary. if (!this.projectMgr.QueryEditProjectFile(false)) { throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED); } } IVsTextStream stream; string inputFileContents = this.GetBufferContents(moniker, out stream); ErrorHandler.ThrowOnFailure(generator.Generate(moniker, inputFileContents, customToolNamespace, output, out outPutSize, this)); byte[] data = new byte[outPutSize]; if (output[0] != IntPtr.Zero) { Marshal.Copy(output[0], data, 0, (int)outPutSize); Marshal.FreeCoTaskMem(output[0]); } //Todo - Create a file and add it to the Project string fileToAdd = this.UpdateGeneratedCodeFile(fileNode, data, (int)outPutSize, dependentNodeName); } } finally { this.runningGenerator = false; } }
/// <summary> /// Update the ChildNodes after the parent node has been renamed /// </summary> /// <param name="newFileNode">The new FileNode created as part of the rename of this node</param> private void SetNewParentOnChildNodes(FileNode newFileNode) { foreach (HierarchyNode childNode in GetChildNodes()) { childNode.Parent = newFileNode; } }
/// <summary> /// Rename all childnodes /// </summary> /// <param name="newFileNode">The newly added Parent node.</param> protected virtual void RenameChildNodes(FileNode parentNode) { foreach (FileNode childNode in GetChildNodes()) { string newfilename; if (childNode.HasParentNodeNameRelation) { string relationalName = childNode.Parent.GetRelationalName(); string extension = childNode.GetRelationNameExtension(); newfilename = relationalName + extension; newfilename = Path.Combine(Path.GetDirectoryName(childNode.Parent.GetMkDocument()), newfilename); } else { newfilename = Path.Combine(Path.GetDirectoryName(childNode.Parent.GetMkDocument()), childNode.Caption); } childNode.RenameDocument(childNode.GetMkDocument(), newfilename); //We must update the DependsUpon property since the rename operation will not do it if the childNode is not renamed //which happens if the is no name relation between the parent and the child string dependentOf = childNode.ItemNode.GetMetadata(ProjectFileConstants.DependentUpon); if (!string.IsNullOrEmpty(dependentOf)) { childNode.ItemNode.SetMetadata(ProjectFileConstants.DependentUpon, childNode.Parent.ItemNode.GetMetadata(ProjectFileConstants.Include)); } } }
public OAVSProjectItem(FileNode fileNode) { this.FileNode = fileNode; }
public OAIronPythonFileItem(OAProject project, FileNode node) : base(project, node) { }
/// <summary> /// Invokes the specified generator /// </summary> /// <param name="fileNode">The node on which to invoke the generator.</param> protected internal virtual void InvokeGenerator(FileNode fileNode) { if (fileNode == null) { throw new ArgumentNullException("node"); } SingleFileGeneratorNodeProperties nodeproperties = fileNode.NodeProperties as SingleFileGeneratorNodeProperties; if (nodeproperties == null) { throw new InvalidOperationException(); } string customToolProgID = nodeproperties.CustomTool; if (string.IsNullOrEmpty(customToolProgID)) { return; } string customToolNamespace = nodeproperties.CustomToolNamespace; try { if (!this.runningGenerator) { //Get the buffer contents for the current node string moniker = fileNode.GetMkDocument(); this.runningGenerator = true; //Get the generator IVsSingleFileGenerator generator; int generateDesignTimeSource; int generateSharedDesignTimeSource; int generateTempPE; SingleFileGeneratorFactory factory = new SingleFileGeneratorFactory(this.projectMgr.ProjectGuid, this.projectMgr.Site); ErrorHandler.ThrowOnFailure(factory.CreateGeneratorInstance(customToolProgID, out generateDesignTimeSource, out generateSharedDesignTimeSource, out generateTempPE, out generator)); //Check to see if the generator supports siting IObjectWithSite objWithSite = generator as IObjectWithSite; if (objWithSite != null) { objWithSite.SetSite(fileNode.OleServiceProvider); } //Determine the namespace if (string.IsNullOrEmpty(customToolNamespace)) { customToolNamespace = this.ComputeNamespace(moniker); } //Run the generator IntPtr[] output = new IntPtr[1]; output[0] = IntPtr.Zero; uint outPutSize; string extension; ErrorHandler.ThrowOnFailure(generator.DefaultExtension(out extension)); //Find if any dependent node exists string dependentNodeName = Path.GetFileNameWithoutExtension(fileNode.FileName) + extension; HierarchyNode dependentNode = fileNode.FirstChild; while (dependentNode != null) { if (string.Compare(dependentNode.ItemNode.GetMetadata(ProjectFileConstants.DependentUpon), fileNode.FileName, StringComparison.OrdinalIgnoreCase) == 0) { dependentNodeName = ((FileNode)dependentNode).FileName; break; } dependentNode = dependentNode.NextSibling; } //If you found a dependent node. if (dependentNode != null) { //Then check out the node and dependent node from SCC if (!this.CanEditFile(dependentNode.GetMkDocument())) { throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED); } } else //It is a new node to be added to the project { // Check out the project file if necessary. if (!this.projectMgr.QueryEditProjectFile(false)) { throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED); } } IVsTextStream stream; string inputFileContents = this.GetBufferContents(moniker, out stream); ErrorHandler.ThrowOnFailure(generator.Generate(moniker, inputFileContents, customToolNamespace, output, out outPutSize, this)); byte[] data = new byte[outPutSize]; if (output[0] != IntPtr.Zero) { Marshal.Copy(output[0], data, 0, (int)outPutSize); Marshal.FreeCoTaskMem(output[0]); } //Todo - Create a file and add it to the Project string fileToAdd = this.UpdateGeneratedCodeFile(fileNode, data, (int)outPutSize, dependentNodeName); } } finally { this.runningGenerator = false; } }
public FileDocumentManager(FileNode node) : base(node) { }
public RubyFileNodeAutomation(RubyProjectAutomation project, FileNode node) : base(project, node) { }
/// <summary> /// Renames the file in the hierarchy by removing old node and adding a new node in the hierarchy. /// </summary> /// <param name="oldFileName">The old file name.</param> /// <param name="newFileName">The new file name</param> /// <param name="newParentId">The new parent id of the item.</param> /// <returns>The newly added FileNode.</returns> /// <remarks>While a new node will be used to represent the item, the underlying MSBuild item will be the same and as a result file properties saved in the project file will not be lost.</remarks> protected virtual FileNode RenameFileNode(string oldFileName, string newFileName, uint newParentId) { if (string.Compare(oldFileName, newFileName, StringComparison.InvariantCulture) == 0) { // We do not want to rename the same file return(null); } this.OnItemDeleted(); this.Parent.RemoveChild(this); // Since this node has been removed all of its state is zombied at this point // Do not call virtual methods after this point since the object is in a deleted state. string[] file = new string[1]; file[0] = newFileName; VSADDRESULT[] result = new VSADDRESULT[1]; Guid emptyGuid = Guid.Empty; ErrorHandler.ThrowOnFailure(this.ProjectMgr.AddItemWithSpecific(newParentId, VSADDITEMOPERATION.VSADDITEMOP_OPENFILE, null, 0, file, IntPtr.Zero, 0, ref emptyGuid, null, ref emptyGuid, result)); FileNode childAdded = this.ProjectMgr.FindChild(newFileName) as FileNode; Debug.Assert(childAdded != null, "Could not find the renamed item in the hierarchy"); // Update the itemid to the newly added. this.ID = childAdded.ID; // Remove the item created by the add item. We need to do this otherwise we will have two items. // Please be aware that we have not removed the ItemNode associated to the removed file node from the hierrachy. // What we want to achieve here is to reuse the existing build item. // We want to link to the newly created node to the existing item node and addd the new include. //temporarily keep properties from new itemnode since we are going to overwrite it string newInclude = childAdded.ItemNode.Item.Include; string dependentOf = childAdded.ItemNode.GetMetadata(ProjectFileConstants.DependentUpon); childAdded.ItemNode.RemoveFromProjectFile(); // Assign existing msbuild item to the new childnode childAdded.ItemNode = this.ItemNode; childAdded.ItemNode.Item.Name = this.ItemNode.ItemName; childAdded.ItemNode.Item.Include = newInclude; if (!string.IsNullOrEmpty(dependentOf)) { childAdded.ItemNode.SetMetadata(ProjectFileConstants.DependentUpon, dependentOf); } childAdded.ItemNode.RefreshProperties(); //Update the new document in the RDT. DocumentManager.RenameDocument(this.ProjectMgr.Site, oldFileName, newFileName, childAdded.ID); //Select the new node in the hierarchy IVsUIHierarchyWindow uiWindow = UIHierarchyUtilities.GetUIHierarchyWindow(this.ProjectMgr.Site, SolutionExplorer); uiWindow.ExpandItem(this.ProjectMgr, this.ID, EXPANDFLAGS.EXPF_SelectItem); //Update FirstChild childAdded.FirstChild = this.FirstChild; //Update ChildNodes SetNewParentOnChildNodes(childAdded); RenameChildNodes(childAdded); return(childAdded); }
private int Open(bool newFile, bool openWith, uint editorFlags, ref Guid editorType, string physicalView, ref Guid logicalView, IntPtr docDataExisting, out IVsWindowFrame windowFrame, WindowFrameShowAction windowFrameAction) { windowFrame = null; if (this.Node == null || this.Node.ProjectMgr == null || this.Node.ProjectMgr.IsClosed) { return(VSConstants.E_FAIL); } Debug.Assert(this.Node != null, "No node has been initialized for the document manager"); Debug.Assert(this.Node.ProjectMgr != null, "No project manager has been initialized for the document manager"); Debug.Assert(this.Node is FileNode, "Node is not FileNode object"); int returnValue = VSConstants.S_OK; string caption = this.GetOwnerCaption(); string fullPath = this.GetFullPathForDocument(); // Make sure that the file is on disk before we open the editor and display message if not found FileNode nodeAsFileNode = (FileNode)this.Node; if (!nodeAsFileNode.IsFileOnDisk(true)) { // Inform clients that we have an invalid item (wrong icon) this.Node.OnInvalidateItems(this.Node.Parent); nodeAsFileNode.FilePreviouslyUnavailable = true; // Bail since we are not able to open the item // Do not return an error code otherwise an internal error message is shown. The scenario for this operation // normally is already a reaction to a dialog box telling that the item has been removed. return(VSConstants.S_FALSE); } else if (nodeAsFileNode.FilePreviouslyUnavailable) { // The file is now available, update the icon nodeAsFileNode.FilePreviouslyUnavailable = false; this.Node.ReDraw(UIHierarchyElement.SccState | UIHierarchyElement.Icon); } IVsUIShellOpenDocument uiShellOpenDocument = this.Node.ProjectMgr.Site.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument; IOleServiceProvider serviceProvider = this.Node.ProjectMgr.Site.GetService(typeof(IOleServiceProvider)) as IOleServiceProvider; try { this.Node.ProjectMgr.OnOpenItem(fullPath); int result = VSConstants.E_FAIL; if (openWith) { result = uiShellOpenDocument.OpenStandardEditor((uint)__VSOSEFLAGS.OSE_UseOpenWithDialog, fullPath, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame); } else { __VSOSEFLAGS openFlags = 0; if (newFile) { openFlags |= __VSOSEFLAGS.OSE_OpenAsNewFile; } //NOTE: we MUST pass the IVsProject in pVsUIHierarchy and the itemid // of the node being opened, otherwise the debugger doesn't work. if (editorType != Guid.Empty) { result = uiShellOpenDocument.OpenSpecificEditor(editorFlags, fullPath, ref editorType, physicalView, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame); } else { openFlags |= __VSOSEFLAGS.OSE_ChooseBestStdEditor; result = uiShellOpenDocument.OpenStandardEditor((uint)openFlags, fullPath, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame); } } if (result != VSConstants.S_OK && result != VSConstants.S_FALSE && result != VSConstants.OLE_E_PROMPTSAVECANCELLED) { ErrorHandler.ThrowOnFailure(result); } if (windowFrame != null) { object var; if (newFile) { ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out var)); IVsPersistDocData persistDocData = (IVsPersistDocData)var; ErrorHandler.ThrowOnFailure(persistDocData.SetUntitledDocPath(fullPath)); } var = null; ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocCookie, out var)); this.Node.DocCookie = (uint)(int)var; if (windowFrameAction == WindowFrameShowAction.Show) { ErrorHandler.ThrowOnFailure(windowFrame.Show()); } else if (windowFrameAction == WindowFrameShowAction.ShowNoActivate) { ErrorHandler.ThrowOnFailure(windowFrame.ShowNoActivate()); } else if (windowFrameAction == WindowFrameShowAction.Hide) { ErrorHandler.ThrowOnFailure(windowFrame.Hide()); } } } catch (COMException e) { Trace.WriteLine("Exception e:" + e.Message); returnValue = e.ErrorCode; this.CloseWindowFrame(ref windowFrame); } return(returnValue); }
/// <summary> /// Initializes a new instance of the <see cref="OAWowFileItem"/> class. /// </summary> /// <param name="project">The project automation object.</param> /// <param name="node">The node.</param> public OAWowFileItem(OAProject project, FileNode node) : base(project, node) { }