Skip to content

Commit 2e827a5

Browse files
authored
Merge pull request #243 from rubberduck-vba/next
sync with main repo
2 parents 9ef72ec + def1ce6 commit 2e827a5

21 files changed

+601
-686
lines changed

RetailCoder.VBE/Inspections/Abstract/InspectionBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected bool IsIgnoringInspectionResultFor(IVBComponent component, int line)
104104
}
105105

106106
// VBE 1-based indexing
107-
for (var i = line - 1; i >= 1; i--)
107+
for (var i = line; i >= 1; i--)
108108
{
109109
var annotation = annotations.SingleOrDefault(a => a.QualifiedSelection.Selection.StartLine == i) as IgnoreAnnotation;
110110
if (annotation != null && annotation.InspectionNames.Contains(AnnotationName))

RetailCoder.VBE/Inspections/QuickFixes/AssignedByValParameterMakeLocalCopyQuickFix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private void InsertLocalVariableDeclarationAndAssignment(IModuleRewriter rewrite
103103
+ (_target.AsTypeDeclaration is ClassModuleDeclaration ? Tokens.Set + " " : string.Empty)
104104
+ localIdentifier + " = " + _target.IdentifierName;
105105

106-
rewriter.InsertAtIndex("\r\n" + content, ((ParserRuleContext)_target.Context.Parent).Stop.TokenIndex + 1);
106+
rewriter.InsertBefore(((ParserRuleContext)_target.Context.Parent).Stop.TokenIndex + 1, "\r\n" + content);
107107
}
108108
}
109109
}

RetailCoder.VBE/Navigation/CodeExplorer/CodeExplorerViewModel.cs

Lines changed: 55 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -35,51 +35,51 @@ public CodeExplorerViewModel(FolderHelper folderHelper, RubberduckParserState st
3535

3636
var reparseCommand = commands.OfType<ReparseCommand>().SingleOrDefault();
3737

38-
_refreshCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(),
38+
RefreshCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(),
3939
reparseCommand == null ? (Action<object>)(o => { }) :
4040
o => reparseCommand.Execute(o),
4141
o => !IsBusy && reparseCommand != null && reparseCommand.CanExecute(o));
4242

43-
_navigateCommand = commands.OfType<UI.CodeExplorer.Commands.NavigateCommand>().SingleOrDefault();
43+
NavigateCommand = commands.OfType<UI.CodeExplorer.Commands.NavigateCommand>().SingleOrDefault();
4444

45-
_addTestModuleCommand = commands.OfType<UI.CodeExplorer.Commands.AddTestModuleCommand>().SingleOrDefault();
46-
_addStdModuleCommand = commands.OfType<AddStdModuleCommand>().SingleOrDefault();
47-
_addClassModuleCommand = commands.OfType<AddClassModuleCommand>().SingleOrDefault();
48-
_addUserFormCommand = commands.OfType<AddUserFormCommand>().SingleOrDefault();
45+
AddTestModuleCommand = commands.OfType<UI.CodeExplorer.Commands.AddTestModuleCommand>().SingleOrDefault();
46+
AddStdModuleCommand = commands.OfType<AddStdModuleCommand>().SingleOrDefault();
47+
AddClassModuleCommand = commands.OfType<AddClassModuleCommand>().SingleOrDefault();
48+
AddUserFormCommand = commands.OfType<AddUserFormCommand>().SingleOrDefault();
4949

50-
_openDesignerCommand = commands.OfType<OpenDesignerCommand>().SingleOrDefault();
51-
_openProjectPropertiesCommand = commands.OfType<OpenProjectPropertiesCommand>().SingleOrDefault();
52-
_renameCommand = commands.OfType<RenameCommand>().SingleOrDefault();
53-
_indenterCommand = commands.OfType<IndentCommand>().SingleOrDefault();
50+
OpenDesignerCommand = commands.OfType<OpenDesignerCommand>().SingleOrDefault();
51+
OpenProjectPropertiesCommand = commands.OfType<OpenProjectPropertiesCommand>().SingleOrDefault();
52+
RenameCommand = commands.OfType<RenameCommand>().SingleOrDefault();
53+
IndenterCommand = commands.OfType<IndentCommand>().SingleOrDefault();
5454

