public TextLineEventListener(IVsTextLines buffer, string fileName, ModuleId id)
 {
     this.buffer = buffer;
     this.fileId = id;
     this.fileName = fileName;
     IConnectionPointContainer container = buffer as IConnectionPointContainer;
     if (null != container) {
         Guid eventsGuid = typeof(IVsTextLinesEvents).GUID;
         container.FindConnectionPoint(ref eventsGuid, out connectionPoint);
         connectionPoint.Advise(this as IVsTextLinesEvents, out connectionCookie);
     }
 }
Example #2
0
        public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
        {
            // Check if this document is in the list of the documents.
            if (documents.ContainsKey(docCookie))
            {
                return(VSConstants.S_OK);
            }
            // Get the information about this document from the RDT.
            IVsRunningDocumentTable rdt = provider.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

            if (null != rdt)
            {
                // Note that here we don't want to throw in case of error.
                uint         flags;
                uint         readLocks;
                uint         writeLoks;
                string       documentMoniker;
                IVsHierarchy hierarchy;
                uint         itemId;
                IntPtr       unkDocData;
                int          hr = rdt.GetDocumentInfo(docCookie, out flags, out readLocks, out writeLoks,
                                                      out documentMoniker, out hierarchy, out itemId, out unkDocData);
                try {
                    if (Microsoft.VisualStudio.ErrorHandler.Failed(hr) || (IntPtr.Zero == unkDocData))
                    {
                        return(VSConstants.S_OK);
                    }
                    // Check if the herarchy is one of the hierarchies this service is monitoring.
                    if (!hierarchies.ContainsKey(hierarchy))
                    {
                        // This hierarchy is not monitored, we can exit now.
                        return(VSConstants.S_OK);
                    }

                    // Check the extension of the file to see if a listener is required.
                    string extension = System.IO.Path.GetExtension(documentMoniker);
                    if (0 != string.Compare(extension, IPyConstants.pythonFileExtension, StringComparison.OrdinalIgnoreCase))
                    {
                        return(VSConstants.S_OK);
                    }

                    // Create the module id for this document.
                    ModuleId docId = new ModuleId(hierarchy, itemId);

                    // Try to get the text buffer.
                    IVsTextLines buffer = Marshal.GetObjectForIUnknown(unkDocData) as IVsTextLines;

                    // Create the listener.
                    TextLineEventListener listener = new TextLineEventListener(buffer, documentMoniker, docId);
                    // Set the event handler for the change event. Note that there is no difference
                    // between the AddFile and FileChanged operation, so we can use the same handler.
                    listener.OnFileChanged += new EventHandler <HierarchyEventArgs>(OnNewFile);
                    // Add the listener to the dictionary, so we will not create it anymore.
                    documents.Add(docCookie, listener);
                } finally {
                    if (IntPtr.Zero != unkDocData)
                    {
                        Marshal.Release(unkDocData);
                    }
                }
            }
            // Always return success.
            return(VSConstants.S_OK);
        }
