Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QualifyWithMeQuickFix for ImplicitContainingWorkbook/WorksheetRef…
…erenceInspection It does what the name suggests.
- Loading branch information
Showing
5 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
Rubberduck.CodeAnalysis/QuickFixes/Concrete/QualifyWithMeQuickFix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Rubberduck.CodeAnalysis.Inspections; | ||
using Rubberduck.CodeAnalysis.Inspections.Concrete; | ||
using Rubberduck.CodeAnalysis.QuickFixes.Abstract; | ||
using Rubberduck.Parsing.Grammar; | ||
using Rubberduck.Parsing.Rewriter; | ||
|
||
namespace Rubberduck.CodeAnalysis.QuickFixes.Concrete | ||
{ | ||
/// <summary> | ||
/// Qualifies an implicit reference with 'Me'. | ||
/// </summary> | ||
/// <inspections> | ||
/// <inspection name="ImplicitContainingWorksheetReferenceInspection" /> | ||
/// <inspection name="ImplicitContainingWorkbookReferenceInspection" /> | ||
/// </inspections> | ||
/// <canfix multiple="true" procedure="true" module="true" project="true" all="true" /> | ||
/// <example> | ||
/// <before> | ||
/// <![CDATA[ | ||
/// Private Sub Example() | ||
/// Dim foo As Range | ||
/// Set foo = Range("A1") | ||
/// End Sub | ||
/// ]]> | ||
/// </before> | ||
/// <after> | ||
/// <![CDATA[ | ||
/// Private Sub Example() | ||
/// Dim foo As Range | ||
/// Set foo = Me.Range("A1") | ||
/// End Sub | ||
/// ]]> | ||
/// </after> | ||
/// </example> | ||
internal class QualifyWithMeQuickFix : QuickFixBase | ||
{ | ||
public QualifyWithMeQuickFix() | ||
: base(typeof(ImplicitContainingWorkbookReferenceInspection), | ||
typeof(ImplicitContainingWorksheetReferenceInspection)) | ||
{} | ||
|
||
public override void Fix(IInspectionResult result, IRewriteSession rewriteSession) | ||
{ | ||
var rewriter = rewriteSession.CheckOutModuleRewriter(result.QualifiedSelection.QualifiedName); | ||
|
||
var context = result.Context; | ||
rewriter.InsertBefore(context.Start.TokenIndex, $"{Tokens.Me}."); | ||
} | ||
|
||
public override string Description(IInspectionResult result) | ||
{ | ||
return Resources.Inspections.QuickFixes.QualifyWithMeQuickFix; | ||
} | ||
|
||
public override bool CanFixMultiple => true; | ||
public override bool CanFixInProcedure => true; | ||
public override bool CanFixInModule => true; | ||
public override bool CanFixInProject => true; | ||
public override bool CanFixAll => true; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
RubberduckTests/QuickFixes/QualifyWithMeQuickFixTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using System.Linq; | ||
using System.Threading; | ||
using NUnit.Framework; | ||
using Rubberduck.CodeAnalysis.Inspections.Concrete; | ||
using Rubberduck.CodeAnalysis.QuickFixes; | ||
using Rubberduck.CodeAnalysis.QuickFixes.Concrete; | ||
using Rubberduck.Parsing.Symbols; | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.VBEditor.SafeComWrappers; | ||
using RubberduckTests.Mocks; | ||
|
||
namespace RubberduckTests.QuickFixes | ||
{ | ||
[TestFixture] | ||
public class QualifyWithMeQuickFixTests : QuickFixTestBase | ||
{ | ||
[Test] | ||
[Category("QuickFixes")] | ||
public void QualifiesImplicitWorkbookReferencesInWorkbooks() | ||
{ | ||
const string inputCode = | ||
@" | ||
Sub foo() | ||
Dim sheet As Worksheet | ||
Set sheet = Worksheets(""Sheet1"") | ||
End Sub"; | ||
|
||
const string expectedCode = | ||
@" | ||
Sub foo() | ||
Dim sheet As Worksheet | ||
Set sheet = Me.Worksheets(""Sheet1"") | ||
End Sub"; | ||
|
||
var actualCode = ApplyQuickFixToFirstInspectionResultForImplicitWorkbookInspection(inputCode); | ||
Assert.AreEqual(expectedCode, actualCode); | ||
} | ||
|
||
private string ApplyQuickFixToFirstInspectionResultForImplicitWorkbookInspection(string inputCode) | ||
{ | ||
var inputModule = ("SomeWorkbook", inputCode, ComponentType.Document); | ||
var vbe = MockVbeBuilder.BuildFromModules(inputModule, ReferenceLibrary.Excel).Object; | ||
|
||
var (state, rewriteManager) = MockParser.CreateAndParseWithRewritingManager(vbe); | ||
using (state) | ||
{ | ||
var documentModule = state.DeclarationFinder.UserDeclarations(DeclarationType.Document) | ||
.OfType<DocumentModuleDeclaration>() | ||
.Single(); | ||
documentModule.AddSupertypeName("Workbook"); | ||
|
||
var inspection = new ImplicitContainingWorkbookReferenceInspection(state); | ||
var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); | ||
|
||
var rewriteSession = rewriteManager.CheckOutCodePaneSession(); | ||
|
||
var quickFix = QuickFix(state); | ||
|
||
var resultToFix = inspectionResults.First(); | ||
quickFix.Fix(resultToFix, rewriteSession); | ||
|
||
var module = state.DeclarationFinder.AllModules.First(qmn => qmn.ComponentName == "SomeWorkbook"); | ||
|
||
return rewriteSession.CheckOutModuleRewriter(module).GetText(); | ||
} | ||
} | ||
|
||
[Test] | ||
[Category("QuickFixes")] | ||
public void QualifiesImplicitWorksheetReferencesInWorksheets() | ||
{ | ||
const string inputCode = | ||
@" | ||
Private Sub Example() | ||
Dim foo As Range | ||
Set foo = Range(""A1"") | ||
End Sub"; | ||
|
||
const string expectedCode = | ||
@" | ||
Private Sub Example() | ||
Dim foo As Range | ||
Set foo = Me.Range(""A1"") | ||
End Sub"; | ||
|
||
var actualCode = ApplyQuickFixToFirstInspectionResultForImplicitWorksheetInspection(inputCode); | ||
Assert.AreEqual(expectedCode, actualCode); | ||
} | ||
|
||
private string ApplyQuickFixToFirstInspectionResultForImplicitWorksheetInspection(string inputCode) | ||
{ | ||
var inputModule = ("Sheet1", inputCode, ComponentType.Document); | ||
var vbe = MockVbeBuilder.BuildFromModules(inputModule, ReferenceLibrary.Excel).Object; | ||
|
||
var (state, rewriteManager) = MockParser.CreateAndParseWithRewritingManager(vbe); | ||
using (state) | ||
{ | ||
var documentModule = state.DeclarationFinder.UserDeclarations(DeclarationType.Document) | ||
.OfType<DocumentModuleDeclaration>() | ||
.Single(); | ||
documentModule.AddSupertypeName("Worksheet"); | ||
|
||
var inspection = new ImplicitContainingWorksheetReferenceInspection(state); | ||
var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); | ||
|
||
var rewriteSession = rewriteManager.CheckOutCodePaneSession(); | ||
|
||
var quickFix = QuickFix(state); | ||
|
||
var resultToFix = inspectionResults.First(); | ||
quickFix.Fix(resultToFix, rewriteSession); | ||
|
||
var module = state.DeclarationFinder.AllModules.First(qmn => qmn.ComponentName == "Sheet1"); | ||
|
||
return rewriteSession.CheckOutModuleRewriter(module).GetText(); | ||
} | ||
} | ||
|
||
protected override IQuickFix QuickFix(RubberduckParserState state) | ||
{ | ||
return new QualifyWithMeQuickFix(); | ||
} | ||
} | ||
} |