Search in sources :

Example 1 with RefactoringActionHandler

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

the class PyExtractMethodTest method doTest.

private void doTest(String newName) {
    final String testName = getTestName(false);
    final String beforeName = testName + ".before.py";
    final String afterName = testName + ".after.py";
    final String dir = "refactoring/extractmethod/";
    myFixture.configureByFile(dir + beforeName);
    final RefactoringSupportProvider provider = LanguageRefactoringSupport.INSTANCE.forLanguage(PythonLanguage.getInstance());
    assertNotNull(provider);
    final RefactoringActionHandler handler = provider.getExtractMethodHandler();
    assertNotNull(handler);
    System.setProperty(PyExtractMethodUtil.NAME, newName);
    try {
        refactorUsingHandler(handler);
    } finally {
        System.clearProperty(PyExtractMethodUtil.NAME);
    }
    myFixture.checkResultByFile(dir + afterName);
}
Also used : RefactoringSupportProvider(com.intellij.lang.refactoring.RefactoringSupportProvider) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler)

Example 2 with RefactoringActionHandler

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

the class BaseRefactoringAction method hasAvailableHandler.

protected boolean hasAvailableHandler(@NotNull DataContext dataContext) {
    final RefactoringActionHandler handler = getHandler(dataContext);
    if (handler != null) {
        if (handler instanceof ContextAwareActionHandler) {
            final Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
            final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
            if (editor != null && file != null && !((ContextAwareActionHandler) handler).isAvailableForQuickList(editor, file, dataContext)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
Also used : RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) Editor(com.intellij.openapi.editor.Editor) ContextAwareActionHandler(com.intellij.lang.ContextAwareActionHandler)

Example 3 with RefactoringActionHandler

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

the class RefactoringQuickFix method doFix.

default default void doFix(@NotNull PsiElement element) {
    final PsiElement elementToRefactor = getElementToRefactor(element);
    if (elementToRefactor == null) {
        return;
    }
    final Consumer<DataContext> consumer = dataContext -> {
        dataContext = enhanceDataContext(dataContext);
        final RefactoringActionHandler handler = getHandler();
        handler.invoke(element.getProject(), new PsiElement[] { elementToRefactor }, dataContext);
    };
    DataManager.getInstance().getDataContextFromFocus().doWhenDone(consumer);
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) NonNls(org.jetbrains.annotations.NonNls) PsiNamedElement(com.intellij.psi.PsiNamedElement) NotNull(org.jetbrains.annotations.NotNull) DataManager(com.intellij.ide.DataManager) Consumer(com.intellij.util.Consumer) DataContext(com.intellij.openapi.actionSystem.DataContext) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) PsiElement(com.intellij.psi.PsiElement)

Example 4 with RefactoringActionHandler

use of com.intellij.refactoring.RefactoringActionHandler in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoRenameQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (!FileModificationService.getInstance().preparePsiElementsForWrite(startElement))
        return;
    Runnable runnable = () -> {
        AsyncResult<DataContext> dataContextContainer = DataManager.getInstance().getDataContextFromFocus();
        dataContextContainer.doWhenDone(new Consumer<DataContext>() {

            @Override
            public void consume(DataContext dataContext) {
                RenameHandler renameHandler = RenameHandlerRegistry.getInstance().getRenameHandler(dataContext);
                if (renameHandler != null) {
                    renameHandler.invoke(project, new PsiElement[] { startElement }, dataContext);
                } else {
                    RefactoringActionHandler renameRefactoringHandler = RefactoringActionHandlerFactory.getInstance().createRenameHandler();
                    renameRefactoringHandler.invoke(project, new PsiElement[] { startElement }, dataContext);
                }
            }
        });
    };
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        runnable.run();
    } else {
        ApplicationManager.getApplication().invokeLater(runnable, project.getDisposed());
    }
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) Consumer(com.intellij.util.Consumer) RenameHandler(com.intellij.refactoring.rename.RenameHandler) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) AsyncResult(com.intellij.openapi.util.AsyncResult)

Example 5 with RefactoringActionHandler

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

the class MoveClassToModuleFix method moveClass.

private void moveClass(Project project, Editor editor, PsiFile file, PsiClass aClass) {
    RefactoringActionHandler moveHandler = RefactoringActionHandlerFactory.getInstance().createMoveHandler();
    DataManager dataManager = DataManager.getInstance();
    DataContext dataContext = dataManager.getDataContext();
    final String fqName = aClass.getQualifiedName();
    LOG.assertTrue(fqName != null);
    PsiDirectory directory = PackageUtil.findOrCreateDirectoryForPackage(myCurrentModule, StringUtil.getPackageName(fqName), mySourceRoot, true);
    DataContext context = SimpleDataContext.getSimpleContext(LangDataKeys.TARGET_PSI_ELEMENT.getName(), directory, dataContext);
    moveHandler.invoke(project, new PsiElement[] { aClass }, context);
    PsiReference reference = file.findReferenceAt(editor.getCaretModel().getOffset());
    PsiClass newClass = JavaPsiFacade.getInstance(project).findClass(fqName, GlobalSearchScope.moduleScope(myCurrentModule));
    if (reference != null && newClass != null) {
        final QuestionAction action = new AddImportAction(project, reference, editor, newClass);
        action.execute();
    }
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) QuestionAction(com.intellij.codeInsight.hint.QuestionAction) AddImportAction(com.intellij.codeInsight.daemon.impl.actions.AddImportAction) DataManager(com.intellij.ide.DataManager) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler)

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