55-
_findAllReferencesCommand = commands.OfType<UI.CodeExplorer.Commands.FindAllReferencesCommand>().SingleOrDefault();
56-
_findAllImplementationsCommand = commands.OfType<UI.CodeExplorer.Commands.FindAllImplementationsCommand>().SingleOrDefault();
55+
FindAllReferencesCommand = commands.OfType<UI.CodeExplorer.Commands.FindAllReferencesCommand>().SingleOrDefault();
56+
FindAllImplementationsCommand = commands.OfType<UI.CodeExplorer.Commands.FindAllImplementationsCommand>().SingleOrDefault();
5757

58-
_collapseAllSubnodesCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteCollapseNodes);
59-
_expandAllSubnodesCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteExpandNodes);
58+
CollapseAllSubnodesCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteCollapseNodes);
59+
ExpandAllSubnodesCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteExpandNodes);
6060

61-
_importCommand = commands.OfType<ImportCommand>().SingleOrDefault();
62-
_exportCommand = commands.OfType<ExportCommand>().SingleOrDefault();
61+
ImportCommand = commands.OfType<ImportCommand>().SingleOrDefault();
62+
ExportCommand = commands.OfType<ExportCommand>().SingleOrDefault();
6363
_externalRemoveCommand = commands.OfType<RemoveCommand>().SingleOrDefault();
6464
if (_externalRemoveCommand != null)
6565
{
66-
_removeCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteRemoveComand, _externalRemoveCommand.CanExecute);
66+
RemoveCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteRemoveComand, _externalRemoveCommand.CanExecute);
6767
}
6868

69-
_printCommand = commands.OfType<PrintCommand>().SingleOrDefault();
69+
PrintCommand = commands.OfType<PrintCommand>().SingleOrDefault();
7070

71-
_commitCommand = commands.OfType<CommitCommand>().SingleOrDefault();
72-
_undoCommand = commands.OfType<UndoCommand>().SingleOrDefault();
71+
CommitCommand = commands.OfType<CommitCommand>().SingleOrDefault();
72+
UndoCommand = commands.OfType<UndoCommand>().SingleOrDefault();
7373

74-
_copyResultsCommand = commands.OfType<CopyResultsCommand>().SingleOrDefault();
74+
CopyResultsCommand = commands.OfType<CopyResultsCommand>().SingleOrDefault();
7575

76-
_setNameSortCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), param =>
76+
SetNameSortCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), param =>
7777
{
7878
SortByName = (bool)param;
7979
SortBySelection = !(bool)param;
8080
});
8181

82-
_setSelectionSortCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), param =>
82+
SetSelectionSortCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), param =>
8383
{
8484
SortBySelection = (bool)param;
8585
SortByName = !(bool)param;
@@ -141,14 +141,11 @@ public bool SortBySelection
141141
}
142142
}
143143

144-
private readonly CommandBase _copyResultsCommand;
145-
public CommandBase CopyResultsCommand { get { return _copyResultsCommand; } }
144+
public CommandBase CopyResultsCommand { get; }
146145

147-
private readonly CommandBase _setNameSortCommand;
148-
public CommandBase SetNameSortCommand { get { return _setNameSortCommand; } }
146+
public CommandBase SetNameSortCommand { get; }
149147

150-
private readonly CommandBase _setSelectionSortCommand;
151-
public CommandBase SetSelectionSortCommand { get { return _setSelectionSortCommand; } }
148+
public CommandBase SetSelectionSortCommand { get; }
152149

153150
private bool _sortByType = true;
154151
public bool SortByType
@@ -193,9 +190,8 @@ public string PanelTitle
193190

194191
var declaration = SelectedItem.GetSelectedDeclaration();
195192

