未验证 提交 0870135e 编写于 作者: J Julien Couvreur 提交者: GitHub

Create AddAwait refactoring (#28930)

上级 57094884
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.CodeRefactorings.AddAwait;
using Microsoft.CodeAnalysis.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings.AddAwait
{
[Trait(Traits.Feature, Traits.Features.AddAwait)]
public class AddAwaitTests : AbstractCSharpCodeActionTest
{
protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspace workspace, TestParameters parameters)
=> new CSharpAddAwaitCodeRefactoringProvider();
[Fact]
public async Task Simple()
{
await TestInRegularAndScriptAsync(@"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = GetNumberAsync()[||];
}
}", @"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = await GetNumberAsync();
}
}");
}
[Fact]
public async Task SimpleWithConfigureAwait()
{
await TestInRegularAndScriptAsync(@"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = GetNumberAsync()[||];
}
}", @"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = await GetNumberAsync().ConfigureAwait(false);
}
}", index: 1);
}
[Fact]
public async Task InArgument()
{
await TestInRegularAndScriptAsync(@"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync(int argument)
{
var x = GetNumberAsync(arg[||]ument);
}
}", @"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync(int argument)
{
var x = await GetNumberAsync(argument);
}
}");
}
[Fact]
public async Task AlreadyAwaited()
{
await TestMissingInRegularAndScriptAsync(@"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = await GetNumberAsync()[||];
}
}");
}
[Fact]
public async Task SimpleWithTrivia()
{
await TestInRegularAndScriptAsync(@"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = // comment
GetNumberAsync()[||] /* comment */
}
}", @"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = // comment
await GetNumberAsync()[||] /* comment */
}
}");
}
[Fact]
public async Task SimpleWithTrivia2()
{
await TestInRegularAndScriptAsync(@"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = /* comment */ GetNumberAsync()[||] // comment
}
}", @"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = /* comment */ await GetNumberAsync()[||] // comment
}
}");
}
[Fact]
public async Task SimpleWithTriviaWithConfigureAwait()
{
await TestInRegularAndScriptAsync(@"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = // comment
GetNumberAsync()[||] /* comment */
}
}", @"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = // comment
await GetNumberAsync().ConfigureAwait(false) /* comment */
}
}", index: 1);
}
[Fact]
public async Task SimpleWithTrivia2WithConfigureAwait()
{
await TestInRegularAndScriptAsync(@"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = /* comment */ GetNumberAsync()[||] // comment
}
}", @"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = /* comment */ await GetNumberAsync().ConfigureAwait(false) // comment
}
}", index: 1);
}
[Fact]
public async Task MissingOnSemiColon()
{
await TestMissingInRegularAndScriptAsync(@"
using System.Threading.Tasks;
class Program
{
async Task<int> GetNumberAsync()
{
var x = GetNumberAsync();[||]
}
}");
}
[Fact]
public async Task ChainedInvocation()
{
await TestInRegularAndScriptAsync(@"
using System.Threading.Tasks;
class Program
{
Task<int> GetNumberAsync() => throw null;
async void M()
{
var x = GetNumberAsync()[||].ToString();
}
}", @"
using System.Threading.Tasks;
class Program
{
Task<int> GetNumberAsync() => throw null;
async void M()
{
var x = (await GetNumberAsync()).ToString();
}
}");
}
}
}
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Option Strict Off
Imports Microsoft.CodeAnalysis.CodeRefactorings
Imports Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.AddAwait
Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeRefactorings.AddAwait
<Trait(Traits.Feature, Traits.Features.AddAwait)>
Public Class AddAwaitTests
Inherits AbstractVisualBasicCodeActionTest
Protected Overrides Function CreateCodeRefactoringProvider(workspace As Workspace, parameters As TestParameters) As CodeRefactoringProvider
Return New VisualBasicAddAwaitCodeRefactoringProvider()
End Function
<Fact>
Public Async Function Simple() As Task
Dim markup =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = GetNumberAsync()[||]
End Function
End Module
</File>
Dim expected =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = Await GetNumberAsync()
End Function
End Module
</File>
Await TestAsync(markup, expected)
End Function
<Fact>
Public Async Function SimpleWithConfigureAwait() As Task
Dim markup =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = GetNumberAsync()[||]
End Function
End Module
</File>
Dim expected =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = Await GetNumberAsync().ConfigureAwait(False)
End Function
End Module
</File>
Await TestAsync(markup, expected, index:=1)
End Function
<Fact>
Public Async Function AlreadyAwaited() As Task
Dim markup =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = Await GetNumberAsync()[||]
End Function
End Module
</File>
Await TestMissingAsync(markup)
End Function
<Fact>
Public Async Function SimpleWithTrivia() As Task
Dim markup =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = GetNumberAsync()[||] ' Comment
End Function
End Module
</File>
Dim expected =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = Await GetNumberAsync() ' Comment
End Function
End Module
</File>
Await TestAsync(markup, expected)
End Function
<Fact>
Public Async Function SimpleWithTriviaAndConfigureAwait() As Task
Dim markup =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = GetNumberAsync()[||] ' Comment
End Function
End Module
</File>
Dim expected =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = Await GetNumberAsync().ConfigureAwait(False) ' Comment
End Function
End Module
</File>
Await TestAsync(markup, expected, index:=1)
End Function
<Fact>
Public Async Function ChainedInvocation() As Task
Dim markup =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = GetNumberAsync()[||].ToString()
End Function
End Module
</File>
Dim expected =
<File>
Imports System.Threading.Tasks
Module Program
Async Function GetNumberAsync() As Task(Of Integer)
Dim x = (Await GetNumberAsync()).ToString()
End Function
End Module
</File>
Await TestAsync(markup, expected)
End Function
End Class
End Namespace
......@@ -79,6 +79,24 @@ internal class CSharpFeaturesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Add await.
/// </summary>
internal static string Add_await {
get {
return ResourceManager.GetString("Add_await", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Add await and ConfigureAwait(false).
/// </summary>
internal static string Add_Await_and_ConfigureAwaitFalse {
get {
return ResourceManager.GetString("Add_Await_and_ConfigureAwaitFalse", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Add parentheses.
/// </summary>
......
......@@ -126,6 +126,12 @@
<data name="Invert_if" xml:space="preserve">
<value>Invert if</value>
</data>
<data name="Add_await" xml:space="preserve">
<value>Add await</value>
</data>
<data name="Add_Await_and_ConfigureAwaitFalse" xml:space="preserve">
<value>Add await and ConfigureAwait(false)</value>
</data>
<data name="Simplify_lambda_expression" xml:space="preserve">
<value>Simplify lambda expression</value>
</data>
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Composition;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CodeRefactorings.AddAwait;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Microsoft.CodeAnalysis.CSharp.CodeRefactorings.AddAwait
{
/// <summary>
/// This refactoring complements the AddAwait fixer. It allows adding `await` and `await ... .ConfigureAwait(false)` even there is no compiler error to trigger the fixer.
/// </summary>
[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = PredefinedCodeRefactoringProviderNames.AddAwait), Shared]
internal partial class CSharpAddAwaitCodeRefactoringProvider : AbstractAddAwaitCodeRefactoringProvider<ExpressionSyntax, InvocationExpressionSyntax>
{
protected override string GetTitle()
=> CSharpFeaturesResources.Add_await;
protected override string GetTitleWithConfigureAwait()
=> CSharpFeaturesResources.Add_Await_and_ConfigureAwaitFalse;
}
}
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="cs" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">Přidat modifikátory dostupnosti</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">Přidat/odebrat složené závorky pro řídicí příkazy na jeden řádek</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="de" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">Zugriffsmodifizierer hinzufügen</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">Geschweifte Klammern für einzeilige Steuerungsanweisungen hinzufügen/entfernen</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="es" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">Agregar modificadores de accesibilidad</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">Agregar o quitar llaves para instrucciones de control de una línea</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="fr" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">Ajouter des modificateurs d'accessibilité</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">Ajouter/supprimer des accolades pour les instructions de contrôle sur une seule ligne</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="it" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">Aggiungi modificatori di accessibilità</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">Aggiungi/rimuovi le parentesi graffe per le istruzioni di controllo a riga singola</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ja" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">アクセシビリティ修飾子を追加します</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">単一行のコントロール ステートメントに対する波かっこの追加/削除を行います</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ko" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">접근성 한정자 추가</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">한 줄 제어문에 대해 중괄호 추가/제거</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pl" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">Dodaj modyfikatory dostępności</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">Dodaj/usuń nawiasy klamrowe w przypadku jednowierszowych instrukcji sterowania</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pt-BR" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">Adicionar modificadores de acessibilidade</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">Adicionar/remover as chaves das instruções de controle de linha única</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ru" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">Добавьте модификаторы доступности</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">Добавлять или удалять фигурные скобки для однострочных операторов управления</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="tr" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">Erişilebilirlik değiştiricileri ekle</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">Tek satır denetim deyimleri için küme ayracı ekle/küme ayraçlarını kaldır</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="zh-Hans" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">添加可访问性修饰符</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">添加/删除单行控制语句的括号</target>
......
......@@ -2,11 +2,21 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="zh-Hant" original="../CSharpFeaturesResources.resx">
<body>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add await and ConfigureAwait(false)</source>
<target state="new">Add await and ConfigureAwait(false)</target>
<note />
</trans-unit>
<trans-unit id="Add_accessibility_modifiers">
<source>Add accessibility modifiers</source>
<target state="translated">新增協助工具修飾詞</target>
<note />
</trans-unit>
<trans-unit id="Add_await">
<source>Add await</source>
<target state="new">Add await</target>
<note />
</trans-unit>
<trans-unit id="Add_remove_braces_for_single_line_control_statements">
<source>Add/remove braces for single-line control statements</source>
<target state="translated">新增/移除單行控制項陳述式的括弧</target>
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CodeRefactorings.AddAwait
{
/// <summary>
/// Refactor:
/// var x = GetAsync();
///
/// Into:
/// var x = await GetAsync();
///
/// Or:
/// var x = await GetAsync().ConfigureAwait(false);
/// </summary>
internal abstract class AbstractAddAwaitCodeRefactoringProvider<TExpressionSyntax, TInvocationExpressionSyntax> : CodeRefactoringProvider
where TExpressionSyntax : SyntaxNode
where TInvocationExpressionSyntax : TExpressionSyntax
{
protected abstract string GetTitle();
protected abstract string GetTitleWithConfigureAwait();
public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var document = context.Document;
var textSpan = context.Span;
var cancellationToken = context.CancellationToken;
if (!textSpan.IsEmpty)
{
return;
}
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var token = root.FindTokenOnLeftOfPosition(textSpan.Start);
var model = await document.GetSemanticModelAsync(cancellationToken);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
var awaitable = GetAwaitableExpression(textSpan, token, model, syntaxFacts, cancellationToken);
if (awaitable == null)
{
return;
}
if (awaitable.OverlapsHiddenPosition(cancellationToken))
{
return;
}
context.RegisterRefactoring(
new MyCodeAction(
GetTitle(),
c => AddAwaitAsync(document, awaitable, withConfigureAwait: false, c)));
context.RegisterRefactoring(
new MyCodeAction(
GetTitleWithConfigureAwait(),
c => AddAwaitAsync(document, awaitable, withConfigureAwait: true, c)));
}
private TExpressionSyntax GetAwaitableExpression(TextSpan textSpan, SyntaxToken token, SemanticModel model, ISyntaxFactsService syntaxFacts, CancellationToken cancellationToken)
{
var invocation = token.GetAncestor<TInvocationExpressionSyntax>();
if (invocation is null)
{
return null;
}
if (syntaxFacts.IsExpressionOfAwaitExpression(invocation))
{
return null;
}
var type = model.GetTypeInfo(invocation).Type;
if (type?.IsAwaitableNonDynamic(model, token.SpanStart) == true)
{
return invocation;
}
return null;
}
private async Task<Document> AddAwaitAsync(
Document document,
TExpressionSyntax invocation,
bool withConfigureAwait,
CancellationToken cancellationToken)
{
var syntaxGenerator = SyntaxGenerator.GetGenerator(document);
SyntaxNode withoutTrivia = invocation.WithoutTrivia();
if (withConfigureAwait)
{
withoutTrivia = syntaxGenerator.InvocationExpression(
syntaxGenerator.MemberAccessExpression(withoutTrivia, "ConfigureAwait"),
syntaxGenerator.FalseLiteralExpression());
}
var awaitExpression = syntaxGenerator
.AddParentheses(syntaxGenerator.AwaitExpression(withoutTrivia))
.WithTriviaFrom(invocation);
return await document.ReplaceNodeAsync(invocation, awaitExpression, cancellationToken);
}
private class MyCodeAction : CodeAction.DocumentChangeAction
{
public MyCodeAction(string title, Func<CancellationToken, Task<Document>> createChangedDocument) :
base(title, createChangedDocument)
{
}
}
}
}
......@@ -4,6 +4,7 @@ namespace Microsoft.CodeAnalysis.CodeRefactorings
{
internal static class PredefinedCodeRefactoringProviderNames
{
public const string AddAwait = "Add Await Code Action Provider";
public const string AddFileBanner = "Add Banner To File Code Action Provider";
public const string AddConstructorParametersFromMembers = "Add Parameters From Members Code Action Provider";
public const string ChangeSignature = "Change Signature Code Action Provider";
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Composition
Imports Microsoft.CodeAnalysis.CodeRefactorings
Imports Microsoft.CodeAnalysis.CodeRefactorings.AddAwait
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.AddAwait
<ExportCodeRefactoringProvider(LanguageNames.VisualBasic, Name:=PredefinedCodeRefactoringProviderNames.AddAwait), [Shared]>
Friend Class VisualBasicAddAwaitCodeRefactoringProvider
Inherits AbstractAddAwaitCodeRefactoringProvider(Of ExpressionSyntax, InvocationExpressionSyntax)
Protected Overrides Function GetTitle() As String
Return VBFeaturesResources.Add_Await
End Function
Protected Overrides Function GetTitleWithConfigureAwait() As String
Return VBFeaturesResources.Add_Await_and_ConfigureAwaitFalse
End Function
End Class
End Namespace
......@@ -93,6 +93,24 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.VBFeaturesResources
End Get
End Property
'''<summary>
''' Looks up a localized string similar to Add Await.
'''</summary>
Friend ReadOnly Property Add_Await() As String
Get
Return ResourceManager.GetString("Add_Await", resourceCulture)
End Get
End Property
'''<summary>
''' Looks up a localized string similar to Add Await and &apos;ConfigureAwait(false)&apos;.
'''</summary>
Friend ReadOnly Property Add_Await_and_ConfigureAwaitFalse() As String
Get
Return ResourceManager.GetString("Add_Await_and_ConfigureAwaitFalse", resourceCulture)
End Get
End Property
'''<summary>
''' Looks up a localized string similar to Add &apos;Me.&apos;.
'''</summary>
......
......@@ -141,6 +141,12 @@
<data name="Invert_If" xml:space="preserve">
<value>Invert If</value>
</data>
<data name="Add_Await" xml:space="preserve">
<value>Add Await</value>
</data>
<data name="Add_Await_and_ConfigureAwaitFalse" xml:space="preserve">
<value>Add Await and 'ConfigureAwait(false)'</value>
</data>
<data name="Move_the_0_statement_to_line_1" xml:space="preserve">
<value>Move the '{0}' statement to line {1}.</value>
</data>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="cs" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">Příkaz if lze zjednodušit.</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="de" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">Die If-Anweisung kann vereinfacht werden.</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="es" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">La instrucción "if" se puede simplificar</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="fr" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">L'instruction 'If' peut être simplifiée</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="it" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">L'istruzione 'If' può essere semplificata</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ja" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">'if' ステートメントは簡素化できます</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ko" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">'if' 문을 간단하게 줄일 수 있습니다.</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pl" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">Instrukcja „if” może zostać uproszczona</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pt-BR" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">A instrução 'If' pode ser simplificada</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ru" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">Оператор if можно упростить</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="tr" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">'If' deyimi basitleştirilebilir</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="zh-Hans" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">可简化“If”语句</target>
......
......@@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="zh-Hant" original="../VBFeaturesResources.resx">
<body>
<trans-unit id="Add_Await">
<source>Add Await</source>
<target state="new">Add Await</target>
<note />
</trans-unit>
<trans-unit id="Add_Await_and_ConfigureAwaitFalse">
<source>Add Await and 'ConfigureAwait(false)'</source>
<target state="new">Add Await and 'ConfigureAwait(false)'</target>
<note />
</trans-unit>
<trans-unit id="If_statement_can_be_simplified">
<source>'If' statement can be simplified</source>
<target state="translated">'If' 陳述式可簡化</target>
......
......@@ -162,6 +162,7 @@ public static class Features
public const string ErrorSquiggles = nameof(ErrorSquiggles);
public const string EventHookup = nameof(EventHookup);
public const string Expansion = nameof(Expansion);
public const string AddAwait = "Refactoring.AddAwait";
public const string ExtractInterface = "Refactoring.ExtractInterface";
public const string ExtractMethod = "Refactoring.ExtractMethod";
public const string F1Help = nameof(F1Help);
......
......@@ -3766,6 +3766,11 @@ public override SyntaxNode DefaultExpression(ITypeSymbol type)
return DefaultExpression(type.GenerateTypeSyntax());
}
internal override SyntaxNode AddParentheses(SyntaxNode expression)
{
return Parenthesize(expression);
}
private static ExpressionSyntax Parenthesize(SyntaxNode expression)
{
return ((ExpressionSyntax)expression).Parenthesize();
......
......@@ -2215,6 +2215,11 @@ public SyntaxNode LambdaParameter(string identifier, ITypeSymbol type)
/// </summary>
public abstract SyntaxNode AwaitExpression(SyntaxNode expression);
/// <summary>
/// Wraps with parens.
/// </summary>
internal abstract SyntaxNode AddParentheses(SyntaxNode expression);
/// <summary>
/// Creates an nameof expression.
/// </summary>
......
......@@ -54,6 +54,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Return SyntaxFactory.TupleExpression(SyntaxFactory.SeparatedList(arguments.Select(AddressOf AsSimpleArgument)))
End Function
Friend Overrides Function AddParentheses(expression As SyntaxNode) As SyntaxNode
Return Parenthesize(expression)
End Function
Private Function Parenthesize(expression As SyntaxNode) As ParenthesizedExpressionSyntax
Return DirectCast(expression, ExpressionSyntax).Parenthesize()
End Function
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册