Пример #1
0
        private CodeElement GetParentElementInCurrentFile(CodeElements waitCheckElements, CodeElement e, ref CodeElement result)
        {
            if (waitCheckElements.Count == 0)
            {
                return(null);
            }

            IEnumerator enumerator = waitCheckElements.GetEnumerator();

            while (enumerator.MoveNext())
            {
                CodeElement current = enumerator.Current as CodeElement;
                if (current == e)
                {
                    return(current);
                }

                //递归找,如果找到则直接返回
                CodeElement c = GetParentElementInCurrentFile(current.Children, e, ref result);
                if (c != null)
                {
                    //这里不能纯粹返回current父元素
                    //在第一次找个匹配元素的时候就要继续下其父元素,否则随着地递归的返回调用这个父元素
                    //会被往上层变
                    if (result == null)
                    {
                        //一旦result被复制就说明已经找到父元素,不会再重复赋值
                        result = current;
                    }
                    return(current);
                }
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// 得到同级别下指定元素前面的一个元素,如果指定元素是当前级别的第一个则返回自己
        /// 如果返回null很有可能当前codeelement不再当前文档中
        ///
        /// 1
        /// --2
        /// --3
        /// ----4
        /// ----5
        /// 6
        /// 7
        /// 例如,我们要找3的前面一个元素为2,2的前一个元素不存在
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        private CodeElement GetPrevElementInCurrenFile(CodeElements waitCheckElements, CodeElement element)
        {
            if (waitCheckElements.Count == 0)
            {
                return(null);
            }

            IEnumerator enumerator = waitCheckElements.GetEnumerator();

            enumerator.MoveNext();
            //同一级别下的第一个元素
            CodeElement firstElementInLevel = enumerator.Current as CodeElement;

            if (firstElementInLevel == element)
            {
                //如果要找元素是第一个元素,则返回自己
                return(firstElementInLevel);
            }
            else
            {
                //递归找第一个元素的子元素
                CodeElement c = GetPrevElementInCurrenFile(firstElementInLevel.Children, element);
                //如果返回值不为空,则说明找到了
                if (c != null)
                {
                    return(c);
                }
            }


            CodeElement prevElement = firstElementInLevel;

            while (enumerator.MoveNext())
            {
                CodeElement e = enumerator.Current as CodeElement;
                if (e == element)
                {
                    return(prevElement);
                }
                prevElement = e;

                //递归找,如果找到则直接返回
                CodeElement c = GetPrevElementInCurrenFile(e.Children, element);
                if (c != null)
                {
                    return(c);
                }
            }

            //没有前一项则返回空,很有可能当前codeelement不再当前文档中
            return(null);
        }
Пример #3
0
        void TraverseCodeElement(CodeElements elements,int indent)
        {
            var elementIter = elements.GetEnumerator();

            while (elementIter.MoveNext())
            {
                var element = elementIter.Current as CodeElement;
                if (element == null)
                {
                    continue;
                }
                var    eleName   = element.Name;
                var    start     = element.StartPoint;
                var    end       = element.EndPoint;
                var    startLine = start.Line;
                var    endLine   = end.Line;
                string indentStr = "";
                for (int i = 0; i < indent; i++)
                {
                    indentStr += "\t";
                }
                Logger.Debug(indentStr + string.Format("element:{0} {1} {2}",eleName,startLine,endLine));


                var children = element.Children;
                if (children != null)
                {
                    TraverseCodeElement(children,indent + 1);
                }

                //var kind = element.Kind;
                //var name = element.Name;
                //if (element.Kind == vsCMElement.vsCMElementNamespace)
                //{
                //    var ns = element as CodeNamespace;
                //    TraverseCodeElement(ns.Members, indent + 1);
                //}
            }
        }
Пример #4
0
 public IEnumerator GetEnumerator() => CodeElements.GetEnumerator();
Пример #5
0
        /// <summary>
        /// 得到同级别下指定元素前面的一个元素,如果指定元素是当前级别的第一个则返回自己
        /// 如果返回null很有可能当前codeelement不再当前文档中
        /// 
        /// 1
        /// --2
        /// --3
        /// ----4
        /// ----5
        /// 6
        /// 7
        /// 例如,我们要找3的前面一个元素为2,2的前一个元素不存在
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        private CodeElement GetPrevElementInCurrenFile(CodeElements waitCheckElements, CodeElement element)
        {
            if (waitCheckElements.Count == 0)
            {
                return null;
            }

            IEnumerator enumerator = waitCheckElements.GetEnumerator();
            enumerator.MoveNext();
            //同一级别下的第一个元素
            CodeElement firstElementInLevel = enumerator.Current as CodeElement;
            if (firstElementInLevel == element)
            {
                //如果要找元素是第一个元素,则返回自己
                return firstElementInLevel;
            }
            else
            {
                //递归找第一个元素的子元素
                CodeElement c = GetPrevElementInCurrenFile(firstElementInLevel.Children, element);
                //如果返回值不为空,则说明找到了
                if (c != null)
                {
                    return c;
                }
            }

            CodeElement prevElement = firstElementInLevel;
            while (enumerator.MoveNext())
            {
                CodeElement e = enumerator.Current as CodeElement;
                if (e == element)
                {
                    return prevElement;
                }
                prevElement = e;

                //递归找,如果找到则直接返回
                CodeElement c = GetPrevElementInCurrenFile(e.Children, element);
                if (c != null)
                {
                    return c;
                }
            }

            //没有前一项则返回空,很有可能当前codeelement不再当前文档中
            return null;
        }
Пример #6
0
        private CodeElement GetParentElementInCurrentFile(CodeElements waitCheckElements, CodeElement e, ref CodeElement result)
        {
            if (waitCheckElements.Count == 0)
            {
                return null;
            }

            IEnumerator enumerator = waitCheckElements.GetEnumerator();
            while (enumerator.MoveNext())
            {
                CodeElement current = enumerator.Current as CodeElement;
                if (current == e)
                {
                    return current;
                }

                //递归找,如果找到则直接返回
                CodeElement c = GetParentElementInCurrentFile(current.Children, e, ref result);
                if (c != null)
                {
                    //这里不能纯粹返回current父元素
                    //在第一次找个匹配元素的时候就要继续下其父元素,否则随着地递归的返回调用这个父元素
                    //会被往上层变
                    if (result == null)
                    {
                        //一旦result被复制就说明已经找到父元素,不会再重复赋值
                        result = current;
                    }
                    return current;
                }
            }

            return null;
        }