Example #1
0
        public void Resume()
        {
            if (!mIsSuspending || mPersistDocData == null)
            {
                return;
            }

            if (mPersistDocData != null && mReloadDocument)
            {
                mPersistDocData.ReloadDocData(0);
            }

            IVsFileChangeEx fileChange = mSite.GetService(typeof(SVsFileChangeEx)) as IVsFileChangeEx;

            if (fileChange == null)
            {
                return;
            }

            mIsSuspending = false;
            ErrorHandler.ThrowOnFailure(fileChange.SyncFile(mDocumentFileName));
            ErrorHandler.ThrowOnFailure(fileChange.IgnoreFile(0, mDocumentFileName, 0));
            if (mFileChangeControl != null)
            {
                ErrorHandler.ThrowOnFailure(mFileChangeControl.IgnoreFileChanges(0));
            }
        }
Example #2
0
        /// <summary>
        /// Reloads file buffer without displaying a GUI dialog.
        /// </summary>
        public static void SilentlyReloadFile(string path)
        {
            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)
            {
                IVsPersistDocData d = (IVsPersistDocData)Marshal.GetObjectForIUnknown(pPunkDocData);
                hr = d.ReloadDocData(0);
                Marshal.ThrowExceptionForHR(hr);
            }
        }
Example #3
0
        private void DisplayDgml(XElement dgml)
        {
            var filePath = Path.Combine(Path.GetTempPath(), "Syntax.dgml");

            uint           docItemId;
            IVsUIHierarchy docUIHierarchy;
            IVsWindowFrame docWindowFrame;

            // Check whether the file is already open in the 'design' view.
            // If the file is already open in the desired view then we will update the
            // contents of the file on disk with the new directed syntax graph and load
            // this new graph into the already open view of the file.
            if (VsShellUtilities.IsDocumentOpen(
                    DTE2ServiceProvider, filePath, GuidList.GuidVsDesignerViewKind,
                    out docUIHierarchy, out docItemId, out docWindowFrame) && docWindowFrame != null)
            {
                IVsHierarchy docHierarchy;
                uint         docCookie;
                IntPtr       docDataIUnknownPointer;

                if (RunningDocumentTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, filePath,
                                                             out docHierarchy, out docItemId,
                                                             out docDataIUnknownPointer,
                                                             out docCookie) == VSConstants.S_OK &&
                    docDataIUnknownPointer != null)
                {
                    IntPtr persistDocDataServicePointer;
                    var    persistDocDataServiceGuid = typeof(IVsPersistDocData).GUID;

                    if (Marshal.QueryInterface(docDataIUnknownPointer, ref persistDocDataServiceGuid,
                                               out persistDocDataServicePointer) == 0 &&
                        persistDocDataServicePointer != null)
                    {
                        try
                        {
                            IVsPersistDocData persistDocDataService =
                                (IVsPersistDocData)Marshal.GetObjectForIUnknown(persistDocDataServicePointer);

                            if (persistDocDataService != null)
                            {
                                const int TRUE = -1, FALSE = 0;

                                // The below call ensures that there are no pop-ups from Visual Studio
                                // prompting the user to reload the file each time it is changed.
                                FileChangeService.IgnoreFile(0, filePath, TRUE);

                                // Update the file on disk with the new directed syntax graph.
                                dgml.Save(filePath);

                                // The below calls ensure that the file is refreshed inside Visual Studio
                                // so that the latest contents are displayed to the user.
                                FileChangeService.SyncFile(filePath);
                                persistDocDataService.ReloadDocData((uint)_VSRELOADDOCDATA.RDD_IgnoreNextFileChange);

                                // Re-enable pop-ups from Visual Studio prompting the user to reload the file
                                // in case the file is ever changed by some other process.
                                FileChangeService.IgnoreFile(0, filePath, FALSE);

                                // Make sure the directed syntax graph window is visible but don't give it focus.
                                docWindowFrame.ShowNoActivate();
                            }
                        }
                        finally
                        {
                            Marshal.Release(persistDocDataServicePointer);
                        }
                    }
                }
            }
            else
            {
                // File is not open in the 'design' view. But it may be open in the 'xml' view.
                // If the file is open in any other view than 'design' view then we will close it
                // so that there are no pop-ups from Visual Studio about the file already being open.
                if (VsShellUtilities.IsDocumentOpen(
                        DTE2ServiceProvider, filePath, Guid.Empty,
                        out docUIHierarchy, out docItemId, out docWindowFrame) && docWindowFrame != null)
                {
                    docWindowFrame.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_NoSave);
                }

                // Update the file on disk with the new directed syntax graph.
                dgml.Save(filePath);

                // Open the new directed syntax graph in the 'design' view.
                VsShellUtilities.OpenDocument(
                    DTE2ServiceProvider, filePath, GuidList.GuidVsDesignerViewKind,
                    out docUIHierarchy, out docItemId, out docWindowFrame);
            }
        }
        private void DisplayDgml(XElement dgml)
        {
            uint           docItemId, cookie;
            IVsUIHierarchy docUIHierarchy;
            IVsWindowFrame docWindowFrame;
            IVsHierarchy   docHierarchy;
            IntPtr         docDataIUnknownPointer;
            const int      TRUE = -1;

            if (string.IsNullOrWhiteSpace(dgmlFilePath))
            {
                var folderPath = Path.Combine(Path.GetTempPath(),
                                              "Syntax-" + System.Diagnostics.Process.GetCurrentProcess().Id.ToString());
                Directory.CreateDirectory(folderPath);
                dgmlFilePath = Path.Combine(folderPath, "Syntax.dgml");
            }

            // Check whether the file is already open in the 'design' view.
            // If the file is already open in the desired view then we will update the
            // contents of the file on disk with the new directed syntax graph and load
            // this new graph into the already open view of the file.
            if (VsShellUtilities.IsDocumentOpen(
                    ServiceProvider.GlobalProvider, dgmlFilePath, GuidList.GuidVsDesignerViewKind,
                    out docUIHierarchy, out docItemId, out docWindowFrame) && docWindowFrame != null)
            {
                if (RunningDocumentTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, dgmlFilePath,
                                                             out docHierarchy, out docItemId,
                                                             out docDataIUnknownPointer,
                                                             out cookie) == VSConstants.S_OK)
                {
                    IntPtr persistDocDataServicePointer;
                    var    persistDocDataServiceGuid = typeof(IVsPersistDocData).GUID;

                    if (Marshal.QueryInterface(docDataIUnknownPointer, ref persistDocDataServiceGuid,
                                               out persistDocDataServicePointer) == 0)
                    {
                        try
                        {
                            IVsPersistDocData persistDocDataService =
                                (IVsPersistDocData)Marshal.GetObjectForIUnknown(persistDocDataServicePointer);

                            if (persistDocDataService != null)
                            {
                                // The below call ensures that there are no pop-ups from Visual Studio
                                // prompting the user to reload the file each time it is changed.
                                FileChangeService.IgnoreFile(0, dgmlFilePath, TRUE);

                                // Update the file on disk with the new directed syntax graph.
                                dgml.Save(dgmlFilePath);

                                // The below calls ensure that the file is refreshed inside Visual Studio
                                // so that the latest contents are displayed to the user.
                                FileChangeService.SyncFile(dgmlFilePath);
                                persistDocDataService.ReloadDocData((uint)_VSRELOADDOCDATA.RDD_IgnoreNextFileChange);

                                // Make sure the directed syntax graph window is visible but don't give it focus.
                                docWindowFrame.ShowNoActivate();
                            }
                        }
                        finally
                        {
                            Marshal.Release(persistDocDataServicePointer);
                        }
                    }
                }
            }
            else
            {
                // Update the file on disk with the new directed syntax graph.
                dgml.Save(dgmlFilePath);

                // Open the new directed syntax graph in the 'design' view.
                VsShellUtilities.OpenDocument(
                    ServiceProvider.GlobalProvider, dgmlFilePath, GuidList.GuidVsDesignerViewKind,
                    out docUIHierarchy, out docItemId, out docWindowFrame);

                // Register event handler to ensure that directed syntax graph file is deleted when the solution is closed.
                // This ensures that the file won't be persisted in the .suo file and that it therefore won't get re-opened
                // when the solution is re-opened.
                SolutionService.AdviseSolutionEvents(this, out cookie);
            }
        }