private void renewFile() { if (myFile == null || !myFile.isValid()) { myFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myDocument); myCompiled = myFile instanceof PsiCompiledFile; if (myCompiled) { myFile = ((PsiCompiledFile)myFile).getDecompiledPsiFile(); } if (myFile != null && !myFile.isValid()) { myFile = null; } } if (myFile != null) { myFile.putUserData(PsiFileEx.BATCH_REFERENCE_PROCESSING, Boolean.TRUE); } }
@Nonnull List<TextEditorHighlightingPass> getPasses(@Nonnull int[] passesToIgnore) { if (myProject.isDisposed()) return Collections.emptyList(); Document[] uncommitted = PsiDocumentManager.getInstance(myProject).getUncommittedDocuments(); LOG.assertTrue(uncommitted.length == 0, "Uncommitted documents: " + Arrays.asList(uncommitted)); renewFile(); PsiFile file = myFile; if (file == null) return Collections.emptyList(); boolean compiled = file instanceof PsiCompiledFile; if (compiled) { file = ((PsiCompiledFile)file).getDecompiledPsiFile(); } if (compiled) { passesToIgnore = EXCEPT_OVERRIDDEN; } else if (!DaemonCodeAnalyzer.getInstance(myProject).isHighlightingAvailable(file)) { return Collections.emptyList(); } TextEditorHighlightingPassRegistrarEx passRegistrar = TextEditorHighlightingPassRegistrarEx.getInstanceEx(myProject); return passRegistrar.instantiatePasses(file, myEditor, passesToIgnore); }
@Override @Nullable public StructureViewBuilder getStructureViewBuilder(@NotNull FileType fileType, @NotNull VirtualFile file, @NotNull Project project) { if(fileType == JavaClassFileType.INSTANCE) { PsiFile psiFile = PsiManager.getInstance(project).findFile(file); if(psiFile instanceof PsiCompiledFile) { psiFile = ((PsiCompiledFile) psiFile).getDecompiledPsiFile(); } if(psiFile != null) { PsiStructureViewFactory factory = LanguageStructureViewBuilder.INSTANCE.forLanguage(psiFile.getLanguage()); if(factory != null) { return factory.getStructureViewBuilder(psiFile); } } } return null; }
@Nullable public static <T extends PsiElement> T getOriginalElement(@NotNull T psi, PsiFile containingFile) { if (containingFile == null) return psi; PsiFile originalFile = containingFile.getOriginalFile(); if (originalFile != containingFile && !(originalFile instanceof PsiCompiledFile) && psi.getTextRange() != null) { TextRange range = psi.getTextRange(); Integer start = range.getStartOffset(); Integer end = range.getEndOffset(); Document document = containingFile.getViewProvider().getDocument(); if (document != null) { Document hostDocument = document instanceof DocumentWindow ? ((DocumentWindow)document).getDelegate() : document; OffsetTranslator translator = hostDocument.getUserData(OffsetTranslator.RANGE_TRANSLATION); if (translator != null) { if (document instanceof DocumentWindow) { TextRange translated = ((DocumentWindow)document).injectedToHost(new TextRange(start, end)); start = translated.getStartOffset(); end = translated.getEndOffset(); } start = translator.translateOffset(start); end = translator.translateOffset(end); if (start == null || end == null) { return null; } if (document instanceof DocumentWindow) { start = ((DocumentWindow)document).hostToInjected(start); end = ((DocumentWindow)document).hostToInjected(end); } } } //noinspection unchecked return (T)PsiTreeUtil.findElementOfClassAtRange(originalFile, start, end, psi.getClass()); } return psi; }
private boolean isEnabled(@NotNull CodeStyleSettings settings, @NotNull PsiFile file) { if (file instanceof PsiCompiledFile) return false; if (ApplicationManager.getApplication().isUnitTestMode()) { return myIsEnabledInTest; } VirtualFile vFile = file.getVirtualFile(); if (vFile == null || vFile instanceof LightVirtualFile || myDisabledFiles.contains(vFile)) return false; return LanguageFormatting.INSTANCE.forContext(file) != null && settings.AUTODETECT_INDENTS; }
private boolean isEnabled(@Nonnull CodeStyleSettings settings, @Nonnull PsiFile file) { if (file instanceof PsiCompiledFile) return false; if (ApplicationManager.getApplication().isUnitTestMode()) { return myIsEnabledInTest; } VirtualFile vFile = file.getVirtualFile(); if (vFile == null || vFile instanceof LightVirtualFile || myDisabledFiles.contains(vFile)) return false; return LanguageFormatting.INSTANCE.forContext(file) != null && settings.AUTODETECT_INDENTS; }
private static void highlightPsiElement(@NotNull Project project, @NotNull PsiElement psiElement, @NotNull Editor editor, @NotNull PsiFile file, boolean shouldClear) { final PsiElement target = SmartPointerManager.getInstance(psiElement.getProject()) .createSmartPsiElementPointer(psiElement) .getElement(); if (target == null) { return; } if (file instanceof PsiCompiledFile) { file = ((PsiCompiledFile) file).getDecompiledPsiFile(); } final Couple<List<TextRange>> usages = getUsages(target, file); final List<TextRange> readRanges = usages.first; final List<TextRange> writeRanges = usages.second; final HighlightManager highlightManager = HighlightManager.getInstance(project); if (shouldClear) { clearHighlights(editor, highlightManager, readRanges); clearHighlights(editor, highlightManager, writeRanges); WindowManager.getInstance().getStatusBar(project).setInfo(""); return; } // TODO: 10/02/2017 pass target? final TextAttributes ta = TextAttributesFactory.getInstance().get(); final Color scrollMarkColor; if (ta.getErrorStripeColor() != null) { scrollMarkColor = ta.getErrorStripeColor(); } else if (ta.getBackgroundColor() != null) { scrollMarkColor = ta.getBackgroundColor().darker(); } else { scrollMarkColor = null; } final String elementName = ElementDescriptionUtil.getElementDescription(target, HighlightUsagesDescriptionLocation.INSTANCE); // TODO: 06/02/2017 highlight write and read access ArrayList<RangeHighlighter> highlighters = new ArrayList<>(); highlight(highlightManager, readRanges, editor, ta, highlighters, scrollMarkColor); highlight(highlightManager, writeRanges, editor, ta, highlighters, scrollMarkColor); final Document doc = editor.getDocument(); for (RangeHighlighter highlighter : highlighters) { highlighter.setErrorStripeTooltip( HighlightHandlerBase.getLineTextErrorStripeTooltip(doc, highlighter.getStartOffset(), true)); } int refCount = readRanges.size() + writeRanges.size(); String msg; if (refCount > 0) { msg = MessageFormat.format("{0} {0, choice, 1#usage|2#usages} of {1} found", refCount, elementName); } else { msg = MessageFormat.format("No usages of {0} found", elementName); } WindowManager.getInstance().getStatusBar(project).setInfo(msg); }