Esempio n. 1
0
        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);
                }
            }
        }
Esempio n. 2
0
        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));
                }
            }
        }
Esempio n. 3
0
        /// <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));
             *              }
             *          }
             *      }
             *  }
             * }
             */
        }
Esempio n. 4
0
        /// <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);
                }
            }
        }
        protected virtual void UnlockRDTEntry()
        {
            if (this.isDisposed || this.ProjectMgr == null || this.ProjectMgr.IsClosed)
            {
                return;
            }
            // First we see if someone else has opened the requested view of the file.
            IVsRunningDocumentTable rdt = this.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

            if (rdt != null && this.DocCookie != (int)ShellConstants.VSDOCCOOKIE_NIL)
            {
                _VSRDTFLAGS flags = _VSRDTFLAGS.RDT_EditLock;

                ErrorHandler.ThrowOnFailure(rdt.UnlockDocument((uint)flags, (uint)this.DocCookie));
            }

            this.DocCookie = (int)ShellConstants.VSDOCCOOKIE_NIL;
        }
        public string GetTextOfFileIfOpenInIde(string filePath)
        {
            IVsRunningDocumentTable rdt = _serviceProvider.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

            IVsHierarchy hierarchy  = null;
            uint         itemid     = 0;
            IntPtr       docDataUnk = IntPtr.Zero;
            uint         lockCookie = 0;

            int hr = rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, filePath, out hierarchy, out itemid, out docDataUnk, out lockCookie);

            try
            {
                if (hr == VSConstants.S_OK)
                {
                    IVsTextLines textLines = Marshal.GetUniqueObjectForIUnknown(docDataUnk) as IVsTextLines;

                    if (textLines != null)
                    {
                        string text     = null;
                        int    endLine  = 0;
                        int    endIndex = 0;

                        hr = textLines.GetLastLineIndex(out endLine, out endIndex);
                        Debug.Assert(hr == VSConstants.S_OK, "GetLastLineIndex did not return S_OK.");

                        hr = textLines.GetLineText(0, 0, endLine, endIndex, out text);
                        Debug.Assert(hr == VSConstants.S_OK, "GetLineText did not return S_OK.");

                        return(text);
                    }
                }

                return(null);
            }
            finally
            {
                if (lockCookie != 0)
                {
                    rdt.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, lockCookie);
                }
            }
        }
        public void NotifyChanges()
        {
            if (_serviceProvider == null)
            {
                return;
            }

            // Get a reference to the Running Document Table
            IVsRunningDocumentTable runningDocTable = (IVsRunningDocumentTable)_serviceProvider.GetService(typeof(SVsRunningDocumentTable));

            // Lock the document
            uint         docCookie;
            IVsHierarchy hierarchy;
            uint         itemID;
            IntPtr       docData;
            int          hr = runningDocTable.FindAndLockDocument(
                (uint)_VSRDTFLAGS.RDT_ReadLock,
                base.Name,
                out hierarchy,
                out itemID,
                out docData,
                out docCookie
                );

            ErrorHandler.ThrowOnFailure(hr);

            IVsUIShell shell = _serviceProvider.GetService(typeof(IVsUIShell)) as IVsUIShell;

            if (shell != null)
            {
                shell.UpdateDocDataIsDirtyFeedback(docCookie, (_dirty == true) ? 1 : 0);
            }

            // Unlock the document.
            // Note that we have to unlock the document even if the previous call failed.
            runningDocTable.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, docCookie);

            // Check ff the call to NotifyDocChanged failed.
            //ErrorHandler.ThrowOnFailure(hr);
        }
