Skip to content

Commit 6aea19a

Browse files
Merge pull request #1094 from icsharpcode/issue/1084
Issue/1084
2 parents 5143c08 + 3ff9806 commit 6aea19a

22 files changed

Lines changed: 82 additions & 66 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
77

88
### Vsix
99

10+
* Compatible with Visual Studio 2022 17.1 onwards
1011

1112
### VB -> C#
1213

1314

1415
### C# -> VB
1516

17+
* Converts file scoped namespaces
1618

1719
## [9.2.5] - 2024-01-31
1820

CodeConverter/CSharp/ClashingMemberRenamer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ internal static class ClashingMemberRenamer
88
/// Renames symbols in a VB project so that they don't clash with rules for C# member names, attempting to rename the least public ones first.
99
/// See https://github.com/icsharpcode/CodeConverter/issues/420
1010
/// </summary>
11-
public static async Task<Project> RenameClashingSymbolsAsync(Project project)
11+
public static async Task<Project> RenameClashingSymbolsAsync(Project project, CancellationToken cancellationToken)
1212
{
13-
var compilation = await project.GetCompilationAsync();
13+
var compilation = await project.GetCompilationAsync(cancellationToken);
1414
var memberRenames = SymbolRenamer.GetNamespacesAndTypesInAssembly(project, compilation)
1515
.SelectMany(x => GetSymbolsWithNewNames(x, compilation));
16-
return await SymbolRenamer.PerformRenamesAsync(project, memberRenames.ToList());
16+
return await SymbolRenamer.PerformRenamesAsync(project, memberRenames.ToList(), cancellationToken);
1717
}
1818

1919
private static IEnumerable<(ISymbol Original, string NewName)> GetSymbolsWithNewNames(INamespaceOrTypeSymbol containerSymbol, Compilation compilation)

