Esempio n. 1
0
 /// <summary>
 /// GetStartPoint can throw NotImplementedException. This will retry the start point without explicit attribute
 /// </summary>
 /// <param name="ce"></param>
 /// <param name="part"></param>
 /// <returns></returns>
 public static EnvDTE.TextPoint GetSafeStartPoint(this CodeElement ce, vsCMPart part = vsCMPart.vsCMPartWholeWithAttributes)
 {
     try {
         return(ce.GetStartPoint(part));
     }
     catch (NotImplementedException) {
         //Catch random notimplementedException
         return(ce.GetStartPoint());
     }
 }
Esempio n. 2
0
        public static bool ShowCodeElement(CodeElement codeElement)
        {
            if (codeElement == null)
            {
                throw new ArgumentNullException("codeElement");
            }

            //Move cursor to beginning of the symbol.
            //TODO: Find a better way ...
            Document document = codeElement.DTE.ActiveDocument;

            if (document == codeElement.ProjectItem.Document)
            {
                TextPoint textPoint = codeElement.GetStartPoint(vsCMPart.vsCMPartNavigate);

                TextSelection ts = (TextSelection)document.Selection;
                ts.MoveToPoint(textPoint, false);
                ts.ActivePoint.TryToShow(vsPaneShowHow.vsPaneShowCentered, null);


                //ts.SelectLine();
                //string text = ts.Text;
                //int pos = text.IndexOf(codeElement.Name) + 1;
                //ts.MoveTo(codeElement.StartPoint.Line, pos, false);
                //codeElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowCentered, null);
                return(true);
            }
            return(false);
        }
        /// <summary>
        /// Deletes the name space using statements.
        /// </summary>
        /// <param name="instance">The instance.</param>
        public static void DeleteNameSpaceUsingStatements(this ProjectItem instance)
        {
            bool continueLoop = true;

            do
            {
                CodeElement codeElement = instance.FileCodeModel.CodeElements.Item(1);

                if (codeElement != null)
                {
                    if (codeElement.Kind == vsCMElement.vsCMElementImportStmt)
                    {
                        EditPoint editPoint = codeElement.GetStartPoint().CreateEditPoint();
                        TextPoint textPoint = codeElement.GetEndPoint();

                        editPoint.Delete(textPoint);

                        //// should get rid of the blank line!
                        editPoint.Delete(1);
                    }
                    else
                    {
                        continueLoop = false;
                    }
                }
                else
                {
                    continueLoop = false;
                }
            }while (continueLoop);
        }
Esempio n. 4
0
        public string GetMethodText(vsCMPart part)
        {
            TextPoint startPoint = m_method.GetStartPoint();
            int       endLine    = m_method.GetEndPoint().Line;

            m_textSelection.MoveToPoint(startPoint);
            StringBuilder sb    = new StringBuilder();
            int           count = 99999;

            while (count-- != 0 && m_textSelection.CurrentLine != endLine)
            {
                m_textSelection.SelectLine();
                sb.Append(m_textSelection.Text);
            }
            return(sb.ToString());
        }
