Пример #1
0
        public static void WalkClass(EnvDTE.CodeClass cls, string indent)
        {
            doClass(cls, indent);
            foreach (EnvDTE.CodeElement codeElem in cls.Bases)
            {
                doInheritsFrom(codeElem, indent);
            }
            foreach (EnvDTE.CodeElement codeElem in cls.Members)
            {
                switch (codeElem.Kind)
                {
                case EnvDTE.vsCMElement.vsCMElementVariable:
                    doCodeVariable((CodeVariable)codeElem, indent);
                    break;

                case EnvDTE.vsCMElement.vsCMElementProperty:
                    doCodeProperty((CodeProperty)codeElem, indent);
                    break;

                case EnvDTE.vsCMElement.vsCMElementFunction:
                    WalkFunction((CodeFunction)codeElem, indent + "...");
                    break;

                default:
                    doCodeElement(codeElem, indent);
                    break;
                }
            }
        }
Пример #2
0
    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
    {
        string className = Microsoft.VisualBasic.Interaction.InputBox("Class name", "Create tests",
                                                                      "VATRate", -1, -1);

        EnvDTE.ProjectItem f = DTE.ItemOperations.AddNewItem("Visual C# Items\\Code\\Class", className + "RepositoryTests.cs");
        EnvDTE.CodeClass   c = FirstClass(FirstNamespace(f.FileCodeModel.CodeElements).Children);
        c.AddFunction("Insert" + className, vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid);
    }
Пример #3
0
        private CodeTypeDeclaration CodeTypeDeclarationFromCodeClass(EnvDTE.CodeClass vsClass)
        {
            // set it up and sink it's handler.
            CodeTypeDeclaration codeTypeDecl = new CodeTypeDeclaration(vsClass.Name);

            codeTypeDecl.LinePragma = new CodeLinePragma(this.FileName, vsClass.StartPoint.Line);
            SetVsElementToCodeObject((CodeElement)vsClass, codeTypeDecl);
            codeTypeDecl.PopulateMembers += new EventHandler(this.OnTypePopulateMembers);

            // should this language have an events tab?
            // we do this so the grid can retrieve this info later...
            //
            if (provider.IsVB)
            {
                codeTypeDecl.UserData[typeof(System.Windows.Forms.Design.EventsTab)] = "Hide"; // just a marker value
            }


            // setup it's modifiers
            //
            codeTypeDecl.Attributes     = GetMemberAttributesFromVsClass(vsClass);
            codeTypeDecl.TypeAttributes = TypeAttributes.Class;

            // check for base types
            //

            try {
                foreach (CodeElement baseElement in vsClass.Bases)
                {
                    if (baseElement.Kind == vsCMElement.vsCMElementClass)
                    {
                        codeTypeDecl.BaseTypes.Add(new CodeTypeReference(GetUrtTypeFromVsType((EnvDTE.CodeClass)baseElement)));
                    }
                }
            }
            catch (Exception ex) {
                if (!provider.CodeGenerator.IsValidIdentifier(vsClass.Name))
                {
                    throw new Exception(SR.GetString(SR.InvalidClassNameIdentifier));
                }
                throw ex;
            }


            return(codeTypeDecl);
        }
    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
    {
        EnvDTE.TextSelection ts = DTE.ActiveWindow.Selection as EnvDTE.TextSelection;
        if (ts == null)
        {
            return;
        }

        EnvDTE.CodeClass c = ts.ActivePoint.CodeElement[vsCMElement.vsCMElementClass]
                             as EnvDTE.CodeClass;
        if (c == null)
        {
            return;
        }

        foreach (EnvDTE.CodeElement e in c.Members)
        {
            if (e.Kind == vsCMElement.vsCMElementFunction)
            {
                EnvDTE.TextPoint p = (e as EnvDTE.CodeFunction).GetStartPoint();
                DTE.Debugger.Breakpoints.Add("", p.Parent.Parent.FullName, p.Line);
            }
        }
    }
