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 ExistsCodeSite(this CodeFunction codeFunction)
 {
     try
     {
         codeFunction.GetStartPoint(vsCMPart.vsCMPartBody);
     }
     catch//表达式主体
     {
         return(false);
     }
     return(codeFunction.DeleteCodeSite(false));
 }