/// <summary>
        /// Get a list of all files in the Running Docuement Table.
        /// </summary>
        /// <returns></returns>
        public List <string> GetRDTFiles()
        {
            List <string>         list = new List <string>();
            IEnumRunningDocuments documents;

            if (DocumentTable != null)
            {
                DocumentTable.GetRunningDocumentsEnum(out documents);
                uint[] docCookie = new uint[1];
                uint   fetched;
                while ((VSConstants.S_OK == documents.Next(1, docCookie, out fetched)) && (1 == fetched))
                {
                    uint         flags;
                    uint         editLocks;
                    uint         readLocks;
                    string       moniker;
                    IVsHierarchy docHierarchy;
                    uint         docId;
                    IntPtr       docData = IntPtr.Zero;
                    DocumentTable.GetDocumentInfo(docCookie[0], out flags, out readLocks, out editLocks, out moniker, out docHierarchy, out docId, out docData);
                    list.Add(moniker);
                }
            }
            return(list);
        }
        // Detaches the SARIF results from all documents.
        public void DetachFromAllDocuments()
        {
            IEnumRunningDocuments documentsEnumerator;

            if (_runningDocTable != null)
            {
                _runningDocTable.GetRunningDocumentsEnum(out documentsEnumerator);

                if (documentsEnumerator != null)
                {
                    uint   requestedCount = 1;
                    uint   actualCount;
                    uint[] cookies = new uint[requestedCount];

                    while (true)
                    {
                        documentsEnumerator.Next(requestedCount, cookies, out actualCount);
                        if (actualCount == 0)
                        {
                            // There are no more documents to process.
                            break;
                        }

                        // Detach from document.
                        DetachFromDocumentChanges(cookies[0]);
                    }
                }
            }
        }
Esempio n. 3
0
        private IntPtr FindDocDataFromRDT()
        {
            // Get a reference to the RDT.
            IVsRunningDocumentTable rdt = Package.GetGlobalService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

            if (null == rdt)
            {
                return(IntPtr.Zero);
            }

            // Get the enumeration of the running documents.
            IEnumRunningDocuments documents;

            ErrorHandler.ThrowOnFailure(rdt.GetRunningDocumentsEnum(out documents));

            IntPtr documentData = IntPtr.Zero;

            uint[] docCookie = new uint[1];
            uint   fetched;

            while ((VSConstants.S_OK == documents.Next(1, docCookie, out fetched)) && (1 == fetched))
            {
                uint         flags;
                uint         editLocks;
                uint         readLocks;
                string       moniker;
                IVsHierarchy docHierarchy;
                uint         docId;
                IntPtr       docData = IntPtr.Zero;
                try
                {
                    ErrorHandler.ThrowOnFailure(
                        rdt.GetDocumentInfo(docCookie[0], out flags, out readLocks, out editLocks, out moniker, out docHierarchy, out docId, out docData));
                    // Check if this document is the one we are looking for.
                    if ((docId == fileId) && (ownerHierarchy.Equals(docHierarchy)))
                    {
                        documentData = docData;
                        docData      = IntPtr.Zero;
                        break;
                    }
                }
                finally
                {
                    if (IntPtr.Zero != docData)
                    {
                        Marshal.Release(docData);
                    }
                }
            }

            return(documentData);
        }
Esempio n. 4
0
        /// <summary>
        /// Iterate all loaded documents of the given <typeparam name="DocumentType"/>
        /// </summary>
        public static IEnumerable <DocumentType> GetLoadedDocuments <DocumentType>(IServiceProvider serviceProvider) where DocumentType : class
        {
            // Walk all the documents and invalidate ORM diagrams if the options have changed
            IVsRunningDocumentTable docTable = (IVsRunningDocumentTable)serviceProvider.GetService(typeof(IVsRunningDocumentTable));
            IEnumRunningDocuments   docIter;

            ErrorHandler.ThrowOnFailure(docTable.GetRunningDocumentsEnum(out docIter));
            int hrIter;

            uint[] currentDocs = new uint[1];
            uint   fetched     = 0;

            do
            {
                ErrorHandler.ThrowOnFailure(hrIter = docIter.Next(1, currentDocs, out fetched));
                if (hrIter == 0)
                {
                    uint         grfRDTFlags;
                    uint         dwReadLocks;
                    uint         dwEditLocks;
                    string       bstrMkDocument;
                    IVsHierarchy pHier;
                    uint         itemId;
                    IntPtr       punkDocData = IntPtr.Zero;
                    ErrorHandler.ThrowOnFailure(docTable.GetDocumentInfo(
                                                    currentDocs[0],
                                                    out grfRDTFlags,
                                                    out dwReadLocks,
                                                    out dwEditLocks,
                                                    out bstrMkDocument,
                                                    out pHier,
                                                    out itemId,
                                                    out punkDocData));
                    try
                    {
                        DocumentType docData = Marshal.GetObjectForIUnknown(punkDocData) as DocumentType;
                        if (docData != null)
                        {
                            yield return(docData);
                        }
                    }
                    finally
                    {
                        if (punkDocData != IntPtr.Zero)
                        {
                            Marshal.Release(punkDocData);
                        }
                    }
                }
            } while (fetched != 0);
        }
