Пример #1
0
        private IVsInvisibleEditor GetInvisibleEditor(string filePath)
        {
            if (_CurrentInvisibleEditor == null)
            {
                IVsHierarchy hierarchy;
                uint         itemID;
                IntPtr       docData;

                IVsInvisibleEditor editor;

                RunningDocumentTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, filePath,
                                                         out hierarchy, out itemID, out docData, out _docCookie);

                ErrorHandler.ThrowOnFailure(InvisibleEditorManager.RegisterInvisibleEditor(filePath, null, 1, null, out editor));
                _CurrentInvisibleEditor = editor;
            }
            return(_CurrentInvisibleEditor);
        }
Пример #2
0
        private void DisplayDgml(XElement dgml)
        {
            uint      cookie;
            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(
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
                    ServiceProvider.GlobalProvider, dgmlFilePath, GuidList.GuidVsDesignerViewKind,
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
                    out var docUIHierarchy, out var docItemId, out var docWindowFrame) && docWindowFrame != null)
            {
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
                if (RunningDocumentTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, dgmlFilePath,
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
                                                             out var docHierarchy, out docItemId,
                                                             out var docDataIUnknownPointer,
                                                             out cookie) == VSConstants.S_OK)
                {
                    var persistDocDataServiceGuid = typeof(IVsPersistDocData).GUID;

                    if (Marshal.QueryInterface(docDataIUnknownPointer, ref persistDocDataServiceGuid,
                                               out var persistDocDataServicePointer) == 0)
                    {
                        try
                        {
                            var persistDocDataService =
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
                                (IVsPersistDocData)Marshal.GetObjectForIUnknown(persistDocDataServicePointer);
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread

                            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.
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
                                FileChangeService.IgnoreFile(0, dgmlFilePath, TRUE);
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread

                                // 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.
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
                                FileChangeService.SyncFile(dgmlFilePath);
                                persistDocDataService.ReloadDocData((uint)_VSRELOADDOCDATA.RDD_IgnoreNextFileChange);
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread

                                // Make sure the directed syntax graph window is visible but don't give it focus.
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
                                docWindowFrame.ShowNoActivate();
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
                            }
                        }
                        finally
                        {
                            Marshal.Release(persistDocDataServicePointer);
                        }
                    }
                }
            }
Пример #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);
            }
        }