use of com.intellij.openapi.util.Condition in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoIdFilter method createIdFilter.
private static IdFilter createIdFilter(@NotNull Project project, @NotNull Key<CachedValue<IdFilter>> cacheKey, @NotNull Condition<VirtualFile> filterCondition) {
return CachedValuesManager.getManager(project).getCachedValue(project, cacheKey, () -> {
BitSet bitSet = new BitSet();
ContentIterator iterator = fileOrDir -> {
if (filterCondition.value(fileOrDir)) {
addToBitSet(bitSet, fileOrDir);
}
ProgressManager.checkCanceled();
return true;
};
FileBasedIndex.getInstance().iterateIndexableFiles(iterator, project, null);
return CachedValueProvider.Result.create(new GoIdFilter(bitSet), ProjectRootManager.getInstance(project), VirtualFileManager.VFS_STRUCTURE_MODIFICATIONS);
}, false);
}
use of com.intellij.openapi.util.Condition in project android by JetBrains.
the class ResourceFolderRepository method equalFilesItems.
// Not a super-robust comparator. Compares a subset of the repo state for testing.
// Returns false if a difference is found.
// Returns true if the repositories are roughly equivalent.
@VisibleForTesting
boolean equalFilesItems(ResourceFolderRepository other) {
File myResourceDirFile = VfsUtilCore.virtualToIoFile(myResourceDir);
File otherResourceDir = VfsUtilCore.virtualToIoFile(other.myResourceDir);
if (!FileUtil.filesEqual(myResourceDirFile, otherResourceDir)) {
return false;
}
if (myResourceFiles.size() != other.myResourceFiles.size()) {
return false;
}
for (Map.Entry<VirtualFile, ResourceFile> fileEntry : myResourceFiles.entrySet()) {
ResourceFile otherResFile = other.myResourceFiles.get(fileEntry.getKey());
if (otherResFile == null) {
return false;
}
if (!FileUtil.filesEqual(fileEntry.getValue().getFile(), otherResFile.getFile())) {
return false;
}
}
if (myItems.size() != other.myItems.size()) {
return false;
}
for (Map.Entry<ResourceType, ListMultimap<String, ResourceItem>> entry : myItems.entrySet()) {
ListMultimap<String, ResourceItem> ownEntries = entry.getValue();
ListMultimap<String, ResourceItem> otherEntries = other.myItems.get(entry.getKey());
if (otherEntries == null || otherEntries.size() != ownEntries.size()) {
return false;
}
for (Map.Entry<String, ResourceItem> itemEntry : ownEntries.entries()) {
List<ResourceItem> otherItemsList = otherEntries.get(itemEntry.getKey());
if (otherItemsList == null) {
return false;
}
final ResourceItem item = itemEntry.getValue();
if (!ContainerUtil.exists(otherItemsList, new Condition<ResourceItem>() {
@Override
public boolean value(ResourceItem resourceItem) {
// Use #compareTo instead of #equals because #equals compares pointers of mSource.
if (resourceItem.compareTo(item) != 0) {
return false;
}
// Skip ID type resources, where the ResourceValues are not important and where blob writing doesn't preserve the value.
if (item.getType() != ResourceType.ID) {
ResourceValue resValue = item.getResourceValue(false);
ResourceValue otherResValue = resourceItem.getResourceValue(false);
if (resValue == null || otherResValue == null) {
if (resValue != otherResValue) {
return false;
}
} else {
String resValueStr = resValue.getValue();
String otherResValueStr = otherResValue.getValue();
if (resValueStr == null || otherResValueStr == null) {
if (resValueStr != otherResValueStr) {
return false;
}
} else {
if (!resValueStr.equals(otherResValueStr)) {
return false;
}
}
}
}
// We can only compareValueWith (compare equivalence of XML nodes) for VALUE items.
// For others, the XML node may be different before and after serialization.
ResourceFile source = item.getSource();
ResourceFile otherSource = resourceItem.getSource();
if (source != null && otherSource != null) {
ResourceFolderType ownFolderType = ResourceHelper.getFolderType(source);
ResourceFolderType otherFolderType = ResourceHelper.getFolderType(otherSource);
if (otherFolderType != ownFolderType) {
return false;
}
if (otherFolderType == VALUES) {
return resourceItem.compareValueWith(item);
}
}
return true;
}
})) {
return false;
}
}
}
// Only compare the keys.
return myDataBindingResourceFiles.keySet().equals(other.myDataBindingResourceFiles.keySet());
}
use of com.intellij.openapi.util.Condition in project android by JetBrains.
the class ChooseResourceDialog method updateFilter.
private void updateFilter() {
ResourcePanel panel = getSelectedPanel();
final String text = mySearchField.getText();
if (text.isEmpty()) {
if (!panel.isFiltered()) {
return;
}
panel.setFilter(null);
return;
}
if (panel.getType() == ResourceType.COLOR) {
Condition<ResourceChooserItem> colorCondition = null;
if (text.startsWith("#")) {
final Color color = ResourceHelper.parseColor(text);
if (color != null) {
colorCondition = item -> {
assert item.getType() == ResourceType.COLOR;
return ResourceHelper.resolveMultipleColors(getResourceResolver(), item.getResourceValue(), myModule.getProject()).contains(color);
};
}
}
if (colorCondition != null) {
panel.setFilter(colorCondition);
return;
}
}
Condition<ResourceChooserItem> condition = item -> {
if (item.getType() == ResourceType.STRING) {
String string = ResourceHelper.resolveStringValue(getResourceResolver(), item.getResourceUrl());
if (StringUtil.containsIgnoreCase(string, text)) {
return true;
}
}
return StringUtil.containsIgnoreCase(item.getName(), text);
};
panel.setFilter(condition);
}
use of com.intellij.openapi.util.Condition in project android by JetBrains.
the class ModuleResourceRepositoryTest method testOverlayUpdates2.
public void testOverlayUpdates2() {
// Like testOverlayUpdates1, but rather than testing changes to layout resources (file-based resource)
// perform document edits in value-documents
myFixture.copyFileToProject(LAYOUT, "res/layout/layout1.xml");
myFixture.copyFileToProject(LAYOUT_OVERLAY, "res2/layout/layout1.xml");
VirtualFile values1 = myFixture.copyFileToProject(VALUES, "res/values/values.xml");
VirtualFile values2 = myFixture.copyFileToProject(VALUES_OVERLAY1, "res2/values/values.xml");
VirtualFile values3 = myFixture.copyFileToProject(VALUES_OVERLAY2, "res3/values/nameDoesNotMatter.xml");
final VirtualFile values3No = myFixture.copyFileToProject(VALUES_OVERLAY2_NO, "res3/values-no/values.xml");
VirtualFile res1 = values1.getParent().getParent();
VirtualFile res2 = values2.getParent().getParent();
VirtualFile res3 = values3.getParent().getParent();
ModuleResourceRepository resources = ModuleResourceRepository.createForTest(myFacet, Arrays.asList(res1, res2, res3));
PsiFile psiValues1 = PsiManager.getInstance(getProject()).findFile(values1);
assertNotNull(psiValues1);
PsiFile psiValues2 = PsiManager.getInstance(getProject()).findFile(values2);
assertNotNull(psiValues2);
PsiFile psiValues3 = PsiManager.getInstance(getProject()).findFile(values3);
assertNotNull(psiValues3);
PsiFile psiValues3No = PsiManager.getInstance(getProject()).findFile(values3No);
assertNotNull(psiValues3No);
// Initial state; sanity check from #testOverlays()
assertStringIs(resources, "title_layout_changes", "Layout Changes");
// Overridden in res2
assertStringIs(resources, "title_crossfade", "Complex Crossfade");
// Overridden in res3
assertStringIs(resources, "title_zoom", "Zoom!");
// Overridden in res2
assertStringIs(resources, "unique_string", "Unique");
// Overridden in res3
assertStringIs(resources, "another_unique_string", "Another Unique", false);
// res3 (not unique because we have a values-no item too)
assertStringIs(resources, "app_name", "Very Different App Name", false);
// Value resource check:
// Verify that an edit in a value file, both in a non-overridden and an overridden
// value, is observed; and that an override in an overridden value is not observed.
assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
assertTrue(resources.hasResourceItem(ResourceType.STRING, "title_layout_changes"));
ResourceItem appName = getFirstItem(resources, ResourceType.STRING, "app_name");
assertItemIsInDir(res3, appName);
// res3 (not unique because we have a values-no item too)
assertStringIs(resources, "app_name", "Very Different App Name", false);
long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiValues3);
assertNotNull(document);
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("Very Different App Name");
document.insertString(offset, "Not ");
documentManager.commitDocument(document);
}
});
// The first edit to psiValues3 causes ResourceFolderRepository to transition from non-Psi -> Psi which requires a rescan.
assertTrue(resources.isScanPending(psiValues3));
UIUtil.dispatchAllInvocationEvents();
assertTrue(resources.getModificationCount() > generation);
// Should still be defined in res3 but have new value.
// The order of items may have swapped if a full rescan is done.
List<ResourceItem> list = resources.getResourceItem(ResourceType.STRING, "app_name");
assertNotNull(list);
assertSize(2, list);
appName = ContainerUtil.find(list, new Condition<ResourceItem>() {
@Override
public boolean value(ResourceItem resourceItem) {
return resourceItem.getQualifiers().isEmpty();
}
});
assertNotNull(appName);
assertItemIsInDir(res3, appName);
ResourceValue appNameResourceValue = appName.getResourceValue(false);
assertNotNull(appNameResourceValue);
assertEquals("Not Very Different App Name", appNameResourceValue.getValue());
// Try renaming the item name.
generation = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("app_name");
document.insertString(offset, "r");
documentManager.commitDocument(document);
}
});
assertTrue(resources.getModificationCount() > generation);
assertTrue(resources.hasResourceItem(ResourceType.STRING, "rapp_name"));
appName = getFirstItem(resources, ResourceType.STRING, "app_name");
// The item is still under res3, but now it's in the Norwegian translation
assertEquals("no", appName.getSource().getQualifiers());
assertStringIs(resources, "app_name", "Forskjellig Navn", false);
// Delete that file:
generation = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
try {
values3No.delete(this);
} catch (IOException e) {
fail(e.toString());
}
}
});
assertTrue(resources.getModificationCount() > generation);
// Now the item is no longer available in res3; should fallback to res 2
appName = getFirstItem(resources, ResourceType.STRING, "app_name");
assertItemIsInDir(res2, appName);
assertStringIs(resources, "app_name", "Different App Name", false);
// Check that editing an overridden attribute does not count as a change
final Document document2 = documentManager.getDocument(psiValues1);
assertNotNull(document2);
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document2.getText().indexOf("Animations Demo");
document2.insertString(offset, "Cool ");
documentManager.commitDocument(document2);
}
});
// The first edit to psiValues1 causes ResourceFolderRepository to transition from non-Psi -> Psi which requires a rescan.
assertTrue(resources.isScanPending(psiValues1));
UIUtil.dispatchAllInvocationEvents();
// Unaffected by above change
assertStringIs(resources, "app_name", "Different App Name", false);
// Finally check that editing an non-overridden attribute also gets picked up as a change
generation = resources.getModificationCount();
// Observe after the rescan, so that an edit causes a generation bump.
assertStringIs(resources, "title_layout_changes", "Layout Changes");
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document2.getText().indexOf("Layout Changes");
document2.insertString(offset, "New ");
documentManager.commitDocument(document2);
}
});
assertTrue(resources.getModificationCount() > generation);
assertStringIs(resources, "title_layout_changes", "New Layout Changes", false);
}
use of com.intellij.openapi.util.Condition in project intellij-plugins by JetBrains.
the class DartAnalysisServerService method doConfigureImportedLibraries.
private static void doConfigureImportedLibraries(@NotNull final Project project, @NotNull final Collection<String> filePaths) {
final DartSdk sdk = DartSdk.getDartSdk(project);
if (sdk == null)
return;
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final SortedSet<String> folderPaths = new TreeSet<>();
final Collection<String> rootsToAddToLib = new THashSet<>();
for (final String path : filePaths) {
if (path != null) {
folderPaths.add(PathUtil.getParentPath(FileUtil.toSystemIndependentName(path)));
}
}
outer: for (final String path : folderPaths) {
final VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(path);
if (!path.startsWith(sdk.getHomePath() + "/") && (vFile == null || !fileIndex.isInContent(vFile))) {
for (String configuredPath : rootsToAddToLib) {
if (path.startsWith(configuredPath + "/")) {
// folderPaths is sorted so subfolders go after parent folder
continue outer;
}
}
rootsToAddToLib.add(path);
}
}
final Processor<? super PsiFileSystemItem> falseProcessor = (Processor<PsiFileSystemItem>) item -> false;
final Condition<Module> moduleFilter = module -> DartSdkLibUtil.isDartSdkEnabled(module) && !FilenameIndex.processFilesByName(PubspecYamlUtil.PUBSPEC_YAML, false, falseProcessor, module.getModuleContentScope(), project, null);
final DartFileListener.DartLibInfo libInfo = new DartFileListener.DartLibInfo(true);
libInfo.addRoots(rootsToAddToLib);
final Library library = DartFileListener.updatePackagesLibraryRoots(project, libInfo);
DartFileListener.updateDependenciesOnDartPackagesLibrary(project, moduleFilter, library);
}
Aggregations