@NotNull @Override public ToolbarComponents init() { if (myRequest instanceof UnknownFileTypeDiffRequest) { String fileName = ((UnknownFileTypeDiffRequest)myRequest).getFileName(); if (fileName != null && FileTypeManager.getInstance().getFileTypeByFileName(fileName) != UnknownFileType.INSTANCE) { // FileType was assigned elsewhere (ex: by other UnknownFileTypeDiffRequest). We should reload request. if (myContext instanceof DiffContextEx) { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { ((DiffContextEx)myContext).reloadDiffRequest(); } }, ModalityState.current()); } } } return new ToolbarComponents(); }
public static PsiFile setName(@NotNull PsiFile file, @NotNull String newName) throws IncorrectOperationException { VirtualFile vFile = file.getViewProvider().getVirtualFile(); PsiManagerImpl manager = (PsiManagerImpl)file.getManager(); try{ final FileType newFileType = FileTypeRegistry.getInstance().getFileTypeByFileName(newName); if (UnknownFileType.INSTANCE.equals(newFileType) || newFileType.isBinary()) { // before the file becomes unknown or a binary (thus, not openable in the editor), save it to prevent data loss final FileDocumentManager fdm = FileDocumentManager.getInstance(); final Document doc = fdm.getCachedDocument(vFile); if (doc != null) { fdm.saveDocumentAsIs(doc); } } vFile.rename(manager, newName); } catch(IOException e){ throw new IncorrectOperationException(e); } return file.getViewProvider().isPhysical() ? manager.findFile(vFile) : file; }
@NotNull public static List<VirtualFile> getVirtualFileListFromAttachedObject(Object attached) { List<VirtualFile> result; List<File> fileList = getFileListFromAttachedObject(attached); if (fileList.isEmpty()) { result = Collections.emptyList(); } else { result = new ArrayList<VirtualFile>(fileList.size()); for (File file : fileList) { VirtualFile virtualFile = VfsUtil.findFileByIoFile(file, true); if (virtualFile == null) continue; result.add(virtualFile); // detect and store file type for Finder-2-IDEA drag-n-drop if (!virtualFile.isDirectory() && virtualFile.getFileType() == UnknownFileType.INSTANCE) { FileTypeRegistry.getInstance().detectFileTypeFromContent(virtualFile); } } } return result; }
public static PsiFile setName(final PsiFile file, String newName) throws IncorrectOperationException { VirtualFile vFile = file.getViewProvider().getVirtualFile(); PsiManagerImpl manager = (PsiManagerImpl)file.getManager(); try{ final FileType newFileType = FileTypeRegistry.getInstance().getFileTypeByFileName(newName); if (UnknownFileType.INSTANCE.equals(newFileType) || newFileType.isBinary()) { // before the file becomes unknown or a binary (thus, not openable in the editor), save it to prevent data loss final FileDocumentManager fdm = FileDocumentManager.getInstance(); final Document doc = fdm.getCachedDocument(vFile); if (doc != null) { fdm.saveDocumentAsIs(doc); } } vFile.rename(manager, newName); } catch(IOException e){ throw new IncorrectOperationException(e.toString(),e); } return file.getViewProvider().isPhysical() ? manager.findFile(vFile) : file; }
@Nonnull private JComponent createComponent(@Nonnull DiffRequest request) { if (request instanceof MessageDiffRequest) { // TODO: explain some of ErrorDiffRequest exceptions ? String message = ((MessageDiffRequest)request).getMessage(); return DiffUtil.createMessagePanel(message); } if (request instanceof ComponentDiffRequest) { return ((ComponentDiffRequest)request).getComponent(myContext); } if (request instanceof ContentDiffRequest) { List<DiffContent> contents = ((ContentDiffRequest)request).getContents(); for (final DiffContent content : contents) { if (content instanceof FileContent && UnknownFileType.INSTANCE == content.getContentType()) { final VirtualFile file = ((FileContent)content).getFile(); UnknownFileTypeDiffRequest unknownFileTypeRequest = new UnknownFileTypeDiffRequest(file, myRequest.getTitle()); return unknownFileTypeRequest.getComponent(myContext); } } } return DiffUtil.createMessagePanel("Can't show diff"); }
@RequiredDispatchThread @Nonnull @Override public ToolbarComponents init() { if (myRequest instanceof UnknownFileTypeDiffRequest) { String fileName = ((UnknownFileTypeDiffRequest)myRequest).getFileName(); if (fileName != null && FileTypeManager.getInstance().getFileTypeByFileName(fileName) != UnknownFileType.INSTANCE) { // FileType was assigned elsewhere (ex: by other UnknownFileTypeDiffRequest). We should reload request. if (myContext instanceof DiffContextEx) { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { ((DiffContextEx)myContext).reloadDiffRequest(); } }, ModalityState.current()); } } } return new ToolbarComponents(); }
@Nonnull private static DocumentContent createImpl(@Nullable Project project, @Nonnull String text, @Nullable FileType fileType, @javax.annotation.Nullable String fileName, @Nullable VirtualFile highlightFile, @javax.annotation.Nullable Charset charset, @javax.annotation.Nullable Boolean bom, boolean respectLineSeparators, boolean readOnly) { if (UnknownFileType.INSTANCE == fileType) fileType = PlainTextFileType.INSTANCE; // TODO: detect invalid (different across the file) separators ? LineSeparator separator = respectLineSeparators ? StringUtil.detectSeparators(text) : null; String correctedContent = StringUtil.convertLineSeparators(text); Document document = createDocument(project, correctedContent, fileType, fileName, readOnly); DocumentContent content = new DocumentContentImpl(project, document, fileType, highlightFile, separator, charset, bom); if (fileName != null) content.putUserData(DiffUserDataKeysEx.FILE_NAME, fileName); return content; }
@Nonnull public static List<VirtualFile> getVirtualFileListFromAttachedObject(Object attached) { List<VirtualFile> result; List<File> fileList = getFileListFromAttachedObject(attached); if (fileList.isEmpty()) { result = Collections.emptyList(); } else { result = new ArrayList<VirtualFile>(fileList.size()); for (File file : fileList) { VirtualFile virtualFile = VfsUtil.findFileByIoFile(file, true); if (virtualFile == null) continue; result.add(virtualFile); // detect and store file type for Finder-2-IDEA drag-n-drop if (!virtualFile.isDirectory() && virtualFile.getFileType() == UnknownFileType.INSTANCE) { FileTypeRegistry.getInstance().detectFileTypeFromContent(virtualFile); } } } return result; }
@Override public void update(AnActionEvent e) { Presentation presentation = e.getPresentation(); VirtualFile file = e.getData(PlatformDataKeys.VIRTUAL_FILE); Project project = e.getData(CommonDataKeys.PROJECT); boolean haveSmthToDo; if (project == null || file == null || file.isDirectory()) { haveSmthToDo = false; } else { // the action should also be available for files which have been auto-detected as text or as a particular language (IDEA-79574) haveSmthToDo = FileTypeManager.getInstance().getFileTypeByFileName(file.getName()) == UnknownFileType.INSTANCE; } presentation.setVisible(haveSmthToDo || ActionPlaces.MAIN_MENU.equals(e.getPlace())); presentation.setEnabled(haveSmthToDo); }
@RequiredDispatchThread public static PsiFile setName(final PsiFile file, String newName) throws IncorrectOperationException { VirtualFile vFile = file.getViewProvider().getVirtualFile(); PsiManagerImpl manager = (PsiManagerImpl)file.getManager(); try{ final FileType newFileType = FileTypeRegistry.getInstance().getFileTypeByFileName(newName); if (UnknownFileType.INSTANCE.equals(newFileType) || newFileType.isBinary()) { // before the file becomes unknown or a binary (thus, not openable in the editor), save it to prevent data loss final FileDocumentManager fdm = FileDocumentManager.getInstance(); final Document doc = fdm.getCachedDocument(vFile); if (doc != null) { fdm.saveDocumentAsIs(doc); } } vFile.rename(manager, newName); } catch(IOException e){ throw new IncorrectOperationException(e); } return file.getViewProvider().isPhysical() ? manager.findFile(vFile) : file; }
private boolean isFileTypeOk(@Nonnull VirtualFile file) { FileType fileType = file.getFileType(); if (fileType == UnknownFileType.INSTANCE) { fileType = FileTypeChooser.associateFileType(file.getName()); if (fileType == null) { PatchApplier .showError(myProject, "Cannot apply content for " + file.getPresentableName() + " file from patch because its type not defined.", true); return false; } } if (fileType.isBinary()) { PatchApplier.showError(myProject, "Cannot apply file " + file.getPresentableName() + " from patch because it is binary.", true); return false; } return true; }
public static boolean openFileWithPsiElement(PsiElement element, boolean searchForOpen, boolean requestFocus) { boolean openAsNative = false; if (element instanceof PsiFile) { VirtualFile virtualFile = ((PsiFile)element).getVirtualFile(); if (virtualFile != null) { openAsNative = virtualFile.getFileType() instanceof INativeFileType || virtualFile.getFileType() == UnknownFileType.INSTANCE; } } if (searchForOpen) { element.putUserData(FileEditorManager.USE_CURRENT_WINDOW, null); } else { element.putUserData(FileEditorManager.USE_CURRENT_WINDOW, true); } if (openAsNative || !activatePsiElementIfOpen(element, searchForOpen, requestFocus)) { final NavigationItem navigationItem = (NavigationItem)element; if (!navigationItem.canNavigate()) return false; navigationItem.navigate(requestFocus); return true; } element.putUserData(FileEditorManager.USE_CURRENT_WINDOW, null); return false; }
/** * FIXME [VISTALL] find way found regexp plugin */ @RequiredReadAction private static List<PsiElement> getTopLevelRegExpChars(String regExpText, Project project) { FileType regexpFileType = FileTypeRegistry.getInstance().getFileTypeByExtension("regexp"); if (regexpFileType == UnknownFileType.INSTANCE) { return Collections.emptyList(); } PsiFile file = PsiFileFactory.getInstance(project).createFileFromText("A.regexp", regexpFileType, regExpText); List<PsiElement> result = null; final PsiElement[] children = file.getChildren(); for (PsiElement child : children) { PsiElement[] grandChildren = child.getChildren(); if (grandChildren.length != 1) return Collections.emptyList(); // a | b, more than one branch, can not predict in current way for (PsiElement grandGrandChild : grandChildren[0].getChildren()) { if (result == null) result = new ArrayList<>(); result.add(grandGrandChild); } } return result != null ? result : Collections.emptyList(); }
/** * {@inheritDoc} */ @Override public int compare(final FileType o1, final FileType o2) { if (o1 == o2) { return 0; } if (o1 == UnknownFileType.INSTANCE) { return 1; } if (o2 == UnknownFileType.INSTANCE) { return -1; } if (o1.isBinary() && !o2.isBinary()) { return 1; } if (!o1.isBinary() && o2.isBinary()) { return -1; } return o1.getName().compareToIgnoreCase(o2.getName()); }
@Nullable private static FileType getEditorFileType(@NotNull AnActionEvent e) { DiffContent content = e.getData(DiffDataKeys.CURRENT_CONTENT); if (content != null && content.getContentType() != null) return content.getContentType(); DiffRequest request = e.getData(DiffDataKeys.DIFF_REQUEST); if (request instanceof ContentDiffRequest) { for (DiffContent diffContent : ((ContentDiffRequest)request).getContents()) { FileType type = diffContent.getContentType(); if (type != null && type != UnknownFileType.INSTANCE) return type; } } return null; }
@Nullable public String getNewFileTemplateName() { FileType fileType = FileTypeRegistry.getInstance().getFileTypeByFileName(myText); if (fileType != UnknownFileType.INSTANCE) { return fileType.getName() + " File." + fileType.getDefaultExtension(); } return null; }
@Override public void beforeContentsChange(@NotNull VirtualFileEvent event) { VirtualFile virtualFile = event.getFile(); // check file type in second order to avoid content detection running if (virtualFile.getLength() == 0 && virtualFile.getFileType() == UnknownFileType.INSTANCE) { virtualFile.putUserData(MUST_RECOMPUTE_FILE_TYPE, Boolean.TRUE); } }
private static boolean isFileName(String name) { final int index = name.lastIndexOf('.'); if (index > 0) { String ext = name.substring(index + 1); if (ext.contains(":")) { ext = ext.substring(0, ext.indexOf(':')); } if (FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(ext) != UnknownFileType.INSTANCE) { return true; } } return false; }
public BaseInjectionPanel(BaseInjection injection, Project project) { super(injection, project); $$$setupUI$$$(); // see IDEA-9987 myHelper = injection.getCompiler(); final FileType groovy = FileTypeManager.getInstance().getFileTypeByExtension("groovy"); final FileType realFileType = groovy == UnknownFileType.INSTANCE ? FileTypes.PLAIN_TEXT : groovy; final PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText("injection." + realFileType.getDefaultExtension(), realFileType, "", 0, true); final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile); psiFile.putUserData(BaseInjection.INJECTION_KEY, injection); myTextArea = new EditorTextField(document, project, realFileType) { @Override protected EditorEx createEditor() { final EditorEx ex = super.createEditor(); ex.setVerticalScrollbarVisible(true); ex.setHorizontalScrollbarVisible(true); return ex; } @Override protected boolean isOneLineMode() { return false; } }; myCenterPanel.add(myTextArea, BorderLayout.CENTER); myTextArea.setFontInheritedFromLAF(false); //myTextArea.setFont(EditorColorsManager.getInstance().getGlobalScheme().getFont(EditorFontType.PLAIN)); init(injection.copy()); }
@Override public void initComponent() { FileType javaScriptFileType = fileTypeManager.getFileTypeByExtension("js"); if (!(javaScriptFileType instanceof UnknownFileType)) { fileTypeManager.associateExtension(javaScriptFileType, "ds"); } }
private static void detectFileType(VirtualFile virtualFile) { if (virtualFile == null || !virtualFile.isValid() || virtualFile.isDirectory()) return; FileType fileType = virtualFile.getFileType(); if (fileType == UnknownFileType.INSTANCE) { FileTypeRegistry.getInstance().detectFileTypeFromContent(virtualFile); } }
@javax.annotation.Nullable private static FileType getEditorFileType(@Nonnull AnActionEvent e) { DiffContent content = e.getData(DiffDataKeys.CURRENT_CONTENT); if (content != null && content.getContentType() != null) return content.getContentType(); DiffRequest request = e.getData(DiffDataKeys.DIFF_REQUEST); if (request instanceof ContentDiffRequest) { for (DiffContent diffContent : ((ContentDiffRequest)request).getContents()) { FileType type = diffContent.getContentType(); if (type != null && type != UnknownFileType.INSTANCE) return type; } } return null; }
public static void assertFilesEqual(VirtualFile fileAfter, VirtualFile fileBefore) throws IOException { try { assertJarFilesEqual(VfsUtilCore.virtualToIoFile(fileAfter), VfsUtilCore.virtualToIoFile(fileBefore)); } catch (IOException e) { FileDocumentManager manager = FileDocumentManager.getInstance(); Document docBefore = manager.getDocument(fileBefore); boolean canLoadBeforeText = !fileBefore.getFileType().isBinary() || fileBefore.getFileType() == UnknownFileType.INSTANCE; String textB = docBefore != null ? docBefore.getText() : !canLoadBeforeText ? null : LoadTextUtil.getTextByBinaryPresentation(fileBefore.contentsToByteArray(false), fileBefore).toString(); Document docAfter = manager.getDocument(fileAfter); boolean canLoadAfterText = !fileBefore.getFileType().isBinary() || fileBefore.getFileType() == UnknownFileType.INSTANCE; String textA = docAfter != null ? docAfter.getText() : !canLoadAfterText ? null : LoadTextUtil.getTextByBinaryPresentation(fileAfter.contentsToByteArray(false), fileAfter).toString(); if (textA != null && textB != null) { assertEquals(fileAfter.getPath(), textA, textB); } else { Assert.assertArrayEquals(fileAfter.getPath(), fileAfter.contentsToByteArray(), fileBefore.contentsToByteArray()); } } }
protected void scrollToSource(final Component tree) { DataContext dataContext=DataManager.getInstance().getDataContext(tree); getReady(dataContext).doWhenDone(new Runnable() { @Override public void run() { DataContext context = DataManager.getInstance().getDataContext(tree); final VirtualFile vFile = context.getData(PlatformDataKeys.VIRTUAL_FILE); if (vFile != null) { // Attempt to navigate to the virtual file with unknown file type will show a modal dialog // asking to register some file type for this file. This behaviour is undesirable when autoscrolling. if (vFile.getFileType() == UnknownFileType.INSTANCE || vFile.getFileType() instanceof INativeFileType) return; //IDEA-84881 Don't autoscroll to very large files if (vFile.getLength() > PersistentFSConstants.getMaxIntellisenseFileSize()) return; } Navigatable[] navigatables = context.getData(PlatformDataKeys.NAVIGATABLE_ARRAY); if (navigatables != null) { if (navigatables.length > 1) { return; } for (Navigatable navigatable : navigatables) { // we are not going to open modal dialog during autoscrolling if (!navigatable.canNavigateToSource()) return; } } OpenSourceUtil.openSourcesFrom(context, false); } }); }
@Override public void beforeContentsChange(@Nonnull VirtualFileEvent event) { VirtualFile virtualFile = event.getFile(); // check file type in second order to avoid content detection running if (virtualFile.getLength() == 0 && virtualFile.getFileType() == UnknownFileType.INSTANCE) { virtualFile.putUserData(MUST_RECOMPUTE_FILE_TYPE, Boolean.TRUE); } myConflictResolver.beforeContentChange(event); }
/** * Collects all files which are located in the passed directory. * * @throws IllegalArgumentException if <code>dir</code> isn't a directory. */ public static void collectFiles(final VirtualFile dir, final List<VirtualFile> files, final boolean recursive, final boolean addDirectories) { if (!dir.isDirectory()) { throw new IllegalArgumentException(VcsBundle.message("exception.text.file.should.be.directory", dir.getPresentableUrl())); } final FileTypeManager fileTypeManager = FileTypeManager.getInstance(); VfsUtilCore.visitChildrenRecursively(dir, new VirtualFileVisitor() { @Override public boolean visitFile(@Nonnull VirtualFile file) { if (file.isDirectory()) { if (addDirectories) { files.add(file); } if (!recursive && !Comparing.equal(file, dir)) { return false; } } else if (fileTypeManager == null || file.getFileType() != UnknownFileType.INSTANCE) { files.add(file); } return true; } }); }
private void detectFileType() { VirtualFile file = myVirtualFile; if (file == null || !file.isValid() || file.isDirectory()) return; FileType fileType = file.getFileType(); if (fileType == UnknownFileType.INSTANCE) { FileTypeRegistry.getInstance().detectFileTypeFromContent(file); } }
private boolean checkContentRevision(ContentRevision rev, final DiffChainContext context, final List<String> errSb) { if (rev == null) return true; if (rev.getFile().isDirectory()) return false; if (! hasContents(rev, errSb)) { return false; } final FileType type = rev.getFile().getFileType(); if (! type.isBinary()) return true; if (type == UnknownFileType.INSTANCE) { final boolean associatedToText = checkAssociate(myProject, rev.getFile().getName(), context); } return true; }
@Nullable @Override public Icon substituteIcon(@Nonnull Project project, @Nonnull VirtualFile file) { FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(file.getName()); if (fileType == UnknownFileType.INSTANCE || fileType == PlainTextFileType.INSTANCE) { return AllIcons.Debugger.ToolConsole; } Icon icon = fileType.getIcon(); Icon subscript = ((ScalableIcon)AllIcons.Debugger.ToolConsole).scale(.5f); LayeredIcon icons = new LayeredIcon(2); icons.setIcon(icon, 0); icons.setIcon(subscript, 1, 8, 8); return icons; }
@Nonnull private static FileType findFileType(final Language language) { FileType[] registeredFileTypes = FileTypeManager.getInstance().getRegisteredFileTypes(); for (FileType fileType : registeredFileTypes) { if (fileType instanceof LanguageFileType && ((LanguageFileType)fileType).getLanguage() == language) { return fileType; } } return UnknownFileType.INSTANCE; }
public UnknownFileTypeDiffRequest(@NotNull String fileName, @Nullable String title) { boolean knownFileType = FileTypeManager.getInstance().getFileTypeByFileName(fileName) != UnknownFileType.INSTANCE; myFileName = knownFileType ? null : fileName; myTitle = title; }
public static boolean isNativeFileType(FileType fileType) { return fileType instanceof INativeFileType && ((INativeFileType) fileType).useNativeIcon() || fileType instanceof UnknownFileType; }
public void processCommand(String[] args, @Nullable String currentDirectory) throws OperationFailedException { final String path1 = args[1]; final String path2 = args[2]; final VirtualFile file1 = findFile(path1, currentDirectory); final VirtualFile file2 = findFile(path2, currentDirectory); final boolean areDirs = areDirs(file1, file2); final boolean areJars = areJars(file1, file2); if (areDirs || areJars) { final DirDiffManager diffManager = DirDiffManager.getInstance(ProjectManager.getInstance().getDefaultProject()); final DiffElement d1 = diffManager.createDiffElement(file1); final DiffElement d2 = diffManager.createDiffElement(file2); if (d1 == null) { throw new OperationFailedException(DiffBundle.message("cannot.create.diff.error", path1)); } if (d2 == null) { throw new OperationFailedException(DiffBundle.message("cannot.create.diff.error", path1)); } else if (!diffManager.canShow(d1, d2)) { throw new OperationFailedException(DiffBundle.message("cannot.compare.error", path1, path2)); } final DirDiffSettings settings = new DirDiffSettings(); settings.showInFrame = false; diffManager.showDiff(d1, d2, settings, null); } else { file1.refresh(false, false); file2.refresh(false, false); if (file1.getFileType() == UnknownFileType.INSTANCE) { throw new OperationFailedException(DiffBundle.message("unknown.file.type.error", path1)); } else if (file2.getFileType() == UnknownFileType.INSTANCE) { throw new OperationFailedException(DiffBundle.message("unknown.file.type.error", path2)); } SimpleDiffRequest request = SimpleDiffRequest.compareFiles(file1, file2, ProjectManager.getInstance().getDefaultProject()); request.addHint(DiffTool.HINT_SHOW_MODAL_DIALOG); DiffManager.getInstance().getIdeaDiffTool().show(request); } }
public CoreFileTypeRegistry() { myAllFileTypes.add(UnknownFileType.INSTANCE); }
@NotNull @Override public FileType getFileTypeByExtension(@NonNls @NotNull String extension) { final FileType result = myExtensionsMap.get(extension); return result == null ? UnknownFileType.INSTANCE : result; }
@NotNull @Override public FileType detectFileTypeFromContent(@NotNull VirtualFile file) { return UnknownFileType.INSTANCE; }