下面列出了怎么用com.intellij.psi.PsiCompiledElement的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean isValidForFile(@NotNull Editor editor, @NotNull PsiFile file) {
if (!(file instanceof PsiJavaFile)) {
return false;
}
if (file instanceof PsiCompiledElement) {
return false;
}
if (!file.isWritable()) {
return false;
}
PsiClass targetClass = getTargetClass(editor, file);
return targetClass != null && isValidForClass(targetClass);
}
public static boolean processUsagesInText(@Nonnull final PsiElement element,
@Nonnull Collection<String> stringToSearch,
@Nonnull GlobalSearchScope searchScope,
@Nonnull Processor<UsageInfo> processor) {
final TextRange elementTextRange = ApplicationManager.getApplication().runReadAction(new NullableComputable<TextRange>() {
@Override
public TextRange compute() {
if (!element.isValid() || element instanceof PsiCompiledElement) return null;
return element.getTextRange();
}
});
UsageInfoFactory factory = new UsageInfoFactory() {
@Override
public UsageInfo createUsageInfo(@Nonnull PsiElement usage, int startOffset, int endOffset) {
if (elementTextRange != null
&& usage.getContainingFile() == element.getContainingFile()
&& elementTextRange.contains(startOffset)
&& elementTextRange.contains(endOffset)) {
return null;
}
PsiReference someReference = usage.findReferenceAt(startOffset);
if (someReference != null) {
PsiElement refElement = someReference.getElement();
for (PsiReference ref : PsiReferenceService.getService().getReferences(refElement, new PsiReferenceService.Hints(element, null))) {
if (element.getManager().areElementsEquivalent(ref.resolve(), element)) {
TextRange range = ref.getRangeInElement().shiftRight(refElement.getTextRange().getStartOffset() - usage.getTextRange().getStartOffset());
return new UsageInfo(usage, range.getStartOffset(), range.getEndOffset(), true);
}
}
}
return new UsageInfo(usage, startOffset, endOffset, true);
}
};
for (String s : stringToSearch) {
if (!PsiSearchHelperImpl.processTextOccurrences(element, s, searchScope, processor, factory)) return false;
}
return true;
}
public static void invoke(@Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiFile file, Surrounder surrounder) {
if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
if (file instanceof PsiCompiledElement) {
HintManager.getInstance().showErrorHint(editor, "Can't modify decompiled code");
return;
}
List<AnAction> applicable = buildSurroundActions(project, editor, file, surrounder);
if (applicable != null) {
showPopup(editor, applicable);
}
else if (!ApplicationManager.getApplication().isUnitTestMode()) {
HintManager.getInstance().showErrorHint(editor, "Couldn't find Surround With variants applicable to the current context");
}
}
private void registerListeners(final Editor editor) {
if (editor.isOneLineMode()) {
return;
}
Project project = editor.getProject();
if (project == null || project.isDisposed()) {
return;
}
PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (psiFile == null || psiFile instanceof PsiCompiledElement || !isSupportedFile(psiFile)) {
return;
}
editor.addEditorMouseMotionListener(this);
KeyListener keyListener = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SHIFT && !editor.isOneLineMode()) {
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
if (pointerInfo != null) {
Point location = pointerInfo.getLocation();
SwingUtilities.convertPointFromScreen(location, editor.getContentComponent());
alarm.cancelAllRequests();
alarm.addRequest(new PreviewRequest(location, editor, true), 100);
}
}
}
};
editor.getContentComponent().addKeyListener(keyListener);
EDITOR_LISTENER_ADDED.set(editor, keyListener);
}
@Nonnull
private static Collection<PsiElement> getPsiElementsAt(Point point, Editor editor) {
if (editor.isDisposed()) {
return Collections.emptySet();
}
Project project = editor.getProject();
if (project == null || project.isDisposed()) {
return Collections.emptySet();
}
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document document = editor.getDocument();
PsiFile psiFile = documentManager.getPsiFile(document);
if (psiFile == null || psiFile instanceof PsiCompiledElement || !psiFile.isValid()) {
return Collections.emptySet();
}
final Set<PsiElement> elements = Collections.newSetFromMap(ContainerUtil.createWeakMap());
final int offset = editor.logicalPositionToOffset(editor.xyToLogicalPosition(point));
if (documentManager.isCommitted(document)) {
ContainerUtil.addIfNotNull(elements, InjectedLanguageUtil.findElementAtNoCommit(psiFile, offset));
}
for (PsiFile file : psiFile.getViewProvider().getAllFiles()) {
ContainerUtil.addIfNotNull(elements, file.findElementAt(offset));
}
return elements;
}
@Nullable
private static PsiElement getOriginalElement(@Nullable PsiElement e) {
if (e == null || !e.isValid()) return null;
PsiFile containingFile = e.getContainingFile();
if (containingFile != null && containingFile.getVirtualFile() == null) return null;
PsiElement orig = e.getOriginalElement();
return !(e instanceof PsiCompiledElement) && orig instanceof PsiCompiledElement ? e : orig;
}
@Override
public int compare(ElementNode n1, ElementNode n2) {
if (n1.getDelegate() instanceof ClassMemberWithElement && n2.getDelegate() instanceof ClassMemberWithElement) {
PsiElement element1 = ((ClassMemberWithElement)n1.getDelegate()).getElement();
PsiElement element2 = ((ClassMemberWithElement)n2.getDelegate()).getElement();
if (!(element1 instanceof PsiCompiledElement) && !(element2 instanceof PsiCompiledElement)) {
return element1.getTextOffset() - element2.getTextOffset();
}
}
return n1.getOrder() - n2.getOrder();
}
public static boolean isBinary(PsiModifierListOwner psiMember) {
return psiMember instanceof PsiCompiledElement;
}
private static boolean isCompiledWithoutSource(Object o) {
return o instanceof PsiCompiledElement && ((PsiCompiledElement)o).getNavigationElement() == o;
}