public void invoke( @NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) { editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); final int offset = TargetElementUtil.adjustOffset( file, editor.getDocument(), editor.getCaretModel().getOffset()); final PsiElement element = file.findElementAt(offset); PsiTypeElement typeElement = PsiTreeUtil.getParentOfType(element, PsiTypeElement.class); while (typeElement != null) { final PsiElement parent = typeElement.getParent(); PsiElement[] toMigrate = null; if (parent instanceof PsiVariable) { toMigrate = extractReferencedVariables(typeElement); } else if ((parent instanceof PsiMember && !(parent instanceof PsiClass)) || isClassArgument(parent)) { toMigrate = new PsiElement[] {parent}; } if (toMigrate != null && toMigrate.length > 0) { invoke(project, toMigrate, null, null, editor); return; } typeElement = PsiTreeUtil.getParentOfType(parent, PsiTypeElement.class, false); } CommonRefactoringUtil.showErrorHint( project, editor, "The caret should be positioned on type of field, variable, method or method parameter to be refactored", REFACTORING_NAME, "refactoring.migrateType"); }
@Nullable public static PsiElement[] findTargetElementsNoVS( Project project, Editor editor, int offset, boolean lookupAccepted) { Document document = editor.getDocument(); PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document); if (file == null) return null; PsiElement elementAt = file.findElementAt(TargetElementUtil.adjustOffset(file, document, offset)); for (GotoDeclarationHandler handler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) { try { PsiElement[] result = handler.getGotoDeclarationTargets(elementAt, offset, editor); if (result != null && result.length > 0) { for (PsiElement element : result) { if (element == null) { LOG.error("Null target element is returned by " + handler.getClass().getName()); return null; } } return result; } } catch (AbstractMethodError e) { LOG.error(new ExtensionException(handler.getClass())); } } int flags = TargetElementUtil.getInstance().getAllAccepted() & ~TargetElementUtil.ELEMENT_NAME_ACCEPTED; if (!lookupAccepted) { flags &= ~TargetElementUtil.LOOKUP_ITEM_ACCEPTED; } PsiElement element = TargetElementUtil.getInstance().findTargetElement(editor, flags, offset); if (element != null) { return new PsiElement[] {element}; } // if no references found in injected fragment, try outer document if (editor instanceof EditorWindow) { EditorWindow window = (EditorWindow) editor; return findTargetElementsNoVS( project, window.getDelegate(), window.getDocument().injectedToHost(offset), lookupAccepted); } return null; }