Search in sources :

Example 1 with VariableInplaceRenamer

use of com.intellij.refactoring.rename.inplace.VariableInplaceRenamer in project intellij-community by JetBrains.

the class JavaWithRunnableSurrounder method surroundStatements.

@Override
public TextRange surroundStatements(Project project, final Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException {
    PsiManager manager = container.getManager();
    PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    final String baseName = "runnable";
    final String uniqueName = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(baseName, container, false);
    @NonNls String text = "Runnable runnable = new Runnable(){\npublic void run(){\n}};";
    PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement) factory.createStatementFromText(text, null);
    declarationStatement = (PsiDeclarationStatement) codeStyleManager.reformat(declarationStatement);
    declarationStatement = (PsiDeclarationStatement) container.addAfter(declarationStatement, statements[statements.length - 1]);
    final PsiVariable variable = (PsiVariable) declarationStatement.getDeclaredElements()[0];
    if (!Comparing.strEqual(uniqueName, baseName)) {
        variable.setName(uniqueName);
    }
    PsiNewExpression newExpression = (PsiNewExpression) variable.getInitializer();
    PsiElement[] children = newExpression.getChildren();
    PsiAnonymousClass anonymousClass = (PsiAnonymousClass) children[children.length - 1];
    PsiMethod method = anonymousClass.getMethods()[0];
    PsiCodeBlock body = method.getBody();
    body.addRange(statements[0], statements[statements.length - 1]);
    container.deleteChildRange(statements[0], statements[statements.length - 1]);
    makeVariablesFinal(body, body);
    final int textOffset = variable.getNameIdentifier().getTextOffset();
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    editor.getCaretModel().moveToOffset(textOffset);
    editor.getSelectionModel().removeSelection();
    new VariableInplaceRenamer(variable, editor) {

        @Override
        protected boolean shouldSelectAll() {
            return true;
        }

        @Override
        protected void moveOffsetAfter(boolean success) {
            super.moveOffsetAfter(success);
            if (success) {
                final PsiNamedElement renamedVariable = getVariable();
                if (renamedVariable != null) {
                    editor.getCaretModel().moveToOffset(renamedVariable.getTextRange().getEndOffset());
                }
            }
        }
    }.performInplaceRename();
    return null;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) VariableInplaceRenamer(com.intellij.refactoring.rename.inplace.VariableInplaceRenamer)

Example 2 with VariableInplaceRenamer

use of com.intellij.refactoring.rename.inplace.VariableInplaceRenamer in project intellij-community by JetBrains.

the class EachToForIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrMethodCallExpression expression = (GrMethodCallExpression) element;
    final GrClosableBlock block = expression.getClosureArguments()[0];
    final GrParameterList parameterList = block.getParameterList();
    final GrParameter[] parameters = parameterList.getParameters();
    String var;
    if (parameters.length == 1) {
        var = parameters[0].getText();
        var = StringUtil.replace(var, GrModifier.DEF, "");
    } else {
        var = "it";
    }
    final GrExpression invokedExpression = expression.getInvokedExpression();
    GrExpression qualifier = ((GrReferenceExpression) invokedExpression).getQualifierExpression();
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(element.getProject());
    if (qualifier == null) {
        qualifier = elementFactory.createExpressionFromText("this");
    }
    StringBuilder builder = new StringBuilder();
    builder.append("for (").append(var).append(" in ").append(qualifier.getText()).append(") {\n");
    String text = block.getText();
    final PsiElement blockArrow = block.getArrow();
    int index;
    if (blockArrow != null) {
        index = blockArrow.getStartOffsetInParent() + blockArrow.getTextLength();
    } else {
        index = 1;
    }
    while (index < text.length() && Character.isWhitespace(text.charAt(index))) index++;
    text = text.substring(index, text.length() - 1);
    builder.append(text);
    builder.append("}");
    final GrStatement statement = elementFactory.createStatementFromText(builder.toString());
    GrForStatement forStatement = (GrForStatement) expression.replaceWithStatement(statement);
    final GrForClause clause = forStatement.getClause();
    GrVariable variable = clause.getDeclaredVariable();
    forStatement = updateReturnStatements(forStatement);
    if (variable == null)
        return;
    if (ApplicationManager.getApplication().isUnitTestMode())
        return;
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    final Document doc = documentManager.getDocument(element.getContainingFile());
    if (doc == null)
        return;
    documentManager.doPostponedOperationsAndUnblockDocument(doc);
    editor.getCaretModel().moveToOffset(variable.getTextOffset());
    new VariableInplaceRenamer(variable, editor).performInplaceRename();
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Document(com.intellij.openapi.editor.Document) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause) VariableInplaceRenamer(com.intellij.refactoring.rename.inplace.VariableInplaceRenamer) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 3 with VariableInplaceRenamer

use of com.intellij.refactoring.rename.inplace.VariableInplaceRenamer in project intellij-community by JetBrains.

the class PyRenameElementQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    if (element instanceof PyReferenceExpression) {
        final PsiReference reference = element.getReference();
        if (reference == null)
            return;
        element = reference.resolve();
    }
    final PsiNameIdentifierOwner nameOwner = element instanceof PsiNameIdentifierOwner ? (PsiNameIdentifierOwner) element : PsiTreeUtil.getParentOfType(element, PsiNameIdentifierOwner.class, true);
    if (nameOwner != null) {
        final VirtualFile virtualFile = nameOwner.getContainingFile().getVirtualFile();
        if (virtualFile != null) {
            final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
            if (ApplicationManager.getApplication().isUnitTestMode()) {
                renameInUnitTestMode(project, nameOwner, editor);
            } else {
                if (checkLocalScope(element) != null && (nameOwner instanceof PyNamedParameter || nameOwner instanceof PyTargetExpression)) {
                    new VariableInplaceRenamer(nameOwner, editor).performInplaceRename();
                } else {
                    PsiElementRenameHandler.invoke(nameOwner, project, ScopeUtil.getScopeOwner(nameOwner), editor);
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiNameIdentifierOwner(com.intellij.psi.PsiNameIdentifierOwner) PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PsiReference(com.intellij.psi.PsiReference) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) Editor(com.intellij.openapi.editor.Editor) VariableInplaceRenamer(com.intellij.refactoring.rename.inplace.VariableInplaceRenamer) PsiElement(com.intellij.psi.PsiElement) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression)

Aggregations

VariableInplaceRenamer (com.intellij.refactoring.rename.inplace.VariableInplaceRenamer)3 PsiElement (com.intellij.psi.PsiElement)2 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiNameIdentifierOwner (com.intellij.psi.PsiNameIdentifierOwner)1 PsiReference (com.intellij.psi.PsiReference)1 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)1 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)1 PyNamedParameter (com.jetbrains.python.psi.PyNamedParameter)1 PyReferenceExpression (com.jetbrains.python.psi.PyReferenceExpression)1 PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)1 NonNls (org.jetbrains.annotations.NonNls)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1