Search in sources :

Example 6 with RefactoringActionHandler

use of com.intellij.refactoring.RefactoringActionHandler in project intellij-community by JetBrains.

the class BasePlatformRefactoringAction method getHandler.

@Override
protected final RefactoringActionHandler getHandler(@NotNull DataContext dataContext) {
    PsiElement element = null;
    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
    if (editor != null && file != null) {
        element = getElementAtCaret(editor, file);
        if (element != null) {
            RefactoringActionHandler handler = getHandler(element.getLanguage(), element);
            if (handler != null) {
                return handler;
            }
        }
    }
    PsiElement referenced = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
    if (referenced != null) {
        RefactoringActionHandler handler = getHandler(referenced.getLanguage(), referenced);
        if (handler != null) {
            return handler;
        }
    }
    PsiElement[] psiElements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(dataContext);
    if (psiElements != null && psiElements.length > 1) {
        RefactoringActionHandler handler = getHandler(psiElements[0].getLanguage(), psiElements[0]);
        if (handler != null && isEnabledOnElements(psiElements)) {
            return handler;
        }
    }
    if (element == null) {
        element = referenced;
    }
    final Language[] languages = LangDataKeys.CONTEXT_LANGUAGES.getData(dataContext);
    if (languages != null) {
        for (Language language : languages) {
            RefactoringActionHandler handler = getHandler(language, element);
            if (handler != null) {
                return handler;
            }
        }
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 7 with RefactoringActionHandler

use of com.intellij.refactoring.RefactoringActionHandler in project intellij-community by JetBrains.

the class BasePlatformRefactoringAction method isEnabledOnElements.

@Override
protected boolean isEnabledOnElements(@NotNull PsiElement[] elements) {
    if (elements.length > 0) {
        Language language = elements[0].getLanguage();
        RefactoringActionHandler handler = getHandler(language, elements[0]);
        return handler instanceof ElementsHandler && ((ElementsHandler) handler).isEnabledOnElements(elements);
    }
    return false;
}
Also used : ElementsHandler(com.intellij.refactoring.lang.ElementsHandler) Language(com.intellij.lang.Language) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler)

Example 8 with RefactoringActionHandler

use of com.intellij.refactoring.RefactoringActionHandler in project intellij-community by JetBrains.

the class BaseRefactoringAction method actionPerformed.

@Override
public final void actionPerformed(@NotNull AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    Project project = e.getProject();
    if (project == null)
        return;
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final Editor editor = e.getData(CommonDataKeys.EDITOR);
    final PsiElement[] elements = getPsiElementArray(dataContext);
    int eventCount = IdeEventQueue.getInstance().getEventCount();
    RefactoringActionHandler handler;
    try {
        handler = getHandler(dataContext);
    } catch (ProcessCanceledException ignored) {
        return;
    }
    if (handler == null) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.symbol.to.refactor"));
        CommonRefactoringUtil.showErrorHint(project, editor, message, RefactoringBundle.getCannotRefactorMessage(null), null);
        return;
    }
    InplaceRefactoring activeInplaceRenamer = InplaceRefactoring.getActiveInplaceRenamer(editor);
    if (!InplaceRefactoring.canStartAnotherRefactoring(editor, project, handler, elements) && activeInplaceRenamer != null) {
        InplaceRefactoring.unableToStartWarning(project, editor);
        return;
    }
    if (activeInplaceRenamer == null) {
        final LookupEx lookup = LookupManager.getActiveLookup(editor);
        if (lookup instanceof LookupImpl) {
            Runnable command = () -> ((LookupImpl) lookup).finishLookup(Lookup.NORMAL_SELECT_CHAR);
            Document doc = editor.getDocument();
            DocCommandGroupId group = DocCommandGroupId.noneGroupId(doc);
            CommandProcessor.getInstance().executeCommand(editor.getProject(), command, "Completion", group, UndoConfirmationPolicy.DEFAULT, doc);
        }
    }
    IdeEventQueue.getInstance().setEventCount(eventCount);
    if (editor != null) {
        final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null)
            return;
        DaemonCodeAnalyzer.getInstance(project).autoImportReferenceAtCursor(editor, file);
        handler.invoke(project, editor, file, dataContext);
    } else {
        handler.invoke(project, elements, dataContext);
    }
}
Also used : RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl) DocCommandGroupId(com.intellij.openapi.editor.actionSystem.DocCommandGroupId) Editor(com.intellij.openapi.editor.Editor) LookupEx(com.intellij.codeInsight.lookup.LookupEx) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) InplaceRefactoring(com.intellij.refactoring.rename.inplace.InplaceRefactoring)

