11// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22
3+ using ICSharpCode . CodeConverter . CSharp ;
4+
35namespace ICSharpCode . CodeConverter . Common ;
46
5- internal static class NameGenerator
7+ internal class NameGenerator
68{
7- public static string GenerateUniqueName ( string baseName , Func < string , bool > canUse )
8- {
9- return GenerateUniqueName ( baseName , string . Empty , canUse ) ;
10- }
9+ private readonly Func < string , string > _getValidIdentifier ;
1110
12- private static string GenerateUniqueName ( string baseName , string extension , Func < string , bool > canUse )
13- {
14- if ( ! string . IsNullOrEmpty ( extension ) && ! extension . StartsWith ( "." , StringComparison . InvariantCulture ) ) {
15- extension = "." + extension ;
16- }
11+ public static NameGenerator CS { get ; } = new NameGenerator ( x => CommonConversions . CsEscapedIdentifier ( x ) . Text ) ;
12+ public static NameGenerator Generic { get ; } = new NameGenerator ( x => x ) ;
1713
18- var name = baseName + extension ;
19- var index = 1 ;
14+ private NameGenerator ( Func < string , string > getValidIdentifier ) => _getValidIdentifier = getValidIdentifier ;
2015
21- // Check for collisions
22- while ( ! canUse ( name ) ) {
23- name = baseName + index + extension ;
24- index ++ ;
25- }
26-
27- return name ;
28- }
16+ public string GenerateUniqueName ( string baseName , Func < string , bool > canUse ) => GenerateUniqueName ( baseName , string . Empty , canUse ) ;
2917
30- public static string GetUniqueVariableNameInScope ( SemanticModel semanticModel , HashSet < string > generatedNames , VBasic . VisualBasicSyntaxNode node , string variableNameBase )
18+ public string GetUniqueVariableNameInScope ( SemanticModel semanticModel , HashSet < string > generatedNames , VBasic . VisualBasicSyntaxNode node , string variableNameBase )
3119 {
3220 // Need to check not just the symbols this node has access to, but whether there are any nested blocks which have access to this node and contain a conflicting name
3321 var scopeStarts = GetScopeStarts ( node ) ;
3422 return GenerateUniqueVariableNameInScope ( semanticModel , generatedNames , variableNameBase , scopeStarts ) ;
3523 }
3624
37- private static string GenerateUniqueVariableNameInScope ( SemanticModel semanticModel , HashSet < string > generatedNames ,
25+ public string GenerateUniqueVariableName ( HashSet < string > generatedNames , string variableNameBase )
26+ {
27+ string uniqueName = GenerateUniqueName ( variableNameBase , string . Empty , n => ! generatedNames . Contains ( n ) ) ;
28+ generatedNames . Add ( uniqueName ) ;
29+ return uniqueName ;
30+ }
31+
32+ private string GenerateUniqueVariableNameInScope ( SemanticModel semanticModel , HashSet < string > generatedNames ,
3833 string variableNameBase , List < int > scopeStarts )
3934 {
4035 string uniqueName = GenerateUniqueName ( variableNameBase , string . Empty ,
@@ -47,16 +42,29 @@ private static string GenerateUniqueVariableNameInScope(SemanticModel semanticMo
4742 return uniqueName ;
4843 }
4944
45+ private string GenerateUniqueName ( string baseName , string extension , Func < string , bool > canUse )
46+ {
47+ baseName = _getValidIdentifier ( baseName ) ;
48+ if ( ! string . IsNullOrEmpty ( extension ) && ! extension . StartsWith ( "." , StringComparison . InvariantCulture ) ) {
49+ extension = "." + extension ;
50+ }
51+
52+ var name = baseName + extension ;
53+ var index = 1 ;
54+
55+ // Check for collisions
56+ while ( ! canUse ( name ) ) {
57+ name = baseName + index + extension ;
58+ index ++ ;
59+ }
60+
61+ return name ;
62+ }
63+
5064 private static List < int > GetScopeStarts ( VBasic . VisualBasicSyntaxNode node )
5165 {
5266 return node . GetAncestorOrThis < VBSyntax . StatementSyntax > ( ) . DescendantNodesAndSelf ( )
5367 . OfType < VBSyntax . StatementSyntax > ( ) . Select ( n => n . SpanStart ) . ToList ( ) ;
5468 }
5569
56- public static string GenerateUniqueVariableName ( HashSet < string > generatedNames , string variableNameBase )
57- {
58- string uniqueName = GenerateUniqueName ( variableNameBase , string . Empty , n => ! generatedNames . Contains ( n ) ) ;
59- generatedNames . Add ( uniqueName ) ;
60- return uniqueName ;
61- }
6270}
0 commit comments