GenerateVariableName() public method

Generates a variable name for a variable of the specified type.
public GenerateVariableName ( AstType type, string baseName = null ) : string
type AstType /// The type of the variable. ///
baseName string /// Suggested base name. ///
return string
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var catchClause = context.GetNode <CatchClause>();

            if (catchClause == null)
            {
                yield break;
            }
            if (!catchClause.Type.IsNull)
            {
                yield break;
            }
            yield return(new CodeAction(context.TranslateString("Add type specifier"), script => {
                var newType = context.CreateShortType("System", "Exception");
                var namingHelper = new NamingHelper(context);
                var newIdentifier = Identifier.Create(namingHelper.GenerateVariableName(newType, "e"));

                script.Replace(catchClause, new CatchClause {
                    Type = newType,
                    VariableNameToken = newIdentifier,
                    Body = catchClause.Body.Clone() as BlockStatement
                });
                script.Select(newType);
            }, catchClause));
        }
Beispiel #2
0
        public void GenerateVariableNameDoesNotRepeatNames()
        {
            var context = MakeContext(@"
class A
{
	void F()
	{
		$
	}
}"
                                      );
            var namingHelper = new NamingHelper(context);
            var name1        = namingHelper.GenerateVariableName(new PrimitiveType("int"));
            var name2        = namingHelper.GenerateVariableName(new PrimitiveType("int"));

            Assert.NotNull(name1);
            Assert.NotNull(name2);
            Assert.AreNotEqual(name1, name2, "The generated names should not repeat.");
        }
        static ForeachStatement MakeForeach(Expression node, IType type, RefactoringContext context)
        {
            var namingHelper = new NamingHelper(context);

            return(new ForeachStatement {
                VariableType = new SimpleType("var"),
                VariableName = namingHelper.GenerateVariableName(type),
                InExpression = node.Clone(),
                EmbeddedStatement = new BlockStatement()
            });
        }
        public IEnumerable<CodeAction> GetActions(RefactoringContext context)
        {
            var catchClause = context.GetNode<CatchClause>();
            if (catchClause == null)
                yield break;
            if (!catchClause.Type.IsNull)
                yield break;
            yield return new CodeAction(context.TranslateString("Add type specifier"), script => {
                var newType = context.CreateShortType("System", "Exception");
                var namingHelper = new NamingHelper(context);
                var newIdentifier = Identifier.Create(namingHelper.GenerateVariableName(newType, "e"));

                script.Replace(catchClause, new CatchClause {
                    Type = newType,
                    VariableNameToken = newIdentifier,
                    Body = catchClause.Body.Clone() as BlockStatement
                });
                script.Select(newType);
            }, catchClause);
        }
Beispiel #5
0
            public override Expression VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, Expression data)
            {
                var creationType = objectCreateExpression.Type.Clone();

                var parameters  = objectCreateExpression.Arguments.Select(arg => arg.Clone());
                var newCreation = new ObjectCreateExpression(creationType, parameters);

                if (objectCreateExpression.Initializer.IsNull)
                {
                    return(newCreation);
                }
                else
                {
                    AstType declarationType     = GetDeclarationType(objectCreateExpression.Type);
                    var     name                = namingHelper.GenerateVariableName(objectCreateExpression.Type);
                    var     variableInitializer = new VariableDeclarationStatement(declarationType, name, newCreation);
                    statements.Add(variableInitializer);

                    var identifier = new IdentifierExpression(name);
                    base.VisitObjectCreateExpression(objectCreateExpression, identifier);

                    return(identifier);
                }
            }
		static ForeachStatement MakeForeach(Expression node, IType type, RefactoringContext context)
		{
			var namingHelper = new NamingHelper(context);
			return new ForeachStatement() {
				VariableType = new SimpleType("var"),
				VariableName = namingHelper.GenerateVariableName(type),
				InExpression = node.Clone(),
				EmbeddedStatement = new BlockStatement()
			};
		}
Beispiel #7
0
				public void GenerateVariableNameDoesNotRepeatNames()
				{
						var context = MakeContext(@"
class A
{
	void F()
	{
		$
	}
}"
						);
						var namingHelper = new NamingHelper(context);
						var name1 = namingHelper.GenerateVariableName(new PrimitiveType("int"));
						var name2 = namingHelper.GenerateVariableName(new PrimitiveType("int"));
						Assert.NotNull(name1);
						Assert.NotNull(name2);
						Assert.AreNotEqual(name1, name2, "The generated names should not repeat.");
				}