Example 9 with RefactoringActionHandler

use of com.intellij.refactoring.RefactoringActionHandler in project intellij-community by JetBrains.

the class ChangeSignatureAction method getRefactoringHandler.

@Nullable
@Override
protected RefactoringActionHandler getRefactoringHandler(@NotNull RefactoringSupportProvider provider, final PsiElement element) {
    abstract class ContextAwareChangeSignatureHandler implements RefactoringActionHandler, ContextAwareActionHandler {
    }
    return new ContextAwareChangeSignatureHandler() {

        @Override
        public boolean isAvailableForQuickList(@NotNull Editor editor, @NotNull PsiFile file, @NotNull DataContext dataContext) {
            return findTargetMember(element) != null;
        }

        @Override
        public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
            editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
            final PsiElement targetMember = findTargetMember(element);
            if (targetMember == null) {
                final ChangeSignatureHandler handler = getChangeSignatureHandler(file.getLanguage());
                if (handler != null) {
                    final String notFoundMessage = handler.getTargetNotFoundMessage();
                    if (notFoundMessage != null) {
                        CommonRefactoringUtil.showErrorHint(project, editor, notFoundMessage, ChangeSignatureHandler.REFACTORING_NAME, null);
                    }
                }
                return;
            }
            final ChangeSignatureHandler handler = getChangeSignatureHandler(targetMember.getLanguage());
            if (handler == null)
                return;
            handler.invoke(project, new PsiElement[] { targetMember }, dataContext);
        }

        @Override
        public void invoke(@NotNull Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
            if (elements.length != 1)
                return;
            final PsiElement targetMember = findTargetMember(elements[0]);
            if (targetMember == null)
                return;
            final ChangeSignatureHandler handler = getChangeSignatureHandler(targetMember.getLanguage());
            if (handler == null)
                return;
            handler.invoke(project, new PsiElement[] { targetMember }, dataContext);
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) ChangeSignatureHandler(com.intellij.refactoring.changeSignature.ChangeSignatureHandler) PsiFile(com.intellij.psi.PsiFile) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) Editor(com.intellij.openapi.editor.Editor) ContextAwareActionHandler(com.intellij.lang.ContextAwareActionHandler) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with RefactoringActionHandler

use of com.intellij.refactoring.RefactoringActionHandler in project intellij-community by JetBrains.

the class ExtractIncludeAction method update.

@Override
public void update(final AnActionEvent e) {
    super.update(e);
    final RefactoringActionHandler handler = getHandler(e.getDataContext());
    if (handler instanceof TitledHandler) {
        e.getPresentation().setText(((TitledHandler) handler).getActionTitle());
    } else {
        e.getPresentation().setText("Include File...");
    }
}
Also used : RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) TitledHandler(com.intellij.ide.TitledHandler)

Aggregations

RefactoringActionHandler (com.intellij.refactoring.RefactoringActionHandler)11 DataContext (com.intellij.openapi.actionSystem.DataContext)4 Editor (com.intellij.openapi.editor.Editor)4 Project (com.intellij.openapi.project.Project)3 PsiElement (com.intellij.psi.PsiElement)3 DataManager (com.intellij.ide.DataManager)2 ContextAwareActionHandler (com.intellij.lang.ContextAwareActionHandler)2 Language (com.intellij.lang.Language)2 PsiFile (com.intellij.psi.PsiFile)2 Consumer (com.intellij.util.Consumer)2 NotNull (org.jetbrains.annotations.NotNull)2 AddImportAction (com.intellij.codeInsight.daemon.impl.actions.AddImportAction)1 QuestionAction (com.intellij.codeInsight.hint.QuestionAction)1 LookupEx (com.intellij.codeInsight.lookup.LookupEx)1 LookupImpl (com.intellij.codeInsight.lookup.impl.LookupImpl)1 TitledHandler (com.intellij.ide.TitledHandler)1 InlineHandler (com.intellij.lang.refactoring.InlineHandler)1 RefactoringSupportProvider (com.intellij.lang.refactoring.RefactoringSupportProvider)1 SimpleDataContext (com.intellij.openapi.actionSystem.impl.SimpleDataContext)1 Document (com.intellij.openapi.editor.Document)1