public static bool EqualsOffset(this CodeFunction codeFunction, CodeFunction other)
 {
     if (other == null || codeFunction.HasBody() != other.HasBody())
     {
         return(false);
     }
     if (codeFunction.HasBody())
     {
         return(codeFunction.GetStartPoint(vsCMPart.vsCMPartBody).AbsoluteCharOffset == other.GetStartPoint(vsCMPart.vsCMPartBody).AbsoluteCharOffset);
     }
     else
     {
         return(codeFunction.StartPoint.AbsoluteCharOffset == other.StartPoint.AbsoluteCharOffset);
     }
 }
        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());
        }
 public static bool HasExpressionBody(this CodeFunction codeFunction)
 {
     try
     {
         if (codeFunction.HasBody())//public string 只读表达式属性 => "只读表达式属性";
         {
             try { codeFunction.GetStartPoint(); } catch { return(true); }
         }
         else
         {
             EditPoint editPointFind = codeFunction.StartPoint.CreateEditPoint();
             if (editPointFind.FindPattern("=>") && editPointFind.LessThan(codeFunction.EndPoint))
             {
                 return(true);
             }
         }
     }
     catch { }
     return(false);
 }
Exemple #4
0
 public CodeElementViewModel(CodeElement codeElement)
 {
     CodeElement = codeElement;
     Name        = Guid.NewGuid().ToString();
     if (CodeElement == null)
     {
         return;
     }
     Name = CodeElement.Name;
     if (CodeElement.Kind == vsCMElement.vsCMElementProperty && CodeElement is CodeProperty codeProperty)
     {
         if (codeProperty.Getter != null)
         {
             LoadChild((CodeElement)codeProperty.Getter);
         }
         if (codeProperty.Setter != null)
         {
             LoadChild((CodeElement)codeProperty.Setter);
         }
     }
     else if (CodeElement.Children != null)
     {
         LoadChildren(CodeElement.Children);
     }
     if (IsCodeFunction)
     {
         if (CodeFunction.MustImplement || !(CodeFunction.HasBody() || CodeFunction.HasExpressionBody()))
         {
             CodeElement = null;
             return;//跳过抽象方法和没有主体的方法
         }
         Name           = CodeFunction.GetSetName() ?? Name;
         ExistsCodeSite = CodeFunction.ExistsCodeSite();
         //IsChecked = ExistsCodeSite;
     }
 }