提交 42d97b24 编写于 作者: B Balaji Krishnan

Merge pull request #5371 from balajikris/GMWinForms-144843

Prefer normal files over auto generated files for code generation destinations
......@@ -115,5 +115,49 @@ public delegate void EvHandler(int x);
Test(input, expected)
End Sub
<WorkItem(144843, "Generate method stub generates into *.Designer.cs")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEvent)>
Public Sub PreferNormalFileOverAutoGeneratedFile_Basic()
Dim input =
<Workspace>
<Project Language="Visual Basic" AssemblyName="VBAssembly1" CommonReferences="true">
<Document>
Class c
WithEvents field as UserControl1
Sub Foo(x as Integer) Handles field.Ev$$
End Sub
End Class
</Document>
<Document FilePath="UserControl1.Designer.vb">
' This file is auto-generated
Partial Class UserControl1
End Class
</Document>
<Document FilePath="UserControl1.vb">
Partial Public Class UserControl1
End Class
</Document>
</Project>
</Workspace>
Dim expectedFileWithText =
New Dictionary(Of String, String) From {
{"UserControl1.vb",
<Text>
Partial Public Class UserControl1
Public Event Ev(x As Integer)
End Class
</Text>.Value.Trim()},
{"UserControl1.Designer.vb",
<Text>
' This file is auto-generated
Partial Class UserControl1
End Class
</Text>.Value.Trim()}
}
Test(input, fileNameToExpected:=expectedFileWithText)
End Sub
End Class
End Namespace
......@@ -1071,5 +1071,276 @@ End Module]]>
Test(input, expected)
End Sub
#Region "Normal tests"
<WorkItem(144843, "Generate method stub generates into *.Designer.cs")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)>
Public Sub PreferNormalFileOverAutoGeneratedFile_CSharp()
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document FilePath="Form1.cs">
class Form1
{
void M()
{
UserControl1 control;
control.Draw$$();
}
}
</Document>
<Document FilePath="UserControl1.Designer.cs">
// This file is auto-generated
partial class UserControl1
{
}
</Document>
<Document FilePath="UserControl1.cs">
public partial class UserControl1
{
}
</Document>
</Project>
</Workspace>
Dim expectedFileWithText =
New Dictionary(Of String, String) From {
{"UserControl1.cs",
<Text>
using System;
public partial class UserControl1
{
internal void Draw()
{
throw new NotImplementedException();
}
}
</Text>.Value.Trim()},
{"UserControl1.Designer.cs",
<Text>
// This file is auto-generated
partial class UserControl1
{
}
</Text>.Value.Trim()}
}
Test(input, fileNameToExpected:=expectedFileWithText)
End Sub
<WorkItem(144843, "Generate method stub generates into *.Designer.cs")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)>
Public Sub IntoAutoGeneratedFileIfNoBetterLocationExists_CSharp()
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document FilePath="Form1.cs">
class Form1
{
void M()
{
UserControl1 control;
control.Draw$$();
}
}
</Document>
<Document FilePath="UserControl1.Designer.cs">
// This file is auto-generated
partial class UserControl1
{
}
</Document>
</Project>
</Workspace>
Dim expectedFileWithText =
New Dictionary(Of String, String) From {
{"UserControl1.Designer.cs",
<Text>
using System;
// This file is auto-generated
partial class UserControl1
{
internal void Draw()
{
throw new NotImplementedException();
}
}
</Text>.Value.Trim()}}
Test(input, fileNameToExpected:=expectedFileWithText)
End Sub
<WorkItem(144843, "Generate method stub generates into *.Designer.cs")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)>
Public Sub InAutoGeneratedFiles_CSharp()
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document FilePath="Form1.Designer.cs">
using System;
// This file is auto-generated
class Form1
{
void M()
{
this.Draw$$();
}
}
</Document>
</Project>
</Workspace>
Dim expectedFileWithText =
New Dictionary(Of String, String) From {
{"Form1.Designer.cs",
<Text>
using System;
// This file is auto-generated
class Form1
{
void M()
{
this.Draw();
}
private void Draw()
{
throw new NotImplementedException();
}
}
</Text>.Value.Trim()}}
Test(input, fileNameToExpected:=expectedFileWithText)
End Sub
<WorkItem(144843, "Generate method stub generates into *.Designer.cs")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)>
Public Sub PreferNormalFileOverAutoGeneratedFile_Basic()
Dim input =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document FilePath="Form1.vb">
Class Form1
Sub M()
Dim control As UserControl1
control.Draw$$()
End Sub
End Class
</Document>
<Document FilePath="UserControl1.Designer.vb">
' This file is auto-generated
Partial Class UserControl1
End Class
</Document>
<Document FilePath="UserControl1.vb">
Partial Public Class UserControl1
End Class
</Document>
</Project>
</Workspace>
Dim expectedFileWithText =
New Dictionary(Of String, String) From {
{"UserControl1.vb",
<Text>
Partial Public Class UserControl1
Friend Sub Draw()
Throw New NotImplementedException()
End Sub
End Class
</Text>.Value.Trim()},
{"UserControl1.Designer.vb",
<Text>
' This file is auto-generated
Partial Class UserControl1
End Class
</Text>.Value.Trim()}
}
Test(input, fileNameToExpected:=expectedFileWithText)
End Sub
<WorkItem(144843, "Generate method stub generates into *.Designer.cs")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)>
Public Sub IntoAutoGeneratedFileIfNoBetterLocationExists_Basic()
Dim input =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document FilePath="Form1.vb">
Class Form1
Sub M()
Dim control As UserControl1
control.Draw$$()
End Sub
End Class
</Document>
<Document FilePath="UserControl1.Designer.vb">
' This file is auto-generated
Partial Class UserControl1
End Class
</Document>
</Project>
</Workspace>
Dim expectedFileWithText =
New Dictionary(Of String, String) From {
{"UserControl1.Designer.vb",
<Text>
' This file is auto-generated
Partial Class UserControl1
Friend Sub Draw()
Throw New NotImplementedException()
End Sub
End Class
</Text>.Value.Trim()}}
Test(input, fileNameToExpected:=expectedFileWithText)
End Sub
<WorkItem(144843, "Generate method stub generates into *.Designer.cs")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)>
Public Sub InAutoGeneratedFiles_Basic()
Dim input =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document FilePath="Form1.Designer.vb">
Class Form1
Sub M()
Me.Draw$$()
End Sub
End Class
</Document>
</Project>
</Workspace>
Dim expectedFileWithText =
New Dictionary(Of String, String) From {
{"Form1.Designer.vb",
<Text>
Class Form1
Sub M()
Me.Draw()
End Sub
Private Sub Draw()
Throw New NotImplementedException()
End Sub
End Class
</Text>.Value.Trim()}}
Test(input, fileNameToExpected:=expectedFileWithText)
End Sub
#End Region
End Class
End Namespace
......@@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.GeneratedCodeRecognition;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
......@@ -57,7 +58,8 @@ public bool CanAddTo(SyntaxNode destination, Solution solution, CancellationToke
return CanAddTo(destination, solution, cancellationToken, out availableIndices);
}
private bool CanAddTo(SyntaxNode destination, Solution solution, CancellationToken cancellationToken, out IList<bool> availableIndices)
private bool CanAddTo(SyntaxNode destination, Solution solution, CancellationToken cancellationToken,
out IList<bool> availableIndices, Func<Document, bool> isGeneratedDocument = null)
{
availableIndices = null;
if (destination == null)
......@@ -65,9 +67,6 @@ private bool CanAddTo(SyntaxNode destination, Solution solution, CancellationTok
return false;
}
// Anything completely hidden is something you can't add to. Anything completely visible
// is something you can add to. Anything that is partially hidden will have to defer to
// the underlying language to make a determination.
var syntaxTree = destination.SyntaxTree;
var document = solution.GetDocument(syntaxTree);
......@@ -76,6 +75,15 @@ private bool CanAddTo(SyntaxNode destination, Solution solution, CancellationTok
return false;
}
// check for generated files if needed.
if (isGeneratedDocument != null && isGeneratedDocument(document))
{
return false;
}
// Anything completely hidden is something you can't add to. Anything completely visible
// is something you can add to. Anything that is partially hidden will have to defer to
// the underlying language to make a determination.
var span = GetSpan(destination);
if (syntaxTree.IsEntirelyHidden(span, cancellationToken))
{
......@@ -164,16 +172,20 @@ class NestedType
}
}
// Generate into any decl we can find.
// If there is a declaration in a non auto-generated file, prefer it.
Func<Document,bool> isGeneratedDocument =
solution.Workspace.Services.GetService<IGeneratedCodeRecognitionService>().IsGeneratedCode;
foreach (var decl in declarations)
{
declaration = await decl.GetSyntaxAsync(cancellationToken).ConfigureAwait(false);
if (CanAddTo(declaration, solution, cancellationToken, out availableIndices))
if (CanAddTo(declaration, solution, cancellationToken, out availableIndices, isGeneratedDocument))
{
return Tuple.Create(declaration, availableIndices);
}
}
// Generate into any declaration we can find.
availableIndices = null;
declaration = fallbackDeclaration ?? await SelectFirstOrDefaultAsync(declarations, node => true, cancellationToken).ConfigureAwait(false);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册