protected override void OnExecute(object parameter)
        {
            if (!CanExecute(parameter) ||
                !(parameter is CodeExplorerItemViewModel node) ||
                node.Declaration == null)
            {
                return;
            }

            switch (node)
            {
            case CodeExplorerProjectViewModel model:
            {
                var declaration = model.Declaration;

                var componentDeclarations = _state.AllUserDeclarations.Where(c =>
                                                                             c.DeclarationType.HasFlag(DeclarationType.Module) &&
                                                                             !c.Annotations.Any(pta => pta.Annotation is NoIndentAnnotation) &&
                                                                             c.ProjectId == declaration.ProjectId);

                foreach (var componentDeclaration in componentDeclarations)
                {
                    _indenter.Indent(_state.ProjectsProvider.Component(componentDeclaration.QualifiedName.QualifiedModuleName));
                }

                break;
            }

            case CodeExplorerCustomFolderViewModel folder:
            {
                var components = folder.Children.OfType <CodeExplorerComponentViewModel>()      //TODO: this has the filter applied.
                                 .Select(s => s.Declaration)
                                 .Where(d => !d.Annotations.Any(pta => pta.Annotation is NoIndentAnnotation))
                                 .Select(d => _state.ProjectsProvider.Component(d.QualifiedName.QualifiedModuleName));

                foreach (var component in components)
                {
                    _indenter.Indent(component);
                }

                break;
            }

            case CodeExplorerComponentViewModel component:
                _indenter.Indent(_state.ProjectsProvider.Component(component.Declaration.QualifiedModuleName));
                break;

            case CodeExplorerMemberViewModel member:
                if (!member.QualifiedSelection.HasValue)
                {
                    return;
                }
                _navigateCommand.Execute(member.QualifiedSelection.Value.GetNavitationArgs());
                _indenter.IndentCurrentProcedure();
                break;
            }
        }
Esempio n. 2
0
        protected override void OnExecute(object parameter)
        {
            if (parameter == null)
            {
                return;
            }

            var node = (CodeExplorerItemViewModel)parameter;

            if (!node.QualifiedSelection.HasValue && !(node is CodeExplorerCustomFolderViewModel))
            {
                return;
            }

            if (node is CodeExplorerProjectViewModel)
            {
                var declaration = ((ICodeExplorerDeclarationViewModel)node).Declaration;

                var componentDeclarations = _state.AllUserDeclarations.Where(c =>
                                                                             c.DeclarationType.HasFlag(DeclarationType.Module) &&
                                                                             c.Annotations.All(a => a.AnnotationType != AnnotationType.NoIndent) &&
                                                                             c.ProjectId == declaration.ProjectId);

                foreach (var componentDeclaration in componentDeclarations)
                {
                    _indenter.Indent(_state.ProjectsProvider.Component(componentDeclaration.QualifiedName.QualifiedModuleName));
                }
            }

            if (node is CodeExplorerCustomFolderViewModel)
            {
                var components = node.Items.OfType <CodeExplorerComponentViewModel>()
                                 .Select(s => s.Declaration)
                                 .Where(d => d.Annotations.All(a => a.AnnotationType != AnnotationType.NoIndent))
                                 .Select(d => _state.ProjectsProvider.Component(d.QualifiedName.QualifiedModuleName));

                foreach (var component in components)
                {
                    _indenter.Indent(component);
                }
            }

            if (node is CodeExplorerComponentViewModel)
            {
                _indenter.Indent(_state.ProjectsProvider.Component(node.QualifiedSelection.Value.QualifiedName));
            }

            if (node is CodeExplorerMemberViewModel)
            {
                _navigateCommand.Execute(node.QualifiedSelection.Value.GetNavitationArgs());

                _indenter.IndentCurrentProcedure();
            }
        }
        private string GetNewTestModuleCode(IVBComponent component)
        {
            var settings   = _settings.Create();
            var baseModule = (GetBaseTestModule(settings) + Environment.NewLine);

            if (settings.DefaultTestStubInNewModule)
            {
                baseModule += Environment.NewLine + GetNewTestMethodCode(component);
            }

            return(string.Join(Environment.NewLine, _indenter.Indent(baseModule)));
        }
