Skip to content

Commit 889ff94

Browse files
author
Andrin Meier
committed
Merge branch 'next' of https://github.com/rubberduck-vba/Rubberduck into next
2 parents 0db6006 + b037a76 commit 889ff94

25 files changed

+1218
-266
lines changed

RetailCoder.VBE/Inspections/ProcedureShouldBeFunctionInspection.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using Rubberduck.Common;
34
using Rubberduck.Parsing;
45
using Rubberduck.Parsing.Grammar;
6+
using Rubberduck.Parsing.Symbols;
57
using Rubberduck.Parsing.VBA;
68

79
namespace Rubberduck.Inspections
@@ -19,14 +21,60 @@ public ProcedureShouldBeFunctionInspection(RubberduckParserState state)
1921

2022
public override IEnumerable<CodeInspectionResultBase> GetInspectionResults()
2123
{
22-
return State.ArgListsWithOneByRefParam
24+
var subStmts = State.ArgListsWithOneByRefParam
2325
.Where(context => context.Context.Parent is VBAParser.SubStmtContext)
26+
.Select(context => (VBAParser.SubStmtContext)context.Context.Parent)
27+
.ToList();
28+
29+
var subStmtsNotImplementingInterfaces = subStmts
30+
.Where(c =>
31+
{
32+
var declaration =
33+
UserDeclarations.SingleOrDefault(d => d.DeclarationType == DeclarationType.Procedure &&
34+
d.IdentifierName == c.ambiguousIdentifier().GetText() &&
35+
d.Context.GetSelection().Equals(c.GetSelection()));
36+
37+
var interfaceImplementation = UserDeclarations.FindInterfaceImplementationMembers().SingleOrDefault(m => m.Equals(declaration));
38+
39+
if (interfaceImplementation == null) { return true; }
40+
41+
var interfaceMember = UserDeclarations.FindInterfaceMember(interfaceImplementation);
42+
return interfaceMember == null;
43+
});
44+
45+
var subStmtsNotImplementingEvents = subStmts
46+
.Where(c =>
47+
{
48+
var declaration = UserDeclarations.SingleOrDefault(d => d.DeclarationType == DeclarationType.Procedure &&
49+
d.IdentifierName == c.ambiguousIdentifier().GetText() &&
50+
d.Context.GetSelection().Equals(c.GetSelection()));
51+
52+
if (declaration == null) { return false; } // rather be safe than sorry
53+
54+
return UserDeclarations.Where(item => item.IsWithEvents)
55+
.All(withEvents => UserDeclarations.FindEventProcedures(withEvents) == null);
56+
});
57+
58+
return State.ArgListsWithOneByRefParam
59+
.Where(context => context.Context.Parent is VBAParser.SubStmtContext &&
60+
subStmtsNotImplementingInterfaces.Contains(context.Context.Parent) &&
61+
subStmtsNotImplementingEvents.Contains(context.Context.Parent))
2462
.Select(context => new ProcedureShouldBeFunctionInspectionResult(this,
2563
State,
2664
new QualifiedContext<VBAParser.ArgListContext>(context.ModuleName,
2765
context.Context as VBAParser.ArgListContext),
2866
new QualifiedContext<VBAParser.SubStmtContext>(context.ModuleName,
2967
context.Context.Parent as VBAParser.SubStmtContext)));
3068
}
69+
70+
private bool IsInterfaceImplementation(Declaration target)
71+
{
72+
var interfaceImplementation = State.AllUserDeclarations.FindInterfaceImplementationMembers().SingleOrDefault(m => m.Equals(target));
73+
74+
if (interfaceImplementation == null) { return false; }
75+
76+
var interfaceMember = State.AllUserDeclarations.FindInterfaceMember(interfaceImplementation);
77+
return interfaceMember != null;
78+
}
3179
}
3280
}

