Skip to content

Commit 089eee3

Browse files
committed
LuceneDev1001_FloatingPointFormattingCSCodeFixProvider: Dynamically build title based off of localized resource and actual code element.
1 parent 1eae0b9 commit 089eee3

4 files changed

Lines changed: 68 additions & 7 deletions

File tree

src/Lucene.Net.CodeAnalysis.Dev.CodeFixes/CodeFixResources.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,8 @@ under the License.
118118
<resheader name="writer">
119119
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
120120
</resheader>
121+
<data name="UseX" xml:space="preserve">
122+
<value>Use {0}</value>
123+
<comment>Title for code fix; {0} is the code element to utilize.</comment>
124+
</data>
121125
</root>

src/Lucene.Net.CodeAnalysis.Dev.CodeFixes/Lucene.Net.CodeAnalysis.Dev.CodeFixes.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version='1.0'?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<!--
33
Licensed to the Apache Software Foundation (ASF) under one
44
or more contributor license agreements. See the NOTICE file

src/Lucene.Net.CodeAnalysis.Dev.CodeFixes/LuceneDev1001_FloatingPointFormattingCSCodeFixProvider .cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
* limitations under the License.
1616
*/
1717

18+
using Lucene.Net.CodeAnalysis.Dev.CodeFixes.Utility;
1819
using Lucene.Net.CodeAnalysis.Dev.Utility;
1920
using Microsoft.CodeAnalysis;
20-
using Microsoft.CodeAnalysis.CodeActions;
2121
using Microsoft.CodeAnalysis.CodeFixes;
2222
using Microsoft.CodeAnalysis.CSharp;
2323
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -36,7 +36,7 @@ namespace Lucene.Net.CodeAnalysis.Dev.CodeFixes
3636
public class LuceneDev1001_FloatingPointFormattingCSCodeFixProvider : CodeFixProvider
3737
{
3838
public override ImmutableArray<string> FixableDiagnosticIds =>
39-
ImmutableArray.Create(Descriptors.LuceneDev1001_FloatingPointFormatting.Id);
39+
[Descriptors.LuceneDev1001_FloatingPointFormatting.Id];
4040

4141
public override FixAllProvider GetFixAllProvider() =>
4242
WellKnownFixAllProviders.BatchFixer;
@@ -65,19 +65,50 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
6565
if (invocation is null)
6666
return;
6767

68+
var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
69+
if (semanticModel is null)
70+
return;
71+
72+
var memberAccess = node as MemberAccessExpressionSyntax
73+
?? node.AncestorsAndSelf().OfType<MemberAccessExpressionSyntax>().FirstOrDefault();
74+
75+
if (memberAccess is null)
76+
return;
77+
78+
// Determine the type name for J2N (Single or Double)
79+
var typeInfo = semanticModel.GetTypeInfo(memberAccess.Expression, context.CancellationToken);
80+
var type = typeInfo.Type;
81+
82+
string? j2nTypeName = type?.SpecialType switch
83+
{
84+
SpecialType.System_Single => "Single",
85+
SpecialType.System_Double => "Double",
86+
_ => null
87+
};
88+
89+
if (j2nTypeName == null)
90+
return;
91+
92+
// Build the code element string
93+
string codeElement = $"J2N.Numerics.{j2nTypeName}.ToString(...)";
94+
95+
// Use the helper to register the code fix
6896
context.RegisterCodeFix(
69-
CodeAction.Create(
70-
title: "Use J2N.Numerics.*.ToString(...)",
71-
createChangedDocument: c => ReplaceWithJ2NToStringAsync(context.Document, invocation, c),
72-
equivalenceKey: "UseJ2NToString"),
97+
CodeActionHelper.CreateFromResource(
98+
CodeFixResources.UseX,
99+
c => ReplaceWithJ2NToStringAsync(context.Document, invocation, c),
100+
"UseJ2NToString",
101+
codeElement),
73102
diagnostic);
103+
74104
}
75105

76106
private async Task<Document> ReplaceWithJ2NToStringAsync(
77107
Document document,
78108
InvocationExpressionSyntax invocation,
79109
CancellationToken cancellationToken)
80110
{
111+
81112
if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess)
82113
return document;
83114

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CodeActions;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace Lucene.Net.CodeAnalysis.Dev.CodeFixes.Utility
10+
{
11+
internal static class CodeActionHelper
12+
{
13+
/// <summary>
14+
/// Create a CodeAction using a resource string and formatting arguments.
15+
/// </summary>
16+
public static CodeAction CreateFromResource(
17+
string resourceValue,
18+
Func<CancellationToken, Task<Document>> createChangedDocument,
19+
string equivalenceKey,
20+
params object[] args)
21+
{
22+
var title = string.Format(resourceValue, args);
23+
return CodeAction.Create(title, createChangedDocument, equivalenceKey);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)