Search in sources :

Example 11 with PsiCompiledElement

use of com.intellij.psi.PsiCompiledElement in project android by JetBrains.

the class ActionBarHandler method getMenuIdNames.

@Override
public List<String> getMenuIdNames() {
    if (myMenus != null) {
        return myMenus;
    }
    boolean token = RenderSecurityManager.enterSafeRegion(myCredential);
    try {
        final XmlFile xmlFile = myRenderTask.getPsiFile();
        String commaSeparatedMenus = xmlFile == null ? null : AndroidPsiUtils.getRootTagAttributeSafely(xmlFile, ATTR_MENU, TOOLS_URI);
        if (commaSeparatedMenus != null) {
            myMenus = new ArrayList<String>();
            Iterables.addAll(myMenus, Splitter.on(',').trimResults().omitEmptyStrings().split(commaSeparatedMenus));
        } else {
            final String fqn = xmlFile == null ? null : AndroidPsiUtils.getDeclaredContextFqcn(myRenderTask.getModule(), xmlFile);
            if (fqn != null) {
                final Project project = xmlFile.getProject();
                DumbService.getInstance(project).smartInvokeLater(new Runnable() {

                    @Override
                    public void run() {
                        // Glance at the onCreateOptionsMenu of the associated context and use any menus found there.
                        // This is just a simple textual search; we need to replace this with a proper model lookup.
                        PsiClass clz = JavaPsiFacade.getInstance(project).findClass(fqn, GlobalSearchScope.allScope(project));
                        if (clz != null) {
                            for (PsiMethod method : clz.findMethodsByName(ON_CREATE_OPTIONS_MENU, true)) {
                                if (method instanceof PsiCompiledElement) {
                                    continue;
                                }
                                // TODO: This should instead try to use the GotoRelated implementation's notion
                                // of associated activities; see what is done in
                                // AndroidMissingOnClickHandlerInspection. However, the AndroidGotoRelatedProvider
                                // will first need to properly handle menus.
                                String matchText = method.getText();
                                Matcher matcher = MENU_FIELD_PATTERN.matcher(matchText);
                                Set<String> menus = Sets.newTreeSet();
                                int index = 0;
                                while (true) {
                                    if (matcher.find(index)) {
                                        menus.add(matcher.group(1));
                                        index = matcher.end();
                                    } else {
                                        break;
                                    }
                                }
                                if (!menus.isEmpty()) {
                                    myMenus = new ArrayList<String>(menus);
                                }
                            }
                        }
                    }
                });
            }
        }
        if (myMenus == null) {
            myMenus = Collections.emptyList();
        }
    } finally {
        RenderSecurityManager.exitSafeRegion(token);
    }
    return myMenus;
}
Also used : Set(java.util.Set) XmlFile(com.intellij.psi.xml.XmlFile) PsiMethod(com.intellij.psi.PsiMethod) Matcher(java.util.regex.Matcher) PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) PsiCompiledElement(com.intellij.psi.PsiCompiledElement) Project(com.intellij.openapi.project.Project)

Example 12 with PsiCompiledElement

use of com.intellij.psi.PsiCompiledElement in project intellij-community by JetBrains.

the class FindUsagesHelper method processUsagesInText.

protected static boolean processUsagesInText(@NotNull final PsiElement element, @NotNull Collection<String> stringToSearch, @NotNull GlobalSearchScope searchScope, @NotNull 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(@NotNull PsiElement usage, int startOffset, int endOffset) {
            if (!element.isValid())
                return new UsageInfo(usage, startOffset, endOffset, true);
            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;
}
Also used : PsiCompiledElement(com.intellij.psi.PsiCompiledElement) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) PsiReferenceService(com.intellij.psi.PsiReferenceService) UsageInfoFactory(com.intellij.usageView.UsageInfoFactory) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 13 with PsiCompiledElement

use of com.intellij.psi.PsiCompiledElement in project intellij-community by JetBrains.

the class MethodInheritanceUtils method findAvailableSuperClasses.