196-
var nameWithDeclarationType = declaration.IdentifierName +
197-
string.Format(" - ({0})", RubberduckUI.ResourceManager.GetString(
198-
"DeclarationType_" + declaration.DeclarationType, CultureInfo.CurrentUICulture));
193+
var nameWithDeclarationType = declaration.IdentifierName +
194+
$" - ({RubberduckUI.ResourceManager.GetString("DeclarationType_" + declaration.DeclarationType, CultureInfo.CurrentUICulture)})";
199195

200196
if (string.IsNullOrEmpty(declaration.AsTypeName))
201197
{
@@ -228,9 +224,9 @@ public string Description
228224
}
229225
}
230226

231-
public bool CanExecuteIndenterCommand { get { return IndenterCommand.CanExecute(SelectedItem); } }
232-
public bool CanExecuteRenameCommand { get { return RenameCommand.CanExecute(SelectedItem); } }
233-
public bool CanExecuteFindAllReferencesCommand { get { return FindAllReferencesCommand.CanExecute(SelectedItem); } }
227+
public bool CanExecuteIndenterCommand => IndenterCommand.CanExecute(SelectedItem);
228+
public bool CanExecuteRenameCommand => RenameCommand.CanExecute(SelectedItem);
229+
public bool CanExecuteFindAllReferencesCommand => FindAllReferencesCommand.CanExecute(SelectedItem);
234230

235231
private ObservableCollection<CodeExplorerItemViewModel> _projects;
236232
public ObservableCollection<CodeExplorerItemViewModel> Projects
@@ -407,14 +403,11 @@ private void SetErrorState(CodeExplorerItemViewModel itemNode, IVBComponent comp
407403
return;
408404
}
409405

410-
if (node is CodeExplorerComponentViewModel)
406+
var componentNode = node as CodeExplorerComponentViewModel;
407+
if (componentNode?.GetSelectedDeclaration().QualifiedName.QualifiedModuleName.Component.Equals(component) == true)
411408
{
412-
var componentNode = (CodeExplorerComponentViewModel)node;
413-
if (componentNode.GetSelectedDeclaration().QualifiedName.QualifiedModuleName.Component.Equals(component))
414-
{
415-
componentNode.IsErrorState = true;
416-
_errorStateSet = true;
417-
}
409+
componentNode.IsErrorState = true;
410+
_errorStateSet = true;
418411
}
419412
}
420413
}
@@ -446,65 +439,36 @@ private void SwitchNodeState(CodeExplorerItemViewModel node, bool expandedState)
446439
}
447440
}
448441

449-
private readonly CommandBase _refreshCommand;
450-
public CommandBase RefreshCommand { get { return _refreshCommand; } }
451-
452-
private readonly CommandBase _navigateCommand;
453-
public CommandBase NavigateCommand { get { return _navigateCommand; } }
454-
455-
private readonly CommandBase _addTestModuleCommand;
456-
public CommandBase AddTestModuleCommand { get { return _addTestModuleCommand; } }
457-
458-
private readonly CommandBase _addStdModuleCommand;
459-
public CommandBase AddStdModuleCommand { get { return _addStdModuleCommand; } }
460-
461-
private readonly CommandBase _addClassModuleCommand;
462-
public CommandBase AddClassModuleCommand { get { return _addClassModuleCommand; } }
463-
464-
private readonly CommandBase _addUserFormCommand;
465-
public CommandBase AddUserFormCommand { get { return _addUserFormCommand; } }
466-
467-
private readonly CommandBase _openDesignerCommand;
468-
public CommandBase OpenDesignerCommand { get { return _openDesignerCommand; } }
469-
470-
private readonly CommandBase _openProjectPropertiesCommand;
471-
public CommandBase OpenProjectPropertiesCommand { get { return _openProjectPropertiesCommand; } }
472-
473-
private readonly CommandBase _renameCommand;
474-
public CommandBase RenameCommand { get { return _renameCommand; } }
475-
476-
private readonly CommandBase _indenterCommand;
477-
public CommandBase IndenterCommand { get { return _indenterCommand; } }
442+
public CommandBase RefreshCommand { get; }
478443

479-
private readonly CommandBase _findAllReferencesCommand;
480-
public CommandBase FindAllReferencesCommand { get { return _findAllReferencesCommand; } }
444+
public CommandBase NavigateCommand { get; }
481445