CodeConverter/CSharp/HandledEventsAnalysis.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private PropertyDeclarationSyntax GetDeclarationsForHandlingBaseMembers((EventCo
8585
var prop = (PropertyDeclarationSyntax) _commonConversions.CsSyntaxGenerator.Declaration(basePropertyEventSubscription.PropertyDetails.Property);
8686
var modifiers = prop.Modifiers.RemoveWhere(m => m.IsKind(SyntaxKind.VirtualKeyword)).Add(SyntaxFactory.Token(SyntaxKind.OverrideKeyword));
8787
//TODO Stash away methodwithandles in constructor that don't match any symbol from that type, to match here against base symbols
88-
return GetDeclarationsForFieldBackedProperty(basePropertyEventSubscription.HandledMethods, SyntaxFactory.List<SyntaxNode>(), modifiers,
88+
return GetDeclarationsForFieldBackedProperty(basePropertyEventSubscription.HandledMethods, SyntaxFactory.List<AttributeListSyntax>(), modifiers,
8989
prop.Type, prop.Identifier, SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.BaseExpression(), ValidSyntaxFactory.IdentifierName(prop.Identifier)));
9090
}
9191

CodeConverter/CSharp/LambdaConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private async Task<CSharpSyntaxNode> ConvertToFunctionDeclarationOrNullAsync(VBS
6767
// Could do: See if we can improve upon returning "object" for pretty much everything (which is what the symbols say)
6868
// I believe that in general, special VB functions such as MultiplyObject are designed to work the same as integer when given two integers for example.
6969
// If all callers currently pass an integer, perhaps it'd be more idiomatic in C# to specify "int", than to have Operators
70-
var paramsWithTypes = anonFuncOp.Symbol.Parameters.Select(p => CommonConversions.CsSyntaxGenerator.ParameterDeclaration(p));
70+
var paramsWithTypes = anonFuncOp.Symbol.Parameters.Select(p => (ParameterSyntax) CommonConversions.CsSyntaxGenerator.ParameterDeclaration(p));
7171

7272
var paramListWithTypes = param.WithParameters(SyntaxFactory.SeparatedList(paramsWithTypes));
7373
if (potentialAncestorDeclarationOperation is IFieldInitializerOperation fieldInit) {

CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ public override async Task<SyntaxList<StatementSyntax>> VisitForBlock(VBSyntax.F
611611
}
612612

613613

614-
var preLoopStatements = new List<SyntaxNode>();
614+
var preLoopStatements = new List<StatementSyntax>();
615615
var csToValue = await stmt.ToValue.AcceptAsync<ExpressionSyntax>(_expressionVisitor);
616616
csToValue = CommonConversions.TypeConversionAnalyzer.AddExplicitConversion(stmt.ToValue, csToValue?.SkipIntoParens(), forceTargetType: controlVarType);
617617

CodeConverter/CSharp/TypeConversionAnalyzer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,17 @@ private CSSyntax.NameSyntax GetCommonDelegateTypeOrNull(VBSyntax.ExpressionSynta
242242
private CSSyntax.NameSyntax CreateCommonDelegateTypeSyntax(IMethodSymbol vbLambda)
243243
{
244244
var parameters = vbLambda.Parameters
245-
.Select(p => _csSyntaxGenerator.TypeExpression(p.Type));
245+
.Select(p => (TypeSyntax) _csSyntaxGenerator.TypeExpression(p.Type));
246246

247247
if (vbLambda.ReturnType.IsSystemVoid()) {
248248
return CreateType("Action", parameters);
249249
}
250250

251-
var typeExpression = _csSyntaxGenerator.TypeExpression(vbLambda.ReturnType);
251+
var typeExpression = (TypeSyntax) _csSyntaxGenerator.TypeExpression(vbLambda.ReturnType);
252252
return CreateType("Func", parameters.Concat(typeExpression));
253253
}
254254

255-
private static CSSyntax.NameSyntax CreateType(string baseTypeName, IEnumerable<SyntaxNode> parameters)
255+
private static CSSyntax.NameSyntax CreateType(string baseTypeName, IEnumerable<TypeSyntax> parameters)
256256
{
257257
var parameterList = SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList(parameters));
258258
if (!parameterList.Arguments.Any()) return ValidSyntaxFactory.IdentifierName(baseTypeName);

CodeConverter/CSharp/VBToCSProjectContentsConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public VBToCSProjectContentsConverter(ConversionOptions conversionOptions,
4040

4141
public async Task InitializeSourceAsync(Project project)
4242
{
43-
project = await ClashingMemberRenamer.RenameClashingSymbolsAsync(project);
43+
project = await ClashingMemberRenamer.RenameClashingSymbolsAsync(project, _cancellationToken);
4444
var cSharpCompilationOptions = CSharpCompiler.CreateCompilationOptions();
4545
_convertedCsProject = project.ToProjectFromAnyOptions(cSharpCompilationOptions, CSharpCompiler.ParseOptions);
4646
_csharpReferenceProject = project.CreateReferenceOnlyProjectFromAnyOptions(cSharpCompilationOptions, CSharpCompiler.ParseOptions);

CodeConverter/CodeConverter.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
</ItemGroup>
3737

3838
<ItemGroup>
39-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.11.0" />
40-
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="3.11.0" />
39+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" Version="4.1.0" />
40+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.1.0" />
41+
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.1.0" />
4142
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
4243
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
4344
<PrivateAssets>all</PrivateAssets>

CodeConverter/Common/SymbolRenamer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static string GetName(ISymbol m) {
2626
return m.Name;
2727
}
2828

29-
public static async Task<Project> PerformRenamesAsync(Project project, IEnumerable<(ISymbol Original, string NewName)> symbolsWithNewNames)
29+
public static async Task<Project> PerformRenamesAsync(Project project, IEnumerable<(ISymbol Original, string NewName)> symbolsWithNewNames, CancellationToken cancellationToken)
3030
{
3131
var solution = project.Solution;
3232
foreach (var (originalSymbol, newName) in symbolsWithNewNames.OrderByDescending(s => s.Original.DeclaringSyntaxReferences.Select(x => x.Span.End).Max())) {

CodeConverter/VB/CSToVBProjectContentsConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public CSToVBProjectContentsConverter(ConversionOptions conversionOptions, IProg
4545
public async Task InitializeSourceAsync(Project project)
4646
{
4747
// TODO: Don't throw away solution-wide effects - write them to referencing files, and use in conversion of any other projects being converted at the same time.
48-
project = await ClashingMemberRenamer.RenameClashingSymbolsAsync(project);
48+
project = await ClashingMemberRenamer.RenameClashingSymbolsAsync(project, _cancellationToken);
4949
_convertedVbProject = project.ToProjectFromAnyOptions(_vbCompilationOptions, _vbParseOptions);
5050
_vbReferenceProject = project.CreateReferenceOnlyProjectFromAnyOptions(_vbCompilationOptions, _vbParseOptions);
5151
_vbViewOfCsSymbols = (VisualBasicCompilation)await _vbReferenceProject.GetCompilationAsync(_cancellationToken);

0 commit comments

Comments
 (0)