Copy structural changes into the C# -> VB converter to keep in step

This commit is contained in:
GrahamTheCoder 2018-02-26 21:00:20 +00:00
Родитель 1f05a8341b
Коммит c254bd676b
15 изменённых файлов: 300 добавлений и 206 удалений

Просмотреть файл

@ -1,4 +1,5 @@
using System.Linq;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.Util;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.VisualBasic;

Просмотреть файл

@ -1,5 +1,6 @@
using System;
using System.Linq;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.Util;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@ -15,16 +16,16 @@ namespace ICSharpCode.CodeConverter.CSharp
public class CommentConvertingNodesVisitor : VisualBasicSyntaxVisitor<CSharpSyntaxNode>
{
public TriviaConverter TriviaConverter { get; }
private readonly VisualBasicSyntaxVisitor<CSharpSyntaxNode> wrappedVisitor;
private readonly VisualBasicSyntaxVisitor<CSharpSyntaxNode> _wrappedVisitor;
public CommentConvertingNodesVisitor(VisualBasicSyntaxVisitor<CSharpSyntaxNode> wrappedVisitor)
{
TriviaConverter = new TriviaConverter();
this.wrappedVisitor = wrappedVisitor;
this._wrappedVisitor = wrappedVisitor;
}
public override CSharpSyntaxNode DefaultVisit(SyntaxNode node)
{
return TriviaConverter.PortConvertedTrivia(node, wrappedVisitor.Visit(node));
return TriviaConverter.PortConvertedTrivia(node, _wrappedVisitor.Visit(node));
}
public override CSharpSyntaxNode VisitModuleBlock(VbSyntax.ModuleBlockSyntax node)
@ -60,7 +61,7 @@ namespace ICSharpCode.CodeConverter.CSharp
private TDest WithPortedTrivia<TSource, TDest>(SyntaxNode node, Func<TSource, TDest, TDest> portExtraTrivia) where TSource : SyntaxNode where TDest : CSharpSyntaxNode
{
var cSharpSyntaxNode = portExtraTrivia((TSource)node, (TDest)wrappedVisitor.Visit(node));
var cSharpSyntaxNode = portExtraTrivia((TSource)node, (TDest)_wrappedVisitor.Visit(node));
return TriviaConverter.PortConvertedTrivia(node, cSharpSyntaxNode);
}

Просмотреть файл

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.Util;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

Просмотреть файл

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.Util;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

Просмотреть файл

@ -1,5 +1,6 @@
using System;
using ICSharpCode.CodeConverter.CSharp;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.VB;
namespace ICSharpCode.CodeConverter

Просмотреть файл

@ -1,6 +1,6 @@
using System;
namespace ICSharpCode.CodeConverter.CSharp
namespace ICSharpCode.CodeConverter.Shared
{
internal class NotImplementedOrRequiresSurroundingMethodDeclaration : NotImplementedException
{

Просмотреть файл

@ -4,11 +4,12 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ICSharpCode.CodeConverter.CSharp;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Text;
namespace ICSharpCode.CodeConverter.CSharp
namespace ICSharpCode.CodeConverter.Shared
{
public class ProjectConversion<TLanguageConversion> where TLanguageConversion : ILanguageConversion, new()
{

Просмотреть файл

@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.CodeConverter.Util;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
using VBSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax;
using CSSyntax = Microsoft.CodeAnalysis.CSharp.Syntax;
namespace ICSharpCode.CodeConverter.CSharp
namespace ICSharpCode.CodeConverter.Shared
{
public class TriviaConverter
{
@ -40,9 +40,7 @@ namespace ICSharpCode.CodeConverter.CSharp
destination = WithAnnotations(sourceNode, destination);
var containingSubModuleBlock = sourceNode.FirstAncestorOrSelf<StatementSyntax>(IsFirstLineOfBlockConstruct);
var hasVisitedContainingBlock = containingSubModuleBlock == null;
var firstLineOfBlockConstruct = sourceNode.ChildNodes().OfType<StatementSyntax>().FirstOrDefault(IsFirstLineOfBlockConstruct);
var firstLineOfBlockConstruct = sourceNode.ChildNodes().OfType<VBSyntax.StatementSyntax>().FirstOrDefault(IsFirstLineOfBlockConstruct);
if (firstLineOfBlockConstruct != null) {
var endOfFirstLineConstructOrDefault = destination.ChildTokens().FirstOrDefault(t => t.IsKind(SyntaxKind.CloseParenToken, SyntaxKind.OpenBraceToken));
if (endOfFirstLineConstructOrDefault.IsKind(SyntaxKind.OpenBraceToken)) {
@ -54,9 +52,19 @@ namespace ICSharpCode.CodeConverter.CSharp
}
}
var hasVisitedContainingBlock = HasVisitedContainingBlock(sourceNode);
return WithTrailingTriviaConversions(destination, sourceNode.Parent?.GetLastToken(), hasVisitedContainingBlock);
}
private static bool HasVisitedContainingBlock(SyntaxNode sourceNode)
{
var containingSubModuleBlock =
(SyntaxNode)sourceNode.FirstAncestorOrSelf<VBSyntax.StatementSyntax>(IsFirstLineOfBlockConstruct)
?? sourceNode.FirstAncestorOrSelf<CSSyntax.StatementSyntax>();
return containingSubModuleBlock == null;
}
private static T WithAnnotations<T>(SyntaxNode sourceNode, T destination) where T : SyntaxNode
{
destination = sourceNode.CopyAnnotationsTo(destination);
@ -130,9 +138,9 @@ namespace ICSharpCode.CodeConverter.CSharp
return destination;
}
private static bool IsFirstLineOfBlockConstruct(StatementSyntax s)
private static bool IsFirstLineOfBlockConstruct(SyntaxNode s)
{
return !(s is DeclarationStatementSyntax);
return !(s is VBSyntax.DeclarationStatementSyntax);
}
/// <summary>

Просмотреть файл

@ -37,7 +37,7 @@ namespace ICSharpCode.CodeConverter.VB
public static VisualBasicSyntaxNode ConvertCompilationTree(CS.CSharpCompilation compilation, CS.CSharpSyntaxTree tree)
{
var visualBasicSyntaxVisitor = new NodesVisitor(compilation.GetSemanticModel(tree, true));
return tree.GetRoot().Accept(visualBasicSyntaxVisitor);
return tree.GetRoot().Accept(visualBasicSyntaxVisitor.TriviaConvertingVisitor);
}
public static ConversionResult ConvertText(string text, MetadataReference[] references)
@ -128,7 +128,7 @@ namespace ICSharpCode.CodeConverter.VB
return token == SyntaxKind.None ? null : new SyntaxToken?(SyntaxFactory.Token(token));
}
static SeparatedSyntaxList<VariableDeclaratorSyntax> RemodelVariableDeclaration(CSS.VariableDeclarationSyntax declaration, NodesVisitor nodesVisitor)
static SeparatedSyntaxList<VariableDeclaratorSyntax> RemodelVariableDeclaration(CSS.VariableDeclarationSyntax declaration, CS.CSharpSyntaxVisitor<VisualBasicSyntaxNode> nodesVisitor)
{
var type = (TypeSyntax)declaration.Type.Accept(nodesVisitor);
var declaratorsWithoutInitializers = new List<CSS.VariableDeclaratorSyntax>();

Просмотреть файл

@ -0,0 +1,28 @@
using ICSharpCode.CodeConverter.CSharp;
using ICSharpCode.CodeConverter.Shared;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using VBSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax;
namespace ICSharpCode.CodeConverter.VB
{
public class CommentConvertingMethodBodyVisitor : CSharpSyntaxVisitor<SyntaxList<VBSyntax.StatementSyntax>>
{
private readonly CSharpSyntaxVisitor<SyntaxList<VBSyntax.StatementSyntax>> wrappedVisitor;
private readonly TriviaConverter triviaConverter;
public CommentConvertingMethodBodyVisitor(CSharpSyntaxVisitor<SyntaxList<VBSyntax.StatementSyntax>> wrappedVisitor, TriviaConverter triviaConverter)
{
this.wrappedVisitor = wrappedVisitor;
this.triviaConverter = triviaConverter;
}
public override SyntaxList<VBSyntax.StatementSyntax> DefaultVisit(SyntaxNode node)
{
var syntaxNodes = wrappedVisitor.Visit(node);
// Port trivia to the last statement in the list
var lastWithConvertedTrivia = triviaConverter.PortConvertedTrivia(node, syntaxNodes.LastOrDefault());
return syntaxNodes.Replace(syntaxNodes.LastOrDefault(), lastWithConvertedTrivia);
}
}
}

Просмотреть файл

@ -0,0 +1,30 @@
using System;
using System.Linq;
using ICSharpCode.CodeConverter.CSharp;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.Util;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VisualBasic;
using VbSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax;
using CsSyntax = Microsoft.CodeAnalysis.CSharp.Syntax;
namespace ICSharpCode.CodeConverter.VB
{
public class CommentConvertingNodesVisitor : CSharpSyntaxVisitor<VisualBasicSyntaxNode>
{
public TriviaConverter TriviaConverter { get; }
private readonly CSharpSyntaxVisitor<VisualBasicSyntaxNode> _wrappedVisitor;
public CommentConvertingNodesVisitor(CSharpSyntaxVisitor<VisualBasicSyntaxNode> wrappedVisitor)
{
TriviaConverter = new TriviaConverter();
this._wrappedVisitor = wrappedVisitor;
}
public override VisualBasicSyntaxNode DefaultVisit(SyntaxNode node)
{
return TriviaConverter.PortConvertedTrivia(node, _wrappedVisitor.Visit(node));
}
}
}

Просмотреть файл

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.CodeConverter.CSharp;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.Util;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.VisualBasic;
@ -26,22 +28,26 @@ namespace ICSharpCode.CodeConverter.VB
class MethodBodyVisitor : CS.CSharpSyntaxVisitor<SyntaxList<StatementSyntax>>
{
SemanticModel semanticModel;
NodesVisitor nodesVisitor;
CS.CSharpSyntaxVisitor<VisualBasicSyntaxNode> nodesVisitor;
Stack<BlockInfo> blockInfo = new Stack<BlockInfo>(); // currently only works with switch blocks
int switchCount = 0;
public bool IsInterator { get; private set; }
public bool IsIterator { get; private set; }
class BlockInfo
{
public readonly List<VisualBasicSyntaxNode> GotoCaseExpressions = new List<VisualBasicSyntaxNode>();
}
public CommentConvertingMethodBodyVisitor CommentConvertingVisitor { get; }
public MethodBodyVisitor(SemanticModel semanticModel, NodesVisitor nodesVisitor)
public MethodBodyVisitor(SemanticModel semanticModel,
CS.CSharpSyntaxVisitor<VisualBasicSyntaxNode> nodesVisitor, TriviaConverter triviaConverter)
{
this.semanticModel = semanticModel;
this.nodesVisitor = nodesVisitor;
CommentConvertingVisitor = new CommentConvertingMethodBodyVisitor(this, triviaConverter);
}
public override SyntaxList<StatementSyntax> DefaultVisit(SyntaxNode node)
{
throw new NotImplementedException(node.GetType() + " not implemented!");
@ -496,7 +502,7 @@ namespace ICSharpCode.CodeConverter.VB
public override SyntaxList<StatementSyntax> VisitYieldStatement(CSS.YieldStatementSyntax node)
{
IsInterator = true;
IsIterator = true;
StatementSyntax stmt;
if (node.Expression == null)
stmt = SyntaxFactory.ReturnStatement();

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -4,6 +4,7 @@ using System.Collections.Immutable;
using System.Linq;
using System.Text;
using ICSharpCode.CodeConverter.CSharp;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.Util;
using ICSharpCode.CodeConverter.VB;
using Microsoft.CodeAnalysis;

Просмотреть файл

@ -5,6 +5,7 @@ using System.Linq;
using System.Threading.Tasks;
using ICSharpCode.CodeConverter;
using ICSharpCode.CodeConverter.CSharp;
using ICSharpCode.CodeConverter.Shared;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServices;
using Microsoft.VisualStudio.Shell.Interop;