RetailCoder.VBE/Inspections/ProcedureShouldBeFunctionInspectionResult.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public ChangeProcedureToFunction(RubberduckParserState state,
5555

5656
public override void Fix()
5757
{
58-
UpdateSignature();
5958
UpdateCalls();
59+
UpdateSignature();
6060
}
6161

6262
private void UpdateSignature()
@@ -68,10 +68,26 @@ private void UpdateSignature()
6868
var newArgText = argText.Contains("ByRef ") ? argText.Replace("ByRef ", "ByVal ") : "ByVal " + argText;
6969

7070
var newFunctionWithoutReturn = subStmtText.Insert(subStmtText.IndexOf(argListText, StringComparison.Ordinal) + argListText.Length,
71-
_argQualifiedContext.Context.asTypeClause().GetText())
71+
" " + _argQualifiedContext.Context.asTypeClause().GetText())
7272
.Replace("Sub", "Function")
7373
.Replace(argText, newArgText);
7474

75+
var indexOfInstructionSeparators = new List<int>();
76+
var functionWithoutStringLiterals = newFunctionWithoutReturn.StripStringLiterals();
77+
for (var i = 0; i < functionWithoutStringLiterals.Length; i++)
78+
{
79+
if (functionWithoutStringLiterals[i] == ':')
80+
{
81+
indexOfInstructionSeparators.Add(i);
82+
}
83+
}
84+
85+
if (indexOfInstructionSeparators.Count > 1)
86+
{
87+
indexOfInstructionSeparators.Reverse();
88+
newFunctionWithoutReturn = indexOfInstructionSeparators.Aggregate(newFunctionWithoutReturn, (current, index) => current.Remove(index, 1).Insert(index, Environment.NewLine));
89+
}
90+
7591
var newfunctionWithReturn = newFunctionWithoutReturn
7692
.Insert(newFunctionWithoutReturn.LastIndexOf(Environment.NewLine, StringComparison.Ordinal),
7793
Environment.NewLine + " " + _subStmtQualifiedContext.Context.ambiguousIdentifier().GetText() +

RetailCoder.VBE/Refactorings/ImplementInterface/ImplementInterfaceRefactoring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ImplementInterfaceRefactoring : IRefactoring
1818
private Declaration _targetClass;
1919
private readonly IMessageBox _messageBox;
2020

21-
private const string MemberBody = " Err.Raise 5 'TODO: implement interface member";
21+
private const string MemberBody = " Err.Raise 5 'TODO implement interface member";
2222

2323
public ImplementInterfaceRefactoring(RubberduckParserState state, IActiveCodePaneEditor editor, IMessageBox messageBox)
2424
{

RetailCoder.VBE/Refactorings/IntroduceParameter/IntroduceParameterRefactoring.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,7 @@ private string GetOldSignature(Declaration target)
340340

341341
private Declaration GetInterfaceImplementation(Declaration target)
342342
{
343-
var declaration = target;
344-
var interfaceImplementation = _declarations.FindInterfaceImplementationMembers().SingleOrDefault(m => m.Equals(declaration));
343+
var interfaceImplementation = _declarations.FindInterfaceImplementationMembers().SingleOrDefault(m => m.Equals(target));
345344

346345
if (interfaceImplementation == null) { return null; }
347346

RetailCoder.VBE/Refactorings/RemoveParameters/RemoveParametersModel.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ private void AcquireTarget(QualifiedSelection selection)
3838
{
3939
TargetDeclaration = Declarations.FindTarget(selection, ValidDeclarationTypes);
4040
TargetDeclaration = PromptIfTargetImplementsInterface();
41+
TargetDeclaration = GetEvent();
4142
TargetDeclaration = GetGetter();
4243
}
4344

@@ -98,6 +99,18 @@ private Declaration PromptIfTargetImplementsInterface()
9899
return confirm == DialogResult.No ? null : interfaceMember;
99100
}
100101

102+
private Declaration GetEvent()
103+
{
104+
foreach (var events in Declarations.Where(item => item.DeclarationType == DeclarationType.Event))
105+
{
106+
if (Declarations.FindHandlersForEvent(events).Any(reference => Equals(reference.Item2, TargetDeclaration)))
107+
{
108+
return events;
109+
}
110+
}
111+
return TargetDeclaration;
112+
}
113+
101114
private Declaration GetGetter()
102115
{
103116
if (TargetDeclaration == null) { return null; }

RetailCoder.VBE/Refactorings/ReorderParameters/ReorderParametersModel.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ private void AcquireTarget(QualifiedSelection selection)
3939
{
4040
TargetDeclaration = Declarations.FindTarget(selection, ValidDeclarationTypes);
4141
TargetDeclaration = PromptIfTargetImplementsInterface();
42+
TargetDeclaration = GetEvent();
4243
TargetDeclaration = GetGetter();
4344
}
4445

@@ -88,6 +89,18 @@ private Declaration PromptIfTargetImplementsInterface()
8889
return confirm == DialogResult.No ? null : interfaceMember;
8990
}
9091

92+
private Declaration GetEvent()
93+
{
94+
foreach (var events in Declarations.Where(item => item.DeclarationType == DeclarationType.Event))
95+
{
96+
if (Declarations.FindHandlersForEvent(events).Any(reference => Equals(reference.Item2, TargetDeclaration)))
97+
{
98+
return events;
99+
}
100+
}
101+
return TargetDeclaration;
102+
}
103+
91104
private Declaration GetGetter()
92105
{
93106
if (TargetDeclaration == null)

RetailCoder.VBE/Resources/rubberduck.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<UserSettings>
44
<ToDoListSettings>
55
<ToDoMarkers>
6-
<ToDoMarker Text="NOTE:" Priority="Low" />
7-
<ToDoMarker Text="TODO:" Priority="Normal" />
8-
<ToDoMarker Text="BUG:" Priority="High" />
6+
<ToDoMarker Text="NOTE " Priority="Low" />
7+
<ToDoMarker Text="TODO " Priority="Normal" />
8+
<ToDoMarker Text="BUG " Priority="High" />
99
</ToDoMarkers>
1010
</ToDoListSettings>
1111
<CodeInspectionSettings>

RetailCoder.VBE/ToDoItems/ToDoItem.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,20 @@ public class ToDoItem
3434
private readonly QualifiedSelection _selection;
3535
public QualifiedSelection GetSelection() { return _selection; }
3636

37-
public ToDoItem(TodoPriority priority, CommentNode comment)
38-
: this(priority, comment.CommentText, comment.QualifiedSelection)
37+
public ToDoItem(string markerText, TodoPriority priority, CommentNode comment)
38+
: this(markerText, priority, comment.CommentText, comment.QualifiedSelection)
3939
{
4040
}
4141

42-
public ToDoItem(TodoPriority priority, string description, QualifiedSelection qualifiedSelection)
42+
public ToDoItem(string markerText, TodoPriority priority, string description, QualifiedSelection qualifiedSelection)
4343
{
4444
_priority = priority;
4545
_description = description;
4646
_selection = qualifiedSelection;
47-
_projectName = qualifiedSelection.QualifiedName.Project.Name;
48-
_moduleName = qualifiedSelection.QualifiedName.Component.Name;
47+
_projectName = qualifiedSelection.QualifiedName.ProjectName;
48+
_moduleName = qualifiedSelection.QualifiedName.ComponentName;
4949
_lineNumber = qualifiedSelection.Selection.StartLine;
50-
int idxDelimiter = description.IndexOf(":", StringComparison.InvariantCulture);
51-
_type = description.Substring(0, idxDelimiter);
50+
_type = markerText;
5251
}
5352
}
5453
}

RetailCoder.VBE/UI/DockableToolwindowPresenter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ protected virtual void Dispose(bool disposing)
108108
UserControl.Dispose();
109109
}
110110

111-
Marshal.ReleaseComObject(_window);
111+
if (_window != null)
112+
{
113+
Marshal.ReleaseComObject(_window);
114+
}
112115
}
113116
}
114117
}

RetailCoder.VBE/UI/RubberduckUI.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)