@ -17,16 +17,19 @@ namespace SharpRefactoring.ContextActions
{
{
public void Initialize ( EditorContext context )
public void Initialize ( EditorContext context )
{
{
this . context = context ;
this . VariableName = GetVariableName ( context ) ;
this . VariableName = GetVariableName ( context ) ;
this . CodeGenerator = GetCodeGenerator ( context ) ;
this . CodeGenerator = GetCodeGenerator ( context ) ;
this . Element = GetElement ( context ) ;
this . Element = GetElement ( context ) ;
this . ElementRegion = ( Element = = null ) ? DomRegion . Empty : DomRegion . FromLocation ( Element . StartLocation , Element . EndLocation ) ;
this . ElementRegion = ( Element = = null ) ? DomRegion . Empty : DomRegion . FromLocation ( Element . StartLocation , Element . EndLocation ) ;
}
}
EditorContext context ;
public bool IsActionAvailable
public bool IsActionAvailable
{
{
get {
get {
return ! string . IsNullOrEmpty ( this . VariableName ) & & ( this . CodeGenerator ! = null ) ;
return ! string . IsNullOrEmpty ( this . VariableName ) & & ( this . CodeGenerator ! = null ) & & ( this . context . CurrentLine . Text . Contains ( ";" ) ) ;
}
}
}
}
@ -76,7 +79,7 @@ namespace SharpRefactoring.ContextActions
var identifier = assignment . Left as IdentifierExpression ;
var identifier = assignment . Left as IdentifierExpression ;
if ( identifier = = null )
if ( identifier = = null )
return null ;
return null ;
if ( ! ExpressionCanBeNull ( assignment . Right ) )
if ( ( ! ExpressionCanBeNull ( assignment . Right ) ) | | ExpressionIsValueType ( assignment . Right ) )
// don't offer action where it makes no sense
// don't offer action where it makes no sense
return null ;
return null ;
return identifier . Identifier ;
return identifier . Identifier ;
@ -89,12 +92,19 @@ namespace SharpRefactoring.ContextActions
if ( declaration . Variables . Count ! = 1 )
if ( declaration . Variables . Count ! = 1 )
return null ;
return null ;
VariableDeclaration varDecl = declaration . Variables [ 0 ] ;
VariableDeclaration varDecl = declaration . Variables [ 0 ] ;
if ( ! ExpressionCanBeNull ( varDecl . Initializer ) )
if ( ! ExpressionCanBeNull ( varDecl . Initializer ) | | ExpressionIsValueType ( varDecl . Initializer ) )
// don't offer action where it makes no sense
// don't offer action where it makes no sense
return null ;
return null ;
return varDecl . Name ;
return varDecl . Name ;
}
}
bool ExpressionIsValueType ( Expression expr )
{
ResolveResult rr = this . context . ResolveExpression ( expr ) ;
if ( rr = = null ) return false ; // when cannot resolve the assignment right, still offer the action
return ( rr . ResolvedType ! = null & & rr . ResolvedType . IsReferenceType = = false ) ;
}
bool ExpressionCanBeNull ( Expression expr )
bool ExpressionCanBeNull ( Expression expr )
{
{
if ( expr = = null ) return false ;
if ( expr = = null ) return false ;