Esempio n. 5
0
        public static string GetMonikerForHierarchyAndItemId(this IVsRunningDocumentTable runningDocTable, IVsHierarchy hierarchy, uint itemid)
        {
            if (runningDocTable == null)
            {
                throw new ArgumentNullException("runningDocTable");
            }

            if (hierarchy == null)
            {
                throw new ArgumentNullException("hierarchy");
            }

            // First, get the doc cookie for this
            IEnumRunningDocuments runningDocsEnum;

            Marshal.ThrowExceptionForHR(runningDocTable.GetRunningDocumentsEnum(out runningDocsEnum));
            var  cookies = new uint[1];
            uint cookiesFetched;

            while (runningDocsEnum.Next(1, cookies, out cookiesFetched) == VSConstants.S_OK && cookiesFetched == 1)
            {
                uint         documentFlags;
                uint         documentReadLocks;
                uint         documentEditLocks;
                string       documentId;
                IVsHierarchy documentHierarchy;
                uint         documentItemID;
                IntPtr       pDocData;

                Marshal.ThrowExceptionForHR(runningDocTable.GetDocumentInfo(cookies[0], out documentFlags, out documentReadLocks, out documentEditLocks, out documentId, out documentHierarchy, out documentItemID, out pDocData));

                try
                {
                    if (documentHierarchy == hierarchy && documentItemID == itemid)
                    {
                        return(documentId);
                    }
                }
                finally
                {
                    Marshal.Release(pDocData);
                }
            }

            // Uh, OK, that's probably not good that we're supposedly an open file but not in the RDT. We'll fall back
            // to the IVsHierarchy's name property for this item.
            object property;

            Marshal.ThrowExceptionForHR(hierarchy.GetProperty(itemid, (int)__VSHPROPID.VSHPROPID_Name, out property));
            return((string)property);
        }
