public void CloseInEditorWithoutSaving(string fullFileName) { // Get the RDT service IVsRunningDocumentTable runningDocumentTableService = (IVsRunningDocumentTable)VsIdeTestHostContext.ServiceProvider.GetService(typeof(IVsRunningDocumentTable)); Assert.IsNotNull(runningDocumentTableService, "Failed to get the Running Document Table Service"); // Get our document cookie and hierarchy for the file uint docCookie; IntPtr docData; IVsHierarchy hierarchy; uint itemId; runningDocumentTableService.FindAndLockDocument( (uint)Microsoft.VisualStudio.Shell.Interop._VSRDTFLAGS.RDT_NoLock, fullFileName, out hierarchy, out itemId, out docData, out docCookie); // Get the SolutionService IVsSolution solutionService = VsIdeTestHostContext.ServiceProvider.GetService(typeof(IVsSolution)) as IVsSolution; Assert.IsNotNull(solutionService, "Failed to get IVsSolution service"); // Close the document solutionService.CloseSolutionElement( (uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_NoSave, hierarchy, docCookie); }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// If the document is open, it returns the IntPtr to the doc data. /// (This is ref-counted and must be released with Marshal.Release()) /// </summary> ///------------------------------------------------------------------------------------------------------------- public IntPtr GetRunningDocData() { IntPtr docData = IntPtr.Zero; if (IsFile()) { string fullPath = FullPath(); if (!string.IsNullOrEmpty(fullPath)) { IVsRunningDocumentTable rdt = GetService <IVsRunningDocumentTable>(); if (rdt != null) { _VSRDTFLAGS flags = _VSRDTFLAGS.RDT_NoLock; uint itemid; IVsHierarchy hierarchy; uint docCookie; rdt.FindAndLockDocument ( (uint)flags, fullPath, out hierarchy, out itemid, out docData, out docCookie ); } } } return(docData); }
private bool GoToSource(string filePath, int lineNumber, int offset) { IVsRunningDocumentTable runningDoc = (IVsRunningDocumentTable)GetService(typeof(SVsRunningDocumentTable)); IntPtr ptr; IVsHierarchy hierarchy; uint cookie; uint id; if (runningDoc.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, filePath, out hierarchy, out id, out ptr, out cookie) != VSConstants.S_OK) { return(false); } try { var lines = Marshal.GetObjectForIUnknown(ptr) as IVsTextLines; if (lines == null) { return(false); } var textManager = (IVsTextManager)GetService(typeof(SVsTextManager)); if (textManager == null) { return(false); } return(textManager.NavigateToLineAndColumn( lines, VSConstants.LOGVIEWID.TextView_guid, lineNumber, offset, lineNumber, offset) == VSConstants.S_OK); } finally { if (ptr != IntPtr.Zero) { Marshal.Release(ptr); } } }
/// <summary> /// Open a file in a document window with a std editor /// </summary> /// <param name="newFile">Open the file as a new file</param> /// <param name="openWith">Use a dialog box to determine which editor to use</param> /// <param name="logicalView">In MultiView case determines view to be activated by IVsMultiViewDocumentView. For a list of logical view GUIDS, see constants starting with LOGVIEWID_ defined in NativeMethods class</param> /// <param name="frame">A reference to the window frame that is mapped to the file</param> /// <param name="windowFrameAction">Determine the UI action on the document window</param> /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns an error code.</returns> public int Open(bool newFile, bool openWith, Guid logicalView, out IVsWindowFrame frame, WindowFrameShowAction windowFrameAction) { frame = null; IVsRunningDocumentTable rdt = this.Node.ProjectMgr.Site.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; Debug.Assert(rdt != null, " Could not get running document table from the services exposed by this project"); if (rdt == null) { return(VSConstants.E_FAIL); } // First we see if someone else has opened the requested view of the file. _VSRDTFLAGS flags = _VSRDTFLAGS.RDT_NoLock; uint itemid; IntPtr docData = IntPtr.Zero; IVsHierarchy ivsHierarchy; uint docCookie; string path = this.GetFullPathForDocument(); int returnValue = VSConstants.S_OK; try { ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)flags, path, out ivsHierarchy, out itemid, out docData, out docCookie)); ErrorHandler.ThrowOnFailure(this.Open(newFile, openWith, ref logicalView, docData, out frame, windowFrameAction)); } catch (COMException e) { Trace.WriteLine("Exception :" + e.Message); returnValue = e.ErrorCode; } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); } } return(returnValue); }
/// <summary> /// Get's called to rename the eventually running document this hierarchyitem points to /// </summary> /// returns FALSE if the doc can not be renamed bool RenameDocument(string strOldName, string strNewName) { IVsRunningDocumentTable pRDT = this.GetService(typeof(IVsRunningDocumentTable)) as IVsRunningDocumentTable; if (pRDT == null) return false; IntPtr docData = IntPtr.Zero; IVsHierarchy pIVsHierarchy; uint itemId; uint uiVsDocCookie; bool fReturn = false; SuspendFileChanges sfc = new SuspendFileChanges(this.ProjectMgr.Site, strOldName); sfc.Suspend(); try { pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, strOldName, out pIVsHierarchy, out itemId, out docData, out uiVsDocCookie); if (pIVsHierarchy != null && pIVsHierarchy != (IVsHierarchy)this.projectMgr) { // Don't rename it if it wasn't opened by us. return false; } // ask other potentially running packages if (this.projectMgr.Tracker.CanRenameFile(strOldName, strNewName) != true) { return false; } // Allow the user to "fix" the project by renaming the item in the hierarchy // to the real name of the file on disk. if (File.Exists(strOldName) || !File.Exists(strNewName)) { File.Move(strOldName, strNewName); } // point the docData at the new path and update any open window frames. VsShell.RenameDocument(this.projectMgr.Site, strOldName, strNewName); fReturn = true; bool caseOnlyChange = (String.Compare(strOldName, strNewName, true) == 0); if (!caseOnlyChange) { // Remove the item and re-insert it at the right location (sorted). this.OnItemDeleted(); this.parentNode.RemoveChild(this); this.parentNode.AddChild(this); try { this.OnInvalidateItems(this.parentNode); } catch (System.Runtime.InteropServices.COMException) { // this call triggers OleDispatchErrors.DISP_E_MEMBERNOTFOUND in // GetProperty which is returned to us here via Interop as // a COMException, so we ignore it. } } else { this.OnInvalidate(); } this.projectMgr.Tracker.AfterRenameFile(strOldName, strNewName); } finally { sfc.Resume(); } if (docData != IntPtr.Zero) { Marshal.Release(docData); } return fReturn; }
/// <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> /// /// </summary> /// <param name="fullFileName"></param> public static void CloseInEditorWithoutSaving(IServiceProvider serviceProvider, string fullFileName) { #region Input validation Debug.Assert(serviceProvider != null, "ServiceProvider cannot be null"); Debug.Assert(fullFileName != null && fullFileName.Length > 0, "fullFileName cannot be empty"); #endregion // Get the RDT service IVsRunningDocumentTable runningDocumentTableService = (IVsRunningDocumentTable)serviceProvider.GetService(typeof(IVsRunningDocumentTable)); Debug.Assert(runningDocumentTableService != null, "Failed to get the Running Document Table Service"); // Get our document cookie and hierarchy for the file uint docCookie; IntPtr docData; IVsHierarchy hierarchy; uint itemId; runningDocumentTableService.FindAndLockDocument( (uint)Microsoft.VisualStudio.Shell.Interop._VSRDTFLAGS.RDT_NoLock, fullFileName, out hierarchy, out itemId, out docData, out docCookie); // Get the SolutionService IVsSolution solutionService = serviceProvider.GetService(typeof(IVsSolution)) as IVsSolution; Debug.Assert(solutionService != null, "Failed to get IVsSolution service"); // Close the document solutionService.CloseSolutionElement( (uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_NoSave, hierarchy, docCookie); }
private string GetDisplayName(string fileName) { if (owner != null) { IVsRunningDocumentTable pRDT = (IVsRunningDocumentTable)owner.GetService(typeof(SVsRunningDocumentTable)); if (pRDT != null) { IntPtr punkDocData; uint docCookie; uint[] pitemid = new uint[1]; IVsHierarchy ppIVsHierarchy; int hr = pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, fileName, out ppIVsHierarchy, out pitemid[0], out punkDocData, out docCookie); if (NativeMethods.Succeeded(hr) && punkDocData != IntPtr.Zero && ppIVsHierarchy != null) { Marshal.Release(punkDocData); object isNew; hr = ppIVsHierarchy.GetProperty(pitemid[0], (int)__VSHPROPID.VSHPROPID_IsNewUnsavedItem, out isNew); if (NativeMethods.Succeeded(hr) && (bool)isNew) { object fileCaption; hr = ppIVsHierarchy.GetProperty(pitemid[0], (int)__VSHPROPID.VSHPROPID_Caption, out fileCaption); string caption = fileCaption as string; if (NativeMethods.Succeeded(hr) && !String.IsNullOrEmpty(caption)) { return(caption); } } } } } return(fileName); }
public static IVsProject PathToProject(string filePath, IVsRunningDocumentTable rdt) { IVsHierarchy hierarchy; uint itemId; IntPtr docData = IntPtr.Zero; uint cookie; try { var hr = rdt.FindAndLockDocument( (uint)_VSRDTFLAGS.RDT_NoLock, filePath, out hierarchy, out itemId, out docData, out cookie); ErrorHandler.ThrowOnFailure(hr); } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); docData = IntPtr.Zero; } } return(hierarchy as IVsProject); }
private async Task <IVsWindowFrame> CreateWindowFrameAsync() { var documentName = project.FullPath; var docData = IntPtr.Zero; var hr = 0; uint itemId; IVsHierarchy hierarchy; try { uint cookie; hr = docTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, documentName, out hierarchy, out itemId, out docData, out cookie); if (hr != VSConstants.S_OK) { //No registered document window. hierarchy = ProjectHierarchy; itemId = (uint)VSConstants.VSITEMID.Root; } } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); docData = IntPtr.Zero; } } return(await CreateDocWindowAsync(project, documentName, hierarchy, itemId)); }
private EnvDTE.ProjectItem LoadFile() { IVsRunningDocumentTable rdt = (IVsRunningDocumentTable)this.DesignerLoader.Host.GetService(typeof(SVsRunningDocumentTable)); IVsHierarchy projectHierarchy; uint xmlItemid; IntPtr xmlDocData; uint dwCookie; int ret = rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, this.DesignerLoader.DocumentMoniker, out projectHierarchy, out xmlItemid, out xmlDocData, out dwCookie); object firstChildValue; ret = projectHierarchy.GetProperty(xmlItemid, (int)__VSHPROPID.VSHPROPID_FirstChild, out firstChildValue); if (firstChildValue == null) { return(null); } int fileHandle = (int)firstChildValue; if (fileHandle < 0) { return(null); } object firstChildObject; ret = projectHierarchy.GetProperty((uint)fileHandle, (int)__VSHPROPID.VSHPROPID_ExtObject, out firstChildObject); EnvDTE.ProjectItem firstChildItem = (EnvDTE.ProjectItem)firstChildObject; return (firstChildItem); }
private bool IsInPythonProject(IWpfTextView textView) { try { if (textView.TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument textDocument) && !string.IsNullOrEmpty(textDocument?.FilePath)) { ErrorHandler.ThrowOnFailure(_rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, textDocument.FilePath, out IVsHierarchy hier, out uint itemId, out IntPtr docData, out _)); try { ErrorHandler.ThrowOnFailure(hier.GetProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID5.VSHPROPID_ProjectCapabilities, out object propVal)); var capabilities = propVal as string; if (capabilities != null && capabilities.Contains("Python")) { return(true); } } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); } } } return(false); } catch (Exception ex) when(!ex.IsCriticalException()) { return(false); } }
internal static DjangoProject GetProject(IServiceProvider serviceProvider, string filename) { IVsHierarchy hierarchy; IVsRunningDocumentTable rdt = serviceProvider.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; uint itemid; IntPtr docData = IntPtr.Zero; uint cookie; try { int hr = rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, filename, out hierarchy, out itemid, out docData, out cookie); if (ErrorHandler.Succeeded(hr)) { rdt.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, cookie); } var res = hierarchy as IDjangoProject; if (res != null) { return(res.GetDjangoProject().Project); } return(null); } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); } } }
private static void MarkDocumentAsTemporary(string moniker) { IVsRunningDocumentTable docTable = (IVsRunningDocumentTable) ServiceProvider.GlobalProvider.GetService(typeof(SVsRunningDocumentTable)); uint lockType = (uint)_VSRDTFLAGS.RDT_DontAddToMRU | (uint)_VSRDTFLAGS.RDT_NonCreatable | (uint)_VSRDTFLAGS.RDT_VirtualDocument | (uint)_VSRDTFLAGS.RDT_PlaceHolderDoc; IVsHierarchy hierarchy; uint itemid; uint documentCookie; IntPtr docData; int hr = docTable.FindAndLockDocument( dwRDTLockType: lockType, pszMkDocument: moniker, ppHier: out hierarchy, pitemid: out itemid, ppunkDocData: out docData, pdwCookie: out documentCookie ); CheckError(hr, "FindAndLockDocument"); docTable.ModifyDocumentFlags(documentCookie, lockType, 1); }
private void NotifyDocChanged() { IVsHierarchy hierarchy; uint itemID; IntPtr docData; uint docCookie = 0; if (_FileName.Length == 0) { return; } IVsRunningDocumentTable runningDocTable = (IVsRunningDocumentTable)GetService( typeof(SVsRunningDocumentTable)); try { ErrorHandler.ThrowOnFailure(runningDocTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, _FileName, out hierarchy, out itemID, out docData, out docCookie)); ErrorHandler.ThrowOnFailure(runningDocTable.NotifyDocumentChanged(docCookie, (uint)__VSRDTATTRIB.RDTA_DocDataReloaded)); } finally { if (runningDocTable != null) { ErrorHandler.ThrowOnFailure(runningDocTable.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, docCookie)); } } }
protected virtual void LockRDTEntry() { // Define flags for the nested project document _VSRDTFLAGS flags = _VSRDTFLAGS.RDT_VirtualDocument | _VSRDTFLAGS.RDT_ProjSlnDocument;; // Request the RDT service IVsRunningDocumentTable rdt = this.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; Debug.Assert(rdt != null, " Could not get running document table from the services exposed by this project"); if (rdt == null) { throw new InvalidOperationException(); } // First we see if someone else has opened the requested view of the file. uint itemid; IntPtr docData = IntPtr.Zero; IVsHierarchy ivsHierarchy; uint docCookie; IntPtr projectPtr = IntPtr.Zero; try { ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)flags, this.projectPath, out ivsHierarchy, out itemid, out docData, out docCookie)); flags |= _VSRDTFLAGS.RDT_EditLock; if (ivsHierarchy != null && docCookie != (uint)ShellConstants.VSDOCCOOKIE_NIL) { if (docCookie != this.DocCookie) { this.DocCookie = docCookie; } } else { // get inptr for hierarchy projectPtr = Marshal.GetIUnknownForObject(this.nestedHierarchy); Debug.Assert(projectPtr != IntPtr.Zero, " Project pointer for the nested hierarchy has not been initialized"); ErrorHandler.ThrowOnFailure(rdt.RegisterAndLockDocument((uint)flags, this.projectPath, this.ProjectMgr, this.ID, projectPtr, out docCookie)); this.DocCookie = docCookie; Debug.Assert(this.DocCookie != (uint)ShellConstants.VSDOCCOOKIE_NIL, "Invalid cookie when registering document in the running document table."); //we must also set the doc cookie on the nested hier this.SetDocCookieOnNestedHier(this.DocCookie); } } finally { // Release all Inptr's that that were given as out pointers if (docData != IntPtr.Zero) { Marshal.Release(docData); } if (projectPtr != IntPtr.Zero) { Marshal.Release(projectPtr); } } }
/// <summary> /// Opens the file item in the specified view. /// </summary> /// <param name="ViewKind">Specifies the view kind in which to open the item (file)</param> /// <returns>Window object</returns> public override EnvDTE.Window Open(string viewKind) { return(UIThread.DoOnUIThread(delegate() { if (this.Node == null || this.Node.ProjectMgr == null || this.Node.ProjectMgr.IsClosed || this.Node.ProjectMgr.Site == null) { throw new InvalidOperationException(); } IVsWindowFrame windowFrame = null; IntPtr docData = IntPtr.Zero; using (AutomationScope scope = new AutomationScope(this.Node.ProjectMgr.Site)) { try { // Validate input params Guid logicalViewGuid = VSConstants.LOGVIEWID_Primary; try { if (!(String.IsNullOrEmpty(viewKind))) { logicalViewGuid = new Guid(viewKind); } } catch (FormatException) { // Not a valid guid throw new ArgumentException(SR.GetString(SR.ParameterMustBeAValidGuid, CultureInfo.CurrentUICulture), "viewKind"); } uint itemid; IVsHierarchy ivsHierarchy; uint docCookie; IVsRunningDocumentTable rdt = this.Node.ProjectMgr.Site.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; Debug.Assert(rdt != null, " Could not get running document table from the services exposed by this project"); if (rdt == null) { throw new InvalidOperationException(); } ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, this.Node.Url, out ivsHierarchy, out itemid, out docData, out docCookie)); // Open the file using the IVsProject3 interface ErrorHandler.ThrowOnFailure(this.Node.ProjectMgr.OpenItem(this.Node.ID, ref logicalViewGuid, docData, out windowFrame)); } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); } } } // Get the automation object and return it return ((windowFrame != null) ? VsShellUtilities.GetWindowObject(windowFrame) : null); })); }
/// <summary> /// Rename document in the running document table from oldName to newName. /// </summary> /// <param name="provider">The service provider.</param> /// <param name="oldName">Full path to the old name of the document.</param> /// <param name="newName">Full path to the new name of the document.</param> /// <param name="newItemId">The new item id of the document</param> public static void RenameDocument(IServiceProvider site, string oldName, string newName, uint newItemId) { Utilities.ArgumentNotNull("site", site); if (String.IsNullOrEmpty(oldName)) { throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty), "oldName"); } if (String.IsNullOrEmpty(newName)) { throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty), "newName"); } if (newItemId == VSConstants.VSITEMID_NIL) { throw new ArgumentNullException("newItemId"); } IVsRunningDocumentTable pRDT = site.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; IVsUIShellOpenDocument doc = site.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument; if (pRDT == null || doc == null) { return; } IVsHierarchy pIVsHierarchy; uint itemId; IntPtr docData; uint uiVsDocCookie; ErrorHandler.ThrowOnFailure(pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, oldName, out pIVsHierarchy, out itemId, out docData, out uiVsDocCookie)); if (docData != IntPtr.Zero && pIVsHierarchy != null) { try { IntPtr pUnk = Marshal.GetIUnknownForObject(pIVsHierarchy); Guid iid = typeof(IVsHierarchy).GUID; IntPtr pHier; Marshal.QueryInterface(pUnk, ref iid, out pHier); try { ErrorHandler.ThrowOnFailure(pRDT.RenameDocument(oldName, newName, pHier, newItemId)); } finally { if (pHier != IntPtr.Zero) { Marshal.Release(pHier); } if (pUnk != IntPtr.Zero) { Marshal.Release(pUnk); } } } finally { Marshal.Release(docData); } } }
/// <summary> /// Gets an instance of the RunningDocumentTable (RDT) service which manages the set of currently open /// documents in the environment and then notifies the client that an open document has changed /// </summary> private void NotifyDocChanged() { // Make sure that we have a file name if (_fileName.Length == 0) { return; } // Get a reference to the Running Document Table IVsRunningDocumentTable runningDocTable = (IVsRunningDocumentTable)GetService(typeof(SVsRunningDocumentTable)); // Lock the document IVsHierarchy hierarchy; int hr = runningDocTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, _fileName, out hierarchy, out uint itemID, out IntPtr docData, out uint docCookie); ErrorHandler.ThrowOnFailure(hr); // Send the notification hr = runningDocTable.NotifyDocumentChanged(docCookie, (uint)__VSRDTATTRIB.RDTA_DocDataReloaded); // Unlock the document. // Note that we have to unlock the document even if the previous call failed. ErrorHandler.ThrowOnFailure(runningDocTable.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, docCookie)); // Check ff the call to NotifyDocChanged failed. ErrorHandler.ThrowOnFailure(hr); /* * IVsFileChangeEx fileChange; * fileChange = GetService(typeof(SVsFileChangeEx)) as IVsFileChangeEx; * * if (fileChange != null) * { * * ErrorHandler.ThrowOnFailure(fileChange.IgnoreFile(0, this.FileName, 1)); * if (docData != IntPtr.Zero) * { * IVsPersistDocData persistDocData = null; * * // if interface is not supported, return null * object unknown = Marshal.GetObjectForIUnknown(docData); * if (unknown is IVsPersistDocData) * { * persistDocData = (IVsPersistDocData)unknown; * if (persistDocData is IVsDocDataFileChangeControl) * { * _ManifestDesignerControl = (IVsDocDataFileChangeControl)persistDocData; * if (_ManifestDesignerControl != null) * { * ErrorHandler.ThrowOnFailure(_ManifestDesignerControl.IgnoreFileChanges(1)); * } * } * } * } * } */ }
/// <summary> /// Opens the file item in the specified view. /// </summary> /// <param name="ViewKind">Specifies the view kind in which to open the item (file)</param> /// <returns>Window object</returns> public override EnvDTE.Window Open(string viewKind) { CheckProjectIsValid(); IVsWindowFrame windowFrame = null; IntPtr docData = IntPtr.Zero; using (AutomationScope scope = new AutomationScope(this.Node.ProjectMgr.Site)) { Node.ProjectMgr.Site.GetUIThread().Invoke(() => { try { // Validate input params Guid logicalViewGuid = VSConstants.LOGVIEWID_Primary; try { if (!(String.IsNullOrEmpty(viewKind))) { logicalViewGuid = new Guid(viewKind); } } catch (FormatException) { // Not a valid guid throw new ArgumentException(SR.GetString(SR.ParameterMustBeAValidGuid), "viewKind"); } uint itemid; IVsHierarchy ivsHierarchy; uint docCookie; IVsRunningDocumentTable rdt = this.Node.ProjectMgr.Site.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; if (rdt == null) { throw new InvalidOperationException("Could not get running document table from the services exposed by this project"); } ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, this.Node.Url, out ivsHierarchy, out itemid, out docData, out docCookie)); // Open the file using the IVsProject interface // We get the outer hierarchy so that projects can customize opening. var project = Node.ProjectMgr.GetOuterInterface <IVsProject>(); ErrorHandler.ThrowOnFailure(project.OpenItem(Node.ID, ref logicalViewGuid, docData, out windowFrame)); } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); } } }); } // Get the automation object and return it return((windowFrame != null) ? VsShellUtilities.GetWindowObject(windowFrame) : null); }
public static void RenameDocument(ServiceProvider site, string oldName, string newName) { IVsRunningDocumentTable pRDT = (IVsRunningDocumentTable)site.QueryService(VsConstants.SID_SVsRunningDocumentTable, typeof(IVsRunningDocumentTable)); IVsUIShellOpenDocument doc = (IVsUIShellOpenDocument)site.QueryService(VsConstants.SID_VsUIShellOpenDocument, typeof(IVsUIShellOpenDocument)); IVsUIShell uiShell = (IVsUIShell)site.QueryService(VsConstants.guidShellIID, typeof(IVsUIShell)); if (pRDT == null || doc == null) { return; } IVsHierarchy pIVsHierarchy; uint itemId; IntPtr docData; uint uiVsDocCookie; pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, oldName, out pIVsHierarchy, out itemId, out docData, out uiVsDocCookie); if (docData != IntPtr.Zero) { IntPtr pUnk = Marshal.GetIUnknownForObject(pIVsHierarchy); Guid iid = typeof(IVsHierarchy).GUID; IntPtr pHier; Marshal.QueryInterface(pUnk, ref iid, out pHier); try { pRDT.RenameDocument(oldName, newName, pHier, itemId); } finally { Marshal.Release(pHier); Marshal.Release(pUnk); } string newCaption = Path.GetFileName(newName); // now we need to tell the windows to update their captions. IEnumWindowFrames ppenum; uiShell.GetDocumentWindowEnum(out ppenum); IVsWindowFrame[] rgelt = new IVsWindowFrame[1]; uint fetched; while (ppenum.Next(1, rgelt, out fetched) == 0 && fetched == 1) { IVsWindowFrame windowFrame = rgelt[0]; object data; windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out data); IntPtr ptr = Marshal.GetIUnknownForObject(data); if (ptr == docData) { windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_OwnerCaption, newCaption); } Marshal.Release(ptr); } Marshal.Release(docData); } }
public void Suspend() { if (this.fSuspending) { return; } try { IVsRunningDocumentTable pRDT = (IVsRunningDocumentTable)this.site.QueryService(VsConstants.SID_SVsRunningDocumentTable, typeof(IVsRunningDocumentTable)); IntPtr docData; IVsHierarchy pIVsHierarchy; uint itemId; uint uiVsDocCookie; IVsFileChangeEx vsFileChange; if (pRDT == null) { return; } pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, this.strDocumentFileName, out pIVsHierarchy, out itemId, out docData, out uiVsDocCookie); if ((uiVsDocCookie == VsConstants.VSDOCCOOKIE_NIL) || docData == IntPtr.Zero) { return; } vsFileChange = (IVsFileChangeEx)this.site.QueryService(VsConstants.SID_SVsFileChangeEx, typeof(IVsFileChangeEx)); if (vsFileChange != null) { this.fSuspending = true; vsFileChange.IgnoreFile(0, this.strDocumentFileName, (int)1); if (docData != IntPtr.Zero) { try { // if interface is not supported, return null IVsPersistDocData ppIVsPersistDocData = (IVsPersistDocData)Marshal.GetObjectForIUnknown(docData); this.fileChangeControl = (IVsDocDataFileChangeControl)ppIVsPersistDocData; if (this.fileChangeControl != null) { this.fileChangeControl.IgnoreFileChanges(1); } } catch { }; } } } catch { } return; }
/// <summary> /// Find the document and return its cookie to the lock to the document. /// </summary> /// <param name="runningDocTable">The object having a table of all running documents.</param> /// <param name="file">The file to be looked in the documents table.</param> /// <returns>The cookie to the document lock.</returns> internal static uint FindDocument(IVsRunningDocumentTable runningDocTable, string file) { ThreadHelper.ThrowIfNotOnUIThread(); // Unused variables IVsHierarchy hierarchy; uint itemId; IntPtr docData = IntPtr.Zero; uint cookieDocLock; int hr = runningDocTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, file, out hierarchy, out itemId, out docData, out cookieDocLock); // Although we don't use it, we still need to release the it if (docData != IntPtr.Zero) { Marshal.Release(docData); docData = IntPtr.Zero; } if (ErrorHandler.Failed(hr)) { throw Marshal.GetExceptionForHR(hr); } if (cookieDocLock > 0) { // Document is already open uint rdtFlags; // Unused variables uint readLocks; uint editLocks; string documentName; hr = runningDocTable.GetDocumentInfo(cookieDocLock, out rdtFlags, out readLocks, out editLocks, out documentName, out hierarchy, out itemId, out docData); // Although we don't use it, we still need to release the it if (docData != IntPtr.Zero) { Marshal.Release(docData); docData = IntPtr.Zero; } if (ErrorHandler.Failed(hr)) { throw Marshal.GetExceptionForHR(hr); } if ((rdtFlags & ((uint)_VSRDTFLAGS.RDT_ProjSlnDocument)) > 0) { throw Marshal.GetExceptionForHR(VSConstants.E_FAIL); } } return(cookieDocLock); }
/// <summary> /// Turns on/off file ignore option. When file changes are ignored, VS doesn't display a dialog asking user about reloading the changes. /// </summary> public static void SetIgnoreFileChanges(string path, bool ignore) { if (string.IsNullOrEmpty(path)) { return; } IVsHierarchy ppHier; uint pitemid; IntPtr pPunkDocData; uint pdwCookie; int hr = IVsRunningDocumentTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, path, out ppHier, out pitemid, out pPunkDocData, out pdwCookie); Marshal.ThrowExceptionForHR(hr); if (pPunkDocData != IntPtr.Zero) { IVsFileChangeEx fileChange = (IVsFileChangeEx)Package.GetGlobalService(typeof(SVsFileChangeEx)); if (fileChange == null) { throw new InvalidOperationException("Cannot consume IVsFileChangeEx."); } IVsDocDataFileChangeControl changeControl = (IVsDocDataFileChangeControl)Marshal.GetObjectForIUnknown(pPunkDocData); if (ignore) { hr = fileChange.IgnoreFile(0, path, 1); Marshal.ThrowExceptionForHR(hr); hr = changeControl.IgnoreFileChanges(1); } else { hr = fileChange.IgnoreFile(0, path, 0); Marshal.ThrowExceptionForHR(hr); hr = changeControl.IgnoreFileChanges(0); } } }
public static RunningDocumentInfo FindAndLockDocument(string moniker, RdtLocks locks) { uint itemId; IVsHierarchy hierarchy; uint docCookie; IntPtr docData; NativeMethods.ThrowOnFailure(_Rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, moniker, out hierarchy, out itemId, out docData, out docCookie)); return(docData == IntPtr.Zero ? null : GetDocumentInfo(docCookie)); }
public ProjectItem GetProjectItemByName(string lpszname) { IVsHierarchy hierarchy; uint pitemid, cookie; IntPtr ppunkDocData; if (VSConstants.S_OK == rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, lpszname, out hierarchy, out pitemid, out ppunkDocData, out cookie)) { return(GetProjectItem(hierarchy, pitemid)); } return(null); }
public static void OpenDocument(ServiceProvider provider, string fullPath, out IVsUIHierarchy hierarchy, out uint itemID, out IVsWindowFrame windowFrame, out IVsTextView view) { view = null; windowFrame = null; itemID = VsConstants.VSITEMID_NIL; hierarchy = null; //open document IVsUIShellOpenDocument shellOpenDoc = (IVsUIShellOpenDocument)provider.QueryService(VsConstants.SID_SVsUIShellOpenDocument, typeof(IVsUIShellOpenDocument)); IVsRunningDocumentTable pRDT = (IVsRunningDocumentTable)provider.QueryService(VsConstants.SID_SVsRunningDocumentTable, typeof(IVsRunningDocumentTable)); if (pRDT != null) { IntPtr punkDocData; uint docCookie; uint pitemid; IVsHierarchy ppIVsHierarchy; pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, fullPath, out ppIVsHierarchy, out pitemid, out punkDocData, out docCookie); if (punkDocData == IntPtr.Zero) { Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp; uint itemid; Guid logicalView = Guid.Empty; shellOpenDoc.OpenDocumentViaProject(fullPath, ref logicalView, out psp, out hierarchy, out itemid, out windowFrame); if (windowFrame != null) { windowFrame.Show(); } psp = null; } else { Marshal.Release(punkDocData); Guid logicalView = Guid.Empty; int pfOpen; shellOpenDoc.IsDocumentOpen((IVsUIHierarchy)ppIVsHierarchy, pitemid, fullPath, ref logicalView, (uint)__VSIDOFLAGS.IDO_IgnoreLogicalView, out hierarchy, out itemID, out windowFrame, out pfOpen); if (windowFrame != null) { windowFrame.Show(); } } } //return objects WindowFrameGetTextView(windowFrame, out view); }
/// <summary> /// Gets an instance of the RunningDocumentTable (RDT) service which manages the set of currently open /// documents in the environment and then notifies the client that an open document has changed /// </summary> private void NotifyDocChanged() { // Make sure that we have a file name if (fileName.Length == 0) { return; } // Get a reference to the Running Document Table IVsRunningDocumentTable runningDocTable = (IVsRunningDocumentTable)GetService(typeof(SVsRunningDocumentTable)); uint docCookie; IVsHierarchy hierarchy; uint itemID; IntPtr docData = IntPtr.Zero; try { // Lock the document int hr = runningDocTable.FindAndLockDocument( (uint)_VSRDTFLAGS.RDT_ReadLock, fileName, out hierarchy, out itemID, out docData, out docCookie ); ErrorHandler.ThrowOnFailure(hr); // Send the notification hr = runningDocTable.NotifyDocumentChanged(docCookie, (uint)__VSRDTATTRIB.RDTA_DocDataReloaded); // Unlock the document. // Note that we have to unlock the document even if the previous call failed. ErrorHandler.ThrowOnFailure(runningDocTable.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, docCookie)); // Check ff the call to NotifyDocChanged failed. ErrorHandler.ThrowOnFailure(hr); } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); } } }
/// <summary> /// Returns TRUE if open and dirty. Note that documents can be open without a /// window frame so be careful. Returns the DocData and doc cookie if requested /// </summary> /// <param name="document">document path</param> /// <param name="pHier">hierarchy</param> /// <param name="ppDocData">doc data associated with document</param> /// <param name="cookie">item cookie</param> /// <returns>True if FIle is dirty</returns> private bool VerifyFileDirtyInRdt(string document, out IVsHierarchy pHier, out IVsPersistDocData ppDocData, out uint cookie) { int ret = 0; pHier = null; ppDocData = null; cookie = 0; IVsRunningDocumentTable rdt = this.projectMgr.GetService(typeof(IVsRunningDocumentTable)) as IVsRunningDocumentTable; if (rdt != null) { IntPtr docData = IntPtr.Zero; uint dwCookie = 0; IVsHierarchy srpHier; uint itemid = VSConstants.VSITEMID_NIL; try { ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, document, out srpHier, out itemid, out docData, out dwCookie)); IVsPersistHierarchyItem srpIVsPersistHierarchyItem = srpHier as IVsPersistHierarchyItem; if (srpIVsPersistHierarchyItem != null) { // Found in the RDT. See if it is dirty try { ErrorHandler.ThrowOnFailure(srpIVsPersistHierarchyItem.IsItemDirty(itemid, docData, out ret)); cookie = dwCookie; ppDocData = Marshal.GetObjectForIUnknown(docData) as IVsPersistDocData; } finally { pHier = srpHier; } } } finally { if (docData != IntPtr.Zero) { Marshal.Release(docData); } } } return(ret == 1); }
private object FetchDocument() { ThreadHelper.ThrowIfNotOnUIThread(); if ((_flags & RDT_PendingInitialization) != 0) { return(null); } _fetchedRaw = true; // Normally when a document is open we get a handle via the opendocument tracker; but we might be to soon IVsRunningDocumentTable rd = GetService <IVsRunningDocumentTable>(typeof(SVsRunningDocumentTable)); if (rd == null) { return(null); } uint id; IVsHierarchy hier; IntPtr data; uint cookie; // We can't store the cookie, as that would mean it is in the lookup table if (VSErr.Succeeded(rd.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, Name, out hier, out id, out data, out cookie))) { if (hier != null) { Hierarchy = hier; } if ((id != 0) && id != VSItemId.Nil) { ItemId = id; } if (data != IntPtr.Zero) { _rawDocument = Marshal.GetUniqueObjectForIUnknown(data); Marshal.Release(data); } } return(_rawDocument); }
private static void ReloadDocument(IVsRunningDocumentTable runningDocumentTable, string outputFilePath) { if (runningDocumentTable == null) { // SVsRunningDocumentTable service is not available (as in a unit test). return; } IVsHierarchy hierarchy; uint itemId; IntPtr persistDocDataPointer; uint cookie; ErrorHandler.ThrowOnFailure(runningDocumentTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, outputFilePath, out hierarchy, out itemId, out persistDocDataPointer, out cookie)); if (persistDocDataPointer == IntPtr.Zero) { // Document is not currently opened in Visual Studio editor. return; } var persistDocData = (IVsPersistDocData)Marshal.GetObjectForIUnknown(persistDocDataPointer); ErrorHandler.ThrowOnFailure(persistDocData.ReloadDocData((uint)(_VSRELOADDOCDATA.RDD_IgnoreNextFileChange | _VSRELOADDOCDATA.RDD_RemoveUndoStack))); }