Esempio n. 1
0
 private EnvDTE.CodeElement GetCodeElement(EnvDTE.vsCMElement scope)
 {
     try
     {
         return(_codeModel?.CodeElementFromPoint(Begin, scope));
     }
     catch (Exception)
     {
         return(null);
     }
 }
Esempio n. 2
0
        private static CodeElementKind ToInternalKind(this EnvDTE.vsCMElement vsKind)
        {
            switch (vsKind)
            {
            case vsCMElementFunction: return(CodeElementKind.Method);

            case vsCMElementClass: return(CodeElementKind.Class);

            case vsCMElementNamespace: return(CodeElementKind.Namespace);

            default: throw new NotSupportedException("Unknown code element kind.");
            }
        }
Esempio n. 3
0
        public EnvDTE.CodeElement CodeElementFromPoint(EnvDTE.TextPoint point, EnvDTE.vsCMElement scope)
        {
            // Can't use point.AbsoluteCharOffset because it's calculated by the native
            // implementation in GetAbsoluteOffset (in env\msenv\textmgr\autoutil.cpp)
            // to only count each newline as a single character. We need to ask for line and
            // column and calculate the right offset ourselves. See DevDiv2 530496 for details.
            var position = GetPositionFromTextPoint(point);

            var result = CodeElementFromPosition(position, scope);

            if (result == null)
            {
                throw Exceptions.ThrowEFail();
            }

            return(result);
        }
 public EnvDTE.CodeElement get_CodeElement(EnvDTE.vsCMElement scope)
 {
     throw new NotImplementedException();
 }
