Skip to content

Commit e300691

Browse files
committed
2 parents 171becf + 7149649 commit e300691

17 files changed

+147
-47
lines changed

RetailCoder.VBE/App.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@ public App(VBE vbe, AddIn addIn)
3030
var constructorInfo = type.GetConstructor(Type.EmptyTypes);
3131
return constructorInfo != null ? constructorInfo.Invoke(Type.EmptyTypes) : null;
3232
})
33+
.Where(syntax => syntax != null)
3334
.Cast<ISyntax>()
3435
.ToList();
3536

3637
_inspections = Assembly.GetExecutingAssembly()
3738
.GetTypes()
38-
.Where(type => type.GetInterfaces().Contains(typeof(IInspection)))
39+
.Where(type => type.BaseType == typeof(CodeInspection))
3940
.Select(type =>
4041
{
4142
var constructor = type.GetConstructor(Type.EmptyTypes);
4243
return constructor != null ? constructor.Invoke(Type.EmptyTypes) : null;
4344
})
45+
.Where(inspection => inspection != null)
4446
.Cast<IInspection>()
4547
.ToList();
4648

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.InteropServices;
3+
using Rubberduck.VBA.Parser;
4+
5+
namespace Rubberduck.Inspections
6+
{
7+
[ComVisible(false)]
8+
public abstract class CodeInspection : IInspection
9+
{
10+
protected CodeInspection(string name, string message, CodeInspectionType type, CodeInspectionSeverity severity)
11+
{
12+
_name = name;
13+
_message = message;
14+
_inspectionType = type;
15+
Severity = severity;
16+
}
17+
18+
private readonly string _name;
19+
public string Name { get { return _name; } }
20+
21+
private readonly string _message;
22+
public string QuickFixMessage { get { return _message; } }
23+
24+
private readonly CodeInspectionType _inspectionType;
25+
public CodeInspectionType InspectionType { get { return _inspectionType; } }
26+
27+
public CodeInspectionSeverity Severity { get; set; }
28+
public bool IsEnabled { get; set; }
29+
30+
/// <summary>
31+
/// Inspects specified tree node, searching for code issues.
32+
/// </summary>
33+
/// <param name="node"></param>
34+
/// <returns></returns>
35+
public abstract IEnumerable<CodeInspectionResultBase> Inspect(SyntaxTreeNode node);
36+
}
37+
}

RetailCoder.VBE/Inspections/CodeInspectionResultBase.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ namespace Rubberduck.Inspections
77
[ComVisible(false)]
88
public abstract class CodeInspectionResultBase
99
{
10-
public CodeInspectionResultBase(Instruction instruction, CodeInspectionSeverity type, string message)
10+
public CodeInspectionResultBase(string inspection, Instruction instruction, CodeInspectionSeverity type, string message)
1111
{
12+
_name = inspection;
1213
_instruction = instruction;
1314
_type = type;
1415
_message = message;
1516
}
1617

18+
private readonly string _name;
19+
/// <summary>
20+
/// Gets a string containing the name of the code inspection.
21+
/// </summary>
22+
public string Name { get { return _name; } }
23+
1724
private readonly Instruction _instruction;
1825
/// <summary>
1926
/// Gets the <see cref="Instruction"/> containing a code issue.

RetailCoder.VBE/Inspections/CodeInspectionSeverity.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ namespace Rubberduck.Inspections
55
[ComVisible(false)]
66
public enum CodeInspectionSeverity
77
{
8+
DoNotShow,
9+
Hint,
810
Suggestion,
9-
Warning
11+
Warning,
12+
Error
1013
}
1114
}

RetailCoder.VBE/Inspections/ObsoleteCommentSyntaxInspection.cs

