コード例 #1
0
 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);
 }
コード例 #2
0
        public void CommentSelectedText()
        {
            TextSelection textSelection = ActiveTextSelection;

            if (textSelection != null)
            {
                //editpoint,virsualpoint 都继承于TextPoint
                EditPoint topEditPoint = textSelection.TopPoint.CreateEditPoint();
                TextPoint bottomPoint  = textSelection.BottomPoint;

                UndoContext undoContext = ApplicationObject.UndoContext;
                undoContext.Open("Comment Region");
                while (topEditPoint.LessThan(bottomPoint))
                {
                    topEditPoint.Insert("//");
                    topEditPoint.LineDown();
                    topEditPoint.StartOfLine();
                }
                undoContext.Close();
            }
        }
コード例 #3
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);
        }
コード例 #4
0
        public static void Execute(ProjectItem projectItem)
        {
            var objTextDoc = (TextDocument)projectItem.Document.Object("TextDocument");

            EditPoint editPoint = objTextDoc.StartPoint.CreateEditPoint();

            editPoint.LineDown();

            bool doEncountered = false;

            while (editPoint.LessThan(objTextDoc.EndPoint))
            {
                editPoint.StartOfLine();
                var start = editPoint.CreateEditPoint();

                string previousLine = start.GetPreviousLineText();
                string line         = start.GetLineText();
                string nextLine     = start.GetNextLineText();

                if (line.Trim() == "do")
                {
                    doEncountered = true;
                }

                if (line.Trim().StartsWith("namespace ") && previousLine.EndsWith("</copyright>"))
                {
                    start.StartOfLine();
                    start.Insert(Environment.NewLine);
                    continue;
                }

                if (line.Trim().StartsWith("using ") &&
                    !line.Trim().StartsWith("using (") &&
                    !nextLine.Trim().StartsWith("using ") &&
                    !string.IsNullOrWhiteSpace(nextLine))
                {
                    start.EndOfLine();
                    start.Insert(Environment.NewLine);
                    continue;
                }

                //if ((line.Trim() == "return" || line.TrimStart().StartsWith("return "))
                //	&& !string.IsNullOrWhiteSpace(previousLine)
                //	&& previousLine.Trim() != "{"
                //	&& !previousLine.Trim().StartsWith("case ")
                //	&& !previousLine.Trim().StartsWith("default:")
                //	&& !previousLine.Trim().StartsWith(@"// "))
                //{
                //	start.StartOfLine();
                //	start.Insert(Environment.NewLine);
                //	continue;
                //}

                if (line.Trim() == "}" &&
                    !nextLine.Trim().StartsWith("else") &&
                    nextLine.Trim() != "}" && nextLine.Trim() != "};" && nextLine.Trim() != "});" && nextLine.Trim() != "}," &&
                    nextLine.Trim() != "finally" && nextLine.Trim() != "#endif" &&
                    !nextLine.Trim().StartsWith("catch (") && !nextLine.Trim().StartsWith("#endregion") && nextLine.Trim() != ("catch") &&
                    !(nextLine.Trim().StartsWith("while (") && doEncountered) &&
                    !string.IsNullOrWhiteSpace(nextLine))
                {
                    start.EndOfLine();
                    start.Insert(Environment.NewLine);
                    continue;
                }

                if ((string.IsNullOrWhiteSpace(line) && string.IsNullOrWhiteSpace(previousLine)) ||
                    (string.IsNullOrWhiteSpace(line) && previousLine.Trim() == "{") ||
                    (string.IsNullOrWhiteSpace(line) && previousLine.TrimStart().StartsWith("using ") && nextLine.TrimStart().StartsWith("using ")))
                {
                    start.DeleteCurrentLine();
                    continue;
                }

                if ((line.Trim() == "}" || line.Trim() == "{") && string.IsNullOrWhiteSpace(previousLine))
                {
                    start.DeletePreviousLine();
                    continue;
                }

                editPoint.LineDown();
            }
        }