public void TestInitialization()
 {
     _vbe = new Mock<VBE>();
     _vbProject = new Mock<VBProject>();
     _declarations = new Declarations();
     _module = new QualifiedModuleName();
     _view = new Mock<IRenameView>();
 }
Esempio n. 2
0
        public RenameModel(VBE vbe, VBProjectParseResult parseResult, QualifiedSelection selection)
        {
            _vbe = vbe;
            _parseResult = parseResult;
            _declarations = parseResult.Declarations;
            _selection = selection;

            AcquireTarget(out _target, Selection);
        }
        public RemoveParametersModel(VBProjectParseResult parseResult, QualifiedSelection selection)
        {
            _parseResult = parseResult;
            _declarations = parseResult.Declarations;

            AcquireTarget(selection);

            Parameters = new List<Parameter>();
            LoadParameters();
        }
        private bool IsIgnoredDeclaration(Declarations declarations, Declaration declaration, IEnumerable<Declaration> handlers, IEnumerable<Declaration> classes, IEnumerable<Declaration> modules)
        {
            var enumerable = classes as IList<Declaration> ?? classes.ToList();
            var result = !ProcedureTypes.Contains(declaration.DeclarationType)
                || declaration.References.Any(r => !r.IsAssignment)
                || handlers.Contains(declaration)
                || IsPublicModuleMember(modules, declaration)
                || IsClassLifeCycleHandler(enumerable, declaration)
                || IsInterfaceMember(declarations, enumerable, declaration);

            return result;
        }
        public ExtractMethodModel(IActiveCodePaneEditor editor, Declarations declarations, QualifiedSelection selection)
        {
            _sourceMember = declarations.FindSelectedDeclaration(selection, Declarations.ProcedureTypes, d => ((ParserRuleContext)d.Context.Parent).GetSelection());
            if (_sourceMember == null)
            {
                throw new InvalidOperationException("Invalid selection.");
            }

            _extractedMethod = new ExtractedMethod();

            _selection = selection;
            _selectedCode = editor.GetLines(selection.Selection);

            var inScopeDeclarations = declarations.Items.Where(item => item.ParentScope == _sourceMember.Scope).ToList();

            var inSelection = inScopeDeclarations.SelectMany(item => item.References)
                .Where(item => selection.Selection.Contains(item.Selection))
                .ToList();

            var usedInSelection = new HashSet<Declaration>(inScopeDeclarations.Where(item =>
                item.References.Any(reference => inSelection.Contains(reference))));

            var usedBeforeSelection = new HashSet<Declaration>(inScopeDeclarations.Where(item =>
                item.References.Any(reference => reference.Selection.StartLine < selection.Selection.StartLine)));

            var usedAfterSelection = new HashSet<Declaration>(inScopeDeclarations.Where(item =>
                item.References.Any(reference => reference.Selection.StartLine > selection.Selection.EndLine)));

            // identifiers used inside selection and before selection (or if it's a parameter) are candidates for parameters:
            var input = inScopeDeclarations.Where(item =>
                usedInSelection.Contains(item) && (usedBeforeSelection.Contains(item) || item.DeclarationType == DeclarationType.Parameter)).ToList();

            // identifiers used inside selection and after selection are candidates for return values:
            var output = inScopeDeclarations.Where(item =>
                usedInSelection.Contains(item) && usedAfterSelection.Contains(item))
                .ToList();

            // identifiers used only inside and/or after selection are candidates for locals:
            _locals = inScopeDeclarations.Where(item => item.DeclarationType != DeclarationType.Parameter && (
                item.References.All(reference => inSelection.Contains(reference))
                || (usedAfterSelection.Contains(item) && (!usedBeforeSelection.Contains(item)))))
                .ToList();

            // locals that are only used in selection are candidates for being moved into the new method:
            _declarationsToMove = _locals.Where(item => !usedAfterSelection.Contains(item)).ToList();

            _output = output.Select(declaration =>
                new ExtractedParameter(declaration.AsTypeName, ExtractedParameter.PassedBy.ByRef, declaration.IdentifierName));

            _input = input.Where(declaration => !output.Contains(declaration))
                .Select(declaration =>
                    new ExtractedParameter(declaration.AsTypeName, ExtractedParameter.PassedBy.ByVal, declaration.IdentifierName));
        }
        public VBProjectParseResult(VBProject project, IEnumerable<VBComponentParseResult> parseResults)
        {
            _project = project;
            _parseResults = parseResults;
            _declarations = new Declarations();

            var projectIdentifier = project.Name;
            var memberName = new QualifiedMemberName(new QualifiedModuleName(project), projectIdentifier);
            var projectDeclaration = new Declaration(memberName, "VBE", projectIdentifier, false, false, Accessibility.Global, DeclarationType.Project, false);
            _declarations.Add(projectDeclaration);

            foreach (var declaration in VbaStandardLib.Declarations)
            {
                _declarations.Add(declaration);
            }

            foreach (var declaration in _parseResults.SelectMany(item => item.Declarations))
            {
                _declarations.Add(declaration);
            }
        }
        public IdentifierReferenceResolver(QualifiedModuleName qualifiedModuleName, Declarations declarations)
        {
            _qualifiedModuleName = qualifiedModuleName;
            _declarations        = declarations;

            _withBlockQualifiers = new Stack <Declaration>();
            _alreadyResolved     = new HashSet <RuleContext>();

            _moduleTypes = new List <DeclarationType>(new[]
            {
                DeclarationType.Module,
                DeclarationType.Class,
                DeclarationType.Project,
            });

            _memberTypes = new List <DeclarationType>(new[]
            {
                DeclarationType.Function,
                DeclarationType.Procedure,
                DeclarationType.PropertyGet,
                DeclarationType.PropertyLet,
                DeclarationType.PropertySet,
            });

            _returningMemberTypes = new List <DeclarationType>(new[]
            {
                DeclarationType.Function,
                DeclarationType.PropertyGet,
            });

            _projectScopePublicModifiers = new List <Accessibility>(new[]
            {
                Accessibility.Public,
                Accessibility.Global,
                Accessibility.Friend,
                Accessibility.Implicit,
            });

            SetCurrentScope();
        }
        private IEnumerable<string> GetImplementedInterfaceMembers(Declarations declarations, IEnumerable<Declaration> classes, string componentName)
        {
            var interfaces = classes.Where(item => item.References.Any(reference =>
                    reference.Context.Parent is VBAParser.ImplementsStmtContext
                    && reference.QualifiedModuleName.Component.Name == componentName));

            var members = interfaces.SelectMany(declarations.FindMembers)
                .Select(member => member.ComponentName + "_" + member.IdentifierName);
            return members;
        }
        /// <remarks>
        /// Interface implementation members are private, they're not called from an object
        /// variable reference of the type of the procedure's class, and whether they're called or not,
        /// they have to be implemented anyway, so removing them would break the code.
        /// Best just ignore them.
        /// </remarks>
        private bool IsInterfaceMember(Declarations declarations, IEnumerable<Declaration> classes, Declaration procedure)
        {
            // get the procedure's parent module
            var enumerable = classes as IList<Declaration> ?? classes.ToList();
            var parent = enumerable.Where(item => item.Project == procedure.Project)
                        .SingleOrDefault(item => item.IdentifierName == procedure.ComponentName);

            if (parent == null)
            {
                return false;
            }

            var interfaces = enumerable.Where(item => item.References.Any(reference =>
                    reference.Context.Parent is VBAParser.ImplementsStmtContext));

            if (interfaces.Select(i => i.ComponentName).Contains(procedure.ComponentName))
            {
                return true;
            }

            var result = GetImplementedInterfaceMembers(declarations, enumerable, procedure.ComponentName)
                .Contains(procedure.IdentifierName);

            return result;
        }
 public IdentifierReferenceListener(QualifiedModuleName qualifiedName, Declarations declarations)
 {
     _qualifiedName = qualifiedName;
     _declarations  = declarations;
     SetCurrentScope();
 }
 public IdentifierReferenceListener(VBComponentParseResult result, Declarations declarations)
     : this(result.QualifiedName, declarations)
 {
 }
 private bool IsUsedAsByRefParam(Declarations declarations, Declaration parameter)
 {
     // todo: enable tracking parameter references 
     // by linking Parameter declarations to their parent Procedure/Function/Property member.
     return false;
 }