public RemoveParametersRefactoring(IDeclarationFinderProvider declarationFinderProvider, IRefactoringPresenterFactory factory, IRewritingManager rewritingManager, ISelectionService selectionService)
 {
     _declarationFinderProvider = declarationFinderProvider;
     _selectionService          = selectionService;
     _rewritingManager          = rewritingManager;
     _presenterFactory          = (model => DisposalActionContainer.Create(factory.Create <IRemoveParametersPresenter, RemoveParametersModel>(model), factory.Release));
 }
Example #2
0
        public void Refactor()
        {
            _model = InitializeModel();
            if (_model == null)
            {
                return;
            }

            using (var container = DisposalActionContainer.Create(_factory.Create <IEncapsulateFieldPresenter, EncapsulateFieldModel>(_model), p => _factory.Release(p)))
            {
                var presenter = container.Value;
                if (presenter == null)
                {
                    return;
                }

                _model = presenter.Show();
                if (_model == null)
                {
                    return;
                }

                var rewriteSession = _rewritingManager.CheckOutCodePaneSession();
                AddProperty(rewriteSession);
                rewriteSession.TryRewrite();
            }
        }
        public void Refactor()
        {
            _model = InitializeModel();
            if (_model == null)
            {
                return;
            }

            using (var container = DisposalActionContainer.Create(_factory.Create <IReorderParametersPresenter, ReorderParametersModel>(_model), p => _factory.Release(p)))
            {
                var presenter = container.Value;
                if (presenter == null)
                {
                    return;
                }

                _model = presenter.Show();
                if (_model == null || !_model.Parameters.Where((param, index) => param.Index != index).Any() ||
                    !IsValidParamOrder())
                {
                    return;
                }

                var rewriteSession = _rewritingManager.CheckOutCodePaneSession();
                AdjustReferences(_model.TargetDeclaration.References, rewriteSession);
                AdjustSignatures(rewriteSession);
                rewriteSession.TryRewrite();
                _model.State.OnParseRequested(this);
            }
        }
Example #4
0
        public void ValueReturnsValuePassedIn()
        {
            var testValue     = 42;
            var dac           = DisposalActionContainer.Create(testValue, () => { });
            var returnedValue = dac.Value;

            Assert.AreEqual(testValue, returnedValue);
        }
 public ExtractInterfaceRefactoring(IDeclarationFinderProvider declarationFinderProvider, IParseManager parseManager, IMessageBox messageBox, IRefactoringPresenterFactory factory, IRewritingManager rewritingManager, ISelectionService selectionService)
 {
     _declarationFinderProvider = declarationFinderProvider;
     _parseManager     = parseManager;
     _rewritingManager = rewritingManager;
     _messageBox       = messageBox;
     _selectionService = selectionService;
     _presenterFactory = ((model) => DisposalActionContainer.Create(factory.Create <IExtractInterfacePresenter, ExtractInterfaceModel>(model), factory.Release));
 }
Example #6
0
        public void FirstDisposeTriggersActionPassedIn()
        {
            var useCount = 0;
            var dac      = DisposalActionContainer.Create(42, () => useCount++);

            dac.Dispose();
            var expectedUseCount = 1;

            Assert.AreEqual(expectedUseCount, useCount);
        }
Example #7
0
        public void ArgumentForActionPassedInIsTheValuePassedIn()
        {
            var actualArgumentValue = 0;
            var dac = DisposalActionContainer.Create(42, (a) => actualArgumentValue = a);

            dac.Dispose();
            var expectedArgumentValue = 42;

            Assert.AreEqual(expectedArgumentValue, actualArgumentValue);
        }
Example #8
0
        protected InteractiveRefactoringBase(
            IRewritingManager rewritingManager,
            ISelectionProvider selectionProvider,
            IRefactoringPresenterFactory factory,
            IUiDispatcher uiDispatcher)
            : base(rewritingManager, selectionProvider)
        {
            Action <TPresenter> presenterDisposalAction = (TPresenter presenter) => uiDispatcher.Invoke(() => factory.Release(presenter));

            _presenterFactory = ((model) => DisposalActionContainer.Create(factory.Create <TPresenter, TModel>(model), presenterDisposalAction));
        }
        private void GetEnumerationMembers(ITypeInfo info, TYPEATTR attrib)
        {
            var count = attrib.cVars;

            for (var index = 0; index < count; index++)
            {
                info.GetVarDesc(index, out IntPtr varPtr);
                using (DisposalActionContainer.Create(varPtr, info.ReleaseVarDesc))
                {
                    var desc = Marshal.PtrToStructure <VARDESC>(varPtr);
                    Members.Add(new ComEnumerationMember(this, info, desc));
                }
            }
        }
