Ejemplo n.º 1
0
        public static void AddCodeSite(this CodeFunction codeFunction)
        {
            if (codeFunction.HasExpressionBody())
            {//展开表达式主体为程序块主体,不做逆向处理
                var addReturn = !(codeFunction.Type.TypeKind == vsCMTypeRef.vsCMTypeRefVoid ||
                                  codeFunction.Parent is CodeProperty codeProperty && codeFunction.EqualsOffset(codeProperty.Setter));
                if (codeFunction.HasBody())
                {
                    var epFind = codeFunction.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                    var epEEnd = codeFunction.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                    epFind.FindPattern("=>", (int)vsFindOptions.vsFindOptionsBackwards);
                    epFind.Delete(2);
                    epFind.Insert("{get{");
                    if (addReturn)
                    {
                        epFind.Insert("return ");
                    }
                    epEEnd.CharRight();
                    epEEnd.Insert("}}");
                }
                else
                {
                    var epFind = codeFunction.GetStartPoint().CreateEditPoint();
                    var epEEnd = codeFunction.GetEndPoint().CreateEditPoint();
                    epFind.FindPattern("=>");
                    epFind.Delete(2);
                    epFind.Insert("{");
                    if (addReturn)
                    {
                        epFind.Insert("return ");
                    }
                    epEEnd.Insert("}");
                }
            }
            if (codeFunction.ExistsCodeSite())
            {
                codeFunction.DeleteCodeSite();
            }
            EditPoint epStart = codeFunction.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
            EditPoint epEnd   = codeFunction.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();

            if (epStart.Line == epEnd.Line)
            {
                epEnd.Insert(Environment.NewLine);
            }
            epStart.Insert(codeFunction.CSEnterText());
            if (Properties.Settings.Default.IncludeCatch)
            {
                epEnd.Insert(codeFunction.CSCatchText());
            }
            epEnd.Insert(codeFunction.CSExitText());

            //格式化指定范围内的文本
            codeFunction.StartPoint.CreateEditPoint().SmartFormat(codeFunction.EndPoint.CreateEditPoint());
        }
Ejemplo n.º 2
0
        public static bool DeleteCodeSite(this CodeFunction value, bool bDelete = true)
        {
            if (bDelete && !value.ExistsCodeSite())
            {
                return(true);
            }
            EditPoint epStart = value.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
            EditPoint epEnd = value.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
            EditPoint epFind = null, epFindStart = null;

            bool Find(string text, bool start)
            {
                string[] textSplit;
                if (start)
                {
                    epFind    = epStart.CreateEditPoint();
                    textSplit = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                }
                else
                {
                    epFind    = epEnd.CreateEditPoint();
                    textSplit = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Reverse().ToArray();
                }
                int iLine = 0;

                foreach (var item in textSplit)
                {
                    int    i = item.IndexOfAny(new char[] { '(', ' ' });
                    string s = i == -1 ? item : item.Substring(0, i + 1);

                    if ((start ? epFind.FindPattern(s) : epFind.FindPattern(s, (int)vsFindOptions.vsFindOptionsBackwards)) &&
                        (start ? epFind.LessThan(epEnd) : epFind.GreaterThan(epStart)) &&
                        (iLine == 0 || iLine == epFind.Line - (start ? 1 : -1)))
                    {
                        if (iLine == 0)
                        {
                            epFindStart = epFind.CreateEditPoint();
                        }
                        iLine = epFind.Line;
                        if (start)
                        {
                            epFind.EndOfLine();
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                return(true);
            }

            void Delete()
            {
                if (epFind.GreaterThan(epFindStart))
                {
                    epFind.LineDown(1);
                }
                else
                {
                    epFindStart.LineDown(1);
                }
                epFind.StartOfLine();
                epFindStart.StartOfLine();
                epFindStart.Delete(epFind);
            }

            if (!Find(value.CSEnterText(), true))
            {
                return(false);
            }
            else if (bDelete)
            {
                Delete();
            }

            if (!Find(value.CSExitText(), false))
            {
                return(false);
            }
            else if (bDelete)
            {
                Delete();
            }
            else
            {
                return(true);
            }

            if (Find(value.CSCatchText(), false) && bDelete)
            {
                Delete();
            }

            //格式化指定范围内的文本
            value.StartPoint.CreateEditPoint().SmartFormat(value.EndPoint);
            return(true);
        }