private static void findAvailableSuperClasses(PsiMethod method, List<PsiClass> sourceClasses) {
    final PsiMethod[] superMethods = method.findSuperMethods(true);
    for (PsiMethod superMethod : superMethods) {
        final PsiClass containingClass = superMethod.getContainingClass();
        if (!(containingClass instanceof PsiCompiledElement)) {
            sourceClasses.add(containingClass);
            findAvailableSuperClasses(superMethod, sourceClasses);
        }
    }
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PsiCompiledElement(com.intellij.psi.PsiCompiledElement)

Example 14 with PsiCompiledElement

use of com.intellij.psi.PsiCompiledElement in project intellij-community by JetBrains.

the class DebuggerContextUtil method findNearest.

public static SourcePosition findNearest(@NotNull DebuggerContextImpl context, @NotNull PsiElement psi, @NotNull PsiFile file) {
    if (psi instanceof PsiCompiledElement) {
        // it makes no sense to compute text range of compiled element
        return null;
    }
    final DebuggerSession session = context.getDebuggerSession();
    if (session != null) {
        try {
            final XDebugSession debugSession = session.getXDebugSession();
            if (debugSession != null) {
                final XSourcePosition position = debugSession.getCurrentPosition();
                Editor editor = ((FileEditorManagerImpl) FileEditorManager.getInstance(file.getProject())).getSelectedTextEditor(true);
                //final Editor editor = fileEditor instanceof TextEditorImpl ? ((TextEditorImpl)fileEditor).getEditor() : null;
                if (editor != null && position != null && position.getFile().equals(file.getOriginalFile().getVirtualFile())) {
                    PsiMethod method = PsiTreeUtil.getParentOfType(PositionUtil.getContextElement(context), PsiMethod.class, false);
                    final Collection<TextRange> ranges = IdentifierHighlighterPass.getUsages(psi, method != null ? method : file, false);
                    final int breakPointLine = position.getLine();
                    int bestLine = -1;
                    int bestOffset = -1;
                    for (TextRange range : ranges) {
                        final int line = editor.offsetToLogicalPosition(range.getStartOffset()).line;
                        if (line > bestLine && line < breakPointLine) {
                            bestLine = line;
                            bestOffset = range.getStartOffset();
                        } else if (line == breakPointLine) {
                            bestOffset = range.getStartOffset();
                            break;
                        }
                    }
                    if (bestOffset > -1) {
                        return SourcePosition.createFromOffset(file, bestOffset);
                    }
                }
            }
        } catch (Exception ignore) {
        }
    }
    return null;
}
Also used : XDebugSession(com.intellij.xdebugger.XDebugSession) PsiMethod(com.intellij.psi.PsiMethod) FileEditorManagerImpl(com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl) PsiCompiledElement(com.intellij.psi.PsiCompiledElement) TextRange(com.intellij.openapi.util.TextRange) XSourcePosition(com.intellij.xdebugger.XSourcePosition) Editor(com.intellij.openapi.editor.Editor)

Aggregations

PsiCompiledElement (com.intellij.psi.PsiCompiledElement)14 PsiClass (com.intellij.psi.PsiClass)6 PsiFile (com.intellij.psi.PsiFile)5 PsiElement (com.intellij.psi.PsiElement)4 PsiMethod (com.intellij.psi.PsiMethod)4 ArrayList (java.util.ArrayList)4 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)3 Document (com.intellij.openapi.editor.Document)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2 TextEditorHighlightingPass (com.intellij.codeHighlighting.TextEditorHighlightingPass)1 FileHighlightingSetting (com.intellij.codeInsight.daemon.impl.analysis.FileHighlightingSetting)1 HighlightingSettingsPerFile (com.intellij.codeInsight.daemon.impl.analysis.HighlightingSettingsPerFile)1 Language (com.intellij.lang.Language)1 UndoConfirmationPolicy (com.intellij.openapi.command.UndoConfirmationPolicy)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 GlobalUndoableAction (com.intellij.openapi.command.undo.GlobalUndoableAction)1 UndoableAction (com.intellij.openapi.command.undo.UndoableAction)1 Editor (com.intellij.openapi.editor.Editor)1