Example #10
0
        private void GetFieldType(TYPEDESC desc, ITypeInfo info)
        {
            var      vt = (VarEnum)desc.vt;
            TYPEDESC tdesc;

            if (vt == VarEnum.VT_PTR)
            {
                tdesc = Marshal.PtrToStructure <TYPEDESC>(desc.lpValue);
                GetFieldType(tdesc, info);
            }
            else if (vt == VarEnum.VT_USERDEFINED)
            {
                int href;
                unchecked
                {
                    //The href is a long, but the size of lpValue depends on the platform, so truncate it after the lword.
                    href = (int)(desc.lpValue.ToInt64() & 0xFFFFFFFF);
                }
                try
                {
                    info.GetRefTypeInfo(href, out ITypeInfo refTypeInfo);
                    refTypeInfo.GetTypeAttr(out IntPtr attribPtr);
                    using (DisposalActionContainer.Create(attribPtr, refTypeInfo.ReleaseTypeAttr))
                    {
                        var attribs = Marshal.PtrToStructure <TYPEATTR>(attribPtr);
                        if (attribs.typekind == TYPEKIND.TKIND_ENUM)
                        {
                            _enumGuid = attribs.guid;
                        }
                        IsReferenceType = ReferenceTypeKinds.Contains(attribs.typekind);
                        _valueType      = new ComDocumentation(refTypeInfo, ComDocumentation.LibraryIndex).Name;
                    }
                }
                catch (COMException) { }
            }
            else if (vt == VarEnum.VT_SAFEARRAY || vt == VarEnum.VT_CARRAY || vt.HasFlag(VarEnum.VT_ARRAY))
            {
                tdesc = Marshal.PtrToStructure <TYPEDESC>(desc.lpValue);
                GetFieldType(tdesc, info);
                IsArray = true;
            }
            else
            {
                if (ComVariant.TypeNames.TryGetValue(vt, out string result))
                {
                    _valueType = result;
                }
            }
        }
Example #11
0
        public void MultipleCallsOfDisposeTriggerTheActionPassedInOnce()
        {
            var useCount = 0;
            var dac      = DisposalActionContainer.Create(42, () => useCount++);

            dac.Dispose();
            dac.Dispose();
            dac.Dispose();
            dac.Dispose();
            dac.Dispose();
            dac.Dispose();
            var expectedUseCount = 1;

            Assert.AreEqual(expectedUseCount, useCount);
        }
Example #12
0
 private void GetComMembers(ITypeInfo info, TYPEATTR attrib)
 {
     for (var index = 0; index < attrib.cFuncs; index++)
     {
         info.GetFuncDesc(index, out IntPtr memberPtr);
         using (DisposalActionContainer.Create(memberPtr, info.ReleaseFuncDesc))
         {
             var member = Marshal.PtrToStructure <FUNCDESC>(memberPtr);
             if (member.callconv != CALLCONV.CC_STDCALL)
             {
                 continue;
             }
             _members.Add(new ComMember(this, info, member));
         }
     }
 }
Example #13
0
        private void GetComFields(ITypeInfo info, TYPEATTR attrib)
        {
            var names = new string[1];

            for (var index = 0; index < attrib.cVars; index++)
            {
                info.GetVarDesc(index, out IntPtr varPtr);
                using (DisposalActionContainer.Create(varPtr, info.ReleaseVarDesc))
                {
                    var desc = Marshal.PtrToStructure <VARDESC>(varPtr);
                    info.GetNames(desc.memid, names, names.Length, out int length);
                    Debug.Assert(length == 1);

                    _fields.Add(new ComField(this, info, names[0], desc, index, DeclarationType.Constant));
                }
            }
        }
Example #14
0
        public ReferenceInfo GetReferenceInfo(ITypeLib typelib, string name, string path)
        {
            try
            {
                typelib.GetLibAttr(out var attributes);
                using (DisposalActionContainer.Create(attributes, typelib.ReleaseTLibAttr))
                {
                    var typeAttr = Marshal.PtrToStructure <System.Runtime.InteropServices.ComTypes.TYPELIBATTR>(attributes);

                    return(new ReferenceInfo(typeAttr.guid, name, path, typeAttr.wMajorVerNum, typeAttr.wMinorVerNum));
                }
            }
            catch
            {
                return(ReferenceInfo.Empty);
            }
        }
