use of com.intellij.openapi.fileTypes.FileType in project intellij-community by JetBrains.
the class FrameworkDetectionIndex method getIndexer.
@NotNull
@Override
public DataIndexer<Integer, Void, FileContent> getIndexer() {
final MultiMap<FileType, Pair<ElementPattern<FileContent>, Integer>> detectors = new MultiMap<>();
FrameworkDetectorRegistry registry = FrameworkDetectorRegistry.getInstance();
for (FrameworkDetector detector : FrameworkDetector.EP_NAME.getExtensions()) {
detectors.putValue(detector.getFileType(), Pair.create(detector.createSuitableFilePattern(), registry.getDetectorId(detector)));
}
return new DataIndexer<Integer, Void, FileContent>() {
@NotNull
@Override
public Map<Integer, Void> map(@NotNull FileContent inputData) {
final FileType fileType = inputData.getFileType();
if (!detectors.containsKey(fileType)) {
return Collections.emptyMap();
}
Map<Integer, Void> result = null;
for (Pair<ElementPattern<FileContent>, Integer> pair : detectors.get(fileType)) {
if (pair.getFirst().accepts(inputData)) {
if (LOG.isDebugEnabled()) {
LOG.debug(inputData.getFile() + " accepted by detector " + pair.getSecond());
}
if (result == null) {
result = new HashMap<>();
}
myDispatcher.getMulticaster().fileUpdated(inputData.getFile(), pair.getSecond());
result.put(pair.getSecond(), null);
}
}
return result != null ? result : Collections.<Integer, Void>emptyMap();
}
};
}
use of com.intellij.openapi.fileTypes.FileType in project intellij-community by JetBrains.
the class FrameworkDetectionProcessor method collectSuitableFiles.
private void collectSuitableFiles(@NotNull VirtualFile file) {
try {
VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {
@Override
public boolean visitFile(@NotNull VirtualFile file) {
// Since this code is invoked from New Project Wizard it's very possible that VFS isn't loaded to memory yet, so we need to do it
// manually, otherwise refresh will do nothing
myProgressIndicator.checkCanceled();
return true;
}
});
file.refresh(false, true);
VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {
@Override
public boolean visitFile(@NotNull VirtualFile file) {
myProgressIndicator.checkCanceled();
if (!myProcessedFiles.add(file)) {
return false;
}
if (!file.isDirectory()) {
final FileType fileType = file.getFileType();
if (myDetectorsByFileType.containsKey(fileType)) {
myProgressIndicator.setText2(file.getPresentableUrl());
try {
final FileContent fileContent = new FileContentImpl(file, file.contentsToByteArray(false));
for (FrameworkDetectorData detector : myDetectorsByFileType.get(fileType)) {
if (detector.myFilePattern.accepts(fileContent)) {
detector.mySuitableFiles.add(file);
}
}
} catch (IOException e) {
LOG.info(e);
}
}
}
return true;
}
});
} catch (ProcessCanceledException ignored) {
}
}
use of com.intellij.openapi.fileTypes.FileType in project intellij-community by JetBrains.
the class GotoFileAction method gotoActionPerformed.
@Override
public void gotoActionPerformed(AnActionEvent e) {
final Project project = e.getData(CommonDataKeys.PROJECT);
if (project == null)
return;
FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.file");
final GotoFileModel gotoFileModel = new GotoFileModel(project);
GotoActionCallback<FileType> callback = new GotoActionCallback<FileType>() {
@Override
protected ChooseByNameFilter<FileType> createFilter(@NotNull ChooseByNamePopup popup) {
return new GotoFileFilter(popup, gotoFileModel, project);
}
@Override
public void elementChosen(final ChooseByNamePopup popup, final Object element) {
if (element == null)
return;
ApplicationManager.getApplication().assertIsDispatchThread();
Navigatable n = (Navigatable) element;
//this is for better cursor position
if (element instanceof PsiFile) {
VirtualFile file = ((PsiFile) element).getVirtualFile();
if (file == null)
return;
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, popup.getLinePosition(), popup.getColumnPosition());
n = descriptor.setUseCurrentWindow(popup.isOpenInCurrentWindowRequested());
}
if (n.canNavigate()) {
n.navigate(true);
}
}
};
GotoFileItemProvider provider = new GotoFileItemProvider(project, getPsiContext(e), gotoFileModel);
showNavigationPopup(e, gotoFileModel, callback, IdeBundle.message("go.to.file.toolwindow.title"), true, true, provider);
}
use of com.intellij.openapi.fileTypes.FileType in project intellij-community by JetBrains.
the class RootType method substituteIcon.
@Nullable
public Icon substituteIcon(@NotNull Project project, @NotNull VirtualFile file) {
Language language = substituteLanguage(project, file);
FileType fileType = LanguageUtil.getLanguageFileType(language);
if (fileType == null)
fileType = ScratchUtil.getFileTypeFromName(file);
return fileType != null ? fileType.getIcon() : null;
}
use of com.intellij.openapi.fileTypes.FileType 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);
}
}
Aggregations