下面列出了com.intellij.psi.util.PsiTreeUtil#findElementOfClassAtOffset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Map<String, List<PsiElement>> collectRedSymbols(
PsiFile psiFile, Document document, Project project, ProgressIndicator daemonIndicator) {
Map<String, List<PsiElement>> redSymbolToElements = new HashMap<>();
List<HighlightInfo> infos =
((DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(project))
.runMainPasses(psiFile, document, daemonIndicator);
for (HighlightInfo info : infos) {
if (!info.getSeverity().equals(HighlightSeverity.ERROR)) continue;
final String redSymbol = document.getText(new TextRange(info.startOffset, info.endOffset));
if (!StringUtil.isJavaIdentifier(redSymbol) || !StringUtil.isCapitalized(redSymbol)) continue;
final PsiJavaCodeReferenceElement ref =
PsiTreeUtil.findElementOfClassAtOffset(
psiFile, info.startOffset, PsiJavaCodeReferenceElement.class, false);
if (ref == null) continue;
redSymbolToElements.putIfAbsent(redSymbol, new ArrayList<>());
redSymbolToElements.get(redSymbol).add(ref);
}
return redSymbolToElements;
}
@Nullable
protected Method getCurrentTestMethod(AnActionEvent e) {
PhpFile file = getPhpFile(e);
Editor editor = getEditor(e);
if (file == null || editor == null) {
return null;
}
Method method = PsiTreeUtil.findElementOfClassAtOffset(file, editor.getCaretModel().getOffset(), Method.class, false);
if (method != null && method.getName().startsWith("test")) {
return method;
}
return null;
}
/** The text range of our annotation should be based on the element at that offset. */
@Nullable
private TextRange getTextRange(@NotNull PsiFile psiFile) {
final String text = psiFile.getText();
final int offsetStart = getOffsetStart(text);
if (offsetStart == -1) return null;
PsiElement el = PsiTreeUtil.findElementOfClassAtOffset(psiFile, offsetStart, PsiElement.class, false);
if (el == null) return null;
// It's prettier to show the entire import line as unused instead of just the `import` keyword.
if (isUnusedImport && el.getParent() instanceof HaskellImpdecl) return el.getParent().getTextRange();
return el.getTextRange();
}