public void Refactor() { var presenter = _factory.Create(); if (presenter == null) { return; } _model = presenter.Show(); if (_model == null || !_model.Parameters.Where((param, index) => param.Index != index).Any() || !IsValidParamOrder()) { return; } QualifiedSelection?oldSelection = null; if (_vbe.ActiveCodePane != null) { oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection(); } AdjustReferences(_model.TargetDeclaration.References); AdjustSignatures(); if (oldSelection.HasValue) { oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection); oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus(); } _model.State.OnParseRequested(this); }
public RenameRefactoring(IVBE vbe, IRefactoringPresenterFactory <IRenamePresenter> factory, IMessageBox messageBox, IDeclarationFinderProvider declarationFinderProvider, IProjectsProvider projectsProvider, IRewritingManager rewritingManager) { _vbe = vbe; _factory = factory; _messageBox = messageBox; _declarationFinderProvider = declarationFinderProvider; _projectsProvider = projectsProvider; _rewritingManager = rewritingManager; _model = null; using (var activeCodePane = _vbe.ActiveCodePane) { _initialSelection = activeCodePane.GetQualifiedSelection(); } _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(); }
public void Refactor() { var presenter = _factory.Create(); if (presenter == null) { return; } _model = presenter.Show(); if (_model == null) { return; } QualifiedSelection?oldSelection = null; if (_vbe.ActiveCodePane != null) { oldSelection = _vbe.ActiveCodePane.CodeModule.GetQualifiedSelection(); } AddProperty(); if (oldSelection.HasValue) { var module = oldSelection.Value.QualifiedName.Component.CodeModule; var pane = module.CodePane; { pane.Selection = oldSelection.Value.Selection; } } _model.State.OnParseRequested(this); }
private void PromoteVariable(Declaration target) { if (!PromptIfMethodImplementsInterface(target)) { return; } 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.GetSelection(); } RemoveVariable(target); UpdateSignature(target); if (oldSelection.HasValue) { oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection); oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus(); } _state.OnParseRequested(this); }
public void Refactor(QualifiedSelection selection) { _targetInterface = _declarations.FindInterface(selection); _targetClass = _declarations.SingleOrDefault(d => !d.IsBuiltIn && ImplementingModuleTypes.Contains(d.DeclarationType) && d.QualifiedSelection.QualifiedName.Equals(selection.QualifiedName)); if (_targetClass == null || _targetInterface == null) { _messageBox.Show(RubberduckUI.ImplementInterface_InvalidSelectionMessage, RubberduckUI.ImplementInterface_Caption, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); return; } QualifiedSelection?oldSelection = null; if (_vbe.ActiveCodePane != null) { oldSelection = _vbe.ActiveCodePane.CodeModule.GetQualifiedSelection(); } ImplementMissingMembers(); if (oldSelection.HasValue) { var module = oldSelection.Value.QualifiedName.Component.CodeModule; var pane = module.CodePane; pane.Selection = oldSelection.Value.Selection; } _state.OnParseRequested(this); }
public void Refactor() { var presenter = _factory.Create(); if (presenter == null) { return; } _model = presenter.Show(); if (_model == null) { return; } QualifiedSelection?oldSelection = null; if (_vbe.ActiveCodePane != null) { oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection(); } AddInterface(); if (oldSelection.HasValue) { oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection); oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus(); } _state.OnParseRequested(this); }
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(); }
public void Refactor() { var presenter = _factory.Create(); if (presenter == null) { return; } _model = presenter.Show(); if (_model == null || !_model.Parameters.Any(item => item.IsRemoved)) { return; } QualifiedSelection?oldSelection = null; if (_vbe.ActiveCodePane != null) { oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection(); } RemoveParameters(); if (oldSelection.HasValue) { oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection); oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus(); } _model.State.OnParseRequested(this); }
private void PromoteVariable(Declaration target) { if (!PromptIfMethodImplementsInterface(target)) { return; } if (new[] { DeclarationType.ClassModule, DeclarationType.ProceduralModule }.Contains(target.ParentDeclaration.DeclarationType)) { _messageBox.NotifyWarn(RubberduckUI.PromoteVariable_InvalidSelection, RubberduckUI.IntroduceParameter_Caption); return; } using (var pane = _vbe.ActiveCodePane) { QualifiedSelection?oldSelection = null; if (pane != null && !pane.IsWrappingNullReference) { oldSelection = pane.GetQualifiedSelection(); } var rewriteSession = _rewritingManager.CheckOutCodePaneSession(); var rewriter = rewriteSession.CheckOutModuleRewriter(target.QualifiedModuleName); UpdateSignature(target, rewriteSession); rewriter.Remove(target); rewriteSession.TryRewrite(); if (oldSelection.HasValue && !pane.IsWrappingNullReference) { pane.Selection = oldSelection.Value.Selection; } } }
private void UpdateOtherModules(IRewriteSession rewriteSession) { QualifiedSelection?oldSelection = null; using (var pane = _vbe.ActiveCodePane) { if (!pane.IsWrappingNullReference) { oldSelection = pane.GetQualifiedSelection(); } var newTarget = _state.DeclarationFinder.MatchName(_target.IdentifierName).FirstOrDefault( item => item.ComponentName == _target.ComponentName && item.ParentScope == _target.ParentScope && item.ProjectId == _target.ProjectId && Equals(item.Selection, _target.Selection)); if (newTarget != null) { UpdateCallsToOtherModule(newTarget.References.ToList(), rewriteSession); } if (oldSelection.HasValue) { pane.Selection = oldSelection.Value.Selection; } } }
public void Refactor() { var presenter = _factory.Create(); if (presenter == null) { return; } _model = presenter.Show(); if (_model == null || !_model.Parameters.Any(item => item.IsRemoved)) { return; } QualifiedSelection?oldSelection = null; var pane = _vbe.ActiveCodePane; var module = pane.CodeModule; if (!module.IsWrappingNullReference) { oldSelection = module.GetQualifiedSelection(); } RemoveParameters(); if (oldSelection.HasValue) { pane.Selection = oldSelection.Value.Selection; } _model.State.OnParseRequested(this); }
public void Refactor() { var presenter = _factory.Create(); _model = presenter.Show(); QualifiedSelection?oldSelection = null; var pane = _vbe.ActiveCodePane; var module = pane.CodeModule; if (!pane.IsWrappingNullReference) { oldSelection = module.GetQualifiedSelection(); } if (_model != null && _model.Declarations != null) { Rename(); } if (oldSelection.HasValue) { pane.Selection = oldSelection.Value.Selection; } }
private static ISelectionService MockedSelectionService() { QualifiedSelection?activeSelection = null; var selectionServiceMock = new Mock <ISelectionService>(); selectionServiceMock.Setup(m => m.ActiveSelection()).Returns(() => activeSelection); selectionServiceMock.Setup(m => m.TrySetActiveSelection(It.IsAny <QualifiedSelection>())) .Returns(() => true).Callback((QualifiedSelection selection) => activeSelection = selection); return(selectionServiceMock.Object); }
private void MoveCloserToUsage() { if (!_target.References.Any()) { var message = string.Format(RubberduckUI.MoveCloserToUsage_TargetHasNoReferences, _target.IdentifierName); _messageBox.Show(message, RubberduckUI.MoveCloserToUsage_Caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (TargetIsReferencedFromMultipleMethods(_target)) { var message = string.Format(RubberduckUI.MoveCloserToUsage_TargetIsUsedInMultipleMethods, _target.IdentifierName); _messageBox.Show(message, RubberduckUI.MoveCloserToUsage_Caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } QualifiedSelection?oldSelection = null; using (var pane = _vbe.ActiveCodePane) { if (!pane.IsWrappingNullReference) { oldSelection = pane.GetQualifiedSelection(); } InsertNewDeclaration(); RemoveOldDeclaration(); UpdateOtherModules(); if (oldSelection.HasValue && !pane.IsWrappingNullReference) { pane.Selection = oldSelection.Value.Selection; } } foreach (var rewriter in _rewriters) { rewriter.Rewrite(); } }
private void MoveCloserToUsage() { if (!_target.References.Any()) { var message = string.Format(RubberduckUI.MoveCloserToUsage_TargetHasNoReferences, _target.IdentifierName); _messageBox.Show(message, RubberduckUI.MoveCloserToUsage_Caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (TargetIsReferencedFromMultipleMethods(_target)) { var message = string.Format(RubberduckUI.MoveCloserToUsage_TargetIsUsedInMultipleMethods, _target.IdentifierName); _messageBox.Show(message, RubberduckUI.MoveCloserToUsage_Caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } QualifiedSelection?oldSelection = null; var pane = _vbe.ActiveCodePane; var module = pane.CodeModule; { if (!module.IsWrappingNullReference) { oldSelection = module.GetQualifiedSelection(); } // it doesn't make sense to do it backwards, but we need to work from the bottom up so our selections are accurate InsertDeclaration(); if (oldSelection.HasValue) { pane.Selection = oldSelection.Value.Selection; } _state.StateChanged += _state_StateChanged; _state.OnParseRequested(this); } }
private void PromoteVariable(Declaration target) { if (!PromptIfMethodImplementsInterface(target)) { return; } if (new[] { DeclarationType.ClassModule, DeclarationType.ProceduralModule }.Contains(target.ParentDeclaration.DeclarationType)) { _messageBox.Show(RubberduckUI.PromoteVariable_InvalidSelection, RubberduckUI.IntroduceParameter_Caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } var rewriter = _state.GetRewriter(target); _rewriters.Add(rewriter); using (var pane = _vbe.ActiveCodePane) { QualifiedSelection?oldSelection = null; if (pane != null && !pane.IsWrappingNullReference) { oldSelection = pane.GetQualifiedSelection(); } UpdateSignature(target); rewriter.Remove(target); if (oldSelection.HasValue && !pane.IsWrappingNullReference) { pane.Selection = oldSelection.Value.Selection; } } foreach (var tokenRewriter in _rewriters) { tokenRewriter.Rewrite(); } }
private void MoveCloserToUsage() { if (!_target.References.Any()) { var message = string.Format(RubberduckUI.MoveCloserToUsage_TargetHasNoReferences, _target.IdentifierName); _messageBox.NotifyWarn(message, RubberduckUI.MoveCloserToUsage_Caption); return; } if (TargetIsReferencedFromMultipleMethods(_target)) { var message = string.Format(RubberduckUI.MoveCloserToUsage_TargetIsUsedInMultipleMethods, _target.IdentifierName); _messageBox.NotifyWarn(message, RubberduckUI.MoveCloserToUsage_Caption); return; } QualifiedSelection?oldSelection = null; using (var pane = _vbe.ActiveCodePane) { if (!pane.IsWrappingNullReference) { oldSelection = pane.GetQualifiedSelection(); } var rewriteSession = _rewritingManager.CheckOutCodePaneSession(); InsertNewDeclaration(rewriteSession); RemoveOldDeclaration(rewriteSession); UpdateOtherModules(rewriteSession); rewriteSession.TryRewrite(); if (oldSelection.HasValue && !pane.IsWrappingNullReference) { pane.Selection = oldSelection.Value.Selection; } } }
private void _state_StateChanged(object sender, ParserStateEventArgs e) { if (e.State != ParserState.Ready) { return; } QualifiedSelection?oldSelection = null; var pane = _vbe.ActiveCodePane; var module = pane.CodeModule; { if (!module.IsWrappingNullReference) { oldSelection = module.GetQualifiedSelection(); } var newTarget = _state.AllUserDeclarations.FirstOrDefault( item => item.ComponentName == _target.ComponentName && item.IdentifierName == _target.IdentifierName && item.ParentScope == _target.ParentScope && item.ProjectId == _target.ProjectId && Equals(item.Selection, _target.Selection)); if (newTarget != null) { UpdateCallsToOtherModule(newTarget.References.ToList()); RemoveField(newTarget); } if (oldSelection.HasValue) { pane.Selection = oldSelection.Value.Selection; } _state.StateChanged -= _state_StateChanged; _state.OnParseRequested(this); } }
public void Refactor(Declaration target) { var presenter = _factory.Create(); _model = presenter.Show(target); QualifiedSelection?oldSelection = null; if (_vbe.ActiveCodePane != null) { oldSelection = _vbe.ActiveCodePane.CodeModule.GetSelection(); } if (_model != null && _model.Declarations != null) { Rename(); } if (oldSelection.HasValue) { oldSelection.Value.QualifiedName.Component.CodeModule.SetSelection(oldSelection.Value.Selection); oldSelection.Value.QualifiedName.Component.CodeModule.CodePane.ForceFocus(); } }
private IRefactoring TestRefactoring(IRewritingManager rewritingManager, RubberduckParserState state, IMessageBox msgBox = null, QualifiedSelection?initialSelection = null) { var selectionService = MockedSelectionService(initialSelection); return(TestRefactoring(rewritingManager, state, selectionService, msgBox)); }