Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This quickfix adjusts an attribute out of sync with a corresponding annotation to the value corresponding to the annotation.
- Loading branch information
Showing
6 changed files
with
179 additions
and
7 deletions.
There are no files selected for viewing
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
45 changes: 45 additions & 0 deletions
45
Rubberduck.CodeAnalysis/QuickFixes/AdjustAttributeValuesQuickFix.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,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Rubberduck.Inspections.Abstract; | ||
using Rubberduck.Inspections.Concrete; | ||
using Rubberduck.Parsing.Annotations; | ||
using Rubberduck.Parsing.Inspections.Abstract; | ||
using Rubberduck.Parsing.Rewriter; | ||
using Rubberduck.Parsing.Symbols; | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.Parsing.VBA.Parsing; | ||
|
||
namespace Rubberduck.Inspections.QuickFixes | ||
{ | ||
public class AdjustAttributeValuesQuickFix : QuickFixBase | ||
{ | ||
private readonly IAttributesUpdater _attributesUpdater; | ||
|
||
public AdjustAttributeValuesQuickFix(IAttributesUpdater attributesUpdater) | ||
: base(typeof(AttributeValueOutOfSyncInspection)) | ||
{ | ||
_attributesUpdater = attributesUpdater; | ||
} | ||
|
||
public override void Fix(IInspectionResult result, IRewriteSession rewriteSession) | ||
{ | ||
var declaration = result.Target; | ||
IAttributeAnnotation annotation = result.Properties.Annotation; | ||
IReadOnlyList<string> attributeValues = result.Properties.AttributeValues; | ||
|
||
var attributeName = declaration.DeclarationType.HasFlag(DeclarationType.Module) | ||
? annotation.Attribute | ||
: $"{declaration.IdentifierName}.{annotation.Attribute}"; | ||
|
||
_attributesUpdater.UpdateAttribute(rewriteSession, declaration, attributeName, annotation.AttributeValues, attributeValues); | ||
} | ||
|
||
public override string Description(IInspectionResult result) => Resources.Inspections.QuickFixes.AdjustAttributeValuesQuickFix; | ||
|
||
public override CodeKind TargetCodeKind => CodeKind.AttributesCode; | ||
|
||
public override bool CanFixInProcedure => true; | ||
public override bool CanFixInModule => true; | ||
public override bool CanFixInProject => 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
109 changes: 109 additions & 0 deletions
109
RubberduckTests/QuickFixes/AdjustAttributeValuesQuickFixTests.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,109 @@ | ||
using NUnit.Framework; | ||
using Rubberduck.Inspections.Concrete; | ||
using Rubberduck.Inspections.QuickFixes; | ||
using Rubberduck.Parsing.Inspections.Abstract; | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.Parsing.VBA.Parsing; | ||
|
||
namespace RubberduckTests.QuickFixes | ||
{ | ||
public class AdjustAttributeValuesQuickFixTests : QuickFixTestBase | ||
{ | ||
[Test] | ||
[Category("QuickFixes")] | ||
public void ModuleAttributeOutOfSync_QuickFixWorks() | ||
{ | ||
const string inputCode = | ||
@"Attribute VB_Description = ""NotDesc"" | ||
'@ModuleAttribute VB_Description, ""Desc"" | ||
Public Sub Foo() | ||
Const const1 As Integer = 9 | ||
End Sub"; | ||
|
||
const string expectedCode = | ||
@"Attribute VB_Description = ""Desc"" | ||
'@ModuleAttribute VB_Description, ""Desc"" | ||
Public Sub Foo() | ||
Const const1 As Integer = 9 | ||
End Sub"; | ||
|
||
var actualCode = ApplyQuickFixToFirstInspectionResult(inputCode, state => new AttributeValueOutOfSyncInspection(state), CodeKind.AttributesCode); | ||
Assert.AreEqual(expectedCode, actualCode); | ||
} | ||
|
||
[Test] | ||
[Category("QuickFixes")] | ||
public void VbExtKeyModuleAttributeOutOfSync_QuickFixWorks() | ||
{ | ||
const string inputCode = | ||
@"Attribute VB_Ext_Key = ""Key"", ""NotValue"" | ||
Attribute VB_Ext_Key = ""OtherKey"", ""OtherValue"" | ||
'@ModuleAttribute VB_Ext_Key, ""Key"", ""Value"" | ||
Public Sub Foo() | ||
Const const1 As Integer = 9 | ||
End Sub"; | ||
|
||
const string expectedCode = | ||
@"Attribute VB_Ext_Key = ""Key"", ""Value"" | ||
Attribute VB_Ext_Key = ""OtherKey"", ""OtherValue"" | ||
'@ModuleAttribute VB_Ext_Key, ""Key"", ""Value"" | ||
Public Sub Foo() | ||
Const const1 As Integer = 9 | ||
End Sub"; | ||
|
||
var actualCode = ApplyQuickFixToFirstInspectionResult(inputCode, state => new AttributeValueOutOfSyncInspection(state), CodeKind.AttributesCode); | ||
Assert.AreEqual(expectedCode, actualCode); | ||
} | ||
|
||
[Test] | ||
[Category("QuickFixes")] | ||
public void MemberAttributeOutOfSync_QuickFixWorks() | ||
{ | ||
const string inputCode = | ||
@"'@MemberAttribute VB_Description, ""Desc"" | ||
Public Sub Foo() | ||
Attribute Foo.VB_Description = ""NotDesc"" | ||
Const const1 As Integer = 9 | ||
End Sub"; | ||
|
||
const string expectedCode = | ||
@"'@MemberAttribute VB_Description, ""Desc"" | ||
Public Sub Foo() | ||
Attribute Foo.VB_Description = ""Desc"" | ||
Const const1 As Integer = 9 | ||
End Sub"; | ||
|
||
var actualCode = ApplyQuickFixToFirstInspectionResult(inputCode, state => new AttributeValueOutOfSyncInspection(state), CodeKind.AttributesCode); | ||
Assert.AreEqual(expectedCode, actualCode); | ||
} | ||
|
||
[Test] | ||
[Category("QuickFixes")] | ||
public void VbExtKeyMemberAttributeOutOfSync_QuickFixWorks() | ||
{ | ||
const string inputCode = | ||
@"'@MemberAttribute VB_Ext_Key, ""Key"", ""Value"" | ||
Public Sub Foo() | ||
Attribute Foo.VB_Ext_Key = ""Key"", ""NotValue"" | ||
Attribute Foo.VB_Ext_Key = ""OtherKey"", ""OtherValue"" | ||
Const const1 As Integer = 9 | ||
End Sub"; | ||
|
||
const string expectedCode = | ||
@"'@MemberAttribute VB_Ext_Key, ""Key"", ""Value"" | ||
Public Sub Foo() | ||
Attribute Foo.VB_Ext_Key = ""Key"", ""Value"" | ||
Attribute Foo.VB_Ext_Key = ""OtherKey"", ""OtherValue"" | ||
Const const1 As Integer = 9 | ||
End Sub"; | ||
|
||
var actualCode = ApplyQuickFixToFirstInspectionResult(inputCode, state => new AttributeValueOutOfSyncInspection(state), CodeKind.AttributesCode); | ||
Assert.AreEqual(expectedCode, actualCode); | ||
} | ||
|
||
protected override IQuickFix QuickFix(RubberduckParserState state) | ||
{ | ||
return new AdjustAttributeValuesQuickFix(new AttributesUpdater(state)); | ||
} | ||
} | ||
} |