Esempio n. 6
0
        /// <summary>
        /// 关闭一个已经打开的文档
        /// </summary>
        /// <param name="name">文档路径(包含文件后缀)</param>
        /// <param name="saveOption">制定如何关闭一个文档,默认为强制保存后关闭</param>
        public static void CloseDocument(string name, __VSSLNSAVEOPTIONS saveOption = __VSSLNSAVEOPTIONS.SLNSAVEOPT_ForceSave)
        {
            IVsRunningDocumentTable runningTabs = RunningDocumentTableObject;
            IVsSolution             solution    = VSSolution.SolutionObject;

            if (runningTabs != null)
            {
                IEnumRunningDocuments runningDocuments;
                runningTabs.GetRunningDocumentsEnum(out runningDocuments);

                IntPtr documentData = IntPtr.Zero;
                uint[] docCookie    = new uint[1];
                uint   fetched;

                //遍历所有已经打开的文档
                while ((VSConstants.S_OK == runningDocuments.Next(1, docCookie, out fetched)) && (fetched == 1))
                {
                    uint         flags;
                    uint         editLocks;
                    uint         readLocks;
                    string       filePath;
                    IVsHierarchy docHierarchy;
                    uint         docId;
                    IntPtr       docData = IntPtr.Zero;

                    try
                    {
                        ErrorHandler.ThrowOnFailure(runningTabs.GetDocumentInfo(docCookie[0], out flags, out readLocks,
                                                                                out editLocks, out filePath, out docHierarchy,
                                                                                out docId, out docData));

                        if (solution != null && !filePath.EndsWith("sln") && !filePath.EndsWith("csproj"))
                        {
                            //string currentName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
                            if (name == filePath)
                            {
                                solution.CloseSolutionElement((uint)saveOption, docHierarchy, docCookie[0]);
                            }
                        }
                    }
                    finally
                    {
                        if (docData != IntPtr.Zero)
                        {
                            Marshal.Release(docData);
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Get the document cookies for the documents in the running document table
        /// </summary>
        /// <remarks>
        /// This method simple asks for the cookies and hence won't force the document to be loaded
        /// if it is being loaded in a lazy fashion
        /// </remarks>
        public static List <uint> GetRunningDocumentCookies(this IVsRunningDocumentTable runningDocumentTable)
        {
            var list = new List <uint>();
            IEnumRunningDocuments enumDocuments;

            if (!ErrorHandler.Succeeded(runningDocumentTable.GetRunningDocumentsEnum(out enumDocuments)))
            {
                return(list);
            }

            uint[] array        = new uint[1];
            uint   pceltFetched = 0;

            while (ErrorHandler.Succeeded(enumDocuments.Next(1, array, out pceltFetched)) && (pceltFetched == 1))
            {
                list.Add(array[0]);
            }

            return(list);
        }
Esempio n. 8
0
        void LoadInitial()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            IVsRunningDocumentTable rdt = RunningDocumentTable;

            if (rdt == null)
            {
                return;
            }

            IEnumRunningDocuments docEnum;

            if (!VSErr.Succeeded(rdt.GetRunningDocumentsEnum(out docEnum)))
            {
                return;
            }

            uint[] cookies = new uint[256];
            uint   nFetched;

            while (VSErr.Succeeded(docEnum.Next((uint)cookies.Length, cookies, out nFetched)))
            {
                if (nFetched == 0)
                {
                    break;
                }

                for (int i = 0; i < nFetched; i++)
                {
                    SccDocumentData data;
                    if (TryGetDocument(cookies[i], out data))
                    {
                        data.OnCookieLoad(_poller);
                    }
                }
            }
        }
Esempio n. 9
0
        // Enumerate the running documents
        public IEnumerator <RunningDocumentInfo> GetEnumerator()
        {
            IList <RunningDocumentInfo> list = new List <RunningDocumentInfo>();
            IEnumRunningDocuments       ppenum;

            if (NativeMethods.Succeeded(rdt.GetRunningDocumentsEnum(out ppenum)))
            {
                uint[] rgelt   = new uint[1];
                uint   fetched = 0;
                while (true)
                {
                    if (NativeMethods.Succeeded(ppenum.Next(1, rgelt, out fetched)) && fetched == 1)
                    {
                        list.Add(GetDocumentInfo(rgelt[0]));
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(list.GetEnumerator());
        }
Esempio n. 10
0
        private static void AddMarkersToOpenDocuments()
        {
            // If there is no clone detective result loaded we're already done.
            if (!IsCloneReportAvailable)
            {
                return;
            }

            // Loop through all open documents and insert a text marker for each
            // clone in an open document.

            // Get an enumerator of all open documents.
            IEnumRunningDocuments   runningDocumentsEnum;
            IVsRunningDocumentTable rdt = (IVsRunningDocumentTable)VSPackage.Instance.GetService(typeof(SVsRunningDocumentTable));

            ErrorHandler.ThrowOnFailure(rdt.GetRunningDocumentsEnum(out runningDocumentsEnum));

            while (true)
            {
                // Move enumerator to next document if there is one.
                uint[] docCookies = new uint[1];
                uint   fetched;
                ErrorHandler.ThrowOnFailure(runningDocumentsEnum.Next(1, docCookies, out fetched));
                if (fetched < 1)
                {
                    break;
                }

                // Look up the text buffer of the document.
                IVsTextLines textLines = GetDocumentTextLines(docCookies[0]);
                if (textLines != null)
                {
                    AddMarkersToDocument(textLines);
                }
            }
        }
Esempio n. 11
0
        // Считывает содержимое файла из буфера, связанного с документом.
        // Такой способ позволяет получить еще не сохраненный на диск контент.
        private string ReadSourceFromBuffer()
        {
            IVsRunningDocumentTable rdt = NemerlePackage.GetGlobalService(typeof(IVsRunningDocumentTable)) as IVsRunningDocumentTable;

            if (rdt != null)
            {
                IEnumRunningDocuments documents;

                rdt.GetRunningDocumentsEnum(out documents);

                IntPtr documentData = IntPtr.Zero;
                uint[] docCookie    = new uint[1];
                uint   fetched;

                while ((Microsoft.VisualStudio.VSConstants.S_OK == documents.Next(1, docCookie, out fetched)) && (1 == fetched))
                {
                    uint         flags;
                    uint         editLocks;
                    uint         readLocks;
                    string       moniker;
                    IVsHierarchy docHierarchy;
                    uint         docId;
                    IntPtr       docData = IntPtr.Zero;

                    try
                    {
                        ErrorHandler.ThrowOnFailure(rdt.GetDocumentInfo(docCookie[0], out flags, out readLocks, out editLocks, out moniker, out docHierarchy, out docId, out docData));

                        // Check if this document is the one we are looking for.
                        if (docId == _id && _hierarchy.Equals(docHierarchy))
                        {
                            documentData = docData;
                            docData      = IntPtr.Zero;
                            break;
                        }
                    }
                    finally
                    {
                        if (docData != IntPtr.Zero)
                        {
                            Marshal.Release(docData);
                        }
                    }
                }

                if (documentData != IntPtr.Zero)
                {
                    object       obj      = Marshal.GetObjectForIUnknown(documentData);
                    IVsTextLines txtLines = obj as IVsTextLines;

                    int totalLines;
                    ErrorHandler.ThrowOnFailure(txtLines.GetLineCount(out totalLines));
                    StringBuilder sb = new StringBuilder();

                    for (int line = 0; line < totalLines; ++line)
                    {
                        int lineLen;
                        ErrorHandler.ThrowOnFailure(txtLines.GetLengthOfLine(line, out lineLen));

                        string lineText;
                        ErrorHandler.ThrowOnFailure(txtLines.GetLineText(line, 0, line, lineLen, out lineText));

                        sb.AppendLine(lineText);
                    }

                    return(sb.ToString());
                }
            }

            return(null);
        }
Esempio n. 12
0
        public static void TraceRunningDocuments()
        {
            // Get the RDT (Running Document Table)
            IVsRunningDocumentTable rdt = Package.Instance.Context.ServiceProvider.GetService(typeof(IVsRunningDocumentTable)) as IVsRunningDocumentTable;

            if (rdt == null)
            {
                Tracer.WriteLineWarning(classType, "TraceRunningDocuments", "Cannot get an instance of IVsRunningDocumentTable to use for enumerating the running documents.");
                return;
            }

            // Get the enumerator for the currently running documents.
            IEnumRunningDocuments enumerator;
            int hr = rdt.GetRunningDocumentsEnum(out enumerator);

            if (NativeMethods.Failed(hr))
            {
                Tracer.WriteLineWarning(classType, "TraceRunningDocuments", "Cannot get an instance of IEnumRunningDocuments to use for enumerating the running documents.");
                return;
            }

            // Enumerate.
            StringCollection traceLines = new StringCollection();

            uint[] cookies = new uint[1];
            uint   fetchCount;

            while (true)
            {
                hr = enumerator.Next(1, cookies, out fetchCount);
                if (NativeMethods.Failed(hr))
                {
                    Tracer.WriteLineWarning(classType, "TraceRunningDocuments", "The enumeration failed for the running documents. Hr=0x{0:X}", hr);
                    return;
                }

                if (fetchCount == 0)
                {
                    break;
                }

                uint cookie = cookies[0];

                // We shouldn't be getting a nil cookie.
                if (cookie == DocumentInfo.NullCookie)
                {
                    Tracer.WriteLineWarning(classType, "TraceRunningDocuments", "There is a null cookie value in the RDT, which shouldn't be happening.");
                }
                else
                {
                    // Now we have a document cookie, so let's get some information about it.
                    DocumentInfo docInfo = Package.Instance.Context.RunningDocumentTable.FindByCookie(cookie);
                    string       traceMessage;
                    if (docInfo == null)
                    {
                        traceMessage = PackageUtility.SafeStringFormatInvariant("The document with cookie '{0}' could not be found in the RDT. There's something weird going on.", cookie);
                    }
                    else
                    {
                        // Here's where we actually do the trace finally.
                        traceMessage = PackageUtility.SafeStringFormatInvariant("RDT document: Cookie={0} Path={1} IsOpen={2} IsDirty={3}", docInfo.Cookie, docInfo.AbsolutePath, docInfo.IsOpen, docInfo.IsDirty);
                    }

                    // We don't want to trace immediately because we want all of these lines to appear together. If we
                    // trace immediately, then the messages will be split up.
                    traceLines.Add(traceMessage);
                }
            }

            // Now trace all of the messages at once.
            foreach (string traceMessage in traceLines)
            {
                Tracer.WriteLine(classType, "TraceRunningDocuments", Tracer.Level.Information, traceMessage);
            }
        }