@Nullable public static PsiElement correctSearchTargets(@Nullable PsiElement target) { if (target != null && !target.isPhysical()) { return target.getNavigationElement(); } return target; }
public static boolean validElement(@NotNull PsiElement element) { if (element instanceof PsiFile) return true; if (!element.isPhysical()) return false; final RefactoringSupportProvider provider = LanguageRefactoringSupport.INSTANCE.forLanguage(element.getLanguage()); return provider.isSafeDeleteAvailable(element); }
protected void assertPhysical(final PsiElement element) { if (!element.isPhysical()) { LOG.error( "Non-physical PsiElement. Physical element is required to be able to anchor the problem in the source tree: " + element + "; file: " + element.getContainingFile()); } }
public static void sendBeforeChildrenChangeEvent( @NotNull PsiManagerImpl manager, @NotNull PsiElement scope, boolean isGenericChange) { if (!scope.isPhysical()) { manager.beforeChange(false); return; } PsiTreeChangeEventImpl event = new PsiTreeChangeEventImpl(manager); event.setParent(scope); event.setFile(scope.getContainingFile()); TextRange range = scope.getTextRange(); event.setOffset(range == null ? 0 : range.getStartOffset()); event.setOldLength(scope.getTextLength()); // the "generic" event is being sent on every PSI change. It does not carry any specific info // except the fact that "something has changed" event.setGenericChange(isGenericChange); manager.beforeChildrenChange(event); }
public int getLineNumber() { if (myLineNumber == -1) { PsiElement psiElement = getPsiElement(); if (psiElement == null) return -1; if (!psiElement.isValid()) return -1; LOG.assertTrue(psiElement.isPhysical()); PsiFile containingFile = InjectedLanguageUtil.getTopLevelFile(psiElement); Document document = PsiDocumentManager.getInstance(psiElement.getProject()).getDocument(containingFile); if (document == null) return -1; TextRange textRange = getTextRange(); if (textRange == null) return -1; textRange = InjectedLanguageManager.getInstance(containingFile.getProject()) .injectedToHost(psiElement, textRange); myLineNumber = document.getLineNumber(textRange.getStartOffset()) + 1; } return myLineNumber; }
public RunnerAndConfigurationSettings createConfigurationByElement( Location location, final ConfigurationContext context) { location = JavaExecutionUtil.stepIntoSingleClass(location); if (location == null) return null; final PsiElement element = location.getPsiElement(); if (!element.isPhysical()) return null; PsiElement currentElement = element; PsiMethod method; while ((method = findMain(currentElement)) != null) { final PsiClass aClass = method.getContainingClass(); if (ConfigurationUtil.MAIN_CLASS.value(aClass)) { myPsiElement = method; return createConfiguration(aClass, context, location); } currentElement = method.getParent(); } final PsiClass aClass = ApplicationConfigurationType.getMainClass(element); if (aClass == null) return null; myPsiElement = aClass; return createConfiguration(aClass, context, location); }
@Override @NotNull public AnnotationPlace chooseAnnotationsPlace(@NotNull final PsiElement element) { if (!element.isPhysical()) return AnnotationPlace.IN_CODE; // element just created if (!element.getManager().isInProject(element)) return AnnotationPlace.EXTERNAL; final Project project = myPsiManager.getProject(); final PsiFile containingFile = element.getContainingFile(); final VirtualFile virtualFile = containingFile.getVirtualFile(); LOG.assertTrue(virtualFile != null); final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile); if (!entries.isEmpty()) { for (OrderEntry entry : entries) { if (!(entry instanceof ModuleOrderEntry)) { if (AnnotationOrderRootType.getUrls(entry).length > 0) { return AnnotationPlace.EXTERNAL; } break; } } } final MyExternalPromptDialog dialog = ApplicationManager.getApplication().isUnitTestMode() || ApplicationManager.getApplication().isHeadlessEnvironment() ? null : new MyExternalPromptDialog(project); if (dialog != null && dialog.isToBeShown()) { final PsiElement highlightElement = element instanceof PsiNameIdentifierOwner ? ((PsiNameIdentifierOwner) element).getNameIdentifier() : element.getNavigationElement(); LOG.assertTrue(highlightElement != null); final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); final List<RangeHighlighter> highlighters = new ArrayList<RangeHighlighter>(); final boolean highlight = editor != null && editor.getDocument() == PsiDocumentManager.getInstance(project).getDocument(containingFile); try { if (highlight) { // do not highlight for batch inspections final EditorColorsManager colorsManager = EditorColorsManager.getInstance(); final TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES); final TextRange textRange = highlightElement.getTextRange(); HighlightManager.getInstance(project) .addRangeHighlight( editor, textRange.getStartOffset(), textRange.getEndOffset(), attributes, true, highlighters); final LogicalPosition logicalPosition = editor.offsetToLogicalPosition(textRange.getStartOffset()); editor.getScrollingModel().scrollTo(logicalPosition, ScrollType.CENTER); } dialog.show(); if (dialog.getExitCode() == 2) { return AnnotationPlace.EXTERNAL; } else if (dialog.getExitCode() == 1) { return AnnotationPlace.NOWHERE; } } finally { if (highlight) { HighlightManager.getInstance(project) .removeSegmentHighlighter(editor, highlighters.get(0)); } } } else if (dialog != null) { dialog.close(DialogWrapper.OK_EXIT_CODE); } return AnnotationPlace.IN_CODE; }
@Nullable private Info getInfoAt(@NotNull Editor editor, PsiFile file, int offset, BrowseMode browseMode) { PsiElement targetElement = null; if (browseMode == BrowseMode.TypeDeclaration) { try { targetElement = GotoTypeDeclarationAction.findSymbolType(editor, offset); } catch (IndexNotReadyException e) { showDumbModeNotification(myProject); } } else if (browseMode == BrowseMode.Declaration) { final PsiReference ref = TargetElementUtilBase.findReference(editor, offset); final List<PsiElement> resolvedElements = ref != null ? resolve(ref) : Collections.<PsiElement>emptyList(); final PsiElement resolvedElement = resolvedElements.size() == 1 ? resolvedElements.get(0) : null; final PsiElement[] targetElements = GotoDeclarationAction.findTargetElementsNoVS(myProject, editor, offset, false); final PsiElement elementAtPointer = file.findElementAt( TargetElementUtilBase.adjustOffset(file, editor.getDocument(), offset)); if (targetElements != null) { if (targetElements.length == 0) { return null; } else if (targetElements.length == 1) { if (targetElements[0] != resolvedElement && elementAtPointer != null && targetElements[0].isPhysical()) { return ref != null ? new InfoSingle(ref, targetElements[0]) : new InfoSingle(elementAtPointer, targetElements[0]); } } else { return elementAtPointer != null ? new InfoMultiple(elementAtPointer) : null; } } if (resolvedElements.size() == 1) { return new InfoSingle(ref, resolvedElements.get(0)); } else if (resolvedElements.size() > 1) { return elementAtPointer != null ? new InfoMultiple(elementAtPointer, ref) : null; } } else if (browseMode == BrowseMode.Implementation) { final PsiElement element = TargetElementUtilBase.getInstance() .findTargetElement(editor, ImplementationSearcher.getFlags(), offset); PsiElement[] targetElements = new ImplementationSearcher() { @Override @NotNull protected PsiElement[] searchDefinitions(final PsiElement element, Editor editor) { final List<PsiElement> found = new ArrayList<PsiElement>(2); DefinitionsScopedSearch.search(element, getSearchScope(element, editor)) .forEach( new Processor<PsiElement>() { @Override public boolean process(final PsiElement psiElement) { found.add(psiElement); return found.size() != 2; } }); return PsiUtilCore.toPsiElementArray(found); } }.searchImplementations(editor, element, offset); if (targetElements.length > 1) { PsiElement elementAtPointer = file.findElementAt(offset); if (elementAtPointer != null) { return new InfoMultiple(elementAtPointer); } return null; } if (targetElements.length == 1) { Navigatable descriptor = EditSourceUtil.getDescriptor(targetElements[0]); if (descriptor == null || !descriptor.canNavigate()) { return null; } targetElement = targetElements[0]; } } if (targetElement != null && targetElement.isPhysical()) { PsiElement elementAtPointer = file.findElementAt(offset); if (elementAtPointer != null) { return new InfoSingle(elementAtPointer, targetElement); } } return null; }
@Override public boolean isPhysical() { final PsiElement parent = getParent(); return parent != null && parent.isPhysical(); }