Index: VCCMHelper.cs
===================================================================
--- VCCMHelper.cs	(revision 133)
+++ VCCMHelper.cs	(working copy)
@@ -18,6 +18,30 @@
 			public VCCodeElement _hitElem; 
 		}
 
+        internal sealed class ElemPosition
+        {
+            public TextPoint _stp;
+            public TextPoint _etp;
+
+            // override object.Equals
+            public override bool Equals (object obj)
+            {
+                if (obj == null || GetType() != obj.GetType()) 
+                {
+                    return false;
+                }
+
+                ElemPosition pos = obj as ElemPosition;
+                return (this._stp.EqualTo(pos._stp) && this._etp.EqualTo(pos._etp)); 
+            }
+    
+            // override object.GetHashCode
+            public override int GetHashCode()
+            {
+                return ((((((37 + this._stp.Line) * 37 + this._stp.DisplayColumn) * 37) + this._etp.Line) * 37) + this._etp.DisplayColumn) * 37;
+            }
+        }
+
 		/// <summary>
 		/// Search the input document code model for code elements
 		/// </summary>
@@ -57,41 +81,84 @@
 		/// </remarks>
 		static private void RecursiveCollectCodeElements(VCCodeElements ceColl,
 			string thisDoc,ArrayList hitColl,TextPoint cursorTP) 
-		{ 
-			foreach (VCCodeElement curElem in ceColl) 
-			{ 
-				TextPoint stp; 
-				TextPoint etp; 
-				bool useDecl = false;
-				vsCMWhere where = vsCMWhere.vsCMWhereDefault;
-				if (curElem.File.ToLower() != thisDoc.ToLower()) 
-				{
-					// Element is not defined in the active file- it is probably
-					// the definition of a function or static variable.
-					if (curElem.get_Location(vsCMWhere.vsCMWhereDeclaration).ToLower() ==
-						thisDoc.ToLower())
-					{
-						where = vsCMWhere.vsCMWhereDeclaration;
-						useDecl = true;
-					}
-					else
-					{
-						// Should never happen
-						continue;
-					}
-				} 
+		{
+            Hashtable ht = new Hashtable();
+            
+            foreach (VCCodeElement curElem in ceColl)
+            {
+                ElemContainer cont = new ElemContainer();
+                cont._hitElem = curElem;
+                cont._useDecl = false;
+                ElemPosition pos = new ElemPosition();
+                vsCMWhere where = vsCMWhere.vsCMWhereDefault;
+                if (curElem.File.ToLower() != thisDoc.ToLower())
+                {
+                    // Element is not defined in the active file- it is probably
+                    // the definition of a function or static variable.
+                    if (curElem.get_Location(vsCMWhere.vsCMWhereDeclaration).ToLower() ==
+                        thisDoc.ToLower())
+                    {
+                        where = vsCMWhere.vsCMWhereDeclaration;
+                        cont._useDecl = true;
+                    }
+                    else
+                    {
+                        // Should never happen
+                        continue;
+                    }
+                }
+                else
+                {
+                    // Element is both declared and defined in the active file.
+                    if (curElem.get_Location(vsCMWhere.vsCMWhereDeclaration).ToLower() == thisDoc.ToLower() &&
+                        curElem.get_Location(vsCMWhere.vsCMWhereDefinition).ToLower() ==
+                        curElem.get_Location(vsCMWhere.vsCMWhereDeclaration).ToLower())
+                    {
+                        cont._useDecl = true;
+                        where = vsCMWhere.vsCMWhereDeclaration;
+                        try
+                        {
+                            pos._stp = curElem.get_StartPointOf(vsCMPart.vsCMPartWhole, where);
+                            pos._etp = curElem.get_EndPointOf(vsCMPart.vsCMPartWhole, where);
+                            ht[pos] = cont;
+                        }
+                        catch (Exception)
+                        {
+                            // Failed to get start or end point - skip element
+                            continue;
+                        }
+                        cont = new ElemContainer();
+                        cont._hitElem = curElem;
+                        cont._useDecl = false;
+                        pos = new ElemPosition();
+                        where = vsCMWhere.vsCMWhereDefinition;
+                    }
+                }
 
-				try
-				{
-					stp = curElem.get_StartPointOf(vsCMPart.vsCMPartWhole, where);
-					etp = curElem.get_EndPointOf(vsCMPart.vsCMPartWhole, where);
-				}
-				catch (Exception)
-				{
-					// Failed to get start or end point - skip element
-					continue;
-				}
 
+                try
+                {
+                    pos._stp = curElem.get_StartPointOf(vsCMPart.vsCMPartWhole, where);
+                    pos._etp = curElem.get_EndPointOf(vsCMPart.vsCMPartWhole, where);
+                    ht[pos] = cont;
+                }
+                catch (Exception)
+                {
+                    // Failed to get start or end point - skip element
+                    continue;
+                }
+            }
+
+            foreach (DictionaryEntry entry in ht) 
+			{
+                ElemPosition pos = (ElemPosition)entry.Key;
+                TextPoint stp = pos._stp;
+                TextPoint etp = pos._etp;
+
+                ElemContainer cont = (ElemContainer)entry.Value;
+                VCCodeElement curElem = (VCCodeElement)cont._hitElem;
+                bool useDecl = cont._useDecl;
+
 				// Check if space between curElem's start and end points contain
 				// the cursor position
 				if (cursorTP.Line < stp.Line) 
@@ -117,9 +184,17 @@
 					// Cursor is at the same line as the end of the code
 					// element, but to the right of the end column.
 					continue; 
-				} 
+				}
 
-				// Since we got here cursor must be with the space occupied by
+                /*
+                System.Windows.Forms.MessageBox.Show("Code elements at cursor position (l:" +
+                    stp.Line.ToString() + ",c:" + stp.DisplayColumn.ToString() +
+                    ") in file '" + curElem.File + "':\r\n"
+                    + "Code elements at cursor position (l:" +
+                    etp.Line.ToString() + ",c:" + etp.DisplayColumn.ToString() +
+                    ") in file '" + curElem.File + "':");
+                */
+                // Since we got here cursor must be with the space occupied by
 				// the curElem.
 				ElemContainer newElem; 
 				newElem = new ElemContainer(); 