Пример #5
0
        public void test(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
        {
            EnvDTE.TextSelection textSelection = DTE.ActiveWindow.Selection as EnvDTE.TextSelection;
            if (textSelection == null)
            {
                return;
            }
            EnvDTE.CodeClass codeClass = textSelection.ActivePoint.CodeElement[vsCMElement.vsCMElementClass] as EnvDTE.CodeClass;
            if (codeClass == null)
            {
                return;
            }

            string properties = "";

            foreach (CodeElement elem in codeClass.Members)
            {
                if (elem.Kind == vsCMElement.vsCMElementProperty)
                {
                    properties += elem.Name + elem.+ System.Environment.NewLine;
                }
            }
            System.Windows.Clipboard.SetText(properties);
        }
Пример #6
0
 public abstract void DoCheck(EnvDTE.CodeClass codeClass);
Пример #7
0
 public static void doClass(EnvDTE.CodeClass cls, string indent)
 {
     Debug.WriteLine(indent + "class:" + cls.Name);
 }
Пример #8
0
        //解析接口或类
        void parse_body(EnvDTE.CodeClass codeClass)
        {
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
            var children = codeClass.Members;

            foreach (CodeElement codeElement in children)
            {
                //解析方法定义,只解析公有方法
                if (codeElement.Kind == vsCMElement.vsCMElementFunction)
                {
                    CodeFunction codeFunction = codeElement as CodeFunction;
                    if (codeFunction.Access == vsCMAccess.vsCMAccessPublic)
                    {
                        CodeFunctionInfo func_info = new CodeFunctionInfo();
                        //解析返回值
                        var returnType = codeFunction.Type.AsFullName;
                        func_info.ReturnTypeName = returnType;
                        func_info.IsStatic       = codeFunction.IsShared;

                        //解析参数
                        //string parms = "";
                        string parms_for_invoke = "";

                        foreach (CodeParameter param in codeFunction.Parameters)
                        {
                            //TextPoint start = param.GetStartPoint();
                            //TextPoint finish = param.GetEndPoint();
                            //parms += start.CreateEditPoint().GetText(finish) + ",";
                            func_info.ParamList.Add(new CodeParamInfo()
                            {
                                Name = param.Name, TypeName = param.Type.AsFullName
                            });
                            parms_for_invoke += param.Name + ",";
                        }
                        //if (parms.Length > 0) parms = parms.Remove(parms.Length - 1, 1);
                        if (parms_for_invoke.Length > 0)
                        {
                            parms_for_invoke = parms_for_invoke.Remove(parms_for_invoke.Length - 1, 1);
                        }
                        //func_info.ParamString = parms;
                        func_info.InvokeParamString = parms_for_invoke;
                        func_info.ShortName         = codeFunction.Name;
                        var tmps = codeFunction.FullName.Split('.');
                        func_info.Name = tmps[tmps.Length - 1];
                        if (func_info.Name.IndexOf("<") != -1)
                        {
                            var s = func_info.Name.IndexOf("<") + 1;
                            var l = func_info.Name.Length - s - 1;
                            func_info.GenericTypeArguments = func_info.Name.Substring(s, l).Split(',').Select(it => it.Trim()).ToList();
                        }
                        if (func_info.ShortName == this.ShortClassName)
                        {
                            CodeConstructorList.Add(func_info);
                        }
                        else if (func_info.ShortName != codeClass.Name)
                        {
                            CodeFunctionList.Add(func_info);
                        }
                    }
                }
                ////解析属性定义,只解析公有属性
                else if (codeElement.Kind == vsCMElement.vsCMElementProperty)
                {
                    CodeProperty codeProperty = codeElement as CodeProperty;
                    if (codeProperty.Access == vsCMAccess.vsCMAccessPublic)
                    {
                        CodePropertyInfo property_info = new CodePropertyInfo();
                        property_info.Name     = codeProperty.Name;
                        property_info.TypeName = codeProperty.Type.AsFullName;
                        try
                        {
                            var getter = codeProperty.Getter;
                            property_info.HasGet   = getter != null && getter.Access == vsCMAccess.vsCMAccessPublic;
                            property_info.IsStatic = codeProperty.Getter.IsShared;
                        }
                        catch
                        {
                            property_info.HasGet = false;
                        }
                        try
                        {
                            var setter = codeProperty.Setter;
                            property_info.HasSet   = setter != null && setter.Access == vsCMAccess.vsCMAccessPublic;
                            property_info.IsStatic = codeProperty.Setter.IsShared;
                            foreach (CodeParameter param in setter.Parameters)
                            {
                                //TextPoint start = param.GetStartPoint();
                                //TextPoint finish = param.GetEndPoint();
                                //parms += start.CreateEditPoint().GetText(finish) + ",";
                                property_info.ParamList.Add(new CodeParamInfo()
                                {
                                    Name = param.Name, TypeName = param.Type.AsFullName
                                });
                            }
                        }
                        catch
                        {
                            property_info.HasSet = false;
                        }
                        CodePropertyList.Add(property_info);
                    }
                }
                else if (codeElement.Kind == vsCMElement.vsCMElementEvent)
                {
                    CodeEvent codeEvent = codeElement as CodeEvent;
                    if (codeEvent.Access == vsCMAccess.vsCMAccessPublic)
                    {
                        CodeEventInfo codeEventInfo = new CodeEventInfo();
                        codeEventInfo.Name     = codeEvent.Name;
                        codeEventInfo.TypeName = codeEvent.Type.AsFullName;
                        codeEventInfo.IsStatic = codeEvent.IsShared;
                        CodeEventList.Add(codeEventInfo);
                    }
                }
            }
            foreach (CodeClass baseClass in codeClass.Bases)
            {
                if (baseClass.Kind == vsCMElement.vsCMElementClass)
                {
                    parse_body(baseClass);
                    break;
                }
            }
        }
Пример #9
0
        /// <include file='doc\VsCodeDomParser.uex' path='docs/doc[@for="VsCodeDomParser.OnTypePopulateMembers"]/*' />
        /// <devdoc>
        ///     Populate the methods and fields of a class declaration
        /// </devdoc>
        private void OnTypePopulateMembers(object sender, EventArgs e)
        {
            CodeTypeDeclaration codeTypeDecl = (CodeTypeDeclaration)sender;

            EnvDTE.CodeClass vsCodeClass = (CodeClass)GetVsElementFromCodeObject(codeTypeDecl);

            // We create another collection, which we stash in the decl's user
            // data.  This is used to reconcile deletes when we code
            // gen.
            //
            CodeTypeMemberCollection originalMemberCollection = new CodeTypeMemberCollection();

            codeTypeDecl.UserData[VsOriginalCollectionKey] = originalMemberCollection;

            string shortName = codeTypeDecl.Name;
            int    lastDot   = shortName.LastIndexOf('.');

            if (lastDot != -1)
            {
                shortName = shortName.Substring(lastDot + 1);
            }

            foreach (CodeElement codeElement in vsCodeClass.Members)
            {
                switch (codeElement.Kind)
                {
                case vsCMElement.vsCMElementVariable:
                    CodeVariable vsField = (CodeVariable)codeElement;

                    // create the element
                    //
                    CodeMemberField codeField = new CodeMemberField(new CodeTypeReference(GetUrtTypeFromVsType(vsField.Type)), vsField.Name);

                    codeField.LinePragma = new CodeLinePragma(this.FileName, vsField.StartPoint.Line);

                    // get the attributes
                    //
                    codeField.Attributes = GetMemberAttributesFromVsField(vsField);

                    // add it to the members collection
                    SetVsElementToCodeObject((CodeElement)vsField, codeField);
                    codeTypeDecl.Members.Add(codeField);
                    originalMemberCollection.Add(codeField);
                    break;

                case vsCMElement.vsCMElementFunction:
                    CodeFunction     vsFunction = (CodeFunction)codeElement;
                    CodeMemberMethod codeMethod;


                    if (vsFunction.Name == shortName)
                    {
                        codeMethod = new CodeTypeConstructor();
                    }
                    else
                    {
                        codeMethod = new CodeMemberMethod();
                        string returnType = GetUrtTypeFromVsType(vsFunction.Type);
                        if (returnType == null || returnType.Length == 0)
                        {
                            returnType = typeof(void).FullName;
                        }
                        codeMethod.ReturnType = new CodeTypeReference(returnType);
                    }

                    // sync up the name with what VS thinks it is.  This is important
                    // for things like constructors because the default name from CodeDom is
                    // ".cctor", but VS uses the shortname.
                    //
                    codeMethod.Name = vsFunction.Name;

                    codeMethod.UserData[typeof(EventHandler)] = ehFunctionPointHandler;

                    // add the ref
                    SetVsElementToCodeObject((CodeElement)vsFunction, codeMethod);

                    // setup the attriubutes
                    //
                    codeMethod.Attributes = GetMemberAttributesFromVsFunction(vsFunction);

                    // sink population events.
                    codeMethod.PopulateParameters += new EventHandler(this.OnMethodPopulateParameters);
                    codeMethod.PopulateStatements += new EventHandler(this.OnMethodPopulateStatements);
                    codeTypeDecl.Members.Add(codeMethod);
                    originalMemberCollection.Add(codeMethod);

                    // we'll need to navigate back up on this guy to it's code type.
                    //
                    codeMethod.UserData[typeof(CodeTypeDeclaration)] = codeTypeDecl;

                    // now look for any default event hookups for the object
                    //
                    if (provider.IsVB && codeElement is IEventHandler)
                    {
                        string[]  hookups  = GetHandlesClauses((IEventHandler)codeElement);
                        Hashtable handlers = (Hashtable)codeTypeDecl.UserData[VbHandlesClausesKey];

                        // for each handles item like "button1.Click",
                        // push in the handles clause and the function name
                        //
                        foreach (string s in hookups)
                        {
                            if (s != null)
                            {
                                if (handlers == null)
                                {
                                    handlers = new Hashtable();
                                    codeTypeDecl.UserData[VbHandlesClausesKey] = handlers;
                                }
                                handlers[s] = codeMethod;
                            }
                        }
                    }
                    break;

                default:
                    // we really *should* care about the various element types that
                    // the vs codedom is providing, but it turns out vs is
                    // changing those things too often for us to keep up with.
                    //
                    // so i'm removing the individual cases and the assert.  we
                    // will just ignore any types we don't directly care about.
                    // case vsCMElement.vsCMElementClass:
                    // case vsCMElement.vsCMElementEvent:
                    // case vsCMElement.vsCMElementProperty:
                    // case vsCMElement.vsCMElementEnum:
                    // case vsCMElement.vsCMElementDelegate:
                    // case vsCMElement.vsCMElementStruct:
                    // case vsCMElement.vsCMElementEventsDeclaration:

                    // default:
                    // Debug.Fail("Unexpected element type '" + codeElement.Kind.ToString() + "' in Type member declaration");
                    break;
                }
            }
        }