use of com.intellij.util.Consumer in project intellij-community by JetBrains.
the class TemplateModuleBuilder method unzip.
private void unzip(@Nullable final String projectName, String path, final boolean moduleMode, @Nullable ProgressIndicator pI, boolean reportFailuresWithDialog) {
final WizardInputField basePackage = getBasePackageField();
try {
final File dir = new File(path);
class ExceptionConsumer implements Consumer<VelocityException> {
private String myPath;
private String myText;
private SmartList<Trinity<String, String, VelocityException>> myFailures = new SmartList<>();
@Override
public void consume(VelocityException e) {
myFailures.add(Trinity.create(myPath, myText, e));
}
private void setCurrentFile(String path, String text) {
myPath = path;
myText = text;
}
private void reportFailures() {
if (myFailures.isEmpty()) {
return;
}
if (reportFailuresWithDialog) {
String dialogMessage;
if (myFailures.size() == 1) {
dialogMessage = "Failed to decode file \'" + myFailures.get(0).getFirst() + "\'";
} else {
StringBuilder dialogMessageBuilder = new StringBuilder();
dialogMessageBuilder.append("Failed to decode files: \n");
for (Trinity<String, String, VelocityException> failure : myFailures) {
dialogMessageBuilder.append(failure.getFirst()).append("\n");
}
dialogMessage = dialogMessageBuilder.toString();
}
Messages.showErrorDialog(dialogMessage, "Decoding Template");
}
StringBuilder reportBuilder = new StringBuilder();
for (Trinity<String, String, VelocityException> failure : myFailures) {
reportBuilder.append("File: ").append(failure.getFirst()).append("\n");
reportBuilder.append("Exception:\n").append(ExceptionUtil.getThrowableText(failure.getThird())).append("\n");
reportBuilder.append("File content:\n\'").append(failure.getSecond()).append("\'\n");
reportBuilder.append("\n===========================================\n");
}
LOG.error(LogMessageEx.createEvent("Cannot decode files in template", "", new Attachment("Files in template", reportBuilder.toString())));
}
}
ExceptionConsumer consumer = new ExceptionConsumer();
List<File> filesToRefresh = new ArrayList<>();
myTemplate.processStream(new ArchivedProjectTemplate.StreamProcessor<Void>() {
@Override
public Void consume(@NotNull ZipInputStream stream) throws IOException {
ZipUtil.unzip(ProgressManager.getInstance().getProgressIndicator(), dir, stream, path1 -> {
if (moduleMode && path1.contains(Project.DIRECTORY_STORE_FOLDER)) {
return null;
}
if (basePackage != null) {
return path1.replace(getPathFragment(basePackage.getDefaultValue()), getPathFragment(basePackage.getValue()));
}
return path1;
}, new ZipUtil.ContentProcessor() {
@Override
public byte[] processContent(byte[] content, File file) throws IOException {
if (pI != null) {
pI.checkCanceled();
}
FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(FileUtilRt.getExtension(file.getName()));
String text = new String(content, CharsetToolkit.UTF8_CHARSET);
consumer.setCurrentFile(file.getName(), text);
return fileType.isBinary() ? content : processTemplates(projectName, text, file, consumer);
}
}, true);
myTemplate.handleUnzippedDirectories(dir, filesToRefresh);
return null;
}
});
if (pI != null) {
pI.setText("Refreshing...");
}
String iml = ContainerUtil.find(dir.list(), s -> s.endsWith(".iml"));
if (moduleMode) {
File from = new File(path, iml);
File to = new File(getModuleFilePath());
if (!from.renameTo(to)) {
throw new IOException("Can't rename " + from + " to " + to);
}
}
RefreshQueue refreshQueue = RefreshQueue.getInstance();
LOG.assertTrue(!filesToRefresh.isEmpty());
for (File file : filesToRefresh) {
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
if (virtualFile == null) {
throw new IOException("Can't find " + file);
}
refreshQueue.refresh(false, true, null, virtualFile);
}
consumer.reportFailures();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.intellij.util.Consumer in project intellij-community by JetBrains.
the class TargetElementUtil method getNamedElement.
@Override
@Nullable
public PsiElement getNamedElement(@Nullable final PsiElement element, final int offsetInElement) {
if (element == null)
return null;
final List<PomTarget> targets = ContainerUtil.newArrayList();
final Consumer<PomTarget> consumer = target -> {
if (target instanceof PsiDeclaredTarget) {
final PsiDeclaredTarget declaredTarget = (PsiDeclaredTarget) target;
final PsiElement navigationElement = declaredTarget.getNavigationElement();
final TextRange range = declaredTarget.getNameIdentifierRange();
if (range != null && !range.shiftRight(navigationElement.getTextRange().getStartOffset()).contains(element.getTextRange().getStartOffset() + offsetInElement)) {
return;
}
}
targets.add(target);
};
PsiElement parent = element;
int offset = offsetInElement;
while (parent != null) {
for (PomDeclarationSearcher searcher : PomDeclarationSearcher.EP_NAME.getExtensions()) {
searcher.findDeclarationsAt(parent, offset, consumer);
if (!targets.isEmpty()) {
final PomTarget target = targets.get(0);
return target == null ? null : PomService.convertToPsi(element.getProject(), target);
}
}
offset += parent.getStartOffsetInParent();
parent = parent.getParent();
}
return getNamedElement(element);
}
use of com.intellij.util.Consumer in project intellij-community by JetBrains.
the class ChangeSignatureDialogBase method createOptionsPanel.
protected JComponent createOptionsPanel() {
final JPanel panel = new JPanel(new BorderLayout());
if (myAllowDelegation) {
myDelegationPanel = createDelegationPanel();
panel.add(myDelegationPanel, BorderLayout.WEST);
}
myPropagateParamChangesButton = new AnActionButton(RefactoringBundle.message("changeSignature.propagate.parameters.title"), null, AllIcons.Hierarchy.Caller) {
@Override
public void actionPerformed(AnActionEvent e) {
final Ref<CallerChooserBase<Method>> chooser = new Ref<>();
Consumer<Set<Method>> callback = callers -> {
myMethodsToPropagateParameters = callers;
myParameterPropagationTreeToReuse = chooser.get().getTree();
};
try {
String message = RefactoringBundle.message("changeSignature.parameter.caller.chooser");
chooser.set(createCallerChooser(message, myParameterPropagationTreeToReuse, callback));
} catch (ProcessCanceledException ex) {
// user cancelled initial callers search, don't show dialog
return;
}
chooser.get().show();
}
};
final JPanel result = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP));
result.add(panel);
return result;
}
use of com.intellij.util.Consumer in project intellij-community by JetBrains.
the class RefactoringQuickFix method doFix.
default default void doFix(@NotNull PsiElement element) {
final PsiElement elementToRefactor = getElementToRefactor(element);
if (elementToRefactor == null) {
return;
}
final Consumer<DataContext> consumer = dataContext -> {
dataContext = enhanceDataContext(dataContext);
final RefactoringActionHandler handler = getHandler();
handler.invoke(element.getProject(), new PsiElement[] { elementToRefactor }, dataContext);
};
DataManager.getInstance().getDataContextFromFocus().doWhenDone(consumer);
}
use of com.intellij.util.Consumer in project intellij-community by JetBrains.
the class PostfixTemplatesCheckboxTree method setState.
public void setState(@NotNull final Map<String, Set<String>> langToDisabledTemplates) {
final TreeState treeState = TreeState.createOn(this, myRoot);
Consumer<PostfixTemplateCheckedTreeNode> consumer = template -> {
Set<String> disabledTemplates = langToDisabledTemplates.get(template.getLang());
String key = template.getTemplate().getKey();
if (disabledTemplates != null && disabledTemplates.contains(key)) {
template.setChecked(false);
return;
}
template.setChecked(true);
};
visit(consumer);
myModel.nodeStructureChanged(myRoot);
treeState.applyTo(this);
TreeUtil.expandAll(this);
}
Aggregations