use of com.intellij.codeInsight.hint.QuestionAction in project intellij-community by JetBrains.
the class ImportClassFixBase method doFix.
public Result doFix(@NotNull final Editor editor, boolean allowPopup, final boolean allowCaretNearRef) {
List<PsiClass> classesToImport = getClassesToImport();
if (classesToImport.isEmpty())
return Result.POPUP_NOT_SHOWN;
try {
String name = getQualifiedName(myElement);
if (name != null) {
Pattern pattern = Pattern.compile(DaemonCodeAnalyzerSettings.getInstance().NO_AUTO_IMPORT_PATTERN);
Matcher matcher = pattern.matcher(name);
if (matcher.matches()) {
return Result.POPUP_NOT_SHOWN;
}
}
} catch (PatternSyntaxException e) {
//ignore
}
final PsiFile psiFile = myElement.getContainingFile();
if (classesToImport.size() > 1) {
reduceSuggestedClassesBasedOnDependencyRuleViolation(psiFile, classesToImport);
}
PsiClass[] classes = classesToImport.toArray(new PsiClass[classesToImport.size()]);
final Project project = myElement.getProject();
CodeInsightUtil.sortIdenticalShortNamedMembers(classes, myRef);
final QuestionAction action = createAddImportAction(classes, project, editor);
boolean canImportHere = true;
boolean isInModlessContext = Registry.is("ide.perProjectModality") ? !LaterInvocator.isInModalContextForProject(editor.getProject()) : !LaterInvocator.isInModalContext();
if (classes.length == 1 && (canImportHere = canImportHere(allowCaretNearRef, editor, psiFile, classes[0].getName())) && isAddUnambiguousImportsOnTheFlyEnabled(psiFile) && (ApplicationManager.getApplication().isUnitTestMode() || DaemonListeners.canChangeFileSilently(psiFile)) && isInModlessContext && !autoImportWillInsertUnexpectedCharacters(classes[0])) {
CommandProcessor.getInstance().runUndoTransparentAction(() -> action.execute());
return Result.CLASS_AUTO_IMPORTED;
}
if (allowPopup && canImportHere) {
String hintText = ShowAutoImportPass.getMessage(classes.length > 1, classes[0].getQualifiedName());
if (!ApplicationManager.getApplication().isUnitTestMode() && !HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(true)) {
HintManager.getInstance().showQuestionHint(editor, hintText, getStartOffset(myElement, myRef), getEndOffset(myElement, myRef), action);
}
return Result.POPUP_SHOWN;
}
return Result.POPUP_NOT_SHOWN;
}
use of com.intellij.codeInsight.hint.QuestionAction in project intellij-plugins by JetBrains.
the class HintWrapperQuickFix method showHint.
@Override
public boolean showHint(@NotNull final Editor editor) {
if (HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(true)) {
return false;
}
if (ApplicationManager.getApplication().isUnitTestMode()) {
return false;
}
String message = getText() + "? " + KeymapUtil.getFirstKeyboardShortcutText(ActionManager.getInstance().getAction(IdeActions.ACTION_SHOW_INTENTION_ACTIONS));
if (!myPsiElement.isValid()) {
return false;
}
TextRange hintRange = calcRange();
HintManager.getInstance().showQuestionHint(editor, message, hintRange.getStartOffset(), hintRange.getEndOffset(), new QuestionAction() {
@Override
public boolean execute() {
myDelegate.invoke(myPsiElement.getProject(), editor, myPsiElement.getContainingFile());
return true;
}
});
return true;
}
use of com.intellij.codeInsight.hint.QuestionAction in project intellij-community by JetBrains.
the class StaticImportMemberFix method doFix.
private ImportClassFixBase.Result doFix(Editor editor) {
if (!CodeInsightSettings.getInstance().ADD_MEMBER_IMPORTS_ON_THE_FLY) {
return ImportClassFixBase.Result.POPUP_NOT_SHOWN;
}
final List<T> candidates = getMembersToImport(true);
if (candidates.isEmpty()) {
return ImportClassFixBase.Result.POPUP_NOT_SHOWN;
}
final PsiElement element = getElement();
if (element == null) {
return ImportClassFixBase.Result.POPUP_NOT_SHOWN;
}
final QuestionAction action = createQuestionAction(candidates, element.getProject(), editor);
/* PsiFile psiFile = element.getContainingFile();
if (candidates.size() == 1 &&
ImportClassFixBase.isAddUnambiguousImportsOnTheFlyEnabled(psiFile) &&
(ApplicationManager.getApplication().isUnitTestMode() || DaemonListeners.canChangeFileSilently(psiFile)) &&
!LaterInvocator.isInModalContext()) {
CommandProcessor.getInstance().runUndoTransparentAction(() -> action.execute());
return ImportClassFixBase.Result.CLASS_AUTO_IMPORTED;
}
*/
String hintText = ShowAutoImportPass.getMessage(candidates.size() > 1, getMemberPresentableText(candidates.get(0)));
if (!ApplicationManager.getApplication().isUnitTestMode() && !HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(true)) {
final TextRange textRange = element.getTextRange();
HintManager.getInstance().showQuestionHint(editor, hintText, textRange.getStartOffset(), textRange.getEndOffset(), action);
}
return ImportClassFixBase.Result.POPUP_SHOWN;
}
use of com.intellij.codeInsight.hint.QuestionAction 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();
}
}
Aggregations