482-
private readonly CommandBase _findAllImplementationsCommand;
483-
public CommandBase FindAllImplementationsCommand { get { return _findAllImplementationsCommand; } }
446+
public CommandBase AddTestModuleCommand { get; }
447+
public CommandBase AddStdModuleCommand { get; }
448+
public CommandBase AddClassModuleCommand { get; }
449+
public CommandBase AddUserFormCommand { get; }
484450

485-
private readonly CommandBase _collapseAllSubnodesCommand;
486-
public CommandBase CollapseAllSubnodesCommand { get { return _collapseAllSubnodesCommand; } }
451+
public CommandBase OpenDesignerCommand { get; }
452+
public CommandBase OpenProjectPropertiesCommand { get; }
487453

488-
private readonly CommandBase _expandAllSubnodesCommand;
489-
public CommandBase ExpandAllSubnodesCommand { get { return _expandAllSubnodesCommand; } }
454+
public CommandBase RenameCommand { get; }
490455

491-
private readonly CommandBase _importCommand;
492-
public CommandBase ImportCommand { get { return _importCommand; } }
456+
public CommandBase IndenterCommand { get; }
493457

494-
private readonly CommandBase _exportCommand;
495-
public CommandBase ExportCommand { get { return _exportCommand; } }
458+
public CommandBase FindAllReferencesCommand { get; }
459+
public CommandBase FindAllImplementationsCommand { get; }
496460

497-
private readonly CommandBase _removeCommand;
498-
public CommandBase RemoveCommand { get { return _removeCommand; } }
461+
public CommandBase CollapseAllSubnodesCommand { get; }
462+
public CommandBase ExpandAllSubnodesCommand { get; }
499463

500-
private readonly CommandBase _printCommand;
501-
public CommandBase PrintCommand { get { return _printCommand; } }
464+
public CommandBase ImportCommand { get; }
465+
public CommandBase ExportCommand { get; }
466+
public CommandBase RemoveCommand { get; }
502467

503-
private readonly CommandBase _commitCommand;
504-
public CommandBase CommitCommand { get { return _commitCommand; } }
468+
public CommandBase PrintCommand { get; }
505469

506-
private readonly CommandBase _undoCommand;
507-
public CommandBase UndoCommand { get { return _undoCommand; } }
470+
public CommandBase CommitCommand { get; }
471+
public CommandBase UndoCommand { get; }
508472

509473
private readonly CommandBase _externalRemoveCommand;
510474

RetailCoder.VBE/Refactorings/EncapsulateField/EncapsulateFieldRefactoring.cs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using Rubberduck.Parsing.PostProcessing;
45
using Rubberduck.Parsing.Symbols;
@@ -15,6 +16,8 @@ public class EncapsulateFieldRefactoring : IRefactoring
1516
private readonly IRefactoringPresenterFactory<IEncapsulateFieldPresenter> _factory;
1617
private EncapsulateFieldModel _model;
1718

19+
private readonly HashSet<IModuleRewriter> _referenceRewriters = new HashSet<IModuleRewriter>();
20+
1821
public EncapsulateFieldRefactoring(IVBE vbe, IIndenter indenter, IRefactoringPresenterFactory<IEncapsulateFieldPresenter> factory)
1922
{
2023
_vbe = vbe;
@@ -35,6 +38,10 @@ public void Refactor()
3538
AddProperty(rewriter);
3639

3740
rewriter.Rewrite();
41+
foreach (var referenceRewriter in _referenceRewriters)
42+
{
43+
referenceRewriter.Rewrite();
44+
}
3845
}
3946

4047
public void Refactor(QualifiedSelection target)
@@ -58,46 +65,54 @@ private void AddProperty(IModuleRewriter rewriter)
5865

5966
var members = _model.State.DeclarationFinder
6067
.Members(_model.TargetDeclaration.QualifiedName.QualifiedModuleName)
61-
.OrderBy(declaration => declaration.QualifiedSelection)
62-
.ToArray();
68+
.OrderBy(declaration => declaration.QualifiedSelection);
6369