Esempio n. 4
0
        private string GetPropertyText()
        {
            if (TargetDeclaration == null)
            {
                return(string.Empty);
            }

            var getterText = string.Join(Environment.NewLine,
                                         string.Format("Public Property Get {0}() As {1}", NewPropertyName, TargetDeclaration.AsTypeName),
                                         string.Format("    {0}{1} = {2}", MustImplementSetSetterType || !CanImplementLetSetterType ? "Set " : string.Empty, NewPropertyName, TargetDeclaration.IdentifierName),
                                         "End Property");

            var letterText = string.Join(Environment.NewLine,
                                         string.Format(Environment.NewLine + Environment.NewLine + "Public Property Let {0}(ByVal {1} As {2})",
                                                       NewPropertyName, ParameterName, TargetDeclaration.AsTypeName),
                                         string.Format("    {0} = {1}", TargetDeclaration.IdentifierName, ParameterName),
                                         "End Property");

            var setterText = string.Join(Environment.NewLine,
                                         string.Format(Environment.NewLine + Environment.NewLine + "Public Property Set {0}(ByVal {1} As {2})",
                                                       NewPropertyName, ParameterName, TargetDeclaration.AsTypeName),
                                         string.Format("    Set {0} = {1}", TargetDeclaration.IdentifierName, ParameterName),
                                         "End Property");

            var propertyText = getterText +
                               (MustImplementLetSetterType ? letterText : string.Empty) +
                               (MustImplementSetSetterType ? setterText : string.Empty);

            var propertyTextLines = propertyText.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            return(string.Join(Environment.NewLine, _indenter.Indent(propertyTextLines, "test", false)));
        }
        private string GetPropertyText()
        {
            var getterText = string.Join(Environment.NewLine,
                                         string.Format(Environment.NewLine + "Public Property Get {0}() As {1}", _model.PropertyName,
                                                       _model.TargetDeclaration.AsTypeName),
                                         string.Format("    {0}{1} = {2}", !_model.CanImplementLet || _model.ImplementSetSetterType ? "Set " : string.Empty, _model.PropertyName, _model.TargetDeclaration.IdentifierName),
                                         "End Property" + Environment.NewLine);

            var letterText = string.Join(Environment.NewLine,
                                         string.Format(Environment.NewLine + "Public Property Let {0}(ByVal {1} As {2})",
                                                       _model.PropertyName, _model.ParameterName, _model.TargetDeclaration.AsTypeName),
                                         string.Format("    {0} = {1}", _model.TargetDeclaration.IdentifierName, _model.ParameterName),
                                         "End Property" + Environment.NewLine);

            var setterText = string.Join(Environment.NewLine,
                                         string.Format(Environment.NewLine + "Public Property Set {0}(ByVal {1} As {2})",
                                                       _model.PropertyName, _model.ParameterName, _model.TargetDeclaration.AsTypeName),
                                         string.Format("    Set {0} = {1}", _model.TargetDeclaration.IdentifierName, _model.ParameterName),
                                         "End Property" + Environment.NewLine);

            var propertyText = string.Join(string.Empty,
                                           getterText,
                                           (_model.ImplementLetSetterType ? letterText : string.Empty),
                                           (_model.ImplementSetSetterType ? setterText : string.Empty)).TrimEnd() + Environment.NewLine;

            var propertyTextLines = propertyText.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            return(string.Join(Environment.NewLine, _indenter.Indent(propertyTextLines, "test", false)));
        }
        private void InsertBlocks(EncapsulateFieldInsertNewCodeModel model, IRewriteSession rewriteSession)
        {
            var newDeclarationSectionBlock = model.NewContentAggregator.RetrieveBlock(NewContentType.UserDefinedTypeDeclaration, NewContentType.DeclarationBlock, NewContentType.CodeSectionBlock);

            if (string.IsNullOrEmpty(newDeclarationSectionBlock))
            {
                return;
            }

            var allNewContent = string.Join(NewLines.DOUBLE_SPACE, new string[] { newDeclarationSectionBlock });

            var previewMarker = model.NewContentAggregator.RetrieveBlock(RubberduckUI.EncapsulateField_PreviewMarker);

            if (!string.IsNullOrEmpty(previewMarker))
            {
                allNewContent = $"{allNewContent}{Environment.NewLine}{previewMarker}";
            }

            var formattedContent = string.Join(Environment.NewLine, _indenter.Indent(allNewContent));

            var rewriter = rewriteSession.CheckOutModuleRewriter(model.QualifiedModuleName);

            var codeSectionStartIndex = _declarationFinderProvider.DeclarationFinder
                                        .Members(model.QualifiedModuleName).Where(m => m.IsMember())
                                        .OrderBy(c => c.Selection)
                                        .FirstOrDefault()?.Context.Start.TokenIndex;

            if (codeSectionStartIndex.HasValue)
            {
                rewriter.InsertBefore(codeSectionStartIndex.Value, $"{formattedContent}{NewLines.DOUBLE_SPACE}");
                return;
            }
            rewriter.InsertBefore(rewriter.TokenStream.Size - 1, $"{NewLines.DOUBLE_SPACE}{formattedContent}");
        }
