提交 1a4d95ea 编写于 作者: C CyrusNajmabadi

Finish VB support.

上级 f99769df
......@@ -209,28 +209,29 @@ ignoreTrivia:=False)
Public Async Function TestImplementIEquatable1() As Task
Await TestWithPickMembersDialogAsync(
"
imports System.Collections.Generic
Imports System.Collections.Generic
structure Program
public s as string
Public s As String
[||]
end structure",
End structure",
"
imports System.Collections.Generic
Imports System
Imports System.Collections.Generic
structure Program
Implements System.IEquatable(Of Program)
Structure Program
Implements IEquatable(Of Program)
public s as string
Public s As String
Public Overrides Function Equals(obj As Object) As Boolean
Return (TypeOf obj Is Program) AndAlso Equals(DirectCast(obj, Program))
End Function
Public Function Equals(other As Program) As Boolean Implements System.IEquatable(Of Program).Equals
Public Function Equals(other As Program) As Boolean Implements IEquatable(Of Program).Equals
Return s = other.s
End Function
end structure",
End Structure",
chosenSymbols:=Nothing,
optionsCallback:=Sub(Options) EnableOption(Options, ImplementIEquatableId),
ignoreTrivia:=False)
......@@ -240,29 +241,30 @@ ignoreTrivia:=False)
Public Async Function TestImplementIEquatable2() As Task
Await TestWithPickMembersDialogAsync(
"
imports System.Collections.Generic
Imports System.Collections.Generic
class Program
public s as string
Class Program
Public s As String
[||]
end class",
End Class",
"
imports System.Collections.Generic
Imports System
Imports System.Collections.Generic
class Program
Implements System.IEquatable(Of Program)
Class Program
Implements IEquatable(Of Program)
public s as string
Public s As String
Public Overrides Function Equals(obj As Object) As Boolean
Return Equals(TryCast(obj, Program))
End Function
Public Function Equals(other As Program) As Boolean Implements System.IEquatable(Of Program).Equals
Public Function Equals(other As Program) As Boolean Implements IEquatable(Of Program).Equals
Return other IsNot Nothing AndAlso
s = other.s
End Function
end class",
End Class",
chosenSymbols:=Nothing,
optionsCallback:=Sub(Options) EnableOption(Options, ImplementIEquatableId),
ignoreTrivia:=False)
......
......@@ -93,17 +93,40 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
generator.TypeExpression(constructed));
}
var newDocument = await UpdateDocumentAndAddImportsAsync(
oldType, newType, cancellationToken).ConfigureAwait(false);
var formattedDocument = await FormatDocumentAsync(
newDocument, cancellationToken).ConfigureAwait(false);
return formattedDocument;
}
private async Task<Document> UpdateDocumentAndAddImportsAsync(SyntaxNode oldType, SyntaxNode newType, CancellationToken cancellationToken)
{
var oldRoot = await _document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var newDocument = _document.WithSyntaxRoot(
oldRoot.ReplaceNode(oldType, newType));
var options = await _document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var placeSystemNamespaceFirst = options.GetOption(GenerationOptions.PlaceSystemNamespaceFirst);
var codeGenService = _document.GetLanguageService<ICodeGenerationService>();
newDocument = await codeGenService.AddImportsAsync(
newDocument,
new CodeGenerationOptions(placeSystemNamespaceFirst: placeSystemNamespaceFirst),
cancellationToken).ConfigureAwait(false);
return newDocument;
}
private async Task<Document> FormatDocumentAsync(Document newDocument, CancellationToken cancellationToken)
{
var rules = new List<IFormattingRule> { new FormatLargeBinaryExpressionRule(_document.GetLanguageService<ISyntaxFactsService>()) };
rules.AddRange(Formatter.GetDefaultFormattingRules(_document));
var formattedDocument = await Formatter.FormatAsync(
newDocument, s_specializedFormattingAnnotation,
options: null, rules: rules, cancellationToken: cancellationToken).ConfigureAwait(false);
return formattedDocument;
}
......
......@@ -73,12 +73,12 @@ protected override SyntaxNode GetInnermostNamespaceScope(SyntaxNodeOrToken nodeO
}
public override async Task<Document> AddAsync(
IEnumerable<ISymbol> members,
bool placeSystemNamespaceFirst,
CodeGenerationOptions options,
CancellationToken cancellationToken)
{
var importsContainerToMissingNamespaces = await DetermineNamespaceToImportAsync(members, options, cancellationToken).ConfigureAwait(false);
var importsContainerToMissingNamespaces = await DetermineNamespaceToImportAsync(
options, cancellationToken).ConfigureAwait(false);
if (importsContainerToMissingNamespaces.Count == 0)
{
return this.Document;
......@@ -91,4 +91,4 @@ protected override SyntaxNode GetInnermostNamespaceScope(SyntaxNodeOrToken nodeO
return this.Document.WithSyntaxRoot(newRoot);
}
}
}
}
\ No newline at end of file
......@@ -198,13 +198,21 @@ protected static T Cast<T>(object value)
if (options.AddImports)
{
var adder = this.CreateImportsAdder(newDocument);
newDocument = await adder.AddAsync(members, options.PlaceSystemNamespaceFirst, options, cancellationToken).ConfigureAwait(false);
newDocument = await AddImportsAsync(
newDocument, options, cancellationToken).ConfigureAwait(false);
}
return newDocument;
}
public async Task<Document> AddImportsAsync(Document document, CodeGenerationOptions options, CancellationToken cancellationToken)
{
options = options ?? CodeGenerationOptions.Default;
var adder = this.CreateImportsAdder(document);
var newDocument = await adder.AddAsync(options.PlaceSystemNamespaceFirst, options, cancellationToken).ConfigureAwait(false);
return newDocument;
}
protected TDeclarationNode AddMembers<TDeclarationNode>(
TDeclarationNode destination,
IEnumerable<ISymbol> members,
......
......@@ -27,12 +27,10 @@ protected AbstractImportsAdder(Document document)
protected abstract SyntaxNode GetImportsContainer(SyntaxNode node);
protected abstract SyntaxNode GetInnermostNamespaceScope(SyntaxNodeOrToken node);
public abstract Task<Document> AddAsync(IEnumerable<ISymbol> members, bool placeSystemNamespaceFirst, CodeGenerationOptions options, CancellationToken cancellationToken);
public abstract Task<Document> AddAsync(bool placeSystemNamespaceFirst, CodeGenerationOptions options, CancellationToken cancellationToken);
protected async Task<IDictionary<SyntaxNode, ISet<INamedTypeSymbol>>> GetAllReferencedDefinitionsAsync(
Compilation compilation,
IEnumerable<ISymbol> members,
CancellationToken cancellationToken)
Compilation compilation, CancellationToken cancellationToken)
{
var namespaceScopeToReferencedDefinitions = new Dictionary<SyntaxNode, ISet<INamedTypeSymbol>>();
var root = await Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
......@@ -99,15 +97,14 @@ private bool IsBuiltIn(INamedTypeSymbol type)
}
protected async Task<IDictionary<SyntaxNode, IList<INamespaceSymbol>>> DetermineNamespaceToImportAsync(
IEnumerable<ISymbol> members,
CodeGenerationOptions options,
CancellationToken cancellationToken)
CodeGenerationOptions options, CancellationToken cancellationToken)
{
var semanticModel = await this.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var compilation = semanticModel.Compilation;
// First, find all the named types referenced by code that we are trying to generated.
var namespaceScopeToReferencedDefinitions = await GetAllReferencedDefinitionsAsync(compilation, members, cancellationToken).ConfigureAwait(false);
var namespaceScopeToReferencedDefinitions = await GetAllReferencedDefinitionsAsync(
compilation, cancellationToken).ConfigureAwait(false);
var importsContainerToMissingImports = new Dictionary<SyntaxNode, IList<INamespaceSymbol>>();
......
......@@ -119,6 +119,8 @@ internal interface ICodeGenerationService : ILanguageService
/// </summary>
TDeclarationNode AddStatements<TDeclarationNode>(TDeclarationNode destination, IEnumerable<SyntaxNode> statements, CodeGenerationOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) where TDeclarationNode : SyntaxNode;
Task<Document> AddImportsAsync(Document document, CodeGenerationOptions options, CancellationToken cancellationToken);
/// <summary>
/// Adds a field with the provided signature into destination.
/// </summary>
......
......@@ -66,12 +66,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
ToList()
End Function
Public Overrides Async Function AddAsync(members As IEnumerable(Of ISymbol),
placeSystemNamespaceFirst As Boolean,
options As CodeGenerationOptions,
cancellationToken As CancellationToken) As Task(Of Document)
Public Overrides Async Function AddAsync(
placeSystemNamespaceFirst As Boolean,
options As CodeGenerationOptions,
cancellationToken As CancellationToken) As Task(Of Document)
Dim importsContainerToMissingNamespaces = Await DetermineNamespaceToImportAsync(members, options, cancellationToken).ConfigureAwait(False)
Dim importsContainerToMissingNamespaces = Await DetermineNamespaceToImportAsync(
options, cancellationToken).ConfigureAwait(False)
If importsContainerToMissingNamespaces.Count = 0 Then
Return Me.Document
End If
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册