Example #3
0
        private void CreateModuleTree(LibraryNode root, LibraryNode current, ScopeNode scope, string namePrefix, ModuleId moduleId)
        {
            if ((null == root) || (null == scope) || (null == scope.NestedScopes))
            {
                return;
            }
            foreach (ScopeNode subItem in scope.NestedScopes)
            {
                PythonLibraryNode newNode       = new PythonLibraryNode(subItem, namePrefix, moduleId.Hierarchy, moduleId.ItemID, provider);
                string            newNamePrefix = namePrefix;

                // The classes are always added to the root node, the functions to the
                // current node.
                if ((newNode.NodeType & LibraryNode.LibraryNodeType.Members) != LibraryNode.LibraryNodeType.None)
                {
                    current.AddNode(newNode);
                }
                else if ((newNode.NodeType & LibraryNode.LibraryNodeType.Classes) != LibraryNode.LibraryNodeType.None)
                {
                    // Classes are always added to the root.
                    root.AddNode(newNode);
                    newNamePrefix = newNode.Name + ".";
                }

                // Now use recursion to get the other types.
                CreateModuleTree(root, newNode, subItem, newNamePrefix, moduleId);
            }
        }
 private void OnDeleteFile(object sender, HierarchyEventArgs args)
 {
     IVsHierarchy hierarchy = sender as IVsHierarchy;
     if (null == hierarchy) {
         return;
     }
     ModuleId id = new ModuleId(hierarchy, args.ItemID);
     LibraryNode node = null;
     lock (files) {
         if (files.TryGetValue(id, out node)) {
             files.Remove(id);
         }
     }
     if (null != node) {
         library.RemoveNode(node);
     }
 }
 private void CreateParseRequest(string file, string text, ModuleId id)
 {
     LibraryTask task = new LibraryTask(file, text);
     task.ModuleID = id;
     lock (requests) {
         requests.Enqueue(task);
     }
     requestPresent.Set();
 }
        private void CreateModuleTree(LibraryNode root, LibraryNode current, ScopeNode scope, string namePrefix, ModuleId moduleId)
        {
            if ((null == root) || (null == scope) || (null == scope.NestedScopes)) {
                return;
            }
            foreach (ScopeNode subItem in scope.NestedScopes) {
                PythonLibraryNode newNode = new PythonLibraryNode(subItem, namePrefix, moduleId.Hierarchy, moduleId.ItemID, provider);
                string newNamePrefix = namePrefix;

                // The classes are always added to the root node, the functions to the
                // current node.
                if ((newNode.NodeType & LibraryNode.LibraryNodeType.Members) != LibraryNode.LibraryNodeType.None) {
                    current.AddNode(newNode);
                } else if ((newNode.NodeType & LibraryNode.LibraryNodeType.Classes) != LibraryNode.LibraryNodeType.None) {
                    // Classes are always added to the root.
                    root.AddNode(newNode);
                    newNamePrefix = newNode.Name + ".";
                }

                // Now use recursion to get the other types.
                CreateModuleTree(root, newNode, subItem, newNamePrefix, moduleId);
            }
        }
 public void UnregisterHierarchy(IVsHierarchy hierarchy)
 {
     if ((null == hierarchy) || !hierarchies.ContainsKey(hierarchy)) {
         return;
     }
     HierarchyListener listener = hierarchies[hierarchy];
     if (null != listener) {
         listener.Dispose();
     }
     hierarchies.Remove(hierarchy);
     if (0 == hierarchies.Count) {
         UnregisterRDTEvents();
     }
     lock (files) {
         ModuleId[] keys = new ModuleId[files.Keys.Count];
         files.Keys.CopyTo(keys, 0);
         foreach (ModuleId id in keys) {
             if (hierarchy.Equals(id.Hierarchy)) {
                 library.RemoveNode(files[id]);
                 files.Remove(id);
             }
         }
     }
     // Remove the document listeners.
     uint[] docKeys = new uint[documents.Keys.Count];
     documents.Keys.CopyTo(docKeys, 0);
     foreach (uint id in docKeys) {
         TextLineEventListener docListener = documents[id];
         if (hierarchy.Equals(docListener.FileID.Hierarchy)) {
             documents.Remove(id);
             docListener.Dispose();
         }
     }
 }
        public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
        {
            // Check if this document is in the list of the documents.
            if (documents.ContainsKey(docCookie)) {
                return VSConstants.S_OK;
            }
            // Get the information about this document from the RDT.
            IVsRunningDocumentTable rdt = provider.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;
            if (null != rdt) {
                // Note that here we don't want to throw in case of error.
                uint flags;
                uint readLocks;
                uint writeLoks;
                string documentMoniker;
                IVsHierarchy hierarchy;
                uint itemId;
                IntPtr unkDocData;
                int hr = rdt.GetDocumentInfo(docCookie, out flags, out readLocks, out writeLoks,
                                             out documentMoniker, out hierarchy, out itemId, out unkDocData);
                try {
                    if (Microsoft.VisualStudio.ErrorHandler.Failed(hr) || (IntPtr.Zero == unkDocData)) {
                        return VSConstants.S_OK;
                    }
                    // Check if the herarchy is one of the hierarchies this service is monitoring.
                    if (!hierarchies.ContainsKey(hierarchy)) {
                        // This hierarchy is not monitored, we can exit now.
                        return VSConstants.S_OK;
                    }

                    // Check the extension of the file to see if a listener is required.
                    string extension = System.IO.Path.GetExtension(documentMoniker);
                    if (0 != string.Compare(extension, IPyConstants.pythonFileExtension, StringComparison.OrdinalIgnoreCase)) {
                        return VSConstants.S_OK;
                    }

                    // Create the module id for this document.
                    ModuleId docId = new ModuleId(hierarchy, itemId);

                    // Try to get the text buffer.
                    IVsTextLines buffer = Marshal.GetObjectForIUnknown(unkDocData) as IVsTextLines;

                    // Create the listener.
                    TextLineEventListener listener = new TextLineEventListener(buffer, documentMoniker, docId);
                    // Set the event handler for the change event. Note that there is no difference
                    // between the AddFile and FileChanged operation, so we can use the same handler.
                    listener.OnFileChanged += new EventHandler<HierarchyEventArgs>(OnNewFile);
                    // Add the listener to the dictionary, so we will not create it anymore.
                    documents.Add(docCookie, listener);
                } finally {
                    if (IntPtr.Zero != unkDocData) {
                        Marshal.Release(unkDocData);
                    }
                }
            }
            // Always return success.
            return VSConstants.S_OK;
        }
        public void Dispose()
        {
            if ((null != connectionPoint) && (0 != connectionCookie)) {
                connectionPoint.Unadvise(connectionCookie);
                System.Diagnostics.Debug.WriteLine("\n\tUnadvised from TextLinesEvents\n");
            }
            connectionCookie = 0;
            connectionPoint = null;

            this.buffer = null;
            this.fileId = null;
        }