Make generated methods static
This commit is contained in:
Ujjwal Chadha 2022-08-05 10:23:43 -07:00 коммит произвёл GitHub
Родитель 8e36dc0a9d
Коммит 5a555e6dbb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 40 добавлений и 23 удалений

Просмотреть файл

@ -21,6 +21,8 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
[ApplicableComponents(ProjectComponents.WinUI)]
public class WinUIContentDialogCodeFixer : CodeFixProvider
{
private const string DialogSetterComment = "/* TODO You should replace 'this' with the instance of UserControl that is ContentDialog is meant to be a part of. */";
// The Upgrade Assistant will only use analyzers that have an associated code fix provider registered including
// the analyzer's ID in the code fix provider's FixableDiagnosticIds array.
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(WinUIContentDialogAnalyzer.DiagnosticId);
@ -63,24 +65,27 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
{
var newMethodDeclarationSibling = contentDialogMemberAccess.Ancestors().OfType<MethodDeclarationSyntax>().First();
var newMethodAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("this"),
SyntaxFactory.IdentifierName("SetContentDialogRoot"));
var newMethodCall = SyntaxFactory.InvocationExpression(newMethodAccess,
SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.Argument(contentDialogMemberAccess.Expression) })));
var newMethodCall = SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression("SetContentDialogRoot"),
SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[]
{
SyntaxFactory.Argument(contentDialogMemberAccess.Expression),
SyntaxFactory.Argument(SyntaxFactory.ParseExpression("this"))
})));
var newMethodCallComment = SyntaxFactory.Comment(DialogSetterComment);
var documentEditor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
documentEditor.ReplaceNode(contentDialogMemberAccess.Expression, newMethodCall);
documentEditor.ReplaceNode(contentDialogMemberAccess.Expression, newMethodCall.WithLeadingTrivia(newMethodCallComment));
if (!newMethodDeclarationSibling.Parent!.ChildNodes().OfType<MethodDeclarationSyntax>()
.Any(sibling => sibling.Identifier.ValueText == "SetContentDialogRoot"))
{
var newMethodRoot = await CSharpSyntaxTree.ParseText(@"
class A
{
private ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog, UserControl control)
{
if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent(""Windows.Foundation.UniversalApiContract"", 8))
{
contentDialog.XamlRoot = this.Content.XamlRoot;
contentDialog.XamlRoot = control.Content.XamlRoot;
}
return contentDialog;
}

Просмотреть файл

@ -20,6 +20,10 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
[ApplicableComponents(ProjectComponents.WinUI)]
public class WinUIInitializeWindowCodeFixer : CodeFixProvider
{
private const string WindowInitializerComment = @"/* TODO You should replace 'App.WindowHandle' with the your window's handle (HWND)
Read more on retrieving window handle here: https://docs.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd */
";
// The Upgrade Assistant will only use analyzers that have an associated code fix provider registered including
// the analyzer's ID in the code fix provider's FixableDiagnosticIds array.
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(DiagnosticId);
@ -66,12 +70,16 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
var newMethodDeclarationSibling = objectCreationDeclaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
var typeName = objectCreationDeclaration.Type.ToString();
var newMethodAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("this"),
SyntaxFactory.IdentifierName("InitializeWithWindow"));
var newMethodCall = SyntaxFactory.InvocationExpression(newMethodAccess,
SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.Argument(objectCreationDeclaration) })));
var newMethodCall = SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression("InitializeWithWindow"),
SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[]
{
SyntaxFactory.Argument(objectCreationDeclaration),
SyntaxFactory.Argument(SyntaxFactory.ParseExpression("App.WindowHandle"))
})));
var newMethodCallComment = SyntaxFactory.Comment(WindowInitializerComment);
documentEditor.ReplaceNode(objectCreationDeclaration, newMethodCall.WithLeadingTrivia(newMethodCallComment));
documentEditor.ReplaceNode(objectCreationDeclaration, newMethodCall);
if (!newMethodDeclarationSibling.Parent!.ChildNodes().OfType<MethodDeclarationSyntax>()
.Any(sibling => sibling.Identifier.ValueText == "InitializeWithWindow"
&& sibling.ParameterList.Parameters.Any(parameter => parameter.Type!.ToString() == typeName)))
@ -79,9 +87,9 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
var newMethodRoot = await CSharpSyntaxTree.ParseText(@$"
class A
{{
private {typeName} InitializeWithWindow({typeName} obj)
private static {typeName} InitializeWithWindow({typeName} obj, IntPtr windowHandle)
{{
WinRT.Interop.InitializeWithWindow.Initialize(obj, App.WindowHandle);
WinRT.Interop.InitializeWithWindow.Initialize(obj, windowHandle);
return obj;
}}
}}",

Просмотреть файл

@ -20,13 +20,13 @@ namespace TestProject.TestClasses
PrimaryButtonText = "Leave this page",
SecondaryButtonText = "Stay"
};
ContentDialogResult result = this.SetContentDialogRoot(saveDialog).ShowAsync();
ContentDialogResult result = /* TODO You should replace 'this' with the instance of UserControl that is ContentDialog is meant to be a part of. */SetContentDialogRoot(saveDialog, this).ShowAsync();
}
private ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog, UserControl control)
{
if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
{
contentDialog.XamlRoot = this.Content.XamlRoot;
contentDialog.XamlRoot = control.Content.XamlRoot;
}
return contentDialog;
}

Просмотреть файл

@ -14,18 +14,22 @@ namespace TestProject.TestClasses
{
private async void CallContentDialog()
{
var filePicker = this.InitializeWithWindow(new FileSavePicker());
var folderPicker = this.InitializeWithWindow(new FolderPicker());
var filePicker = /* TODO You should replace 'App.WindowHandle' with the your window's handle (HWND)
Read more on retrieving window handle here: https://docs.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd */
InitializeWithWindow(new FileSavePicker(), App.WindowHandle);
var folderPicker = /* TODO You should replace 'App.WindowHandle' with the your window's handle (HWND)
Read more on retrieving window handle here: https://docs.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd */
InitializeWithWindow(new FolderPicker(), App.WindowHandle);
var fileOpenPicker = this.InitializeWithWindow(new FileOpenPicker());
}
private FolderPicker InitializeWithWindow(FolderPicker obj)
private static FolderPicker InitializeWithWindow(FolderPicker obj, IntPtr windowHandle)
{
WinRT.Interop.InitializeWithWindow.Initialize(obj, App.WindowHandle);
WinRT.Interop.InitializeWithWindow.Initialize(obj, windowHandle);
return obj;
}
private FileSavePicker InitializeWithWindow(FileSavePicker obj)
private static FileSavePicker InitializeWithWindow(FileSavePicker obj, IntPtr windowHandle)
{
WinRT.Interop.InitializeWithWindow.Initialize(obj, App.WindowHandle);
WinRT.Interop.InitializeWithWindow.Initialize(obj, windowHandle);
return obj;
}