+9-23
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,24 @@
77
namespace Rubberduck.Inspections
88
{
99
[ComVisible(false)]
10-
public class ObsoleteCommentSyntaxInspection : IInspection
10+
public class ObsoleteCommentSyntaxInspection : CodeInspection
1111
{
1212
/// <summary>
1313
/// Parameterless constructor required for discovery of implemented code inspections.
1414
/// </summary>
1515
public ObsoleteCommentSyntaxInspection()
16+
: base("Use of obsolete Rem comment syntax",
17+
"Replace Rem reserved keyword with single quote.",
18+
CodeInspectionType.MaintainabilityAndReadabilityIssues,
19+
CodeInspectionSeverity.Suggestion)
1620
{
17-
_name = "Use of obsolete Rem comment syntax";
18-
_quickFixMessage = "Replace Rem reserved keyword with single quote.";
19-
_inspectionType = CodeInspectionType.MaintainabilityAndReadabilityIssues;
20-
_severity = CodeInspectionSeverity.Suggestion;
2121
}
2222

23-
private readonly string _name;
24-
public string Name { get { return _name; } }
25-
26-
private readonly string _quickFixMessage;
27-
public string QuickFixMessage { get { return _quickFixMessage; } }
28-
29-
private readonly CodeInspectionType _inspectionType;
30-
public CodeInspectionType InspectionType { get { return _inspectionType; } }
31-
32-
private readonly CodeInspectionSeverity _severity;
33-
public CodeInspectionSeverity Severity { get { return _severity; } }
34-
35-
public bool IsEnabled { get; set; }
36-
37-
public IEnumerable<CodeInspectionResultBase> Inspect(SyntaxTreeNode node)
23+
public override IEnumerable<CodeInspectionResultBase> Inspect(SyntaxTreeNode node)
3824
{
39-
return node.FindAllComments()
40-
.Where(instruction => instruction.Value == ReservedKeywords.Rem)
41-
.Select(instruction => new ObsoleteCommentSyntaxInspectionResult(instruction, _severity, _quickFixMessage));
25+
var comments = node.FindAllComments();
26+
var remComments = comments.Where(instruction => instruction.Value.Trim().StartsWith(ReservedKeywords.Rem));
27+
return remComments.Select(instruction => new ObsoleteCommentSyntaxInspectionResult(Name, instruction, Severity, QuickFixMessage));
4228
}
4329
}
4430
}

