use of com.intellij.openapi.util.Computable in project android by JetBrains.
the class AndroidGotoRelatedProvider method collectRelatedJavaFiles.
@Nullable
private static Computable<List<GotoRelatedItem>> collectRelatedJavaFiles(@NotNull final XmlFile file, @NotNull final AndroidFacet facet) {
final String resType = ResourceType.LAYOUT.getName();
final String resourceName = AndroidCommonUtils.getResourceName(resType, file.getName());
final PsiField[] fields = AndroidResourceUtil.findResourceFields(facet, resType, resourceName, true);
if (fields.length == 0 || fields.length > 1) {
return null;
}
final PsiField field = fields[0];
final Module module = facet.getModule();
final GlobalSearchScope scope = module.getModuleScope(false);
return new Computable<List<GotoRelatedItem>>() {
@Override
public List<GotoRelatedItem> compute() {
final JavaPsiFacade facade = JavaPsiFacade.getInstance(module.getProject());
final List<PsiClass> psiContextClasses = new ArrayList<PsiClass>();
// Explicitly chosen in the layout/menu file with a tools:context attribute?
PsiClass declared = AndroidPsiUtils.getContextClass(module, file);
if (declared != null) {
return Collections.singletonList(new GotoRelatedItem(declared, "JAVA"));
}
for (String contextClassName : CONTEXT_CLASSES) {
final PsiClass contextClass = facade.findClass(contextClassName, module.getModuleWithDependenciesAndLibrariesScope(false));
if (contextClass != null) {
psiContextClasses.add(contextClass);
}
}
if (psiContextClasses.isEmpty()) {
return Collections.emptyList();
}
final List<GotoRelatedItem> result = new ArrayList<GotoRelatedItem>();
ReferencesSearch.search(field, scope).forEach(new Processor<PsiReference>() {
@Override
public boolean process(PsiReference reference) {
PsiElement element = reference.getElement();
if (!(element instanceof PsiReferenceExpression)) {
return true;
}
element = element.getParent();
if (!(element instanceof PsiExpressionList)) {
return true;
}
element = element.getParent();
if (!(element instanceof PsiMethodCallExpression)) {
return true;
}
final String methodName = ((PsiMethodCallExpression) element).getMethodExpression().getReferenceName();
if ("setContentView".equals(methodName) || "inflate".equals(methodName)) {
final PsiClass relatedClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
if (relatedClass != null && isInheritorOfOne(relatedClass, psiContextClasses)) {
result.add(new GotoRelatedItem(relatedClass, "JAVA"));
}
}
return true;
}
});
return result;
}
};
}
use of com.intellij.openapi.util.Computable in project android by JetBrains.
the class CreateFileResourceQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final VirtualFile resourceDir = myFacet.getPrimaryResourceDir();
if (resourceDir == null) {
return;
}
final PsiDirectory psiResDir = PsiManager.getInstance(project).findDirectory(resourceDir);
if (psiResDir == null) {
return;
}
final String resDirName = myResourceType.getName();
PsiDirectory resSubdir = psiResDir.findSubdirectory(resDirName);
if (resSubdir == null) {
resSubdir = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
return psiResDir.createSubdirectory(resDirName);
}
});
}
try {
AndroidResourceUtil.createFileResource(myResourceName, resSubdir, CreateTypedResourceFileAction.getDefaultRootTagByResourceType(myResourceType), resDirName, false);
UndoUtil.markPsiFileForUndo(myFile);
} catch (Exception e) {
LOG.error(e);
}
}
use of com.intellij.openapi.util.Computable in project intellij-plugins by JetBrains.
the class FlashUmlTest method doTestImpl.
private DiagramBuilder doTestImpl(@Nullable File projectRoot, String[] files, String[] additionalClasses, Computable<GlobalSearchScope> scopeProvider, @Nullable EnumSet<FlashUmlDependenciesSettingsOption> dependencies, @Nullable String expectedFileNamePrefix) throws Exception {
List<VirtualFile> vFiles = new ArrayList<>(files.length);
for (String file : files) {
vFiles.add(getVirtualFile(BASE_PATH + file));
}
ApplicationManager.getApplication().runWriteAction(() -> {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
ContentEntry[] contentEntries = rootModel.getContentEntries();
for (ContentEntry contentEntry : contentEntries) {
rootModel.removeContentEntry(contentEntry);
}
rootModel.commit();
});
configureByFiles(projectRoot, VfsUtilCore.toVirtualFileArray(vFiles));
final LinkedHashMap<Integer, String> markers = JSTestUtils.extractPositionMarkers(getProject(), getEditor().getDocument());
assertFalse(markers.isEmpty());
DiagramBuilder builder = null;
int i = 1;
for (Map.Entry<Integer, String> marker : markers.entrySet()) {
getEditor().getCaretModel().moveToOffset(marker.getKey());
i++;
String expectedPrefix = StringUtil.isNotEmpty(marker.getValue()) ? marker.getValue() : expectedFileNamePrefix;
final DataContext dataContext = DataManager.getInstance().getDataContext();
final DiagramProvider[] providers = DiagramProvider.findProviders(dataContext, "unknown");
final FlashUmlProvider provider = ContainerUtil.findInstance(providers, FlashUmlProvider.class);
assertNotNull("Flash UML provider not found", provider);
final String actualOriginFqn = provider.getVfsResolver().getQualifiedName(provider.getElementManager().findInDataContext(dataContext));
final Object actualOrigin = provider.getVfsResolver().resolveElementByFQN(actualOriginFqn, getProject());
builder = UmlGraphBuilderFactory.create(myProject, provider, actualOrigin, null);
Disposer.register(getTestRootDisposable(), builder);
final DiagramDataModel<Object> model = builder.getDataModel();
DiagramConfiguration configuration = DiagramConfiguration.getConfiguration();
String originalCategories = configuration.categories.get(provider.getID());
if (dependencies != null) {
model.setShowDependencies(true);
EnumSet<FlashUmlDependenciesSettingsOption> disabledOptions = EnumSet.complementOf(dependencies);
configuration.categories.put(provider.getID(), StringUtil.join(disabledOptions, option -> option.getDisplayName(), ";"));
} else {
model.setShowDependencies(false);
}
try {
model.refreshDataModel();
// first limit elements by scope
Collection<DiagramNode<Object>> nodesToRemove = new ArrayList<>();
for (DiagramNode<Object> node : model.getNodes()) {
if (node.getIdentifyingElement() instanceof JSClass && !scopeProvider.compute().contains(((JSClass) node.getIdentifyingElement()).getContainingFile().getVirtualFile())) {
nodesToRemove.add(node);
}
}
for (DiagramNode<Object> node : nodesToRemove) {
model.removeNode(node);
}
builder.updateGraph();
// then add explicitly required classes
for (String aClass : additionalClasses) {
JSClass c = JSTestUtils.findClassByQName(aClass, GlobalSearchScope.allScope(myProject));
final DiagramNode node = model.addElement(c);
if (node != null) {
builder.createDraggedNode(node, node.getTooltip(), DiagramUtils.getBestPositionForNode(builder));
builder.updateGraph();
}
}
assertModel(expectedPrefix, provider, actualOriginFqn, model);
} finally {
configuration.categories.put(provider.getID(), originalCategories);
}
}
assert builder != null;
return builder;
}
use of com.intellij.openapi.util.Computable in project intellij-plugins by StepicOrg.
the class ProgrammingLanguageUtils method getOrCreateMainFile.
@Nullable
private static PsiFile getOrCreateMainFile(@NotNull Project project, @NotNull VirtualFile parent, @NotNull SupportedLanguages language, @NotNull StepNode stepNode) {
String fileName = language.getMainFileName();
final VirtualFile[] file = { parent.findChild(fileName) };
Application application = ApplicationManager.getApplication();
if (file[0] == null) {
application.invokeAndWait(() -> application.runWriteAction(() -> {
try {
file[0] = parent.createChildData(null, fileName);
String template = null;
StepikApiClient stepikApiClient = authAndGetStepikApiClient();
User user = getCurrentUser();
if (!user.isGuest()) {
try {
Submissions submissions = stepikApiClient.submissions().get().user(user.getId()).order(Order.DESC).step(stepNode.getId()).execute();
if (!submissions.isEmpty()) {
Optional<Submission> lastSubmission = submissions.getItems().stream().filter(submission -> SupportedLanguages.langOfName(submission.getReply().getLanguage()) == language).limit(1).findFirst();
if (lastSubmission.isPresent()) {
template = lastSubmission.get().getReply().getCode();
}
}
} catch (StepikClientException e) {
logger.warn(e);
}
}
if (template == null) {
template = stepNode.getTemplate(language);
}
file[0].setBinaryContent(template.getBytes());
} catch (IOException e) {
file[0] = null;
}
}));
}
return application.runReadAction((Computable<PsiFile>) () -> PsiManager.getInstance(project).findFile(file[0]));
}
Aggregations