Esempio n. 5
0
        internal EnvDTE.CodeElement CodeElementFromPosition(int position, EnvDTE.vsCMElement scope)
        {
            var root       = GetSyntaxRoot();
            var leftToken  = SyntaxFactsService.FindTokenOnLeftOfPosition(root, position);
            var rightToken = SyntaxFactsService.FindTokenOnRightOfPosition(root, position);

            // We apply a set of heuristics to determine which member we pick to start searching.
            var token = leftToken;

            if (leftToken != rightToken)
            {
                if (leftToken.Span.End == position && rightToken.SpanStart == position)
                {
                    // If both tokens are touching, we prefer identifiers and keywords to
                    // separators. Note that the language doesn't allow both tokens to be a
                    // keyword or identifier.
                    if (SyntaxFactsService.IsKeyword(rightToken) ||
                        SyntaxFactsService.IsIdentifier(rightToken))
                    {
                        token = rightToken;
                    }
                }
                else if (leftToken.Span.End < position && rightToken.SpanStart <= position)
                {
                    // If only the right token is touching, we have to use it.
                    token = rightToken;
                }
            }

            // If we ended up using the left token but the position is after that token,
            // walk up to the first node who's last token is not the leftToken. By doing this, we
            // ensure that we don't find members when the position is actually between them.
            // In that case, we should find the enclosing type or namespace.
            var parent = token.Parent;

            if (token == leftToken && position > token.Span.End)
            {
                while (parent != null)
                {
                    if (parent.GetLastToken() == token)
                    {
                        parent = parent.Parent;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            var node = parent != null
                ? parent.AncestorsAndSelf().FirstOrDefault(n => CodeModelService.MatchesScope(n, scope))
                : null;

            if (node == null)
            {
                return(null);
            }

            if (scope == EnvDTE.vsCMElement.vsCMElementAttribute ||
                scope == EnvDTE.vsCMElement.vsCMElementImportStmt ||
                scope == EnvDTE.vsCMElement.vsCMElementParameter ||
                scope == EnvDTE.vsCMElement.vsCMElementOptionStmt ||
                scope == EnvDTE.vsCMElement.vsCMElementInheritsStmt ||
                scope == EnvDTE.vsCMElement.vsCMElementImplementsStmt ||
                (scope == EnvDTE.vsCMElement.vsCMElementFunction && CodeModelService.IsAccessorNode(node)))
            {
                // Attributes, imports, parameters, Option, Inherts and Implements
                // don't have node keys of their own and won't be included in our
                // collection of elements. Delegate to the service to create these.
                return(CodeModelService.CreateInternalCodeElement(State, this, node));
            }

            return(CreateCodeElement <EnvDTE.CodeElement>(node));
        }
        public void InsertCommentOnEndOfFunction(object sender, EventArgs e)
        {
            var macaron = new VisualStudioMacaron(this);

            // アクティブドキュメントがない場合は処理を抜けます。
            if (macaron.DocumentIsActive == false)
            {
                return;
            }

            // C# 以外の場合はコメントを挿入せずに、処理を抜けます。
            var project = macaron.Dte.ActiveDocument.ProjectItem.ContainingProject;

            if (project == null || project.KnownKind() != ProjectKnownKind.CSharp)
            {
                return;
            }

            var textSelection = macaron.Dte.ActiveDocument.Selection as EnvDTE.TextSelection;

            textSelection.StartOfLine(EnvDTE.vsStartOfLineOptions.vsStartOfLineOptionsFirstText);

            var elements = new EnvDTE.vsCMElement[] {
                EnvDTE.vsCMElement.vsCMElementFunction,
                EnvDTE.vsCMElement.vsCMElementProperty,
                EnvDTE.vsCMElement.vsCMElementClass,
                EnvDTE.vsCMElement.vsCMElementInterface,
                EnvDTE.vsCMElement.vsCMElementStruct,
                EnvDTE.vsCMElement.vsCMElementEnum,
                EnvDTE.vsCMElement.vsCMElementModule,
                EnvDTE.vsCMElement.vsCMElementNamespace
            };

            var comments = new string[] {
                " // end sub",
                " // end property",
                " // end class",
                " // end interface",
                " // end structure",
                " // end enum",
                " // end module",
                " // end namespace"
            };

            var i = 0;

            foreach (var element in elements)
            {
                try
                {
                    var codeElement = textSelection.ActivePoint.CodeElement[element];

                    if (codeElement != null)
                    {
                        textSelection.MoveToPoint(codeElement.EndPoint);
                        textSelection.SelectLine();

                        if ((i != 0 && textSelection.Text.Contains(comments[i]) == false) ||
                            (i == 0 && (
                                 textSelection.Text.Contains(comments[i]) == false &&
                                 textSelection.Text.Contains(" // end function") == false &&
                                 textSelection.Text.Contains(" // end constructor") == false
                                 ))
                            )
                        {
                            var isSub         = false;
                            var isConstructor = false;

                            if (i == 0)
                            {
                                textSelection.MoveToPoint(codeElement.StartPoint);
                                textSelection.SelectLine();

                                isSub = (textSelection.Text.Contains("void"));
                                if (isSub == false)
                                {
                                    var nameText = codeElement.FullName.Split('.');
                                    try
                                    {
                                        isConstructor = nameText[nameText.Length - 2].Equals(nameText[nameText.Length - 1]);
                                    }
                                    catch (IndexOutOfRangeException)
                                    {
                                    } // end try
                                }     // end if
                            }         // end if

                            textSelection.MoveToPoint(codeElement.EndPoint);

                            if (i == 0 && isSub == false)
                            {
                                if (isConstructor)
                                {
                                    textSelection.Insert(" // end constructor");
                                }
                                else
                                {
                                    textSelection.Insert(" // end function");
                                } // end if
                            }
                            else
                            {
                                textSelection.Insert(comments[i]);
                            } // end if
                        }
                        else
                        {
                            textSelection.MoveToPoint(codeElement.EndPoint);
                        } // end if

                        break; // exit for
                    } // end if
                }
                catch
                {
                } // end try

                i += 1;
            } // next element
        }     // end sub
        } // end of function - GetActiveEditPoint

        /*----------------------- GetCodeElementAtActiveEditPoint ---------------*/
        /// <summary>
        /// Gets the code element associated with the active edit point for the
        /// specified code element scope
        /// </summary>
        /// <param name="doc">
        /// The document to search
        /// </param>
        /// <param name="scope">
        /// Scope to look for
        /// </param>
        /// <returns>
        /// The code element for the specified scope at the active point in the
        /// document
        /// </returns>
        public static EnvDTE.CodeElement GetCodeElementAtActiveEditPoint(this EnvDTE.Document doc, EnvDTE.vsCMElement scope)
        {
            var fcm = doc.ProjectItem.FileCodeModel;
            var aep = doc.GetActiveEditPoint();

            return(fcm.CodeElementFromPoint(aep, scope));
        } // end of function - GetCodeElementAtActiveEditPoint
Esempio n. 8
0
 public EnvDTE.CodeElement get_CodeElement(EnvDTE.vsCMElement cal)
 {
     return(null);
 }