RetailCoder.VBE/Inspections/ObsoleteCommentSyntaxInspectionResult.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace Rubberduck.Inspections
66
{
77
public class ObsoleteCommentSyntaxInspectionResult : CodeInspectionResultBase
88
{
9-
public ObsoleteCommentSyntaxInspectionResult(Instruction instruction, CodeInspectionSeverity type, string message)
10-
: base(instruction, type, message)
9+
public ObsoleteCommentSyntaxInspectionResult(string inspection, Instruction instruction, CodeInspectionSeverity type, string message)
10+
: base(inspection, instruction, type, message)
1111
{
1212
}
1313

RetailCoder.VBE/Properties/Resources.Designer.cs

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RetailCoder.VBE/Properties/Resources.resx

+6
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,10 @@
166166
<data name="TestManager_8590_32" type="System.Resources.ResXFileRef, System.Windows.Forms">
167167
<value>..\Resources\Microsoft\TestManager_8590_32.bmp;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
168168
</data>
169+
<data name="ListsofTests_8643_24" type="System.Resources.ResXFileRef, System.Windows.Forms">
170+
<value>..\Resources\ListsofTests_8643_24.bmp;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
171+
</data>
172+
<data name="Step_RunTest_8814_32" type="System.Resources.ResXFileRef, System.Windows.Forms">
173+
<value>..\Resources\Step-RunTest_8814_32.bmp;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
174+
</data>
169175
</root>
822 Bytes
Binary file not shown.
822 Bytes
Binary file not shown.

RetailCoder.VBE/Rubberduck.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Compile Include="Extensions\CodeModuleSelection.cs" />
5353
<Compile Include="Extensions\Selection.cs" />
5454
<Compile Include="Extensions\VbeExtensions.cs" />
55+
<Compile Include="Inspections\CodeInspection.cs" />
5556
<Compile Include="Inspections\CodeInspectionResultBase.cs" />
5657
<Compile Include="Inspections\CodeInspectionSeverity.cs" />
5758
<Compile Include="Inspections\CodeInspectionType.cs" />
@@ -321,11 +322,13 @@
321322
<None Include="Resources\plus-circle.png" />
322323
</ItemGroup>
323324
<ItemGroup>
325+
<None Include="Resources\Step-RunTest_8814_32.bmp" />
324326
<None Include="Resources\Warning.bmp" />
325327
<None Include="Resources\Serious.bmp" />
326328
<None Include="Resources\OK.bmp" />
327329
<None Include="Resources\GoLtrHS.bmp" />
328330
<None Include="Resources\Critical.bmp" />
331+
<None Include="Resources\ListsofTests_8643_24.bmp" />
329332
<Content Include="Resources\Microsoft\AddClass_5561_32.bmp" />
330333
<Content Include="Resources\Microsoft\AddEvent_5539_32.bmp" />
331334
<Content Include="Resources\Microsoft\AddForm_369_32.bmp" />

RetailCoder.VBE/UI/CodeInspections/CodeInspectionsDockablePresenter.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Runtime.InteropServices;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using System.Windows.Forms;
78
using Microsoft.Vbe.Interop;
89
using Rubberduck.Inspections;
910
using Rubberduck.VBA.Parser;
@@ -33,7 +34,11 @@ private void OnRefreshCodeInspections(object sender, EventArgs e)
3334
var results = new List<CodeInspectionResultBase>();
3435
foreach (var inspection in _inspections.Where(inspection => inspection.IsEnabled))
3536
{
36-
results.AddRange(inspection.Inspect(code));
37+
var result = inspection.Inspect(code).ToArray();
38+
if (result.Length != 0)
39+
{
40+
results.AddRange(result);
41+
}
3742
}
3843

3944
DrawResultTree(results);
@@ -44,8 +49,10 @@ private void DrawResultTree(IEnumerable<CodeInspectionResultBase> results)
4449
var tree = Control.CodeInspectionResultsTree;
4550
tree.Nodes.Clear();
4651

47-
foreach (var result in results)
52+
foreach (var result in results.OrderBy(r => r.Severity))
4853
{
54+
var node = new TreeNode(result.Message);
55+
4956
tree.Nodes.Add(result.Message);
5057
}
5158
}

RetailCoder.VBE/UI/CodeInspections/CodeInspectionsWindow.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ public CodeInspectionsWindow()
2222
{
2323
InitializeComponent();
2424
RefreshButton.Click += RefreshButtonClicked;
25+
CodeInspectionResultsTree.NodeMouseDoubleClick += TreeNodeMouseDoubleClicked;
26+
}
27+
28+
public event EventHandler NavigateCodeIssue;
29+
private void TreeNodeMouseDoubleClicked(object sender, TreeNodeMouseClickEventArgs e)
30+
{
31+
var handler = NavigateCodeIssue;
32+
if (handler == null)
33+
{
34+
return;
35+
}
36+
37+
handler(this, EventArgs.Empty);
2538
}
2639

2740
public event EventHandler RefreshCodeInspections;
@@ -35,7 +48,5 @@ private void RefreshButtonClicked(object sender, EventArgs e)
3548

3649
handler(this, EventArgs.Empty);
3750
}
38-
39-
public event EventHandler NavigateCodeIssue;
4051
}
4152
}

RetailCoder.VBE/UI/Menu.cs

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Drawing;
23
using System.Runtime.InteropServices;
34
using Microsoft.Office.Core;
45
using Microsoft.Vbe.Interop;
56
using System.Windows.Forms;
7+
using Rubberduck.Properties;
68

79
namespace Rubberduck.UI
810
{
@@ -20,9 +22,31 @@ public Menu(VBE vbe, AddIn addInInstance)
2022
this.addInInstance = addInInstance;
2123
}
2224

