Пример #1
0
        private static void RemoveAsTypeDeclaration(ParserRuleContext functionContext, IModuleRewriter rewriter)
        {
            var asTypeContext = functionContext.GetChild <VBAParser.AsTypeClauseContext>();

            if (asTypeContext != null)
            {
                rewriter.Remove(asTypeContext);
                rewriter.Remove(
                    functionContext.children.ElementAt(functionContext.children.IndexOf(asTypeContext) -
                                                       1) as ParserRuleContext);
            }
        }
        private static void RemoveDeclarationContext <T>(this IModuleRewriter rewriter, Declaration declaration, bool removeEndOfStmtContext = true) where T : ParserRuleContext
        {
            if (!declaration.Context.TryGetAncestor <T>(out var elementContext))
            {
                throw new ArgumentException();
            }

            rewriter.Remove(elementContext);
            if (removeEndOfStmtContext && elementContext.TryGetFollowingContext <VBAParser.EndOfStatementContext>(out var nextContext))
            {
                rewriter.Remove(nextContext);
            }
        }
Пример #3
0
        private static void RemoveSingleAnnotation(IModuleRewriter rewriter, VBAParser.AnnotationContext annotationContext, VBAParser.AnnotationListContext annotationListContext)
        {
            var commentSeparator = annotationListContext.COLON();

            if (commentSeparator == null)
            {
                RemoveEntireLine(rewriter, annotationContext);
            }
            else
            {
                RemoveAnnotationMarker(rewriter, annotationContext);
                rewriter.Remove(annotationContext);
                rewriter.Remove(commentSeparator);
            }
        }
Пример #4
0
 private void RemoveReturnStatements(IInspectionResult result, IModuleRewriter rewriter)
 {
     foreach (var returnStatement in GetReturnStatements(result.Target))
     {
         rewriter.Remove(returnStatement);
     }
 }
Пример #5
0
 private static void RemoveTypeHint(IInspectionResult result, ParserRuleContext functionContext, IModuleRewriter rewriter)
 {
     if (result.Target.TypeHint != null)
     {
         rewriter.Remove(functionContext.GetDescendent <VBAParser.TypeHintContext>());
     }
 }
Пример #6
0
        private void PromoteVariable(IModuleRewriter rewriter, Declaration target)
        {
            if (new[] { DeclarationType.ClassModule, DeclarationType.ProceduralModule }.Contains(target.ParentDeclaration.DeclarationType))
            {
                _messageBox.NotifyWarn(RubberduckUI.PromoteVariable_InvalidSelection, RubberduckUI.IntroduceParameter_Caption);
                return;
            }

            var oldSelection = _vbe.GetActiveSelection();

            rewriter.Remove(target);
            AddField(rewriter, target);

            if (oldSelection.HasValue)
            {
                using (var module = _state.ProjectsProvider.Component(oldSelection.Value.QualifiedName).CodeModule)
                {
                    using (var pane = module.CodePane)
                    {
                        pane.Selection = oldSelection.Value.Selection;
                    }
                }
            }

            rewriter.Rewrite();
        }
Пример #7
0
        public override void Fix(IInspectionResult result)
        {
            IModuleRewriter rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName);
            var             context  = result.Context;

            rewriter.Remove(context);
        }
Пример #8
0
        private static void RemoveAnnotationMarker(VBAParser.AnnotationListContext annotationList,
                                                   IAnnotation annotation, IModuleRewriter rewriter)
        {
            var index = Array.IndexOf(annotationList.annotation(), annotation.Context);

            rewriter.Remove(annotationList.AT(index));
        }
        /// <summary>
        /// Removes variable declaration and subsequent <c>VBAParser.EndOfStatementContext</c>
        /// depending on the <paramref name="removeEndOfStmtContext"/> flag.
        /// This function is intended to be called only once per rewriter within a given <c>ModuleRewriteSession</c>.
        /// </summary>
        /// <remarks>
        /// Calling this function with <paramref name="removeEndOfStmtContext"/> defaulted to <c>true</c>
        /// avoids leaving residual newlines between the deleted declaration and the next declaration.
        /// The one-time call constraint is required for scenarios where variables to delete are declared in a list.  Specifically,
        /// the use case where all the variables in the list are to be removed.
        /// If the variables to remove are not declared in a list, then this function can be called multiple times.
        /// </remarks>
        public static void RemoveVariables(this IModuleRewriter rewriter, IEnumerable <VariableDeclaration> toRemove, bool removeEndOfStmtContext = true)
        {
            if (!toRemove.Any())
            {
                return;
            }

            var fieldsToDeleteByListContext = toRemove.Distinct()
                                              .ToLookup(f => f.Context.GetAncestor <VBAParser.VariableListStmtContext>());

            foreach (var fieldsToDelete in fieldsToDeleteByListContext)
            {
                var variableList = fieldsToDelete.Key.children.OfType <VBAParser.VariableSubStmtContext>();

                if (variableList.Count() == fieldsToDelete.Count())
                {
                    if (fieldsToDelete.First().ParentDeclaration.DeclarationType.HasFlag(DeclarationType.Module))
                    {
                        rewriter.RemoveDeclarationContext <VBAParser.ModuleDeclarationsElementContext>(fieldsToDelete.First(), removeEndOfStmtContext);
                    }
                    else
                    {
                        rewriter.RemoveDeclarationContext <VBAParser.BlockStmtContext>(fieldsToDelete.First(), removeEndOfStmtContext);
                    }
                    continue;
                }

                foreach (var target in fieldsToDelete)
                {
                    rewriter.Remove(target);
                }
            }
        }
