Fix up struct/class/module
VB has a block containing a statement with the first line This is a common pattern in VB's syntax tree - I'll need to use something similar elsewhere too around other block structures squash! WIP possibly want to use DeclarationStatementSyntax even. See NestedClass bug though
This commit is contained in:
Родитель
f9aaced0a3
Коммит
36c4f04a5c
|
@ -3,6 +3,7 @@ using System.Linq;
|
|||
using ICSharpCode.CodeConverter.Util;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using Microsoft.CodeAnalysis.VisualBasic;
|
||||
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
|
||||
|
@ -15,7 +16,6 @@ namespace ICSharpCode.CodeConverter.CSharp
|
|||
{
|
||||
public TriviaConverter TriviaConverter { get; }
|
||||
private readonly VisualBasicSyntaxVisitor<CSharpSyntaxNode> wrappedVisitor;
|
||||
private static readonly SyntaxTrivia EndOfLine = SyntaxFactory.SyntaxTrivia(SyntaxKind.EndOfLineTrivia, Environment.NewLine);
|
||||
|
||||
public CommentConvertingNodesVisitor(VisualBasicSyntaxVisitor<CSharpSyntaxNode> wrappedVisitor)
|
||||
{
|
||||
|
@ -25,6 +25,15 @@ namespace ICSharpCode.CodeConverter.CSharp
|
|||
public override CSharpSyntaxNode DefaultVisit(SyntaxNode node)
|
||||
{
|
||||
var cSharpSyntaxNode = wrappedVisitor.Visit(node);
|
||||
if (node is TypeStatementSyntax) {
|
||||
return cSharpSyntaxNode;
|
||||
}
|
||||
|
||||
if (node is TypeBlockSyntax typeBlockVbNode && cSharpSyntaxNode is BaseTypeDeclarationSyntax btCsNode) {
|
||||
var beforeOpenBrace = btCsNode.OpenBraceToken.GetPreviousToken();
|
||||
cSharpSyntaxNode = cSharpSyntaxNode.ReplaceToken(beforeOpenBrace,
|
||||
beforeOpenBrace.WithConvertedTriviaFrom(typeBlockVbNode.BlockStatement));
|
||||
}
|
||||
return TriviaConverter.PortConvertedTrivia(node, cSharpSyntaxNode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,20 +25,32 @@ namespace ICSharpCode.CodeConverter.CSharp
|
|||
.Where(tnp => descendantNodes.Select(f => f.FullSpan).Contains(tnp.Value.FullSpan))//TODO Check/fix perf
|
||||
.ToList();
|
||||
foreach (var missedPort in missedPortsWhichAreChildren.ToList()) {
|
||||
destination = destination.ReplaceNode(missedPort.Value,
|
||||
missedPort.Value.WithTrailingTrivia(missedPort.Key.TrailingTrivia));
|
||||
trailingPortsDelegatedToParent.Remove(missedPort.Key);
|
||||
var missedPortValue = missedPort.Value;
|
||||
var missedPortKey = missedPort.Key;
|
||||
destination = destination.ReplaceNode(missedPortValue,
|
||||
missedPortValue.WithTrailingTrivia(missedPortKey.TrailingTrivia));
|
||||
MarkAsPorted(missedPort.Key);
|
||||
}
|
||||
|
||||
if (!lastSourceToken.HasTrailingTrivia) return destination;
|
||||
if (lastSourceToken == sourceNode.Parent?.GetLastToken()) {
|
||||
trailingPortsDelegatedToParent[lastSourceToken] = destination;
|
||||
DelegateToParent(lastSourceToken, destination);
|
||||
return destination;
|
||||
}
|
||||
|
||||
trailingPortsDelegatedToParent.Remove(lastSourceToken);
|
||||
MarkAsPorted(lastSourceToken);
|
||||
var convertedTrivia = lastSourceToken.TrailingTrivia.ConvertTrivia();
|
||||
return destination.WithTrailingTrivia(convertedTrivia);
|
||||
}
|
||||
|
||||
private void DelegateToParent<T>(SyntaxToken lastSourceToken, T destination) where T : SyntaxNode
|
||||
{
|
||||
trailingPortsDelegatedToParent[lastSourceToken] = destination;
|
||||
}
|
||||
|
||||
public void MarkAsPorted(SyntaxToken lastSourceToken)
|
||||
{
|
||||
trailingPortsDelegatedToParent.Remove(lastSourceToken);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -235,6 +235,7 @@ namespace CodeConverter.Tests
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualBasic;
|
||||
|
||||
";
|
||||
if (expectedCsharpCode.StartsWith(start)) {
|
||||
expectedCsharpCode = expectedCsharpCode.Substring(start.Length);
|
||||
|
@ -276,11 +277,11 @@ using Microsoft.VisualBasic;
|
|||
int skipped = 0;
|
||||
var lines = code.Split('\r'); // Don't split at very start
|
||||
var newLines = lines.Select((s, i) => {
|
||||
if (s.Trim() == "{" || i == 0 || s.IndexOf("class ", 0, StringComparison.InvariantCultureIgnoreCase) > -1) {
|
||||
if (s.Trim() == "{" || s.Trim().StartsWith("Inherits")) {
|
||||
skipped++;
|
||||
return s;
|
||||
}
|
||||
if (s.Trim() == "") {
|
||||
if (s.Trim() == "" && i > 0) {
|
||||
s = new string(Enumerable.Repeat(' ', lines[i - 1].Trim('\n').Length).ToArray());
|
||||
}
|
||||
return s + singleLineCommentStart + (i - skipped).ToString();
|
||||
|
|
Загрузка…
Ссылка в новой задаче