@NotNull @Override public StubTree getStubTree() { ApplicationManager.getApplication().assertReadAccessAllowed(); StubTree stubTree = dereference(stub); if (stubTree != null) return stubTree; // build newStub out of lock to avoid deadlock StubTree newStubTree = (StubTree) StubTreeLoader .getInstance() .readOrBuild(getProject(), getVirtualFile(), this); if (newStubTree == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("No stub for BEAM file in index: " + getVirtualFile().getPresentableUrl()); } newStubTree = new StubTree(new ElixirFileStubImpl()); } synchronized (stubLock) { stubTree = dereference(stub); if (stubTree != null) return stubTree; stubTree = newStubTree; @SuppressWarnings("unchecked") PsiFileStubImpl<PsiFile> fileStub = (PsiFileStubImpl) stubTree.getRoot(); fileStub.setPsi(this); stub = new SoftReference<StubTree>(stubTree); } return stubTree; }
@Nullable public static AnAction createInstallAction(@NotNull Sdk sdk, @NotNull Project project, @NotNull Collection<String> libraryNames, @Nullable Runnable actionCallback) { ApplicationManager.getApplication().assertReadAccessAllowed(); return !isAvailable(sdk) ? null : new DumbAwareAction(PerlBundle.message("perl.quickfix.install.family", SCRIPT_NAME)) { @Override public void actionPerformed(@NotNull AnActionEvent e) { new CpanminusAdapter(sdk, project).install(libraryNames); if (actionCallback != null) { actionCallback.run(); } } }; }
@CheckForNull public Module findModuleForFile(VirtualFile file, Project project) { ApplicationManager.getApplication().assertReadAccessAllowed(); return ProjectFileIndex.SERVICE.getInstance(project).getModuleForFile(file, false); }
/** * @return true iff App::cpanminus is available in sdk classpath */ @Contract("null->false") public static boolean isAvailable(@Nullable Sdk sdk) { ApplicationManager.getApplication().assertReadAccessAllowed(); return PerlRunUtil.findScript(sdk, SCRIPT_NAME) != null; }
/** * This method should be called under a read action. */ @Nullable public static <T extends DomElement> T loadDomElementWithReadPermission(@NotNull Project project, @NotNull XmlFile xmlFile, @NotNull Class<T> aClass) { ApplicationManager.getApplication().assertReadAccessAllowed(); DomManager domManager = DomManager.getDomManager(project); DomFileElement<T> element = domManager.getFileElement(xmlFile, aClass); if (element == null) return null; return element.getRootElement(); } }
/** * Attempts to find a script by name in perl's libraries path * * @param sdk perl sdk to search in * @param scriptName script name to find * @return script's virtual file if available * @apiNote returns virtual file of local file, not remote **/ @Contract("null,_->null; _,null->null") @Nullable public static VirtualFile findScript(@Nullable Sdk sdk, @Nullable String scriptName) { if (sdk == null || StringUtil.isEmpty(scriptName)) { return null; } ApplicationManager.getApplication().assertReadAccessAllowed(); PerlOsHandler osHandler = PerlOsHandler.notNullFrom(sdk); return getBinDirectories(sdk) .map(root -> { VirtualFile scriptFile = null; if (osHandler.isMsWindows()) { scriptFile = root.findChild(scriptName + ".bat"); } return scriptFile != null ? scriptFile : root.findChild(scriptName); }) .filter(Objects::nonNull) .findFirst().orElse(null); }
/** * @return list of perl bin directories where script from library may be located **/ @NotNull public static Stream<VirtualFile> getBinDirectories(@Nullable Sdk sdk) { if (sdk == null) { return Stream.empty(); } ApplicationManager.getApplication().assertReadAccessAllowed(); SdkTypeId sdkType = sdk.getSdkType(); if (!(sdkType instanceof PerlSdkType)) { throw new IllegalArgumentException("Got non-perl sdk: " + sdk); } List<VirtualFile> files = new ArrayList<>(ContainerUtil.map(sdk.getRootProvider().getFiles(OrderRootType.CLASSES), PerlRunUtil::findLibsBin)); PerlHostData hostData = PerlHostData.notNullFrom(sdk); File localSdkBinDir = hostData.getLocalPath(new File( StringUtil.notNullize(PerlProjectManager.getInterpreterPath(sdk))).getParentFile()); if (localSdkBinDir != null) { files.add(VfsUtil.findFileByIoFile(localSdkBinDir, false)); } PerlVersionManagerData.notNullFrom(sdk).getBinDirsPath().forEach( it -> ObjectUtils.doIfNotNull(hostData.getLocalPath(it), localPath -> files.add(VfsUtil.findFileByIoFile(localPath, false)))); return files.stream().filter(Objects::nonNull).distinct(); }
/** * Tries to match an SQ issue to an IntelliJ file. * <b>Can only be called with getLive access</b>. */ public RangeMarker match(PsiFile file, IssueLocation issueLocation) throws NoMatchException { ApplicationManager.getApplication().assertReadAccessAllowed(); Preconditions.checkArgument(issueLocation.getStartLine() != null); Document doc = docManager.getDocument(file); if (doc == null) { throw new NoMatchException("No document found for file: " + file.getName()); } TextRange range = getIssueTextRange(file, doc, issueLocation); return doc.createRangeMarker(range.getStartOffset(), range.getEndOffset()); }
@Override public void actionPerformed(AnActionEvent e) { Project project = e.getProject(); ApplicationManager.getApplication().assertReadAccessAllowed(); if (project != null) { IssueManager issueManager = SonarLintUtils.get(project, IssueManager.class); DaemonCodeAnalyzer codeAnalyzer = SonarLintUtils.get(project, DaemonCodeAnalyzer.class); ReadAction.run(() -> { issueManager.clear(); // run annotator to remove highlighting of issues FileEditorManager editorManager = FileEditorManager.getInstance(project); VirtualFile[] openFiles = editorManager.getOpenFiles(); Collection<PsiFile> psiFiles = findFiles(project, openFiles); psiFiles.forEach(codeAnalyzer::restart); }); } }