예제 #1
0
 public static string GetSetName(this CodeFunction codeFunction)
 {
     if (codeFunction.Parent is CodeProperty codeProperty)
     {
         if (codeFunction.EqualsOffset(codeProperty.Getter))
         {
             return("get");
         }
         else if (codeFunction.EqualsOffset(codeProperty.Setter))
         {
             return("set");
         }
         else
         {
             throw new Exception("不是get也不是set");
         }
     }
     return(null);
 }
예제 #2
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());
        }