Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CodeFix Action for Rule0001 #980

Merged
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
10 changes: 10 additions & 0 deletions BusinessCentral.LinterCop/BusinessCentral.LinterCop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
<HintPath>../ALLanguage/extension/bin/Analyzers/Microsoft.Dynamics.Nav.Analyzers.Common.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces">
<SpecificVersion>False</SpecificVersion>
<HintPath>../ALLanguage/extension/bin/Analyzers/Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System.Composition.AttributedModel">
<SpecificVersion>False</SpecificVersion>
<HintPath>../ALLanguage/extension/bin/Analyzers/System.Composition.AttributedModel.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>../ALLanguage/extension/bin/Analyzers/Newtonsoft.Json.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Collections.Immutable;
using Microsoft.Dynamics.Nav.CodeAnalysis;
using Microsoft.Dynamics.Nav.CodeAnalysis.CodeActions;
using Microsoft.Dynamics.Nav.CodeAnalysis.CodeFixes;
using Microsoft.Dynamics.Nav.CodeAnalysis.Syntax;
using Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces;

namespace BusinessCentral.LinterCop.CodeFixes;

[Microsoft.Dynamics.Nav.CodeAnalysis.CodeActions.Mef.CodeFixProvider("Fix0001FlowFieldsShouldNotBeEditableCodeFixProvider")]
public sealed class Fix0001FlowFieldsShouldNotBeEditableCodeFixProvider : CodeFixProvider
{
private class Fix0001FlowFieldsShouldNotBeEditableCodeAction : CodeAction.DocumentChangeAction
{
public override CodeActionKind Kind => CodeActionKind.QuickFix;
public override bool SupportsFixAll { get; }
public override string? FixAllSingleInstanceTitle => string.Empty;
public override string? FixAllTitle => Title;
public Fix0001FlowFieldsShouldNotBeEditableCodeAction(string title, Func<CancellationToken, Task<Document>> createChangedDocument, string equivalenceKey, bool generateFixAll)
: base(title, createChangedDocument, equivalenceKey)
{
SupportsFixAll = generateFixAll;
}
}

public sealed override ImmutableArray<string> FixableDiagnosticIds =>
ImmutableArray.Create(DiagnosticDescriptors.Rule0001FlowFieldsShouldNotBeEditable.Id);

public sealed override FixAllProvider GetFixAllProvider() =>
WellKnownFixAllProviders.BatchFixer;

public override async Task RegisterCodeFixesAsync(CodeFixContext ctx)
{
Document document = ctx.Document;
TextSpan span = ctx.Span;
CancellationToken cancellationToken = ctx.CancellationToken;

SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
RegisterInstanceCodeFix(ctx, syntaxRoot, span, document);
}

private static void RegisterInstanceCodeFix(CodeFixContext ctx, SyntaxNode syntaxRoot, TextSpan span, Document document)
{
SyntaxNode node = syntaxRoot.FindNode(span);
ctx.RegisterCodeFix(CreateCodeAction(node, document, true), ctx.Diagnostics[0]);
}

private static Fix0001FlowFieldsShouldNotBeEditableCodeAction CreateCodeAction(SyntaxNode node, Document document, bool generateFixAll) =>
new(LinterCopAnalyzers.Fix0001FlowFieldsShouldNotBeEditableCodeAction, ct => SetEditablePropertyForField(document, node, ct), "Fix0001FlowFieldsShouldNotBeEditableCodeFixProvider", generateFixAll);

private static async Task<Document> SetEditablePropertyForField(Document document, SyntaxNode node, CancellationToken cancellationToken)
{
if (node.Parent is not FieldSyntax originalFieldNode)
return document;

FieldSyntax newFieldNode;
var editableProperty = originalFieldNode.GetProperty(PropertyKind.Editable.ToString());
if (editableProperty is null)
{
newFieldNode = originalFieldNode.AddPropertyListProperties(GetEditableFalseProperty());
}
else
{
var newPropertyList = UpdateEditablePropertyList(originalFieldNode, editableProperty);
newFieldNode = originalFieldNode.WithPropertyList(newPropertyList);
}

var newRoot = (await document.GetSyntaxRootAsync(cancellationToken)).ReplaceNode(originalFieldNode, newFieldNode);
return document.WithSyntaxRoot(newRoot);
}

private static PropertySyntax GetEditableFalseProperty() =>
SyntaxFactory.Property(PropertyKind.Editable, GetBooleanFalsePropertyValue());

private static PropertyListSyntax UpdateEditablePropertyList(FieldSyntax fieldNode, PropertySyntax editableProperty)
{
var updatedEditableProperty = editableProperty.WithValue(GetBooleanFalsePropertyValue());

var propertyList = fieldNode.PropertyList;
var newProperties = propertyList.Properties.Select(prop =>
prop == editableProperty ? updatedEditableProperty : prop).ToList();

return propertyList.WithProperties(SyntaxFactory.List(newProperties));
}

private static PropertyValueSyntax GetBooleanFalsePropertyValue() =>
SyntaxFactory.BooleanPropertyValue(SyntaxFactory.BooleanLiteralValue(SyntaxFactory.Token(SyntaxKind.FalseKeyword)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@

namespace BusinessCentral.LinterCop
{
[GeneratedCode("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[GeneratedCode("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[DebuggerNonUserCode]
[CompilerGenerated]
internal class LinterCopAnalyzers
{
private static ResourceManager resourceMan;
private static CultureInfo resourceCulture;

internal LinterCopAnalyzers()
{
}
private static ResourceManager? resourceMan;
private static CultureInfo? resourceCulture;

[EditorBrowsable(EditorBrowsableState.Advanced)]
internal static ResourceManager ResourceManager
Expand All @@ -34,11 +30,12 @@ internal static ResourceManager ResourceManager
[EditorBrowsable(EditorBrowsableState.Advanced)]
internal static CultureInfo Culture
{
get => LinterCopAnalyzers.resourceCulture;
get => LinterCopAnalyzers.resourceCulture ?? CultureInfo.CurrentUICulture;
set => LinterCopAnalyzers.resourceCulture = value;
}

internal static string AnalyzerPrefix => LinterCopAnalyzers.ResourceManager.GetString(nameof(AnalyzerPrefix), LinterCopAnalyzers.resourceCulture);
internal static string Fix0001FlowFieldsShouldNotBeEditableCodeAction => LinterCopAnalyzers.ResourceManager.GetString(nameof(Fix0001FlowFieldsShouldNotBeEditableCodeAction), LinterCopAnalyzers.resourceCulture);

internal static LocalizableString GetLocalizableString(string nameOfLocalizableResource)
{
Expand Down
3 changes: 3 additions & 0 deletions BusinessCentral.LinterCop/LinterCopAnalyzers.resx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
<data name="Rule0001FlowFieldsShouldNotBeEditableTitle" xml:space="preserve">
<value>FlowFields should not be editable.</value>
</data>
<data name="Fix0001FlowFieldsShouldNotBeEditableCodeAction" xml:space="preserve">
<value>Set Editable property to false</value>
</data>
<data name="Rule0002CommitMustBeExplainedByCommentDescription" xml:space="preserve">
<value>Commit() needs a comment to justify its existence. Either a leading or a trailing comment.</value>
</data>
Expand Down