Search in sources :

Example 1 with OccurrenceManager

use of com.intellij.refactoring.util.occurrences.OccurrenceManager in project intellij-community by JetBrains.

the class BaseExpressionToFieldHandler method convertExpressionToField.

private boolean convertExpressionToField(PsiExpression selectedExpr, @Nullable Editor editor, PsiFile file, final Project project, PsiType tempType) {
    if (myParentClass == null) {
        if (FileTypeUtils.isInServerPageFile(file)) {
            CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.message("error.not.supported.for.jsp", getRefactoringName()), getRefactoringName(), getHelpID());
            return true;
        } else if ("package-info.java".equals(file.getName())) {
            CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.message("error.not.supported.for.package.info", getRefactoringName()), getRefactoringName(), getHelpID());
            return true;
        } else {
            LOG.error(file);
            return true;
        }
    }
    if (!validClass(myParentClass, editor)) {
        return true;
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file))
        return true;
    final OccurrenceManager occurrenceManager = createOccurrenceManager(selectedExpr, myParentClass);
    final PsiExpression[] occurrences = occurrenceManager.getOccurrences();
    final PsiElement anchorStatementIfAll = occurrenceManager.getAnchorStatementForAll();
    List<RangeHighlighter> highlighters = null;
    if (editor != null) {
        highlighters = RefactoringUtil.highlightAllOccurrences(project, occurrences, editor);
    }
    PsiElement tempAnchorElement = RefactoringUtil.getParentExpressionAnchorElement(selectedExpr);
    if (!Comparing.strEqual(IntroduceConstantHandler.REFACTORING_NAME, getRefactoringName()) && IntroduceVariableBase.checkAnchorBeforeThisOrSuper(project, editor, tempAnchorElement, getRefactoringName(), getHelpID()))
        return true;
    final Settings settings = showRefactoringDialog(project, editor, myParentClass, selectedExpr, tempType, occurrences, tempAnchorElement, anchorStatementIfAll);
    if (settings == null)
        return true;
    if (settings.getForcedType() != null) {
        tempType = settings.getForcedType();
    }
    final PsiType type = tempType;
    if (editor != null) {
        HighlightManager highlightManager = HighlightManager.getInstance(project);
        for (RangeHighlighter highlighter : highlighters) {
            highlightManager.removeSegmentHighlighter(editor, highlighter);
        }
    }
    final Runnable runnable = new ConvertToFieldRunnable(settings.getSelectedExpr(), settings, type, settings.getOccurrences(), occurrenceManager, anchorStatementIfAll, tempAnchorElement, editor, myParentClass);
    new WriteCommandAction(project, getRefactoringName()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            runnable.run();
        }
    }.execute();
    return false;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) OccurrenceManager(com.intellij.refactoring.util.occurrences.OccurrenceManager) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) Result(com.intellij.openapi.application.Result) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter)

Aggregations

HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)1 OccurrenceManager (com.intellij.refactoring.util.occurrences.OccurrenceManager)1