Example #15
0
        private void GetComFields(ITypeInfo info, TYPEATTR attrib)
        {
            var names = new string[1];

            for (var index = 0; index < attrib.cVars; index++)
            {
                info.GetVarDesc(index, out IntPtr varPtr);
                using (DisposalActionContainer.Create(varPtr, info.ReleaseVarDesc))
                {
                    var desc = Marshal.PtrToStructure <VARDESC>(varPtr);
                    info.GetNames(desc.memid, names, names.Length, out int length);
                    Debug.Assert(length == 1);

                    DeclarationType type;
                    if (info is ITypeInfoWrapper wrapped && wrapped.HasVBEExtensions)
                    {
                        type = desc.IsValidVBAConstant() ? DeclarationType.Constant : DeclarationType.Variable;
                    }
Example #16
0
        public ComProject(ITypeLib typeLibrary, string path) : base(null, typeLibrary, -1)
        {
            Path = path;
            try
            {
                typeLibrary.GetLibAttr(out IntPtr attribPtr);
                using (DisposalActionContainer.Create(attribPtr, typeLibrary.ReleaseTLibAttr))
                {
                    var typeAttr = Marshal.PtrToStructure <TYPELIBATTR>(attribPtr);

                    MajorVersion = typeAttr.wMajorVerNum;
                    MinorVersion = typeAttr.wMinorVerNum;
                    _flags       = (TypeLibTypeFlags)typeAttr.wLibFlags;
                    Guid         = typeAttr.guid;
                }
            }
            catch (COMException) { }
            LoadModules(typeLibrary);
        }
Example #17
0
        private void GetImplementedInterfaces(ITypeInfo info, TYPEATTR typeAttr)
        {
            IsExtensible = !typeAttr.wTypeFlags.HasFlag(TYPEFLAGS.TYPEFLAG_FNONEXTENSIBLE);
            for (var implIndex = 0; implIndex < typeAttr.cImplTypes; implIndex++)
            {
                info.GetRefTypeOfImplType(implIndex, out int href);
                info.GetRefTypeInfo(href, out ITypeInfo implemented);

                implemented.GetTypeAttr(out IntPtr attribPtr);
                using (DisposalActionContainer.Create(attribPtr, implemented.ReleaseTypeAttr))
                {
                    var attribs = Marshal.PtrToStructure <TYPEATTR>(attribPtr);

                    ComProject.KnownTypes.TryGetValue(attribs.guid, out ComType inherited);
                    var intface = inherited as ComInterface ?? new ComInterface(Project, implemented, attribs);
                    _inherited.Add(intface);
                    ComProject.KnownTypes.TryAdd(attribs.guid, intface);
                }
            }
        }
Example #18
0
        private void GetImplementedInterfaces(ITypeInfo info, TYPEATTR typeAttr)
        {
            for (var implIndex = 0; implIndex < typeAttr.cImplTypes; implIndex++)
            {
                info.GetRefTypeOfImplType(implIndex, out int href);
                info.GetRefTypeInfo(href, out ITypeInfo implemented);

                implemented.GetTypeAttr(out IntPtr attribPtr);
                using (DisposalActionContainer.Create(attribPtr, info.ReleaseTypeAttr))
                {
                    var attribs = Marshal.PtrToStructure <TYPEATTR>(attribPtr);

                    ComProject.KnownTypes.TryGetValue(attribs.guid, out ComType inherited);
                    var intface = inherited as ComInterface ?? new ComInterface(Project, implemented, attribs);

                    ComProject.KnownTypes.TryAdd(attribs.guid, intface);

                    IMPLTYPEFLAGS flags = 0;
                    try
                    {
                        info.GetImplTypeFlags(implIndex, out flags);
                    }
                    catch (COMException) { }

                    if (flags.HasFlag(IMPLTYPEFLAGS.IMPLTYPEFLAG_FSOURCE))
                    {
                        _events.Add(intface);
                    }
                    else
                    {
                        DefaultInterface = flags.HasFlag(IMPLTYPEFLAGS.IMPLTYPEFLAG_FDEFAULT) ? intface : DefaultInterface;
                    }
                    _interfaces.Add(intface, flags.HasFlag(IMPLTYPEFLAGS.IMPLTYPEFLAG_FRESTRICTED));
                }
            }

            if (DefaultInterface == null)
            {
                DefaultInterface = VisibleInterfaces.FirstOrDefault();
            }
        }
Example #19
0
        public RenameRefactoring(IRefactoringPresenterFactory factory, IMessageBox messageBox, IDeclarationFinderProvider declarationFinderProvider, IProjectsProvider projectsProvider, IRewritingManager rewritingManager, ISelectionService selectionService)
        {
            _messageBox = messageBox;
            _declarationFinderProvider = declarationFinderProvider;
            _projectsProvider          = projectsProvider;
            _rewritingManager          = rewritingManager;
            _selectionService          = selectionService;
            _model            = null;
            _presenterFactory = ((model) => DisposalActionContainer.Create(factory.Create <IRenamePresenter, RenameModel>(model), factory.Release));

            _renameActions = new Dictionary <DeclarationType, Action <IRewriteSession> >
            {
                { DeclarationType.Member, RenameMember },
                { DeclarationType.Parameter, RenameParameter },
                { DeclarationType.Event, RenameEvent },
                { DeclarationType.Variable, RenameVariable },
                { DeclarationType.Module, RenameModule },
                { DeclarationType.Project, RenameProject }
            };
            IsInterfaceMemberRename = false;
            _neverRenameIdentifiers = NeverRenameList();
        }
Example #20
0
        public ComInterface(IComBase parent, ITypeInfo info, TYPEATTR attrib) : base(parent, info, attrib)
        {
            // Since the reference declaration gathering is threaded, this can't be truly recursive, so implemented interfaces may have
            // null parents (for example, if the library references a type library that isn't referenced by the VBA project or if that project
            // hasn't had the implemented interface processed yet).
            try
            {
                info.GetContainingTypeLib(out var typeLib, out _);
                typeLib.GetLibAttr(out IntPtr attribPtr);
                using (DisposalActionContainer.Create(attribPtr, typeLib.ReleaseTLibAttr))
                {
                    var typeAttr = Marshal.PtrToStructure <TYPELIBATTR>(attribPtr);
                    Parent = typeAttr.guid.Equals(parent?.Project.Guid) ? parent?.Project : null;
                }
            }
            catch
            {
                Parent = null;
            }

            GetImplementedInterfaces(info, attrib);
            GetComProperties(info, attrib);
            GetComMembers(info, attrib);
        }
Example #21
0
        private void LoadModules(ITypeLib typeLibrary)
        {
            var typeCount = typeLibrary.GetTypeInfoCount();

            for (var index = 0; index < typeCount; index++)
            {
                try
                {
                    typeLibrary.GetTypeInfo(index, out ITypeInfo info);
                    info.GetTypeAttr(out var typeAttributesPointer);
                    using (DisposalActionContainer.Create(typeAttributesPointer, info.ReleaseTypeAttr))
                    {
                        var typeAttributes = Marshal.PtrToStructure <TYPEATTR>(typeAttributesPointer);
                        KnownTypes.TryGetValue(typeAttributes.guid, out var type);

                        switch (typeAttributes.typekind)
                        {
                        case TYPEKIND.TKIND_ENUM:
                            var enumeration = type ?? new ComEnumeration(this, typeLibrary, info, typeAttributes, index);
                            Debug.Assert(enumeration is ComEnumeration);
                            _enumerations.Add(enumeration as ComEnumeration);
                            if (type == null && !enumeration.Guid.Equals(Guid.Empty))
                            {
                                KnownTypes.TryAdd(typeAttributes.guid, enumeration);
                            }
                            break;

                        case TYPEKIND.TKIND_COCLASS:
                            var coclass = type ?? new ComCoClass(this, typeLibrary, info, typeAttributes, index);
                            Debug.Assert(coclass is ComCoClass && !coclass.Guid.Equals(Guid.Empty));
                            _classes.Add(coclass as ComCoClass);
                            if (type == null)
                            {
                                KnownTypes.TryAdd(typeAttributes.guid, coclass);
                            }
                            break;

                        case TYPEKIND.TKIND_DISPATCH:
                        case TYPEKIND.TKIND_INTERFACE:
                            var intface = type ?? new ComInterface(this, typeLibrary, info, typeAttributes, index);
                            Debug.Assert(intface is ComInterface && !intface.Guid.Equals(Guid.Empty));
                            _interfaces.Add(intface as ComInterface);
                            if (type == null)
                            {
                                KnownTypes.TryAdd(typeAttributes.guid, intface);
                            }
                            break;

                        case TYPEKIND.TKIND_RECORD:
                            var structure = new ComStruct(this, typeLibrary, info, typeAttributes, index);
                            _structs.Add(structure);
                            break;

                        case TYPEKIND.TKIND_MODULE:
                            var module = type ?? new ComModule(this, typeLibrary, info, typeAttributes, index);
                            Debug.Assert(module is ComModule);
                            _modules.Add(module as ComModule);
                            if (type == null && !module.Guid.Equals(Guid.Empty))
                            {
                                KnownTypes.TryAdd(typeAttributes.guid, module);
                            }
                            break;

                        case TYPEKIND.TKIND_ALIAS:
                            var alias = new ComAlias(this, typeLibrary, info, index, typeAttributes);
                            _aliases.Add(alias);
                            if (alias.Guid != Guid.Empty)
                            {
                                KnownAliases.TryAdd(alias.Guid, alias);
                            }
                            break;

                        case TYPEKIND.TKIND_UNION:
                            //TKIND_UNION is not a supported member type in VBA.
                            break;

                        default:
                            throw new NotImplementedException($"Didn't expect a TYPEATTR with multiple typekind flags set in {Path}.");
                        }
                    }
                }
                catch (COMException) { }
            }
            ApplySpecificLibraryTweaks();
        }
Example #22
0
        public RefactoringUserInteraction(IRefactoringPresenterFactory factory, IUiDispatcher uiDispatcher)
        {
            Action <TPresenter> presenterDisposalAction = (TPresenter presenter) => uiDispatcher.Invoke(() => factory.Release(presenter));

            _presenterFactory = ((model) => DisposalActionContainer.Create(factory.Create <TPresenter, TModel>(model), presenterDisposalAction));
        }
Example #23
0
        private void GetParameterType(TYPEDESC desc, ITypeInfo info)
        {
            var      vt = (VarEnum)desc.vt;
            TYPEDESC tdesc;

            if (vt == VarEnum.VT_PTR)
            {
                tdesc = Marshal.PtrToStructure <TYPEDESC>(desc.lpValue);
                GetParameterType(tdesc, info);
                IsByRef = true;
            }
            else if (vt == VarEnum.VT_USERDEFINED)
            {
                int href;
                unchecked
                {
                    href = (int)(desc.lpValue.ToInt64() & 0xFFFFFFFF);
                }

                try
                {
                    info.GetRefTypeInfo(href, out ITypeInfo refTypeInfo);
                    refTypeInfo.GetTypeAttr(out IntPtr attribPtr);
                    using (DisposalActionContainer.Create(attribPtr, refTypeInfo.ReleaseTypeAttr))
                    {
                        var attribs = Marshal.PtrToStructure <TYPEATTR>(attribPtr);
                        var type    = new ComDocumentation(refTypeInfo, ComDocumentation.LibraryIndex).Name;
                        if (attribs.typekind == TYPEKIND.TKIND_ENUM)
                        {
                            _typeName = new ComTypeName(Project, type, attribs.guid, Guid.Empty);
                        }
                        else if (attribs.typekind == TYPEKIND.TKIND_ALIAS)
                        {
                            _typeName = new ComTypeName(Project, type, Guid.Empty, attribs.guid);
                        }
                        else
                        {
                            _typeName = new ComTypeName(Project, type);
                        }
                    }
                }
                catch (COMException)
                {
                    _typeName = new ComTypeName(Project, Tokens.Object);
                }
            }
            else if (vt == VarEnum.VT_SAFEARRAY || vt == VarEnum.VT_CARRAY || vt.HasFlag(VarEnum.VT_ARRAY))
            {
                tdesc = Marshal.PtrToStructure <TYPEDESC>(desc.lpValue);
                GetParameterType(tdesc, info);
                IsArray = true;
            }
            else if (vt == VarEnum.VT_HRESULT)
            {
                _typeName = new ComTypeName(Project, Tokens.Long);
            }
            else
            {
                _typeName = new ComTypeName(Project, (ComVariant.TypeNames.TryGetValue(vt, out string result)) ? result : Tokens.Object);
            }
        }