Skip to content

Commit 753cf92

Browse files
Make ConvertText conversions partial by default - fixes #368
1 parent 85b05ee commit 753cf92

17 files changed

Lines changed: 291 additions & 282 deletions

File tree

ICSharpCode.CodeConverter/CSharp/DeclarationNodeVisitor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,7 @@ private static bool HasPartialKeyword(SyntaxTokenList modifiers)
306306

307307
private bool IsPartialType(VBSyntax.DeclarationStatementSyntax stmt)
308308
{
309-
var declaredSymbol = _semanticModel.GetDeclaredSymbol(stmt);
310-
return declaredSymbol.GetDeclarations().Count() > 1;
309+
return _semanticModel.GetDeclaredSymbol(stmt).IsPartialClassDefinition();
311310
}
312311

313312
public override async Task<CSharpSyntaxNode> VisitEnumBlock(VBSyntax.EnumBlockSyntax node)

ICSharpCode.CodeConverter/CSharp/VBToCSConversion.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Text.RegularExpressions;
1515
using System.Threading.Tasks;
1616
using Microsoft.CodeAnalysis.Operations;
17+
using ISymbolExtensions = ICSharpCode.CodeConverter.Util.ISymbolExtensions;
1718
using LanguageVersion = Microsoft.CodeAnalysis.CSharp.LanguageVersion;
1819

1920
namespace ICSharpCode.CodeConverter.CSharp
@@ -186,8 +187,8 @@ public Document CreateProjectDocumentFromTree(Workspace workspace, SyntaxTree tr
186187
IEnumerable<MetadataReference> references)
187188
{
188189
return VisualBasicCompiler.CreateCompilationOptions(RootNamespace)
189-
.CreateProjectDocumentFromTree(workspace, tree, references,
190-
DoNotAllowImplicitDefault);
190+
.CreateProjectDocumentFromTree(workspace, tree, references, VisualBasicParseOptions.Default,
191+
ISymbolExtensions.ForcePartialTypesAssemblyName);
191192
}
192193
}
193194
}

ICSharpCode.CodeConverter/Shared/CompilationOptionsExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ private static Func<CompilationOptions, byte, CompilationOptions> CreateWithMeta
2222
}
2323

2424
public static Document CreateProjectDocumentFromTree(this CompilationOptions options,
25-
Workspace workspace, SyntaxTree tree, IEnumerable<MetadataReference> references, ParseOptions parseOptions)
25+
Workspace workspace, SyntaxTree tree, IEnumerable<MetadataReference> references, ParseOptions parseOptions,
26+
string singleDocumentAssemblyName = null)
2627
{
28+
singleDocumentAssemblyName = singleDocumentAssemblyName ?? "ProjectToBeConverted";
2729
ProjectId projectId = ProjectId.CreateNewId();
28-
var solution = workspace.CurrentSolution.AddProject(projectId, "ProjectToBeConverted",
29-
"ProjectToBeConverted", options.Language);
30+
var solution = workspace.CurrentSolution.AddProject(projectId, singleDocumentAssemblyName,
31+
singleDocumentAssemblyName, options.Language);
3032

3133
var project = solution.GetProject(projectId)
3234
.WithCompilationOptions(options)
35+
.WithParseOptions(parseOptions)
3336
.WithMetadataReferences(references);
3437
return project.AddDocument("CodeToConvert", tree.GetRoot(), filePath: Path.Combine(Directory.GetCurrentDirectory(), "TempCodeToConvert.txt"));
3538
}

ICSharpCode.CodeConverter/Util/ISymbolExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using ICSharpCode.CodeConverter.Shared;
45
using Microsoft.CodeAnalysis;
56
using Microsoft.CodeAnalysis.Editing;
67

@@ -16,6 +17,8 @@ static class ISymbolExtensions
1617
// as part of #174
1718
private static readonly string[] TypesToConvertToDateTime = new[] { "DateTime" };
1819

20+
public const string ForcePartialTypesAssemblyName = "ProjectToBeConvertedWithPartialTypes";
21+
1922
/// <summary>
2023
/// Checks if 'symbol' is accessible from within 'within'.
2124
/// </summary>
@@ -231,7 +234,8 @@ public static bool IsPartialMethodDefinition(this ISymbol declaredSymbol)
231234

232235
public static bool IsPartialClassDefinition(this ISymbol declaredSymbol)
233236
{
234-
return declaredSymbol is ITypeSymbol ts && ts.DeclaringSyntaxReferences.Length > 1;
237+
return declaredSymbol is ITypeSymbol ts && (ts.DeclaringSyntaxReferences.Length > 1
238+
|| ts.ContainingAssembly.Name == ForcePartialTypesAssemblyName);
235239
}
236240
}
237241
}

ICSharpCode.CodeConverter/VB/CSToVBConversion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public SyntaxTree CreateTree(string text)
173173
public Document CreateProjectDocumentFromTree(Workspace workspace, SyntaxTree tree,
174174
IEnumerable<MetadataReference> references)
175175
{
176-
return CSharpCompiler.CreateCompilationOptions().CreateProjectDocumentFromTree(workspace, tree, references, _visualBasicParseOptions);
176+
return CSharpCompiler.CreateCompilationOptions().CreateProjectDocumentFromTree(workspace, tree, references, CSharpParseOptions.Default);
177177
}
178178
}
179179
}

TestData/CSToVBCharacterization/ConvertSingleProject/EmptyVb/EmptyVb.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<DependentUpon>Settings.settings</DependentUpon>
8888
<DesignTimeSharedInput>True</DesignTimeSharedInput>
8989
</Compile>
90+
<Compile Include="PartialClassPart.vb" />
9091
</ItemGroup>
9192
<ItemGroup>
9293
<EmbeddedResource Include="My Project\Resources.resx">

TestData/CSToVBCharacterization/ConvertSolution/EmptyVb/EmptyVb.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<DependentUpon>Settings.settings</DependentUpon>
8888
<DesignTimeSharedInput>True</DesignTimeSharedInput>
8989
</Compile>
90+
<Compile Include="PartialClassPart.vb" />
9091
</ItemGroup>
9192
<ItemGroup>
9293
<EmbeddedResource Include="My Project\Resources.resx">

0 commit comments

Comments
 (0)