Esempio n. 8
0
        /// <summary>
        /// Returns the buffer contents for a moniker.
        /// </summary>
        /// <returns>Buffer contents</returns>
        private string GetBufferContents(string fileName, out IVsTextStream srpStream)
        {
            Guid   CLSID_VsTextBuffer = new Guid("{8E7B96A8-E33D-11d0-A6D5-00C04FB67F6A}");
            string bufferContents     = "";

            srpStream = null;

            IVsRunningDocumentTable rdt = this.projectMgr.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

            if (rdt != null)
            {
                IVsHierarchy      hier;
                IVsPersistDocData persistDocData;
                uint   itemid, cookie;
                bool   docInRdt = true;
                IntPtr docData  = IntPtr.Zero;
                int    hr       = NativeMethods.E_FAIL;
                try
                {
                    //Getting a read lock on the document. Must be released later.
                    hr = rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, fileName, out hier, out itemid, out docData, out cookie);
                    if (ErrorHandler.Failed(hr) || docData == IntPtr.Zero)
                    {
                        Guid iid = VSConstants.IID_IUnknown;
                        cookie   = 0;
                        docInRdt = false;
                        ILocalRegistry localReg = this.projectMgr.GetService(typeof(SLocalRegistry)) as ILocalRegistry;
                        ErrorHandler.ThrowOnFailure(localReg.CreateInstance(CLSID_VsTextBuffer, null, ref iid, (uint)CLSCTX.CLSCTX_INPROC_SERVER, out docData));
                    }

                    persistDocData = Marshal.GetObjectForIUnknown(docData) as IVsPersistDocData;
                }
                finally
                {
                    if (docData != IntPtr.Zero)
                    {
                        Marshal.Release(docData);
                    }
                }

                //Try to get the Text lines
                IVsTextLines srpTextLines = persistDocData as IVsTextLines;
                if (srpTextLines == null)
                {
                    // Try getting a text buffer provider first
                    IVsTextBufferProvider srpTextBufferProvider = persistDocData as IVsTextBufferProvider;
                    if (srpTextBufferProvider != null)
                    {
                        hr = srpTextBufferProvider.GetTextBuffer(out srpTextLines);
                    }
                }

                if (ErrorHandler.Succeeded(hr))
                {
                    srpStream = srpTextLines as IVsTextStream;
                    if (srpStream != null)
                    {
                        // QI for IVsBatchUpdate and call FlushPendingUpdates if they support it
                        IVsBatchUpdate srpBatchUpdate = srpStream as IVsBatchUpdate;
                        if (srpBatchUpdate != null)
                        {
                            ErrorHandler.ThrowOnFailure(srpBatchUpdate.FlushPendingUpdates(0));
                        }

                        int lBufferSize = 0;
                        hr = srpStream.GetSize(out lBufferSize);

                        if (ErrorHandler.Succeeded(hr))
                        {
                            IntPtr dest = IntPtr.Zero;
                            try
                            {
                                // Note that GetStream returns Unicode to us so we don't need to do any conversions
                                dest = Marshal.AllocCoTaskMem((lBufferSize + 1) * 2);
                                ErrorHandler.ThrowOnFailure(srpStream.GetStream(0, lBufferSize, dest));
                                //Get the contents
                                bufferContents = Marshal.PtrToStringUni(dest);
                            }
                            finally
                            {
                                if (dest != IntPtr.Zero)
                                {
                                    Marshal.FreeCoTaskMem(dest);
                                }
                            }
                        }
                    }
                }
                // Unlock the document in the RDT if necessary
                if (docInRdt && rdt != null)
                {
                    ErrorHandler.ThrowOnFailure(rdt.UnlockDocument((uint)(_VSRDTFLAGS.RDT_ReadLock | _VSRDTFLAGS.RDT_Unlock_NoSave), cookie));
                }

                if (ErrorHandler.Failed(hr))
                {
                    // If this failed then it's probably not a text file.  In that case,
                    // we just read the file as a binary
                    bufferContents = File.ReadAllText(fileName);
                }
            }
            return(bufferContents);
        }
Esempio n. 9
0
        internal void Removed()
        {
            IVsRunningDocumentTable rdt = PythonProfilingPackage.GetGlobalService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

            ErrorHandler.ThrowOnFailure(rdt.UnlockDocument((uint)_VSRDTFLAGS.RDT_EditLock, _docCookie));
        }
