Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Rubberduck.Inspections/QuickFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ public IEnumerable<IQuickFix> QuickFixes(IInspectionResult result)
return _quickFixes[result.Inspection.GetType()];
}

private bool CanFix(IQuickFix fix, Type inspection)
{
return _quickFixes.ContainsKey(inspection) && _quickFixes[inspection].Contains(fix);
}

public void Fix(IQuickFix fix, IInspectionResult result)
{
if (!CanFix(fix, result.Inspection.GetType()))
{
throw new ArgumentException("Fix does not support this inspection.", nameof(result));
}

fix.Fix(result);
_state.GetRewriter(result.QualifiedSelection.QualifiedName).Rewrite();
_state.OnParseRequested(this);
Expand All @@ -46,11 +56,21 @@ public void Fix(IQuickFix fix, IInspectionResult result)
public void FixInProcedure(IQuickFix fix, QualifiedSelection selection, Type inspectionType,
IEnumerable<IInspectionResult> results)
{
if (!CanFix(fix, inspectionType))
{
throw new ArgumentException("Fix does not support this inspection.", nameof(inspectionType));
}

throw new NotImplementedException("A qualified selection does not state which proc we are in, so we could really only use this on inspection results that expose a Declaration.");
}

public void FixInModule(IQuickFix fix, QualifiedSelection selection, Type inspectionType, IEnumerable<IInspectionResult> results)
{
if (!CanFix(fix, inspectionType))
{
throw new ArgumentException("Fix does not support this inspection.", nameof(inspectionType));
}

var filteredResults = results
.Where(result => result.Inspection.GetType() == inspectionType
&& result.QualifiedSelection.QualifiedName == selection.QualifiedName)
Expand All @@ -71,6 +91,11 @@ public void FixInModule(IQuickFix fix, QualifiedSelection selection, Type inspec
public void FixInProject(IQuickFix fix, QualifiedSelection selection, Type inspectionType,
IEnumerable<IInspectionResult> results)
{
if (!CanFix(fix, inspectionType))
{
throw new ArgumentException("Fix does not support this inspection.", nameof(inspectionType));
}

var filteredResults = results
.Where(result => result.Inspection.GetType() == inspectionType
&& result.QualifiedSelection.QualifiedName.ProjectId == selection.QualifiedName.ProjectId)
Expand All @@ -95,6 +120,11 @@ public void FixInProject(IQuickFix fix, QualifiedSelection selection, Type inspe

public void FixAll(IQuickFix fix, Type inspectionType, IEnumerable<IInspectionResult> results)
{
if (!CanFix(fix, inspectionType))
{
throw new ArgumentException("Fix does not support this inspection.", nameof(inspectionType));
}

var filteredResults = results.Where(result => result.Inspection.GetType() == inspectionType);

foreach (var result in filteredResults)
Expand Down
58 changes: 58 additions & 0 deletions RubberduckTests/Inspections/QuickFixProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rubberduck.Inspections;
using Rubberduck.Inspections.Concrete;
using Rubberduck.Inspections.QuickFixes;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using RubberduckTests.Mocks;

namespace RubberduckTests.Inspections
{
[TestClass]
public class QuickFixProviderTests
{
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
[TestCategory("Inspections")]
public void ProviderDoesNotKnowAboutInspection()
{
const string inputCode =
@"Public Sub Foo()
Const const1 As Integer = 9
End Sub";

IVBComponent component;
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
var state = MockParser.CreateAndParse(vbe.Object);

var inspection = new ConstantNotUsedInspection(state);
var inspectionResults = inspection.GetInspectionResults();

var quickFixProvider = new QuickFixProvider(state, new IQuickFix[] {});
quickFixProvider.Fix(new OptionExplicitQuickFix(state), inspectionResults.First());
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
[TestCategory("Inspections")]
public void ProviderKnowsAboutInspection()
{
const string inputCode =
@"Public Sub Foo()
Const const1 As Integer = 9
End Sub";

IVBComponent component;
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
var state = MockParser.CreateAndParse(vbe.Object);

var inspection = new ConstantNotUsedInspection(state);
var inspectionResults = inspection.GetInspectionResults();

var quickFixProvider = new QuickFixProvider(state, new IQuickFix[] {new RemoveUnusedDeclarationQuickFix(state)});
quickFixProvider.Fix(new OptionExplicitQuickFix(state), inspectionResults.First());
}
}
}
1 change: 1 addition & 0 deletions RubberduckTests/RubberduckTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<Compile Include="Inspections\MemberNotOnInterfaceInspectionTests.cs" />
<Compile Include="Inspections\OptionBaseZeroInspectionTests.cs" />
<Compile Include="Inspections\PassParameterByReferenceQuickFixTests.cs" />
<Compile Include="Inspections\QuickFixProviderTests.cs" />
<Compile Include="Inspections\UndeclaredVariableInspectionTests.cs" />
<Compile Include="PostProcessing\ModuleRewriterTests.cs" />
<Compile Include="SmartIndenter\VerticalSpacingTests.cs" />
Expand Down