Esempio n. 5
0
        /// <summary>
        /// Adds missing access modifiers
        /// </summary>
        /// <param name="codeElement">The CodeElement to add missing access modifiers too. Includes children.</param>
        private void AddMissingAccessModifiers(CodeElement codeElement)
        {
            if (codeElement.Kind != vsCMElement.vsCMElementInterface && CodeElementBlockTypes.ContainsKey(codeElement.Kind))
            {
                for (int i = 1; i <= codeElement.Children.Count; i++)
                {
                    CodeElement element = codeElement.Children.Item(i) as CodeElement;

                    if (element.Kind != vsCMElement.vsCMElementImportStmt && element.Kind != vsCMElement.vsCMElementInterface && CodeElementBlockTypes.ContainsKey(codeElement.Kind))
                    {
                        // Get the element's access through reflection rather than a massive switch.
                        vsCMAccess access = (vsCMAccess)CodeElementTypes[element.Kind].InvokeMember("Access", BindingFlags.GetProperty, null, element, null);

                        if (element.Kind == vsCMElement.vsCMElementClass || element.Kind == vsCMElement.vsCMElementStruct)
                        {
                            AddMissingAccessModifiers(element);
                        }

                        EditPoint start;
                        EditPoint end;
                        string    declaration;

                        if (element.Kind == vsCMElement.vsCMElementFunction)
                        {
                            // method, constructor, or finalizer
                            CodeFunction2 function = element as CodeFunction2;

                            // Finalizers don't have access modifiers, neither do static constructors
                            if (function.FunctionKind == vsCMFunction.vsCMFunctionDestructor || (function.FunctionKind == vsCMFunction.vsCMFunctionConstructor && function.IsShared))
                            {
                                continue;
                            }
                        }

                        if (element.Kind == vsCMElement.vsCMElementProperty || element.Kind == vsCMElement.vsCMElementVariable || element.Kind == vsCMElement.vsCMElementEvent)
                        {
                            CodeElements attributes = (CodeElements)CodeElementTypes[element.Kind].InvokeMember("Attributes", BindingFlags.GetProperty, null, element, null);

                            start = attributes.Count > 0 ? element.GetEndPoint(vsCMPart.vsCMPartAttributesWithDelimiter).CreateEditPoint() : element.StartPoint.CreateEditPoint();
                        }
                        else
                        {
                            start = element.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint();
                        }

                        end = start.CreateEditPoint();
                        end.EndOfLine();
                        declaration = start.GetText(end);
                        if (!declaration.StartsWith(CodeAccessKeywords[access]) && !declaration.StartsWith("partial"))
                        {
                            object[] args = new object[1];
                            args.SetValue(access, 0);
                            CodeElementTypes[element.Kind].InvokeMember("Access", BindingFlags.SetProperty, null, element, args);
                        }

                        declaration = start.GetText(end);
                    }
                }
            }
        }
        private string ExtractMember(CodeElement element)
        {
            var memberStart = element.GetStartPoint().CreateEditPoint();
            var memberText  = string.Empty;

            memberText += memberStart.GetText(element.GetEndPoint());
            memberStart.Delete(element.GetEndPoint());
            return(memberText);
        }
        /// <summary>
        /// Refreshes the cached position and name fields on this item.
        /// </summary>
        /// <remarks>
        /// Similar to BaseCodeItemElement's implementation, except ignores the Name property which
        /// is not available for using statements.
        /// </remarks>
        public override void RefreshCachedPositionAndName()
        {
            var startPoint = CodeElement.GetStartPoint();
            var endPoint   = CodeElement.GetEndPoint();

            StartLine   = startPoint.Line;
            StartOffset = startPoint.AbsoluteCharOffset;
            EndLine     = endPoint.Line;
            EndOffset   = endPoint.AbsoluteCharOffset;
        }
        /// <summary>
        /// Replaces a code element with the supplied source code, which should be valid code in the target project's language.
        /// </summary>
        public static void ReplaceWithSourceCode(this CodeElement codeElement, string sourceCode, vsEPReplaceTextOptions replaceTextOptions = vsEPReplaceTextOptions.vsEPReplaceTextAutoformat)
        {
            if (codeElement == null)
            {
                throw new ArgumentNullException("codeElement");
            }
            var startPoint = codeElement.GetStartPoint();

            startPoint.CreateEditPoint().ReplaceText(codeElement.GetEndPoint(), IndentAllButFirstLine(sourceCode ?? string.Empty, startPoint.LineCharOffset - 1), (int)replaceTextOptions);
        }
Esempio n. 9
0
        /// <summary>
        /// Removes whitespace immediately inside a block
        /// </summary>
        /// <param name="element">The current code element</param>
        private void RemoveInternalBlockPadding(CodeElement element)
        {
            EditPoint start = element.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
            EditPoint end   = element.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();

            end.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);
            end.CharLeft(1);

            start.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);
            end.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);
        }
Esempio n. 10
0
        private static string getText(CodeElement el, vsCMPart part)
        {
            if (!el.ProjectItem.IsOpen)
            {
                el.ProjectItem.Open();
            }

            var editPoint = el.GetStartPoint(part).CreateEditPoint();

            return(editPoint.GetText(el.EndPoint).Replace("\r", ""));
        }
        public static EditPoint AtTheFirstLineAfterTheOpeningBrakect(this CodeElement codeElement)
        {
            var editPoint = codeElement.GetStartPoint().CreateEditPoint();

            while (editPoint.GetLineText().Trim() != "{")
            {
                editPoint.LineDown();
            }

            editPoint.StartOfLine();
            return(editPoint);
        }
Esempio n. 12
0
        public static string GetDocComment(this CodeElement ce)
        {
            var commentStart = ce.GetCommentStartPoint();

            if (commentStart == null)
            {
                return("");
            }

            var comment = commentStart.GetText(ce.GetStartPoint());

            return(comment);
        }
        public static string GetDocComment(this CodeElement ce)
        {
            var commentStart = ce.GetCommentStartPoint();

            if (commentStart == null)
            {
                return("");
            }

            var comment = commentStart.GetText(ce.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes));

            return(comment);
        }
Esempio n. 14
0
        string UpdateBodyCode(CodeUIItem item,CodeElement codeElement)
        {
            if (codeElement == null || item == null || !item.IsFunction())
            {
                return("");
            }
            var    funcStart   = codeElement.GetStartPoint(vsCMPart.vsCMPartBody);
            var    funcEnd     = codeElement.GetEndPoint(vsCMPart.vsCMPartBody);
            var    funcEditPnt = funcStart.CreateEditPoint();
            string funcText    = funcEditPnt.GetText(funcEnd).Replace("\r\n","\n");

            item.m_bodyCode = funcText;
            return(funcText);
        }