Esempio n. 10
0
        private ITextBuffer GetTextBufferOnUIThread(bool create)
        {
            IVsTextManager textMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
            var            model   = GetService(typeof(SComponentModel)) as IComponentModel;
            var            adapter = model.GetService <IVsEditorAdaptersFactoryService>();
            uint           itemid;

            IVsRunningDocumentTable rdt = ProjectMgr.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

            if (rdt != null)
            {
                IVsHierarchy      hier;
                IVsPersistDocData persistDocData;
                uint   cookie;
                bool   docInRdt = true;
                IntPtr docData  = IntPtr.Zero;
                int    hr       = NativeMethods.E_FAIL;
                try {
                    //Getting a read lock on the document. Must be released later.
                    hr = rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, GetMkDocument(), out hier, out itemid, out docData, out cookie);
                    if (ErrorHandler.Failed(hr) || docData == IntPtr.Zero)
                    {
                        if (!create)
                        {
                            return(null);
                        }
                        Guid iid = VSConstants.IID_IUnknown;
                        cookie   = 0;
                        docInRdt = false;
                        ILocalRegistry localReg = this.ProjectMgr.GetService(typeof(SLocalRegistry)) as ILocalRegistry;
                        ErrorHandler.ThrowOnFailure(localReg.CreateInstance(CLSID_VsTextBuffer, null, ref iid, (uint)CLSCTX.CLSCTX_INPROC_SERVER, out docData));
                    }
                    persistDocData = Marshal.GetObjectForIUnknown(docData) as IVsPersistDocData;
                } finally {
                    if (docData != IntPtr.Zero)
                    {
                        Marshal.Release(docData);
                    }
                }

                //Try to get the Text lines
                IVsTextLines srpTextLines = persistDocData as IVsTextLines;
                if (srpTextLines == null)
                {
                    // Try getting a text buffer provider first
                    IVsTextBufferProvider srpTextBufferProvider = persistDocData as IVsTextBufferProvider;
                    if (srpTextBufferProvider != null)
                    {
                        hr = srpTextBufferProvider.GetTextBuffer(out srpTextLines);
                    }
                }

                // Unlock the document in the RDT if necessary
                if (docInRdt && rdt != null)
                {
                    ErrorHandler.ThrowOnFailure(rdt.UnlockDocument((uint)(_VSRDTFLAGS.RDT_ReadLock | _VSRDTFLAGS.RDT_Unlock_NoSave), cookie));
                }

                if (srpTextLines != null)
                {
                    return(adapter.GetDocumentBuffer(srpTextLines));
                }
            }

            IWpfTextView view = GetTextView();

            return(view.TextBuffer);
        }
Esempio n. 11
0
        internal void Removed()
        {
            IVsRunningDocumentTable rdt = _serviceProvider.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

            ErrorHandler.ThrowOnFailure(rdt.UnlockDocument((uint)_VSRDTFLAGS.RDT_EditLock, _docCookie));
        }
Esempio n. 12
0
        /// <summary>
        /// This is used to get the content of a specific topic file if it is open in an editor so that the
        /// current content is displayed for it in the topic previewer control.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void ucTopicPreviewer_TopicContentNeeded(object sender, TopicContentNeededEventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            IVsPersistDocData persistDocData;
            IVsTextStream     srpStream;
            IntPtr            docData = IntPtr.Zero;
            uint cookie = 0;
            int  hr     = VSConstants.E_FAIL;

            IVsRunningDocumentTable rdt = Utility.GetServiceFromPackage <IVsRunningDocumentTable,
                                                                         SVsRunningDocumentTable>(true);

            if (rdt == null)
            {
                return;
            }

            try
            {
                // Getting a read lock on the document.  This must be released later.
                hr = rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, e.TopicFilename, out IVsHierarchy hier,
                                             out uint itemid, out docData, out cookie);

                if (ErrorHandler.Failed(hr) || docData == IntPtr.Zero)
                {
                    return;
                }

                persistDocData = Marshal.GetObjectForIUnknown(docData) as IVsPersistDocData;

                // Try to get the Text lines
                IVsTextLines srpTextLines = persistDocData as IVsTextLines;

                if (srpTextLines == null)
                {
                    // Try getting a text buffer provider first
                    if (persistDocData is IVsTextBufferProvider srpTextBufferProvider)
                    {
                        hr = srpTextBufferProvider.GetTextBuffer(out srpTextLines);
                    }
                }

                if (ErrorHandler.Succeeded(hr))
                {
                    srpStream = srpTextLines as IVsTextStream;

                    if (srpStream != null)
                    {
                        if (srpStream is IVsBatchUpdate srpBatchUpdate)
                        {
                            ErrorHandler.ThrowOnFailure(srpBatchUpdate.FlushPendingUpdates(0));
                        }

                        hr = srpStream.GetSize(out int lBufferSize);

                        if (ErrorHandler.Succeeded(hr))
                        {
                            IntPtr dest = IntPtr.Zero;

                            try
                            {
                                // GetStream() returns Unicode data so we need to double the buffer size
                                dest = Marshal.AllocCoTaskMem((lBufferSize + 1) * 2);
                                ErrorHandler.ThrowOnFailure(srpStream.GetStream(0, lBufferSize, dest));

                                // Get the contents
                                e.TopicContent = Marshal.PtrToStringUni(dest);
                            }
                            finally
                            {
                                if (dest != IntPtr.Zero)
                                {
                                    Marshal.FreeCoTaskMem(dest);
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (docData != IntPtr.Zero)
                {
                    Marshal.Release(docData);
                }

                if (cookie != 0)
                {
                    ErrorHandler.ThrowOnFailure(rdt.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, cookie));
                }
            }
        }
Esempio n. 13
0
 public void UnlockDocument(_VSRDTFLAGS lockType, uint cookie)
 {
     NativeMethods.ThrowOnFailure(rdt.UnlockDocument((uint)lockType, cookie));
 }