Intention Power Pack: Additional Intentions

Hi,

I have created two new intentions that I really need ;). But I don't want to create an additional plugin for intentions...


So maybe the maintainer could look over those intentions and add them?

First Wrapptin List-Setter

public boolean isAvailable( Project project, Editor editor, PsiFile file ) {
if ( !( file instanceof PsiJavaFile ) ) {
return false;
}

try {
PsiManager manager = PsiManager.getInstance( project );
PsiJavaFile javaFile = ( PsiJavaFile ) file;
PsiElement actualElement = javaFile.findElementAt( editor.getCaretModel().getOffset() );


PsiAssignmentExpression assignExpr = ( PsiAssignmentExpression ) PsiTreeUtil.getParentOfType( actualElement, PsiAssignmentExpression.class );
PsiMethod psiMethod = ( PsiMethod ) PsiTreeUtil.getParentOfType( actualElement, PsiMethod.class );

PsiClassType listType = manager.getElementFactory().createTypeByFQClassName( "java.util.List", actualElement.getResolveScope() );

if ( !psiMethod.getName().equalsIgnoreCase( "set" + assignExpr.getRExpression().getText() ) ) {
return false;
}

if ( psiMethod.getParameterList().getParameters().length != 1 ) {
return false;
}

if ( !listType.isAssignableFrom( psiMethod.getParameterList().getParameters()[ 0 ].getType() ) ) {
return false;
}

if ( !listType.isAssignableFrom( assignExpr.getLExpression().getType() ) ) {
return false;
}

if ( psiMethod.getBody().getStatements().length != 1 ) {
return false;
}

} catch ( Throwable e ) {
return false;
}

return true;
}

public void invoke( Project project, Editor editor, PsiFile file ) throws IncorrectOperationException {
PsiJavaFile javaFile = ( PsiJavaFile ) file;
PsiElement actualElement = javaFile.findElementAt( editor.getCaretModel().getOffset() );
PsiManager manager = PsiManager.getInstance( project );

PsiAssignmentExpression assignExpr = ( PsiAssignmentExpression ) PsiTreeUtil.getParentOfType( actualElement, PsiAssignmentExpression.class );
PsiExpressionStatement assignStatement = ( PsiExpressionStatement ) PsiTreeUtil.getParentOfType( actualElement, PsiExpressionStatement.class );
PsiMethod method = ( PsiMethod ) PsiTreeUtil.getParentOfType( actualElement, PsiMethod.class );

PsiStatement clearStatement = manager.getElementFactory().createStatementFromText( assignExpr.getLExpression().getText() + ".clear();", method.getContext() );
PsiStatement addAllStatement = manager.getElementFactory().createStatementFromText( assignExpr.getLExpression().getText() + ".addAll(" + assignExpr.getRExpression().getText() + ");", method.getContext() );


PsiElement changedExpression = assignStatement.replace( clearStatement );
changedExpression.getParent().addAfter( addAllStatement, changedExpression );

manager.getCodeStyleManager().reformat( method );
}


Second: Wrapping List getter

public boolean isAvailable( Project project, Editor editor, PsiFile file ) {
if ( !( file instanceof PsiJavaFile ) ) {
return false;
}

try {
PsiJavaFile javaFile = ( PsiJavaFile ) file;
PsiElement actualElement = javaFile.findElementAt( editor.getCaretModel().getOffset() );

PsiReturnStatement returnStatement = ( PsiReturnStatement ) PsiTreeUtil.getParentOfType( actualElement, PsiReturnStatement.class );
PsiMethod psiMethod = ( PsiMethod ) PsiTreeUtil.getParentOfType( actualElement, PsiMethod.class );

PsiReferenceExpression returnReference = ( PsiReferenceExpression ) PsiTreeUtil.getChildOfType( returnStatement, PsiReferenceExpression.class );

PsiManager manager = actualElement.getManager();
PsiClassType listType = manager.getElementFactory().createTypeByFQClassName( "java.util.List", actualElement.getResolveScope() );

if ( !listType.isAssignableFrom( returnReference.getType() ) ) {
return false;
}

if ( !psiMethod.getName().equalsIgnoreCase( "get" + returnReference.getText() ) ) {
return false;
}
} catch ( Throwable e ) {
return false;
}

return true;
}

public void invoke( Project project, Editor editor, PsiFile file ) throws IncorrectOperationException {
PsiJavaFile javaFile = ( PsiJavaFile ) file;
PsiElement actualElement = javaFile.findElementAt( editor.getCaretModel().getOffset() );

PsiReturnStatement returnStatement = ( PsiReturnStatement ) PsiTreeUtil.getParentOfType( actualElement, PsiReturnStatement.class );
PsiReferenceExpression returnReference = ( PsiReferenceExpression ) PsiTreeUtil.getChildOfType( returnStatement, PsiReferenceExpression.class );

PsiManager manager = PsiManager.getInstance( project );
PsiExpression newExpression = manager.getElementFactory().createExpressionFromText( "java.util.Collections.unmodifiableList(" + returnReference.getText() + ")", returnReference.getContext() );

manager.getCodeStyleManager().shortenClassReferences( newExpression );
manager.getCodeStyleManager().reformat( returnReference.replace( newExpression ) );
}

2 comments
Comment actions Permalink

Hmm, maybe. I have a half-finished plugin I was going to release in a couple of weeks which creates access methods like these. Differences are that it works for any sort of Collection or array field (and possibly Maps, soon), and it shows up as a menu item just like creating setters/getters/equals/hashcode. I have to think that's a bit better than an intention for this sort of thing, just from a consistency standpoint. OTOH, it might be worth adding an intention to that plugin, since IIRC there's intentions for create getter/setter as well. For that matter, foldign that whole collection access plugin into IPP has something to be said for it as well.

In short, good job, good idea, I'm not sure how it should be distributed, but I will work on it.

--Dave Griffith

0
Comment actions Permalink

Of course it is your choice ;).


I would be happy to see such a functionality in one of your plugins - of course asap (and in a better way if you can...).

0

Please sign in to leave a comment.