Esempio n. 15
0
        /// <summary>
        /// Process navigating at offset on given element.
        /// </summary>
        /// <param name="element">Element which is base for offset navigation.</param>
        /// <param name="navigationOffset">Offset where user will be navigated to.</param>
        protected void Navigate(CodeElement element, int navigationOffset)
        {
            if (!(element is CodeFunction))
            {
                navigationOffset = 0;
            }

            element.ProjectItem.Open();
            var doc = element.ProjectItem.Document;

            if (doc == null)
            {
                //document is unavailable
                return;
            }
            //activate document to get it visible to user.
            doc.Activate();

            //part of CodeElement where navigation offset start.
            vsCMPart part;

            if (element.Kind == vsCMElement.vsCMElementFunction)
            {
                //functions are navigated into body
                part = vsCMPart.vsCMPartBody;
            }
            else
            {
                //else navigate from element begining
                part = vsCMPart.vsCMPartWholeWithAttributes;
            }

            TextPoint start;

            try
            {
                start = element.GetStartPoint(part);
            }
            catch (Exception)
            {
                //cannot navigate
                return;
            }

            var sel = doc.Selection as TextSelection;

            //Shift cursor at navigation position.
            sel.MoveToAbsoluteOffset(start.AbsoluteCharOffset + navigationOffset - 1);
        }
        /// <summary>Parses for strings by iterating through the text lines.</summary>
        /// <param name="element">The element.</param>
        /// <param name="stringResources">The string resources.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="isCSharp">If set to <c>true</c> it is CSharp code.</param>
        /// <param name="startLine">The start line.</param>
        /// <param name="endLine">The end line.</param>
        /// <returns>The last parsed line number or -1.</returns>
        private int ParseForStrings(CodeElement element,
                                    List <StringResource> stringResources,
                                    ISettings settings,
                                    bool isCSharp,
                                    int startLine,
                                    int endLine)
        {
            TextPoint startPoint = element.StartPoint;
            TextPoint endPoint   = element.EndPoint;

            try
            {
                if (element.Kind == vsCMElement.vsCMElementFunction)
                {
                    try
                    {
                        //we want to have the body only (throws COMException when inspecting an expression bodied function)
                        startPoint = element.GetStartPoint(vsCMPart.vsCMPartBody);
                        endPoint   = element.GetEndPoint(vsCMPart.vsCMPartBody);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.Print("ParseForStrings(vsCMElementFunction, line {0}): {1} - {2}", startPoint.Line, ex.GetType().Name, ex.Message);
                    }
                }

                EditPoint2 editPoint = startPoint.CreateEditPoint() as EditPoint2;

                int    editLine   = editPoint.Line;
                int    editColumn = editPoint.LineCharOffset;
                string text       = editPoint.GetText(endPoint);

                ICodeTools             codeTools        = ViewModelLocator.Instance.GetInstance <ICodeTools>();
                List <CodeTextElement> codeTextelements = codeTools.GetCodeElements(editLine, text);
                List <CodeTextLine>    textLines        = codeTools.GetFilteredLines(
                    codeTextelements, settings);

                bool isComment = false;

                FindStringsInCodeBlock(
                    textLines, editColumn, startLine, endLine, stringResources, settings, ref isComment);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print("### Error: ParseForStrings(): {0} - {1}", ex.GetType().Name, ex.Message);
            }

            return(endPoint?.Line ?? (-1));
        }
        /// <inheritdoc />
        protected override CodeLocation GetMemberSourceLocation(StaticMemberWrapper member)
        {
            CodeElement memberHandle = (CodeElement)member.Handle;
            ProjectItem projectItem  = memberHandle.ProjectItem;

            if (projectItem.FileCount == 0)
            {
                return(CodeLocation.Unknown);
            }

            string    filename = projectItem.get_FileNames(0);
            TextPoint point    = memberHandle.GetStartPoint(vsCMPart.vsCMPartName);

            return(new CodeLocation(filename, point.Line, point.LineCharOffset + 1));
        }