Esempio n. 7
0
        private void UpdatePreview()
        {
            if (TargetDeclaration == null)
            {
                PreviewBox.Text = string.Empty;
            }
            var propertyTextLines = _previewGenerator.AllPropertyCode.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            PreviewBox.Text = string.Join(Environment.NewLine, _indenter.Indent(propertyTextLines, true));
        }
Esempio n. 8
0
        private void GeneratePreview()
        {
            _model.Method.MethodName     = _view.MethodName;
            _model.Method.Accessibility  = _view.Accessibility;
            _model.Method.Parameters     = _view.Parameters;
            _model.Method.ReturnValue    = _view.ReturnValue;
            _model.Method.SetReturnValue = _view.SetReturnValue;

            var code = ExtractMethodRefactoring.GetExtractedMethod(_model).Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            _indenter.Indent(code, "Preview", false);

            _view.Preview = string.Join(Environment.NewLine, code);
        }
        private string GetPropertyText()
        {
            var generator = new PropertyGenerator
            {
                PropertyName   = _model.PropertyName,
                AsTypeName     = _model.TargetDeclaration.AsTypeName,
                BackingField   = _model.TargetDeclaration.IdentifierName,
                ParameterName  = _model.ParameterName,
                GenerateSetter = _model.ImplementSetSetterType,
                GenerateLetter = _model.ImplementLetSetterType
            };

            var propertyTextLines = generator.AllPropertyCode.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            return(string.Join(Environment.NewLine, _indenter.Indent(propertyTextLines, true)));
        }
Esempio n. 10
0
        private void GeneratePreview(IExtractMethodModel extractMethodModel, IExtractMethodProc extractMethodProc)
        {
            extractMethodModel.Method.MethodName    = _view.MethodName;
            extractMethodModel.Method.Accessibility = _view.Accessibility;
            extractMethodModel.Method.Parameters    = _view.Parameters;

            /*
             * extractMethodModel.Method.ReturnValue = _view.ReturnValue;
             * extractMethodModel.Method.SetReturnValue = _view.SetReturnValue;
             */
            var extractedMethod = extractMethodProc.createProc(extractMethodModel);
            var code            = extractedMethod.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            code          = _indenter.Indent(code).ToArray();
            _view.Preview = string.Join(Environment.NewLine, code);
        }