use of com.intellij.openapi.util.Condition in project intellij-community by JetBrains.
the class CreateParameterForFieldIntention method findCandidates.
private static List<GrField> findCandidates(PsiMethod constructor, final GrTypeDefinition clazz) {
final List<GrField> usedFields = new ArrayList<>();
final GrOpenBlock block = constructor instanceof GrMethod ? ((GrMethod) constructor).getBlock() : null;
if (block == null) {
return usedFields;
}
final PsiManager manager = clazz.getManager();
block.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
final PsiElement resolved = referenceExpression.resolve();
if (resolved instanceof GrField && manager.areElementsEquivalent(((GrField) resolved).getContainingClass(), clazz) && PsiUtil.isAccessedForWriting(referenceExpression)) {
usedFields.add((GrField) resolved);
}
}
@Override
public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
}
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
}
});
List<GrField> fields = new ArrayList<>();
for (final GrField field : clazz.getFields()) {
if (field.getInitializerGroovy() != null)
continue;
if (ContainerUtil.find(usedFields, new Condition<PsiField>() {
@Override
public boolean value(PsiField o) {
return manager.areElementsEquivalent(o, field);
}
}) == null) {
fields.add(field);
}
}
return fields;
}
use of com.intellij.openapi.util.Condition in project intellij-community by JetBrains.
the class JavaFxFileReferenceProvider method getReferences.
public static PsiReference[] getReferences(@NotNull PsiElement element, String value, final String acceptedExtension) {
final PsiDirectory directory = element.getContainingFile().getOriginalFile().getParent();
if (directory == null)
return PsiReference.EMPTY_ARRAY;
final boolean startsWithSlash = value.startsWith("/");
final VirtualFileSystem fs = directory.getVirtualFile().getFileSystem();
final FileReferenceSet fileReferenceSet = new FileReferenceSet(value, element, 1, null, fs.isCaseSensitive()) {
@NotNull
@Override
public Collection<PsiFileSystemItem> getDefaultContexts() {
if (startsWithSlash || !directory.isValid()) {
return super.getDefaultContexts();
}
return Collections.singletonList(directory);
}
@Override
protected Condition<PsiFileSystemItem> getReferenceCompletionFilter() {
return item -> {
if (item instanceof PsiDirectory)
return true;
final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(item);
return virtualFile != null && acceptedExtension.equals(virtualFile.getExtension());
};
}
};
if (startsWithSlash) {
fileReferenceSet.addCustomization(FileReferenceSet.DEFAULT_PATH_EVALUATOR_OPTION, FileReferenceSet.ABSOLUTE_TOP_LEVEL);
}
return fileReferenceSet.getAllReferences();
}
use of com.intellij.openapi.util.Condition in project intellij-community by JetBrains.
the class GitCheckinEnvironment method commitWithCaseOnlyRename.
@NotNull
private List<VcsException> commitWithCaseOnlyRename(@NotNull Project project, @NotNull VirtualFile root, @NotNull Set<Change> caseOnlyRenames, @NotNull Set<FilePath> added, @NotNull Set<FilePath> removed, @NotNull File messageFile, @Nullable String author) {
String rootPath = root.getPath();
LOG.info("Committing case only rename: " + getLogString(rootPath, caseOnlyRenames) + " in " + getShortRepositoryName(project, root));
// 1. Check what is staged besides case-only renames
Collection<Change> stagedChanges;
try {
stagedChanges = GitChangeUtils.getStagedChanges(project, root);
LOG.debug("Found staged changes: " + getLogString(rootPath, stagedChanges));
} catch (VcsException e) {
return Collections.singletonList(e);
}
// 2. Reset staged changes which are not selected for commit
Collection<Change> excludedStagedChanges = filter(stagedChanges, change -> !caseOnlyRenames.contains(change) && !added.contains(getAfterPath(change)) && !removed.contains(getBeforePath(change)));
if (!excludedStagedChanges.isEmpty()) {
LOG.info("Staged changes excluded for commit: " + getLogString(rootPath, excludedStagedChanges));
try {
reset(project, root, excludedStagedChanges);
} catch (VcsException e) {
return Collections.singletonList(e);
}
}
List<VcsException> exceptions = new ArrayList<>();
try {
// 3. Stage what else is needed to commit
List<FilePath> newPathsOfCaseRenames = map(caseOnlyRenames, ChangesUtil::getAfterPath);
LOG.debug("Updating index for added:" + added + "\n, removed: " + removed + "\n, and case-renames: " + newPathsOfCaseRenames);
Set<FilePath> toAdd = new HashSet<>(added);
toAdd.addAll(newPathsOfCaseRenames);
updateIndex(project, root, toAdd, removed, exceptions);
if (!exceptions.isEmpty())
return exceptions;
// 4. Commit the staging area
LOG.debug("Performing commit...");
try {
commitWithoutPaths(project, root, messageFile, author);
} catch (VcsException e) {
return Collections.singletonList(e);
}
} finally {
// 5. Stage back the changes unstaged before commit
if (!excludedStagedChanges.isEmpty()) {
LOG.debug("Restoring changes which were unstaged before commit: " + getLogString(rootPath, excludedStagedChanges));
Set<FilePath> toAdd = map2SetNotNull(excludedStagedChanges, ChangesUtil::getAfterPath);
Condition<Change> isMovedOrDeleted = change -> change.getType() == Change.Type.MOVED || change.getType() == Change.Type.DELETED;
Set<FilePath> toRemove = map2SetNotNull(filter(excludedStagedChanges, isMovedOrDeleted), ChangesUtil::getBeforePath);
updateIndex(project, root, toAdd, toRemove, exceptions);
}
}
return exceptions;
}
use of com.intellij.openapi.util.Condition in project intellij-community by JetBrains.
the class GitConfigTest method test_branch_with_hash_symbol.
//inspired by IDEA-135557
public void test_branch_with_hash_symbol() throws IOException {
createRepository();
addRemote("http://example.git");
git("update-ref refs/remotes/origin/a#branch HEAD");
git("branch --track a#branch origin/a#branch");
File gitDir = new File(myProjectPath, ".git");
GitConfig config = GitConfig.read(new File(gitDir, "config"));
VirtualFile dir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(gitDir);
GitRepositoryReader reader = new GitRepositoryReader(GitRepositoryFiles.getInstance(dir));
GitBranchState state = reader.readState(config.parseRemotes());
Collection<GitBranchTrackInfo> trackInfos = config.parseTrackInfos(state.getLocalBranches().keySet(), state.getRemoteBranches().keySet());
assertTrue("Couldn't find correct a#branch tracking information among: [" + trackInfos + "]", ContainerUtil.exists(trackInfos, new Condition<GitBranchTrackInfo>() {
@Override
public boolean value(GitBranchTrackInfo info) {
return info.getLocalBranch().getName().equals("a#branch") && info.getRemoteBranch().getNameForLocalOperations().equals("origin/a#branch");
}
}));
}
use of com.intellij.openapi.util.Condition in project intellij-community by JetBrains.
the class CCCreateCourseArchive method createCourseArchive.
public static void createCourseArchive(final Project project, Module module, String zipName, String locationDir, boolean showMessage) {
final Course course = StudyTaskManager.getInstance(project).getCourse();
if (course == null)
return;
final VirtualFile baseDir = project.getBaseDir();
VirtualFile archiveFolder = CCUtils.generateFolder(project, module, zipName);
if (archiveFolder == null) {
return;
}
CCLanguageManager manager = CCUtils.getStudyLanguageManager(course);
if (manager == null) {
return;
}
FileFilter filter = pathname -> !manager.doNotPackFile(pathname);
for (VirtualFile child : baseDir.getChildren()) {
String name = child.getName();
File fromFile = new File(child.getPath());
if (CCUtils.GENERATED_FILES_FOLDER.equals(name) || Project.DIRECTORY_STORE_FOLDER.equals(name) || name.contains("iml") || manager.doNotPackFile(fromFile)) {
continue;
}
copyChild(archiveFolder, filter, child, fromFile);
}
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
archiveFolder.refresh(false, true);
Course courseCopy = course.copy();
replaceAnswerFilesWithTaskFiles(courseCopy);
generateJson(archiveFolder, courseCopy);
VirtualFileManager.getInstance().refreshWithoutFileWatcher(false);
packCourse(archiveFolder, locationDir, zipName, showMessage);
synchronize(project);
}
private void replaceAnswerFilesWithTaskFiles(Course courseCopy) {
for (Lesson lesson : courseCopy.getLessons()) {
String lessonDirName = EduNames.LESSON + String.valueOf(lesson.getIndex());
final VirtualFile lessonDir = baseDir.findChild(lessonDirName);
if (lessonDir == null)
continue;
for (Task task : lesson.getTaskList()) {
final VirtualFile taskDir = task.getTaskDir(project);
if (taskDir == null)
continue;
String taskDirName = EduNames.TASK + String.valueOf(task.getIndex());
VirtualFile studentFileDir = VfsUtil.findRelativeFile(archiveFolder, lessonDirName, taskDirName);
if (studentFileDir == null) {
continue;
}
VirtualFile srcDir = studentFileDir.findChild(EduNames.SRC);
if (srcDir != null) {
studentFileDir = srcDir;
}
if (task.hasSubtasks()) {
transformSubtaskTestsToTextFiles(studentFileDir);
}
for (String taskFile : task.getTaskFiles().keySet()) {
VirtualFile answerFile = taskDir.findFileByRelativePath(taskFile);
if (answerFile == null) {
continue;
}
EduUtils.createStudentFile(this, project, answerFile, studentFileDir, task, 0);
}
}
}
}
private void transformSubtaskTestsToTextFiles(VirtualFile studentFileDir) {
Condition<VirtualFile> isSubtaskTestFile = file -> CCUtils.isTestsFile(project, file) && file.getName().contains(EduNames.SUBTASK_MARKER);
List<VirtualFile> subtaskTests = ContainerUtil.filter(Arrays.asList(studentFileDir.getChildren()), isSubtaskTestFile);
for (VirtualFile subtaskTest : subtaskTests) {
try {
subtaskTest.rename(this, subtaskTest.getNameWithoutExtension() + ".txt");
} catch (IOException e) {
LOG.error(e);
}
}
}
});
}
Aggregations