64-
var property = Environment.NewLine + Environment.NewLine + GetPropertyText() + Environment.NewLine;
70+
var fields = members.Where(d => d.DeclarationType == DeclarationType.Variable && !d.ParentScopeDeclaration.DeclarationType.HasFlag(DeclarationType.Member)).ToList();
6571

66-
var lastMember = members.LastOrDefault(m => m.DeclarationType.HasFlag(DeclarationType.Member));
67-
if (lastMember == null)
72+
var property = Environment.NewLine + Environment.NewLine + GetPropertyText();
73+
if (members.Any(m => m.DeclarationType.HasFlag(DeclarationType.Member)))
6874
{
69-
rewriter.InsertAtIndex(property, 1);
75+
property += Environment.NewLine;
76+
}
77+
78+
if (_model.TargetDeclaration.Accessibility != Accessibility.Private)
79+
{
80+
var newField = "Private " + _model.TargetDeclaration.IdentifierName + " As " + _model.TargetDeclaration.AsTypeName;
81+
if (fields.Count > 1)
82+
{
83+
newField = Environment.NewLine + newField;
84+
}
85+
86+
property = newField + property;
87+
}
88+
89+
if (_model.TargetDeclaration.Accessibility == Accessibility.Private || fields.Count > 1)
90+
{
91+
rewriter.InsertAfter(fields.Last().Context.Stop.TokenIndex, property);
7092
}
7193
else
7294
{
73-
rewriter.InsertAtIndex(property, lastMember.Context.Stop.TokenIndex);
95+
rewriter.InsertBefore(0, property);
7496
}
7597
}
7698

7799
private void UpdateReferences()
78100
{
79101
foreach (var reference in _model.TargetDeclaration.References)
80102
{
81-
var module = reference.QualifiedModuleName.Component.CodeModule;
82-
var oldLine = module.GetLines(reference.Selection.StartLine, 1);
83-
oldLine = oldLine.Remove(reference.Selection.StartColumn - 1, reference.Selection.EndColumn - reference.Selection.StartColumn);
84-
var newLine = oldLine.Insert(reference.Selection.StartColumn - 1, _model.PropertyName);
103+
var rewriter = _model.State.GetRewriter(reference.QualifiedModuleName);
104+
rewriter.Replace(reference.Context, _model.PropertyName);
85105

86-
module.ReplaceLine(reference.Selection.StartLine, newLine);
106+
_referenceRewriters.Add(rewriter);
87107
}
88108
}
89109

90110
private void SetFieldToPrivate(IModuleRewriter rewriter)
91111
{
92-
var target = _model.TargetDeclaration;
93-
if (target.Accessibility == Accessibility.Private)
112+
if (_model.TargetDeclaration.Accessibility != Accessibility.Private)
94113
{
95-
return;
114+
rewriter.Remove(_model.TargetDeclaration);
96115
}
97-
98-
var newField = "Private " + _model.TargetDeclaration.IdentifierName + " As " + _model.TargetDeclaration.AsTypeName + Environment.NewLine;
99-
100-
rewriter.Replace(target, newField);
101116
}
102117

103118
private string GetPropertyText()

RetailCoder.VBE/Refactorings/IntroduceField/IntroduceFieldRefactoring.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ private void PromoteVariable(IModuleRewriter rewriter, Declaration target)
9595
pane.Selection = oldSelection.Value.Selection;
9696
}
9797
}
98+
99+
rewriter.Rewrite();
98100
}
99101

100102
private void AddField(IModuleRewriter rewriter, Declaration target)
@@ -105,7 +107,7 @@ private void AddField(IModuleRewriter rewriter, Declaration target)
105107
.OrderByDescending(item => item.Selection);
106108

107109
var firstMember = members.FirstOrDefault();
108-
rewriter.InsertAtIndex(content, firstMember?.Context.Start.TokenIndex ?? 0);
110+
rewriter.InsertBefore(firstMember?.Context.Start.TokenIndex ?? 0, content);
109111
}
110112
}
111113
}

0 commit comments

Comments
 (0)