Пример #10
0
 private void RemoveReturnStatements(ModuleBodyElementDeclaration member, IModuleRewriter rewriter)
 {
     foreach (var returnStatement in GetReturnStatements(member))
     {
         rewriter.Remove(returnStatement);
     }
 }
 private void SetFieldToPrivate(IModuleRewriter rewriter)
 {
     if (_model.TargetDeclaration.Accessibility != Accessibility.Private)
     {
         rewriter.Remove(_model.TargetDeclaration);
     }
 }
Пример #12
0
 private static void RemoveTypeHint(ModuleBodyElementDeclaration member, ParserRuleContext functionContext, IModuleRewriter rewriter)
 {
     if (member.TypeHint != null)
     {
         rewriter.Remove(functionContext.GetDescendent <VBAParser.TypeHintContext>());
     }
 }
Пример #13
0
        private void PromoteVariable(IModuleRewriter rewriter, Declaration target)
        {
            if (new[] { DeclarationType.ClassModule, DeclarationType.ProceduralModule }.Contains(target.ParentDeclaration.DeclarationType))
            {
                _messageBox.Show(RubberduckUI.PromoteVariable_InvalidSelection, RubberduckUI.IntroduceParameter_Caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            QualifiedSelection?oldSelection = null;

            if (_vbe.ActiveCodePane != null)
            {
                oldSelection = _vbe.ActiveCodePane.CodeModule.GetQualifiedSelection();
            }

            rewriter.Remove(target);
            AddField(rewriter, target);

            if (oldSelection.HasValue)
            {
                var module = oldSelection.Value.QualifiedName.Component.CodeModule;
                var pane   = module.CodePane;
                {
                    pane.Selection = oldSelection.Value.Selection;
                }
            }

            rewriter.Rewrite();
        }
Пример #14
0
 private static void RemoveNodes(IModuleRewriter rewriter, IEnumerable <AttributeNode> attributeNodes)
 {
     foreach (var node in attributeNodes)
     {
         var attributeContext = node.Context;
         rewriter.Remove(attributeContext);
         if (attributeContext.TryGetFollowingContext(out VBAParser.EndOfLineContext followingEndOfLine))
         {
             rewriter.Remove(followingEndOfLine);
         }
         else if (attributeContext.TryGetPrecedingContext(out VBAParser.EndOfLineContext precedingEndOfLine))
         {
             //We are on the last line. So, we must remove the preceding newline.
             rewriter.Remove(precedingEndOfLine);
         }
     }
 }
        private void UpdateContext(VBAParser.ElseIfBlockContext context, IModuleRewriter rewriter)
        {
            if (BlockHasDeclaration(context.block()))
            {
                rewriter.InsertBefore(((VBAParser.IfStmtContext)context.Parent).Start.TokenIndex, AdjustedBlockText(context.block()));
            }

            rewriter.Remove(context);
        }
        private void UpdateContext(VBAParser.ElseBlockContext context, IModuleRewriter rewriter)
        {
            var elseBlock = context.block();

            if (elseBlock.ChildCount == 0)
            {
                rewriter.Remove(context);
            }
        }
 private void UpdateCondition(VBAParser.LogicalNotOpContext condition, IModuleRewriter rewriter)
 {
     if (condition.whiteSpace() != null)
     {
         rewriter.RemoveRange(condition.NOT().Symbol.TokenIndex, condition.whiteSpace().Stop.TokenIndex);
     }
     else
     {
         rewriter.Remove(condition.NOT());
     }
 }
        private static void RemoveWhiteSpaceAfterAnnotation(VBAParser.AnnotationListContext annotationList,
                                                            IAnnotation annotation, IModuleRewriter rewriter)
        {
            var whitespace = annotationList.whiteSpace().FirstOrDefault(ws =>
                                                                        ws.Start.StartIndex == annotation.Context.Stop.StopIndex + 1);

            if (whitespace != null)
            {
                rewriter.Remove(whitespace);
            }
        }
Пример #19
0
        private void ConvertFunction(IInspectionResult result, VBAParser.FunctionStmtContext functionContext, IModuleRewriter rewriter)
        {
            var asTypeContext = functionContext.GetChild <VBAParser.AsTypeClauseContext>();

            if (asTypeContext != null)
            {
                rewriter.Remove(asTypeContext);
                rewriter.Remove(functionContext.children.ElementAt(functionContext.children.IndexOf(asTypeContext) - 1) as ParserRuleContext);
            }

            if (result.Target.TypeHint != null)
            {
                rewriter.Remove(functionContext.GetDescendent <VBAParser.TypeHintContext>());
            }

            rewriter.Replace(functionContext.FUNCTION(), Tokens.Sub);
            rewriter.Replace(functionContext.END_FUNCTION(), "End Sub");

            foreach (var returnStatement in GetReturnStatements(result.Target))
            {
                rewriter.Remove(returnStatement);
            }
        }
Пример #20
0
        private void ConvertPropertyGet(IInspectionResult result, VBAParser.PropertyGetStmtContext propertyGetContext, IModuleRewriter rewriter)
        {
            var asTypeContext = propertyGetContext.GetChild <VBAParser.AsTypeClauseContext>();

            if (asTypeContext != null)
            {
                rewriter.Remove(asTypeContext);
                rewriter.Remove(propertyGetContext.children.ElementAt(propertyGetContext.children.IndexOf(asTypeContext) - 1) as ParserRuleContext);
            }

            if (result.Target.TypeHint != null)
            {
                rewriter.Remove(propertyGetContext.GetDescendent <VBAParser.TypeHintContext>());
            }

            rewriter.Replace(propertyGetContext.PROPERTY_GET(), Tokens.Sub);
            rewriter.Replace(propertyGetContext.END_PROPERTY(), "End Sub");

            foreach (var returnStatement in GetReturnStatements(result.Target))
            {
                rewriter.Remove(returnStatement);
            }
        }
        private void UpdateContext(VBAParser.IfWithEmptyThenContext context, IModuleRewriter rewriter)
        {
            var elseClause = context.singleLineElseClause();

            if (context.singleLineElseClause().whiteSpace() != null)
            {
                rewriter.RemoveRange(elseClause.ELSE().Symbol.TokenIndex, elseClause.whiteSpace().Stop.TokenIndex);
            }
            else
            {
                rewriter.Remove(elseClause.ELSE());
            }

            Debug.Assert(context.booleanExpression().children.Count == 1);
            UpdateCondition((dynamic)context.booleanExpression().children[0], rewriter);
        }
Пример #22
0
        private void InsertLocalVariableDeclarationAndAssignment(IModuleRewriter rewriter, Declaration target, string localIdentifier)
        {
            var localVariableDeclaration = $"{Tokens.Dim} {localIdentifier} {Tokens.As} {target.AsTypeName}";

            var requiresAssignmentUsingSet =
                target.References.Any(refItem => VariableRequiresSetAssignmentEvaluator.RequiresSetAssignment(refItem, _declarationFinderProvider));

            var localVariableAssignment =
                $"{(requiresAssignmentUsingSet ? $"{Tokens.Set} " : string.Empty)}{localIdentifier} = {target.IdentifierName}";

            var endOfStmtCtxt          = ((ParserRuleContext)target.Context.Parent.Parent).GetChild <VBAParser.EndOfStatementContext>();
            var eosContent             = endOfStmtCtxt.GetText();
            var idxLastNewLine         = eosContent.LastIndexOf(Environment.NewLine, StringComparison.InvariantCultureIgnoreCase);
            var endOfStmtCtxtComment   = eosContent.Substring(0, idxLastNewLine);
            var endOfStmtCtxtEndFormat = eosContent.Substring(idxLastNewLine);

            var insertCtxt = ((ParserRuleContext)target.Context.Parent.Parent).GetChild <VBAParser.AsTypeClauseContext>()
                             ?? (ParserRuleContext)target.Context.Parent;

            rewriter.Remove(endOfStmtCtxt);
            rewriter.InsertAfter(insertCtxt.Stop.TokenIndex, $"{endOfStmtCtxtComment}{endOfStmtCtxtEndFormat}{localVariableDeclaration}" + $"{endOfStmtCtxtEndFormat}{localVariableAssignment}{endOfStmtCtxtEndFormat}");
        }