提交 5e0be57a 编写于 作者: C Cyrus Najmabadi

Add invert conditional code.

上级 dce1f89a
// 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.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Shared.Extensions;
namespace Microsoft.CodeAnalysis.CSharp.InvertConditional
{
[ExportCodeRefactoringProvider(LanguageNames.CSharp), Shared]
internal class CSharpInvertConditionalCodeRefactoringProvider : CodeRefactoringProvider
{
public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var document = context.Document;
var span = context.Span;
var cancellationToken = context.CancellationToken;
if (span.Length > 0)
{
return;
}
var position = span.Start;
var conditional = await FindConditionalAsync(
document, position, cancellationToken).ConfigureAwait(false);
if (conditional == null)
{
return;
}
if (position < conditional.Span.Start || position > conditional.QuestionToken.Span.End)
{
return;
}
if (conditional.ColonToken.IsMissing)
{
return;
}
context.RegisterRefactoring(new MyCodeAction(
FeaturesResources.Invert_conditional,
c => InvertConditionalAsync(document, position, c)));
}
private static async Task<ConditionalExpressionSyntax> FindConditionalAsync(Document document, int position, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var token = root.FindToken(position);
return token.Parent.FirstAncestorOrSelf<ConditionalExpressionSyntax>();
}
private async Task<Document> InvertConditionalAsync(
Document document, int position, CancellationToken cancellationToken)
{
var conditional = await FindConditionalAsync(
document, position, cancellationToken).ConfigureAwait(false);
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var editor = new SyntaxEditor(root, document.Project.Solution.Workspace);
var generator = editor.Generator;
var condition = conditional.Condition;
var whenTrue = conditional.WhenTrue;
var whenFalse = conditional.WhenFalse;
editor.ReplaceNode(
condition, generator.Negate(condition, semanticModel, cancellationToken));
editor.ReplaceNode(whenTrue, whenFalse.WithTriviaFrom(whenTrue));
editor.ReplaceNode(whenFalse, whenTrue.WithTriviaFrom(whenFalse));
return document.WithSyntaxRoot(editor.GetChangedRoot());
}
private class MyCodeAction : CodeAction.DocumentChangeAction
{
public MyCodeAction(string title, Func<CancellationToken, Task<Document>> createChangedDocument)
: base(title, createChangedDocument)
{
}
}
}
}
......@@ -2008,6 +2008,15 @@ internal class FeaturesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Invert conditional.
/// </summary>
internal static string Invert_conditional {
get {
return ResourceManager.GetString("Invert_conditional", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to is.
/// </summary>
......
......@@ -1430,4 +1430,7 @@ This version used in: {2}</value>
<data name="Type_0_has_a_private_member_1_that_can_be_removed_as_the_value_assigned_to_it_is_never_read" xml:space="preserve">
<value>Type '{0}' has a private member '{1}' that can be removed as the value assigned to it is never read.</value>
</data>
<data name="Invert_conditional" xml:space="preserve">
<value>Invert conditional</value>
</data>
</root>
\ No newline at end of file
......@@ -67,6 +67,11 @@
<target state="translated">Formátuje se dokument.</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">Podpisy souvisejících metod nalezené v metadatech se nebudou aktualizovat.</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">Dokument wird formatiert</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">In Metadaten gefundene ähnliche Methodensignaturen werden nicht aktualisiert.</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">Aplicando formato al documento</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">Las signaturas de método relacionadas encontradas en los metadatos no se actualizarán.</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">Mise en forme du document</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">Les signatures de méthode associées trouvées dans les métadonnées ne seront pas mises à jour.</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">Formattazione del documento</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">Le firme del metodo correlate trovate nei metadati non verranno aggiornate.</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">ドキュメントの書式設定</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">メタデータ内に検出される関連するメソッド シグネチャは更新されません。</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">문서 서식 지정</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">메타데이터에서 찾은 관련 메서드 시그니처가 업데이트되지 않습니다.</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">Formatowanie dokumentu</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">Sygnatury powiązanych metod znalezione w metadanych nie zostaną zaktualizowane.</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">Formatando documento</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">As assinaturas de método relacionadas encontradas nos metadados não serão atualizadas.</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">Форматирование документа</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">Связанные сигнатуры методов, найденные в метаданных, не будут обновлены.</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">Belge biçimlendiriliyor</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">Meta verilerde bulunan ilgili metot imzaları güncelleştirilmez.</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">设置文档格式</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">不更新在元数据中发现的相关方法签名。</target>
......
......@@ -67,6 +67,11 @@
<target state="translated">正在將文件格式化</target>
<note />
</trans-unit>
<trans-unit id="Invert_conditional">
<source>Invert conditional</source>
<target state="new">Invert conditional</target>
<note />
</trans-unit>
<trans-unit id="Related_method_signatures_found_in_metadata_will_not_be_updated">
<source>Related method signatures found in metadata will not be updated.</source>
<target state="translated">將不會更新中繼資料中所找到的相關方法簽章。</target>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册