Esempio n. 18
0
        /// <summary>
        /// Checks to see if there should be additional blank lines after the end of a block.
        /// </summary>
        /// <param name="element">The current element to check</param>
        private void CheckBlockEnd(CodeElement element)
        {
            EditPoint endBlock   = element.GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint();
            EditPoint startBlock = element.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint();
            string    original   = startBlock.GetText(endBlock);

            EditPoint endOfEnd = endBlock.CreateEditPoint();

            endOfEnd.EndOfLine();
            string endOfBlockLine = endBlock.GetText(endOfEnd).Trim();

            if (element.Kind == vsCMElement.vsCMElementAttribute || element.Kind == vsCMElement.vsCMElementOther)
            {
                if (endOfBlockLine != "]" && endOfBlockLine != ")]" && !endOfBlockLine.StartsWith(","))
                {
                    endOfEnd = endBlock.CreateEditPoint();
                    endOfEnd.EndOfLine();
                    endOfBlockLine = endBlock.GetText(endOfEnd).Trim();
                    if (endOfBlockLine != string.Empty)
                    {
                        endBlock.Insert(Environment.NewLine);
                        endBlock.SmartFormat(endOfEnd);
                    }
                }
            }
            else if (endOfBlockLine != string.Empty)
            {
                endBlock.Insert(Environment.NewLine);
                endBlock.SmartFormat(endOfEnd);
            }

            if (element.Kind != vsCMElement.vsCMElementImportStmt &&
                element.Kind != vsCMElement.vsCMElementAttribute &&
                element.Kind != vsCMElement.vsCMElementOther)
            {
                endOfEnd.LineDown(1);
                endOfEnd.EndOfLine();
                string lineAfterBlock = endBlock.GetText(endOfEnd).Trim();

                if (lineAfterBlock != string.Empty && !lineAfterBlock.StartsWith("else") && !lineAfterBlock.StartsWith("}"))
                {
                    endBlock.Insert(Environment.NewLine);
                    endBlock.SmartFormat(endOfEnd);
                }
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Refresh Start,End offsets
        /// return true, if offsets differs from stored ones, else return false.
        /// </summary>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        private bool refreshOffset()
        {
            vsCMPart partS, partE;
            int      nStart, nEnd;

            try
            {
                switch (Element.Kind)
                {
                case vsCMElement.vsCMElementFunction:
                    partS = vsCMPart.vsCMPartBody;
                    partE = vsCMPart.vsCMPartWholeWithAttributes;    //to get whole end

                    var f2 = Element as CodeFunction2;
                    if (f2.MustImplement)
                    {
                        return(false);                      //doesn't have body
                    }
                    break;

                default:
                    partE = partS = vsCMPart.vsCMPartWholeWithAttributes;
                    break;
                }

                nStart = Element.GetStartPoint(partS).AbsoluteCharOffset;
                nEnd   = Element.GetEndPoint(partE).AbsoluteCharOffset;
            }
            catch
            {
                nEnd = nStart = Start + 1; //because of returning true, until position will be available
                _owner.VS.Log.Warning("Cannot resolve position of {0}", Element.FullName);
            }

            if (nStart != Start || nEnd != End)
            {
                Start = nStart;
                End   = nEnd;
                return(true);
            }
            return(false);
        }
Esempio n. 20
0
        /// <summary>
        /// Checks to see if there should be additional blank lines immediately after starting a block
        /// </summary>
        /// <param name="element">The current element to check</param>
        private void CheckBlockStart(CodeElement element)
        {
            EditPoint start = element.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
            EditPoint end   = element.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();

            EditPoint beginStart = start.CreateEditPoint();

            beginStart.StartOfLine();

            string beginningStartText = beginStart.GetText(start).Trim();

            if (beginningStartText != string.Empty)
            {
                EditPoint endStart = start.CreateEditPoint();
                endStart.EndOfLine();
                string restofStartText = start.GetText(endStart).Trim();
                if (!restofStartText.StartsWith("get;"))
                {
                    start.Insert(Environment.NewLine);
                    start.SmartFormat(endStart);
                }
            }
        }
 public static string InnerText(this CodeElement codeElement)
 {
     return(codeElement.GetStartPoint().CreateEditPoint().GetText(codeElement.EndPoint));
 }
Esempio n. 22
0
        /// <summary>
        /// Inserts the method call in global ASAX file.
        /// </summary>
        /// <param name="selProject">The sel project.</param>
        internal static void InsertMethodCallInGlobalASAXFile(Project selProject)
        {
            ProjectItem fileProjectItem;
            bool        registerRootsFound         = false;
            bool        registerGlobalFiltersFound = false;

            if (Common.IsProjectItemExist(selProject.ProjectItems, "Global.asax", out fileProjectItem))
            {
                CodeElement   registerRoutes        = null;
                CodeElement   registerGlobalFilters = null;
                FileCodeModel codeModel             = fileProjectItem.ProjectItems.Item(1).FileCodeModel;
                foreach (CodeElement codeElement in codeModel.CodeElements)
                {
                    if (codeElement.Kind == vsCMElement.vsCMElementNamespace)
                    {
                        foreach (CodeElement namespacechildElement in codeElement.Children)
                        {
                            if (namespacechildElement.Kind == vsCMElement.vsCMElementClass)
                            {
                                foreach (CodeElement functionElement in namespacechildElement.Children)
                                {
                                    if (functionElement.Kind == vsCMElement.vsCMElementFunction &&
                                        (functionElement.Name == "RegisterRoutes" ||
                                         functionElement.Name == "RegisterGlobalFilters"))
                                    {
                                        if (functionElement.Name == "RegisterRoutes")
                                        {
                                            registerRoutes     = functionElement;
                                            registerRootsFound = true;
                                        }

                                        if (functionElement.Name == "RegisterGlobalFilters")
                                        {
                                            registerGlobalFilters      = functionElement;
                                            registerGlobalFiltersFound = true;
                                        }

                                        if (registerRootsFound && registerGlobalFiltersFound)
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (registerRoutes != null)
                {
                    EditPoint startEp = registerRoutes.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                    if (!startEp.FindPattern("IntuitRegisterRoutes.RegisterIntuitAnywhereRoutes(routes);"))
                    {
                        EditPoint endEP = registerRoutes.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                        endEP.Insert(Environment.NewLine + Environment.NewLine +
                                     "IntuitRegisterRoutes.RegisterIntuitAnywhereRoutes(routes);");
                    }
                }

                if (registerGlobalFilters != null)
                {
                    EditPoint startEp = registerGlobalFilters.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                    if (!startEp.FindPattern("filters.Add(new IntuitSampleMVC.utils.IppTag());"))
                    {
                        EditPoint endEP = registerGlobalFilters.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                        endEP.Insert(Environment.NewLine + Environment.NewLine +
                                     "filters.Add(new IntuitSampleMVC.utils.IppTag());");
                    }
                }
            }
        }
        /// <summary>Parses for strings by iterating through the text lines.</summary>
        /// <param name="element">The element.</param>
        /// <param name="stringResources">The string resources.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="isCSharp">If set to <c>true</c> it is CSharp code.</param>
        /// <param name="startLine">The start line.</param>
        /// <param name="endLine">The end line.</param>
        /// <returns>The last parsed line number or -1.</returns>
        private static int ParseForStrings(CodeElement element,
                                           List <StringResource> stringResources,
                                           Settings settings,
                                           bool isCSharp,
                                           int startLine,
                                           int endLine)
        {
            TextPoint startPoint = element.StartPoint,
                      endPoint   = element.EndPoint;
            EditPoint2 editPoint = null;

            try
            {
                if (element.Kind == vsCMElement.vsCMElementFunction)
                {
#if DEBUG_OUTPUT
                    System.Diagnostics.Debug.Print("    function kind: {0}", (element as CodeFunction).FunctionKind);
#endif

                    try
                    {
                        //we want to have the body only (throws COMException when inspecting an expression bodied function)
                        startPoint = element.GetStartPoint(vsCMPart.vsCMPartBody);
                        endPoint   = element.GetEndPoint(vsCMPart.vsCMPartBody);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.Print("ParseForStrings(vsCMElementFunction, line {0}): {1} - {2}", startPoint.Line, ex.GetType().Name, ex.Message);
                    }
                }

                editPoint = startPoint.CreateEditPoint() as EditPoint2;

                int editLine       = editPoint.Line,
                    editColumn     = editPoint.LineCharOffset;
                string   text      = editPoint.GetText(endPoint);
                string[] txtLines  = text.Replace("\r", string.Empty).Split('\n');
                bool     isComment = false;
#if IGNORE_METHOD_ARGUMENTS
                bool isIgnoreMethodArguments = false;
#endif

                foreach (string txtLine in txtLines)
                {
                    if ((editLine >= startLine) && (editLine <= endLine))
                    {
                        //this is a changed text line in the block

#if !IGNORE_METHOD_ARGUMENTS
                        if (txtLine.Contains("\""))
                        {
                            ParseForStrings(txtLine, editLine, editColumn, stringResources, settings, isCSharp, ref isComment);
                        }
#else
                        if (line.Contains("\""))
                        {
                            ParseForStrings(line, editLine, editColumn, stringResources, settings, ref isComment, ref isIgnoreMethodArguments);
                        }
#endif
                    }

                    ++editLine;

                    //only for the first line of the text block LineCharOffset will be used
                    if (editColumn > 1)
                    {
                        editColumn = 1;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print("### Error: ParseForStrings(): {0} - {1}", ex.GetType().Name, ex.Message);
            }

            return(endPoint?.Line ?? (-1));
        }
Esempio n. 24
0
 public TextPoint GetStartPoint(vsCMPart Part)
 {
     return(_element.GetStartPoint(Part));
 }
        private TextPoint getStartPoint(CodeElement ce)
        {
            try
            {
                if (ce.Kind == vsCMElement.vsCMElementFunction || ce.Kind == vsCMElement.vsCMElementClass)
                {
                    return ce.GetStartPoint(vsCMPart.vsCMPartBody);

                }
            }
            catch
            {
                _log.Debug("Failed to rtrieve body part of " + ce.Name + ". Reverting to start of element");
            }

            return ce.StartPoint ;
        }
        private int getStartPosition(CodeElement ce)
        {
            var ret = ce.StartPoint.AbsoluteCharOffset;
            var retline = ce.StartPoint.Line;

            if (ce.Kind == vsCMElement.vsCMElementFunction || ce.Kind == vsCMElement.vsCMElementClass)
            {
                var startPoint = ce.GetStartPoint(vsCMPart.vsCMPartBody);
                var ret2 =  startPoint.AbsoluteCharOffset;
                var ret2Line = startPoint.Line;
                return ret2;
            }

            return ret;
        }
Esempio n. 27
0
        /// <summary>
        /// Sorts functions within a class.
        /// </summary>
        /// <param name="codeElement">The code element that represents the class.</param>
        private void SortFunctionsWithinClass(CodeElement codeElement)
        {
            EditPoint  classPoint = codeElement.StartPoint.CreateEditPoint();
            TextRanges trs        = null;

            string classBackup = classPoint.GetText(codeElement.EndPoint);

            try
            {
                if (classPoint.FindPattern("{", (int)vsFindOptions.vsFindOptionsMatchCase, ref classPoint, ref trs))
                {
                    classPoint.Insert("\r\n");

                    List <CodeBlock> blocks       = new List <CodeBlock>();
                    Array            accessLevels = Enum.GetValues(typeof(vsCMAccess));
                    for (int i = 1; i <= codeElement.Children.Count; i++)
                    {
                        CodeElement element = codeElement.Children.Item(i);
                        if (element.Kind != vsCMElement.vsCMElementAttribute)
                        {
                            EditPoint startBlock    = element.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint();
                            EditPoint newStartPoint = startBlock.CreateEditPoint();
                            CodeBlock block         = EvaluateBlock(codeElement, element, ref newStartPoint);

                            if (block != null)
                            {
                                blocks.Add(block);
                                newStartPoint.Delete(element.EndPoint);
                                newStartPoint.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);

                                i--;
                            }
                        }
                    }

                    blocks.Sort(delegate(CodeBlock c1, CodeBlock c2)
                    {
                        int comparison = 0;
                        if (c1.Placement != c2.Placement)
                        {
                            comparison = c1.Placement.CompareTo(c2.Placement);
                        }
                        else if (c1.Access != c2.Access)
                        {
                            comparison = c1.Access.CompareTo(c2.Access);
                        }
                        else if (c1.Name != c2.Name)
                        {
                            comparison = c1.Name.CompareTo(c2.Name);
                        }
                        else
                        {
                            comparison = c1.Weight.CompareTo(c2.Weight);
                        }

                        return(comparison);
                    });

                    classPoint.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);
                    classPoint.Insert("\r\n");

                    for (int i = 0; i < blocks.Count; i++)
                    {
                        classPoint.Insert(blocks[i].Body + "\r\n\r\n");
                    }

                    classPoint.LineUp(1);
                    classPoint.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);

                    for (int i = 1; i <= codeElement.Children.Count; i++)
                    {
                        CodeElement element = codeElement.Children.Item(i);
                        if (element.Kind == vsCMElement.vsCMElementClass || element.Kind == vsCMElement.vsCMElementInterface || element.Kind == vsCMElement.vsCMElementStruct)
                        {
                            SortFunctionsWithinClass(element);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.ToString());
                EditPoint startBackup = codeElement.StartPoint.CreateEditPoint();
                startBackup.Delete(codeElement.EndPoint);
                startBackup.Insert(classBackup);
                Debug.WriteLine("-- Class Reverted --");
            }
        }
Esempio n. 28
0
        /// <summary>
        /// Creates code model element.
        /// </summary>
        private void AddCodeElement(ProjectItem item, CodeElement parentElement, CodeElement element, List <DPackFileCodeModel> model, ProcessorFlags flags,
                                    LanguageSettings languageSet, ICollection <vsCMElement> dteFilter, CodeModelFilterFlags filter)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            EditPoint editPoint;

            // Line number retrieval uses EditPoints, which is expensive to process
            try
            {
                // Not all elements support header part
                TextPoint startPoint;
                if ((element.Kind == vsCMElement.vsCMElementClass) ||
                    (element.Kind == vsCMElement.vsCMElementModule) ||
                    (element.Kind == vsCMElement.vsCMElementInterface) ||
                    (element.Kind == vsCMElement.vsCMElementStruct) ||
                    (element.Kind == vsCMElement.vsCMElementEnum) ||
                    (element.Kind == vsCMElement.vsCMElementFunction))
                {
                    startPoint = element.GetStartPoint(vsCMPart.vsCMPartHeader);
                }
                else
                {
                    startPoint = element.StartPoint;
                }

                if (startPoint == null)
                {
                    _log.LogMessage($"{element.Name} element's of {element.Kind} kind StartPoint cannot be determined");
                    return;
                }

                // Check if this code model element belongs to the current project item
                if (element.ProjectItem != null)
                {
                    var elementItem = element.ProjectItem;
                    if ((item != null) && (item != elementItem))
                    {
                        string itemFileName = item.get_FileNames(1);
                        string epiFileName  = elementItem.get_FileNames(1);

                        if (string.IsNullOrEmpty(itemFileName) ||
                            string.IsNullOrEmpty(epiFileName) ||
                            (string.Compare(itemFileName, epiFileName, StringComparison.OrdinalIgnoreCase) != 0))
                        {
                            _log.LogMessage($"Project item mismatch: '{item.Name}' expected but '{elementItem.Name}' found instead - {element.Name}");
                            return;
                        }
                        else
                        {
                            // This is a work around for project item reference change where essentially new reference
                            // still points at the same file but reference wise these two are no longer equal
                            item = elementItem;
                        }
                    }
                }

                editPoint = startPoint.CreateEditPoint();
            }
            catch (Exception ex)
            {
                // Swallow C++ exception as some invalid code model functions raise it sometimes
                if (languageSet?.Language != CodeModelLanguageConstants.vsCMLanguageVC)
                {
                    _log.LogMessage($"Error adding code member: {element.Name}", ex);
                }

                return;
            }

            var line = editPoint.Line;

            // Decide whether the element is to be filtered out or not
            bool add;

            if (dteFilter?.Count == 0)
            {
                add = true;
            }
            else
            {
                add = dteFilter.Contains(element.Kind);
                if (!add)
                {
                    // TODO: resurrect next statement if need be - used to treat read-only fields as properties
                    //if (dteFilter.Contains(vsCMElement.vsCMElementProperty) && (element.Kind == vsCMElement.vsCMElementVariable))
                    //{
                    //	var varElt = (CodeVariable2)element;
                    //	if (varElt.ConstKind == vsCMConstKind.vsCMConstKindReadOnly)
                    //		add = true;
                    //}
                }
            }

            if (add)
            {
                GetConstructorDestructorInfo(languageSet, parentElement, element, out bool constructor, out bool destructor);

                if ((filter != CodeModelFilterFlags.Constructors) ||
                    (filter.HasFlag(CodeModelFilterFlags.Constructors) && (constructor || destructor)))
                {
                    var    name = element.Name;
                    string parentFullName;
                    var    fullName = flags.HasFlag(ProcessorFlags.IncludeMemeberFullName) ? element.FullName : string.Empty;
                    var    code     = string.Empty;

                    // Setup element's full name information
                    if (string.IsNullOrEmpty(name))
                    {
                        name = UNNAMED_NAME;
                    }
                    if (parentElement == null)
                    {
                        if (!flags.HasFlag(ProcessorFlags.IncludeMemeberFullName) && (languageSet != null) && languageSet.ParentlessFullName)
                        {
                            parentFullName = element.FullName;
                        }
                        else
                        {
                            parentFullName = name;
                        }
                    }
                    else
                    if ((parentElement.Kind == vsCMElement.vsCMElementClass) ||
                        (parentElement.Kind == vsCMElement.vsCMElementModule) ||
                        (parentElement.Kind == vsCMElement.vsCMElementInterface) ||
                        (parentElement.Kind == vsCMElement.vsCMElementStruct))
                    {
                        parentFullName = parentElement.Name + "." + name;
                    }
                    else
                    {
                        parentFullName = name;
                    }

                    // Check for duplicate names
                    if ((languageSet != null) && languageSet.CheckDuplicateNames)
                    {
                        foreach (var modelItem in model)
                        {
                            if ((modelItem.Line == line) &&
                                (modelItem.CodeModelElementKind == (int)element.Kind) &&
                                (modelItem.ParentFullName == parentFullName))
                            {
                                // Duplicate found
                                add = false;
                                break;
                            }
                        }
                    }

                    if (add)
                    {
                        // Setup element's first code line information
                        if (editPoint != null)
                        {
                            int lineLength = editPoint.LineLength - editPoint.LineCharOffset + 1;
                            if (lineLength > 1)
                            {
                                code = editPoint.GetText(lineLength).TrimStart(' ', '\t');
                            }
                        }

                        try
                        {
                            var returnTypeName = constructor || destructor || (element == null) ? string.Empty : GetElementReturnTypeName(element);
                            var xmlDoc         = flags.HasFlag(ProcessorFlags.IncludeMemberXmlDoc) ? GetXmlDoc(element, languageSet) : string.Empty;

                            char?genericSuffix = null;
                            if ((languageSet != null) && languageSet.SupportsGenerics)
                            {
                                var suffix = _shellCodeModelService.GetGenericsSuffix(element);

                                if (!string.IsNullOrEmpty(suffix))
                                {
                                    name           = name + suffix;
                                    parentFullName = parentFullName + suffix;
                                    genericSuffix  = suffix[0];
                                }
                            }

                            var modelItem = new DPackFileCodeModel
                            {
                                ProjectItem          = element.ProjectItem,
                                Name                 = name,
                                FullName             = fullName,
                                ParentFullName       = parentFullName,
                                CodeModelElementKind = (int)element.Kind,
                                ElementKind          = _shellCodeModelService.GetElementKind(element),
                                ElementModifier      = _shellCodeModelService.GetElementModifier(element),
                                IsConstant           = _shellCodeModelService.IsConstant(element),
                                IsStatic             = _shellCodeModelService.IsStatic(element),
                                SupportsGenerics     = languageSet.SupportsGenerics,
                                GenericsSuffix       = _shellCodeModelService.GetGenericsSuffix(element),
                                Line                 = line,
                                Code                 = code,
                                ReturnTypeName       = returnTypeName,
                                XmlDoc               = xmlDoc
                            };

                            model.Add(modelItem);
                        }
                        catch (COMException ex)
                        {
                            _log.LogMessage($"Ignored COM code model error for {name}", ex);
                        }
                    }     // if (add)
                }         // if (memberFilter)
            }             // if (add)
        }
        /// <summary>Parses for strings by iterating through the text lines.</summary>
        /// <param name="element">The element.</param>
        /// <param name="stringResources">The string resources.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="isCSharp">If set to <c>true</c> it is CSharp code.</param>
        /// <param name="startLine">The start line.</param>
        /// <param name="endLine">The end line.</param>
        /// <returns>The last parsed line number or -1.</returns>
        private static int ParseForStrings(CodeElement element,
                                           List <StringResource> stringResources,
                                           Settings settings,
                                           bool isCSharp,
                                           int startLine,
                                           int endLine)
        {
            TextPoint startPoint = element.StartPoint,
                      endPoint   = element.EndPoint;
            EditPoint2 editPoint = null;

            try
            {
                if (element.Kind == vsCMElement.vsCMElementFunction)
                {
#if DEBUG_OUTPUT
                    System.Diagnostics.Debug.Print("    function kind: {0}", (element as CodeFunction).FunctionKind);
#endif

                    try
                    {
                        //we want to have the body only (throws COMException when inspecting an expression bodied function)
                        startPoint = element.GetStartPoint(vsCMPart.vsCMPartBody);
                        endPoint   = element.GetEndPoint(vsCMPart.vsCMPartBody);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.Print("ParseForStrings(vsCMElementFunction, line {0}): {1} - {2}", startPoint.Line, ex.GetType().Name, ex.Message);
                    }
                } //if

                editPoint = startPoint.CreateEditPoint() as EditPoint2;

                //if ((element.Kind == vsCMElement.vsCMElementVariable) && (startPoint.LineCharOffset > 1))
                //  editPoint.CharLeft(startPoint.LineCharOffset - 1);

//#if DEBUG
//        if (element.Kind == vsCMElement.vsCMElementFunction)
//          System.Diagnostics.Debug.Print("got a function, named: {0} in line {1} to {2}", element.Name, element.StartPoint.Line, element.EndPoint.Line);
//        else
//          System.Diagnostics.Debug.Print("got a variable, named: {0} in line {1} to {2}", element.Name, element.StartPoint.Line, element.EndPoint.Line);
//#endif
                //if (element.Children.Count > 0)
                //  editPoint = element.Children.Item(element.Children.Count).EndPoint.CreateEditPoint() as EditPoint2;
                //else
                //  editPoint = element.StartPoint.CreateEditPoint() as EditPoint2;
//#if DEBUG
//        if (element.Children.Count > 0) System.Diagnostics.Debug.Print("      line {0} to {1}", editPoint.Line, element.EndPoint.Line);
//#endif

                #region veeeeeery sloooooow
                //int endPoint      = element.EndPoint.Line,
                //    endColumn     = element.EndPoint.LineCharOffset,
                //    absoluteEnd   = element.EndPoint.AbsoluteCharOffset,
                //    editLine      = editPoint.Line,
                //    editColumn    = editPoint.LineCharOffset,
                //    absoluteStart = editPoint.AbsoluteCharOffset,
                //    editLength    = (editLine == endPoint) ? (absoluteEnd - absoluteStart + 1)
                //                                           : (editPoint.LineLength - editColumn + 1);

                //while ((editLine < endPoint) || ((editLine == endPoint) && (editColumn <= endColumn)))
                //{
                //  string textLine = editPoint.GetText(editLength);

                //  //System.Diagnostics.Debug.Print(">>>{0}<<<", textLine);

                //  if (!string.IsNullOrEmpty(textLine.Trim()))
                //    ParseForStrings(textLine, editLine, editColumn, stringResources, settings);

                //  editPoint.LineDown(1);
                //  editPoint.StartOfLine();

                //  editLine      = editPoint.Line;
                //  editColumn    = editPoint.LineCharOffset;
                //  absoluteStart = editPoint.AbsoluteCharOffset;
                //  editLength    = (editLine == endPoint) ? (absoluteEnd - absoluteStart + 1)
                //                                         : (editPoint.LineLength - editColumn + 1);
                //} //while
                #endregion

                //this is much faster (by factors)!!!
                int editLine       = editPoint.Line,
                    editColumn     = editPoint.LineCharOffset;
                string   text      = editPoint.GetText(endPoint);
                string[] txtLines  = text.Replace("\r", string.Empty).Split('\n');
                bool     isComment = false;
#if IGNORE_METHOD_ARGUMENTS
                bool isIgnoreMethodArguments = false;
#endif

                foreach (string txtLine in txtLines)
                {
                    if ((editLine >= startLine) && (editLine <= endLine))
                    {
                        //this is a changed text line in the block

#if !IGNORE_METHOD_ARGUMENTS
                        if (txtLine.Contains("\""))
                        {
                            ParseForStrings(txtLine, editLine, editColumn, stringResources, settings, isCSharp, ref isComment);
                        }
#else
                        if (line.Contains("\""))
                        {
                            ParseForStrings(line, editLine, editColumn, stringResources, settings, ref isComment, ref isIgnoreMethodArguments);
                        }
#endif
                    } //if

                    ++editLine;

                    //only for the first line of the text block LineCharOffset will be used
                    if (editColumn > 1)
                    {
                        editColumn = 1;
                    }
                } //foreach
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print("### Error: ParseForStrings(): {0} - {1}", ex.GetType().Name, ex.Message);
            }

            return(endPoint?.Line ?? (-1));
        }