Skip to content

Commit f6cda0a

Browse files
committed
Merge pull request #838 from retailcoder/next
Resolver Power-Up
2 parents 941951f + 29979ca commit f6cda0a

File tree

47 files changed

+1632
-416
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1632
-416
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Antlr4.Runtime;
7+
using Microsoft.Vbe.Interop;
8+
using Rubberduck.Parsing;
9+
using Rubberduck.Parsing.Grammar;
10+
using Rubberduck.Parsing.Symbols;
11+
using Rubberduck.UI;
12+
using Rubberduck.VBEditor;
13+
using Rubberduck.VBEditor.Extensions;
14+
using Rubberduck.VBEditor.VBEHost;
15+
16+
namespace Rubberduck.Inspections
17+
{
18+
public class ImplicitActiveSheetReferenceInspection : IInspection
19+
{
20+
private readonly Lazy<IHostApplication> _hostApp;
21+
22+
public ImplicitActiveSheetReferenceInspection(VBE vbe)
23+
{
24+
_hostApp = new Lazy<IHostApplication>(vbe.HostApplication);
25+
Severity = CodeInspectionSeverity.Warning;
26+
}
27+
28+
public string Name { get { return "ImplicitActiveSheetReferenceInspection"; } }
29+
public string Description { get { return RubberduckUI.ImplicitActiveSheetReference_; } }
30+
public CodeInspectionType InspectionType { get { return CodeInspectionType.MaintainabilityAndReadabilityIssues; } }
31+
public CodeInspectionSeverity Severity { get; set; }
32+
33+
private static readonly string[] Targets =
34+
{
35+
"Cells", "Range", "Columns", "Rows"
36+
};
37+
38+
public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
39+
{
40+
if (_hostApp.Value.ApplicationName != "Excel")
41+
{
42+
return new CodeInspectionResultBase[] {};
43+
// if host isn't Excel, the ExcelObjectModel declarations shouldn't be loaded anyway.
44+
}
45+
46+
var issues = parseResult.Declarations.Items.Where(item => item.IsBuiltIn
47+
&& item.ParentScope == "Excel.Global"
48+
&& Targets.Contains(item.IdentifierName)
49+
&& item.References.Any())
50+
.SelectMany(declaration => declaration.References);
51+
52+
return issues.Select(issue =>
53+
new ImplicitActiveSheetReferenceInspectionResult(this, string.Format(Description, issue.Declaration.IdentifierName), issue.Context, issue.QualifiedModuleName));
54+
}
55+
}
56+
57+
public class ImplicitActiveSheetReferenceInspectionResult : CodeInspectionResultBase
58+
{
59+
//private readonly IEnumerable<CodeInspectionQuickFix> _quickFixes;
60+
61+
public ImplicitActiveSheetReferenceInspectionResult(IInspection inspection, string result, ParserRuleContext context, QualifiedModuleName qualifiedName)
62+
: base(inspection, result, qualifiedName, context)
63+
{
64+
//_quickFixes = new CodeInspectionQuickFix[]{};
65+
}
66+
67+
//public override IEnumerable<CodeInspectionQuickFix> QuickFixes { get { return _quickFixes; } }
68+
}
69+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.Vbe.Interop;
5+
using Rubberduck.Parsing;
6+
using Rubberduck.UI;
7+
using Rubberduck.VBEditor.Extensions;
8+
using Rubberduck.VBEditor.VBEHost;
9+
10+
namespace Rubberduck.Inspections
11+
{
12+
public class ImplicitActiveWorkbookReferenceInspection : IInspection
13+
{
14+
private readonly Lazy<IHostApplication> _hostApp;
15+
16+
public ImplicitActiveWorkbookReferenceInspection(VBE vbe)
17+
{
18+
_hostApp = new Lazy<IHostApplication>(vbe.HostApplication);
19+
Severity = CodeInspectionSeverity.Warning;
20+
}
21+
22+
public string Name { get { return "ImplicitActiveWorkbookReferenceInspection"; } }
23+
public string Description { get { return RubberduckUI.ImplicitActiveWorkbookReference_; } }
24+
public CodeInspectionType InspectionType { get { return CodeInspectionType.MaintainabilityAndReadabilityIssues; } }
25+
public CodeInspectionSeverity Severity { get; set; }
26+
27+
private static readonly string[] Targets =
28+
{
29+
"Worksheets", "Sheets", "Names",
30+
};
31+
32+
public IEnumerable<CodeInspectionResultBase> GetInspectionResults(VBProjectParseResult parseResult)
33+
{
34+
if (_hostApp.Value.ApplicationName != "Excel")
35+
{
36+
return new CodeInspectionResultBase[] {};
37+
// if host isn't Excel, the ExcelObjectModel declarations shouldn't be loaded anyway.
38+
}
39+
40+
var issues = parseResult.Declarations.Items.Where(item => item.IsBuiltIn
41+
&& item.ParentScope == "Excel.Global"
42+
&& Targets.Contains(item.IdentifierName)
43+
&& item.References.Any())
44+
.SelectMany(declaration => declaration.References);
45+
46+
return issues.Select(issue =>
47+
new ImplicitActiveSheetReferenceInspectionResult(this, string.Format(Description, issue.Declaration.IdentifierName), issue.Context, issue.QualifiedModuleName));
48+
}
49+
}
50+
}

RetailCoder.VBE/Root/CommandBarsModule.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ private void ConfigureCodePaneContextMenu()
5656
{
5757
const int listMembersMenuId = 2529;
5858
var parent = _kernel.Get<VBE>().CommandBars[CodeWindow].Controls;
59-
var beforeIndex = parent.Cast<CommandBarControl>().First(control => control.Id == listMembersMenuId).Index;
59+
var beforeControl = parent.Cast<CommandBarControl>().FirstOrDefault(control => control.Id == listMembersMenuId);
60+
var beforeIndex = beforeControl == null ? 1 : beforeControl.Index;
6061

6162
var items = GetCodePaneContextMenuItems();
6263
BindParentMenuItem<CodePaneContextParentMenu>(parent, beforeIndex, items);
@@ -66,7 +67,8 @@ private void ConfigureFormDesignerContextMenu()
6667
{
6768
const int viewCodeMenuId = 2558;
6869
var parent = _kernel.Get<VBE>().CommandBars[MsForms].Controls;
69-
var beforeIndex = parent.Cast<CommandBarControl>().First(control => control.Id == viewCodeMenuId).Index;
70+
var beforeControl = parent.Cast<CommandBarControl>().FirstOrDefault(control => control.Id == viewCodeMenuId);
71+
var beforeIndex = beforeControl == null ? 1 : beforeControl.Index;
7072

7173
var items = GetFormDesignerContextMenuItems();
7274
BindParentMenuItem<FormDesignerContextParentMenu>(parent, beforeIndex, items);
@@ -76,7 +78,8 @@ private void ConfigureFormDesignerControlContextMenu()
7678
{
7779
const int viewCodeMenuId = 2558;
7880
var parent = _kernel.Get<VBE>().CommandBars[MsFormsControl].Controls;
79-
var beforeIndex = parent.Cast<CommandBarControl>().First(control => control.Id == viewCodeMenuId).Index;
81+
var beforeControl = parent.Cast<CommandBarControl>().FirstOrDefault(control => control.Id == viewCodeMenuId);
82+
var beforeIndex = beforeControl == null ? 1 : beforeControl.Index;
8083

8184
var items = GetFormDesignerContextMenuItems();
8285
BindParentMenuItem<FormDesignerControlContextParentMenu>(parent, beforeIndex, items);
@@ -86,7 +89,8 @@ private void ConfigureProjectExplorerContextMenu()
8689
{
8790
const int projectPropertiesMenuId = 2578;
8891
var parent = _kernel.Get<VBE>().CommandBars[ProjectWindow].Controls;
89-
var beforeIndex = parent.Cast<CommandBarControl>().First(control => control.Id == projectPropertiesMenuId).Index;
92+
var beforeControl = parent.Cast<CommandBarControl>().FirstOrDefault(control => control.Id == projectPropertiesMenuId);
93+
var beforeIndex = beforeControl == null ? 1 : beforeControl.Index;
9094

9195
var items = GetProjectWindowContextMenuItems();
9296
BindParentMenuItem<ProjectWindowContextParentMenu>(parent, beforeIndex, items);

RetailCoder.VBE/Root/RubberduckModule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Rubberduck.UI.CodeInspections;
1515
using Rubberduck.UI.Command;
1616
using Rubberduck.UI.UnitTesting;
17+
using Rubberduck.VBEditor.Extensions;
1718
using Rubberduck.VBEditor.VBEHost;
1819

1920
namespace Rubberduck.Root

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@
271271
<Compile Include="Common\ClipboardWriter.cs" />
272272
<Compile Include="Common\DeclarationExtensions.cs" />
273273
<Compile Include="Inspections\CodeInspectionQuickFix.cs" />
274+
<Compile Include="Inspections\ImplicitActiveSheetReferenceInspection.cs" />
275+
<Compile Include="Inspections\ImplicitActiveWorkbookReferenceInspection.cs" />
274276
<Compile Include="Inspections\UntypedFunctionUsageInspectionResult.cs" />
275277
<Compile Include="Inspections\WriteOnlyPropertyInspection.cs" />
276278
<Compile Include="Inspections\UntypedFunctionUsageInspection.cs" />

RetailCoder.VBE/UI/ParserProgress/ParserProgessControl.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</Expander.HeaderTemplate>
3434
<ListView Margin="2" FlowDirection="LeftToRight" ItemsSource="{Binding Details}">
3535
<ListView.ItemTemplate>
36-
<DataTemplate>
36+
<DataTemplate DataType="local:ComponentProgressViewModel">
3737
<Grid>
3838
<Grid.ColumnDefinitions>
3939
<ColumnDefinition Width="20" />
@@ -42,7 +42,7 @@
4242
</Grid.ColumnDefinitions>
4343
<Image Grid.Column="0" Margin="2" Width="16" Source="{Binding ComponentIcon}" />
4444
<TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{Binding ComponentName}" TextWrapping="Wrap" />
45-
<ProgressBar Grid.Column="2" Value="{Binding ResolutionProgressPercent}" Minimum="0" Maximum="100" HorizontalAlignment="Stretch" />
45+
<ProgressBar Grid.Column="2" Value="{Binding ResolutionProgressPercent, Mode=OneWay}" Minimum="0" Maximum="100" HorizontalAlignment="Stretch" />
4646
</Grid>
4747
</DataTemplate>
4848
</ListView.ItemTemplate>

RetailCoder.VBE/UI/ParserProgress/ParserProgessViewModel.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ParserProgessViewModel(IRubberduckParser parser, VBProject project)
2424
_parser.ParseStarted += _parser_ParseStarted;
2525
_parser.ParseProgress += _parser_ParseProgress;
2626
_parser.ResolutionProgress += _parser_ResolutionProgress;
27-
_parser.ParseCompleted += _parser_ParseCompleted;
27+
_parser.ResolutionCompleted += _parser_Completed;
2828

2929
_project = project;
3030
var details = _project.VBComponents.Cast<VBComponent>().Select(component => new ComponentProgressViewModel(component)).ToList();
@@ -51,7 +51,7 @@ public void Start()
5151
}
5252

5353
public event EventHandler<ParseCompletedEventArgs> Completed;
54-
void _parser_ParseCompleted(object sender, ParseCompletedEventArgs e)
54+
void _parser_Completed(object sender, ParseCompletedEventArgs e)
5555
{
5656
var handler = Completed;
5757
if (handler != null)
@@ -69,20 +69,11 @@ void _parser_ResolutionProgress(object sender, ResolutionProgressEventArgs e)
6969
return;
7070
}
7171
row.ResolutionProgressPercent = e.PercentProgress;
72-
row.ComponentIcon = e.PercentProgress == 1
73-
? GetImageSource(Resources.hourglass)
74-
: DeclarationIconCache.ComponentIcon(e.Component.Type);
7572
}
7673

7774
void _parser_ParseProgress(object sender, ParseProgressEventArgs e)
7875
{
7976
StatusText = string.Format(RubberduckUI.ParseProgress, e.Component.Collection.Parent.Name + "." + e.Component.Name);
80-
var row = _details.SingleOrDefault(vm => vm.ComponentName == e.Component.Name);
81-
if (row == null)
82-
{
83-
return;
84-
}
85-
row.ComponentIcon = GetImageSource(Resources.tick);
8677
}
8778

8879
void _parser_ParseStarted(object sender, ParseStartedEventArgs e)
@@ -93,20 +84,17 @@ void _parser_ParseStarted(object sender, ParseStartedEventArgs e)
9384

9485
public class ComponentProgressViewModel : ViewModelBase
9586
{
87+
private readonly VBComponent _component;
88+
9689
public ComponentProgressViewModel(VBComponent component)
9790
{
91+
_component = component;
9892
ComponentName = component.Name;
9993
}
10094

101-
private BitmapImage _componentIcon;
10295
public BitmapImage ComponentIcon
10396
{
104-
get { return _componentIcon; }
105-
set
106-
{
107-
_componentIcon = value;
108-
OnPropertyChanged();
109-
}
97+
get { return DeclarationIconCache.ComponentIcon(_component.Type); }
11098
}
11199

112100
public string ComponentName { get; private set; }

RetailCoder.VBE/UI/RubberduckUI.Designer.cs

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

RetailCoder.VBE/UI/RubberduckUI.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,4 +1191,16 @@ Are you sure you want to proceed with this rename?</value>
11911191
<data name="ParserProgress_ShowDetails" xml:space="preserve">
11921192
<value>Details</value>
11931193
</data>
1194+
<data name="ImplicitActiveSheetReference_" xml:space="preserve">
1195+
<value>'{0}' is an implicit reference to the active worksheet</value>
1196+
</data>
1197+
<data name="ImplicitActiveWorkbookReference_" xml:space="preserve">
1198+
<value>'{0}' is an implicit reference to the active workbook</value>
1199+
</data>
1200+
<data name="MakeActiveSheetReferenceExplicit" xml:space="preserve">
1201+
<value>Make ActiveSheet reference explicit</value>
1202+
</data>
1203+
<data name="MakeActiveWorkbookReferenceExplicit" xml:space="preserve">
1204+
<value>Make ActiveWorkbook reference explicit</value>
1205+
</data>
11941206
</root>

Rubberduck.Parsing/IParseResultProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ public interface IParseResultProvider
6161
event EventHandler<ResolutionProgressEventArgs> ResolutionProgress;
6262
event EventHandler<ParseProgressEventArgs> ParseProgress;
6363
event EventHandler<ParseCompletedEventArgs> ParseCompleted;
64+
event EventHandler<ParseCompletedEventArgs> ResolutionCompleted;
6465
}
6566
}

0 commit comments

Comments
 (0)