未验证 提交 8167e488 编写于 作者: M msftbot[bot] 提交者: GitHub

Merge pull request #42970 from dotnet/merges/master-to-master-vs-deps

Merge master to master-vs-deps
......@@ -94,16 +94,18 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
if (parensDesignation is null)
{
var typeSymbol = semanticModel.GetTypeInfo(typeSyntax.StripRefIfNeeded()).ConvertedType;
typeSyntax = typeSyntax.StripRefIfNeeded();
// We're going to be passed through the simplifier. Tell it to not just convert
// this back to var (as that would defeat the purpose of this refactoring entirely).
var typeName = typeSymbol.GenerateTypeSyntax(allowVar: false)
.WithLeadingTrivia(node.GetLeadingTrivia())
.WithTrailingTrivia(node.GetTrailingTrivia());
Debug.Assert(!typeName.ContainsDiagnostics, "Explicit type replacement likely introduced an error in code");
// We're going to be passed through the simplifier. Tell it to not just convert this back to var (as
// that would defeat the purpose of this refactoring entirely).
var newTypeSyntax =
semanticModel.GetTypeInfo(typeSyntax).ConvertedType
.GenerateTypeSyntax(allowVar: false)
.WithTriviaFrom(typeSyntax);
editor.ReplaceNode(node, typeName);
Debug.Assert(!newTypeSyntax.ContainsDiagnostics, "Explicit type replacement likely introduced an error in code");
editor.ReplaceNode(typeSyntax, newTypeSyntax);
}
else
{
......
......@@ -11,6 +11,8 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Roslyn.Utilities;
......@@ -48,19 +50,20 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
foreach (var diagnostic in diagnostics)
{
var node = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
ReplaceTypeWithVar(editor, node);
var typeSyntax = (TypeSyntax)root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
ReplaceTypeWithVar(editor, typeSyntax);
}
return Task.CompletedTask;
}
internal static void ReplaceTypeWithVar(SyntaxEditor editor, SyntaxNode node)
internal static void ReplaceTypeWithVar(SyntaxEditor editor, TypeSyntax type)
{
type = type.StripRefIfNeeded();
var implicitType = SyntaxFactory.IdentifierName("var")
.WithTriviaFrom(node);
.WithTriviaFrom(type);
editor.ReplaceNode(node, implicitType);
editor.ReplaceNode(type, implicitType);
}
private class MyCodeAction : CustomCodeActions.DocumentChangeAction
......
......@@ -144,7 +144,7 @@ Public Class Bar
End Sub
End Class]]></Text>.Value
Dim markup = CreateMarkupForProjecWithProjectReference(file2, file1, LanguageNames.VisualBasic, LanguageNames.CSharp)
Dim markup = CreateMarkupForProjectWithProjectReference(file2, file1, LanguageNames.VisualBasic, LanguageNames.CSharp)
Await VerifyItemExistsAsync(markup, "My", glyph:=Glyph.ClassPublic, inlineDescription:="Foo", expectedDescriptionOrNull:="Class Foo.Myattribute")
Await VerifyItemIsAbsentAsync(markup, "Myattribute", inlineDescription:="Foo")
End Function
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册