23-
protected CommandBarButton AddMenuButton(CommandBarPopup menu)
25+
protected CommandBarButton AddMenuButton(CommandBarPopup menu, string caption, Bitmap image)
2426
{
25-
return menu.Controls.Add(MsoControlType.msoControlButton, Temporary: true) as CommandBarButton;
27+
var result = menu.Controls.Add(MsoControlType.msoControlButton, Temporary: true) as CommandBarButton;
28+
if (result == null)
29+
{
30+
throw new InvalidOperationException("Failed to create menu control.");
31+
}
32+
33+
result.Caption = caption;
34+
SetButtonImage(result, image);
35+
36+
return result;
37+
}
38+
39+
40+
41+
private static void SetButtonImage(CommandBarButton result, Bitmap image)
42+
{
43+
result.FaceId = 0;
44+
45+
if (image != null)
46+
{
47+
Clipboard.SetDataObject(image, true);
48+
result.PasteFace();
49+
}
2650
}
2751

2852
/// <summary>

RetailCoder.VBE/UI/UnitTesting/TestMenu.cs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Drawing;
23
using System.Runtime.InteropServices;
34
using System.Windows.Forms;
45
using Microsoft.Office.Core;
@@ -38,19 +39,11 @@ public void Initialize(CommandBarControls menuControls)
3839

3940
menu.Caption = "Te&st";
4041

41-
_windowsTestExplorerButton = AddMenuButton(menu);
42-
_windowsTestExplorerButton.Caption = "&Test Explorer";
43-
_windowsTestExplorerButton.FaceId = 0;
44-
45-
Clipboard.SetDataObject(Resources.TestManager_8590_32, true);
46-
_windowsTestExplorerButton.PasteFace();
47-
42+
_windowsTestExplorerButton = AddMenuButton(menu, "&Test Explorer", Resources.TestManager_8590_32);
4843
_windowsTestExplorerButton.Click += OnTestExplorerButtonClick;
4944

50-
_runAllTestsButton = AddMenuButton(menu);
45+
_runAllTestsButton = AddMenuButton(menu, "&Run All Tests", Resources.AllLoadedTests_8644_24);
5146
_runAllTestsButton.BeginGroup = true;
52-
_runAllTestsButton.Caption = "&Run All Tests";
53-
_runAllTestsButton.FaceId = 186; // a "play" icon
5447
_runAllTestsButton.Click += OnRunAllTestsButtonClick;
5548
}
5649

RetailCoder.VBE/VBA/Parser/Grammar/StringExtensions.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ public static bool HasComment(this string line, out int index)
3737
index = -1;
3838
var instruction = line.StripStringLiterals();
3939

40-
for (var cursor = 0; cursor < instruction.Length - 1; cursor++)
40+
var firstIndex = instruction.TakeWhile(c => c == ' ').Count();
41+
for (var cursor = firstIndex; cursor < instruction.Length - 1; cursor++)
4142
{
4243
if (!string.IsNullOrWhiteSpace(instruction.Trim())
4344
&&(instruction[cursor] == CommentMarker
4445
|| (cursor == ReservedKeywords.Rem.Length
45-
&& instruction.TrimStart().Substring(0, ReservedKeywords.Rem.Length) == ReservedKeywords.Rem)))
46+
&& instruction.Trim().Substring(0, ReservedKeywords.Rem.Length) == ReservedKeywords.Rem)))
4647
{
4748
index = cursor;
4849
return true;

RetailCoder.VBE/VBA/Parser/Instruction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public Instruction(LogicalCodeLine line, int startColumn, int endColumn, string
2121
if (_content.HasComment(out index))
2222
{
2323
_comment = _content.Substring(index);
24-
_instruction = _content.Substring(0, index);
24+
_instruction = _content.Trim().Substring(0, index);
2525
}
2626
else
2727
{

0 commit comments

Comments
 (0)