private static Language calcBaseLanguage(@NotNull VirtualFile file, @NotNull Project project) { if (file instanceof LightVirtualFile) { final Language language = ((LightVirtualFile) file).getLanguage(); if (language != null) { return language; } } FileType fileType = file.getFileType(); // Do not load content if (fileType == UnknownFileType.INSTANCE) { fileType = FileTypeRegistry.getInstance().detectFileTypeFromContent(file); } if (fileType.isBinary()) return Language.ANY; if (isTooLarge(file)) return PlainTextLanguage.INSTANCE; if (fileType instanceof LanguageFileType) { return LanguageSubstitutors.INSTANCE.substituteLanguage( ((LanguageFileType) fileType).getLanguage(), file, project); } final ContentBasedFileSubstitutor[] processors = Extensions.getExtensions(ContentBasedFileSubstitutor.EP_NAME); for (ContentBasedFileSubstitutor processor : processors) { Language language = processor.obtainLanguageForFile(file); if (language != null) return language; } return PlainTextLanguage.INSTANCE; }
private Icon getIconByExtension() { if (myFileTypeManager != null) { FileType fileType = myFileTypeManager.getFileTypeByExtension(getExtension()); return fileType.getIcon(); } return IdetalkCoreIcons.Nodes.Unknown; }
@NotNull public static Charset detectCharset( @NotNull VirtualFile virtualFile, @NotNull byte[] content, @NotNull FileType fileType) { Charset charset = null; Trinity<Charset, CharsetToolkit.GuessedEncoding, byte[]> guessed = guessFromContent(virtualFile, content, content.length); if (guessed != null && guessed.first != null) { charset = guessed.first; } else { String charsetName = fileType.getCharset(virtualFile, content); if (charsetName == null) { Charset specifiedExplicitly = EncodingRegistry.getInstance().getEncoding(virtualFile, true); if (specifiedExplicitly != null) { charset = specifiedExplicitly; } } else { charset = CharsetToolkit.forName(charsetName); } } if (charset == null) { charset = EncodingRegistry.getInstance().getDefaultCharset(); } if (fileType.getName().equals("Properties") && EncodingRegistry.getInstance().isNative2Ascii(virtualFile)) { charset = Native2AsciiCharset.wrap(charset); } virtualFile.setCharset(charset); return charset; }
@Override public boolean contains(@NotNull VirtualFile file) { if (!super.contains(file)) return false; final FileType fileType = file.getFileType(); for (FileType otherFileType : myFileTypes) { if (fileType.equals(otherFileType)) return true; } return false; }
@Override @Nullable public Document getDocument(@NotNull final VirtualFile file) { DocumentEx document = (DocumentEx) getCachedDocument(file); if (document == null) { if (file.isDirectory() || isBinaryWithoutDecompiler(file) || SingleRootFileViewProvider.isTooLargeForContentLoading(file)) { return null; } final CharSequence text = LoadTextUtil.loadText(file); synchronized (lock) { document = (DocumentEx) getCachedDocument(file); if (document != null) return document; // Double checking document = (DocumentEx) createDocument(text); document.setModificationStamp(file.getModificationStamp()); final FileType fileType = file.getFileType(); document.setReadOnly(!file.isWritable() || fileType.isBinary()); file.putUserData(DOCUMENT_KEY, new WeakReference<Document>(document)); document.putUserData(FILE_KEY, file); if (!(file instanceof LightVirtualFile || file.getFileSystem() instanceof DummyFileSystem)) { document.addDocumentListener( new DocumentAdapter() { @Override public void documentChanged(DocumentEvent e) { final Document document = e.getDocument(); myUnsavedDocuments.add(document); final Runnable currentCommand = CommandProcessor.getInstance().getCurrentCommand(); Project project = currentCommand == null ? null : CommandProcessor.getInstance().getCurrentCommandProject(); String lineSeparator = CodeStyleFacade.getInstance(project).getLineSeparator(); document.putUserData(LINE_SEPARATOR_KEY, lineSeparator); // avoid documents piling up during batch processing if (areTooManyDocumentsInTheQueue(myUnsavedDocuments)) { saveAllDocumentsLater(); } } }); } } myMultiCaster.fileContentLoaded(file, document); } return document; }
public boolean isHighlightingAvailable(PsiFile file) { if (myDisabledHighlightingFiles.contains(file)) return false; if (file == null || !file.isPhysical()) return false; if (file instanceof PsiCompiledElement) return false; final FileType fileType = file.getFileType(); if (fileType == StdFileTypes.GUI_DESIGNER_FORM) { return true; } // To enable T.O.D.O. highlighting return !fileType.isBinary(); }
@Nullable public static Icon getIcon(@NotNull ErlangFile file) { if (!file.isValid()) return null; VirtualFile virtualFile = file.getViewProvider().getVirtualFile(); FileType fileType = virtualFile.getFileType(); if (ErlangFileType.MODULE == fileType) { ErlangModule module = file.getModule(); boolean isEunit = module != null && ErlangPsiImplUtil.isEunitTestFile(file); return isEunit ? ErlangIcons.EUNIT : getModuleType(file).icon; } return fileType.getIcon(); }
private void registerAdditionalIndentOptions(FileType fileType, IndentOptions options) { boolean exist = false; for (final FileType existing : myAdditionalIndentOptions.keySet()) { if (Comparing.strEqual(existing.getDefaultExtension(), fileType.getDefaultExtension())) { exist = true; break; } } if (!exist) { myAdditionalIndentOptions.put(fileType, options); } }
@Override public void writeExternal(Element element) throws WriteExternalException { final CodeStyleSettings parentSettings = new CodeStyleSettings(); DefaultJDOMExternalizer.writeExternal( this, element, new DifferenceFilter<CodeStyleSettings>(this, parentSettings)); List<CustomCodeStyleSettings> customSettings = new ArrayList<CustomCodeStyleSettings>(getCustomSettingsValues()); Collections.sort( customSettings, new Comparator<CustomCodeStyleSettings>() { @Override public int compare(final CustomCodeStyleSettings o1, final CustomCodeStyleSettings o2) { return o1.getTagName().compareTo(o2.getTagName()); } }); for (final CustomCodeStyleSettings settings : customSettings) { final CustomCodeStyleSettings parentCustomSettings = parentSettings.getCustomSettings(settings.getClass()); if (parentCustomSettings == null) { throw new WriteExternalException("Custom settings are null for " + settings.getClass()); } settings.writeExternal(element, parentCustomSettings); } final FileType[] fileTypes = myAdditionalIndentOptions .keySet() .toArray(new FileType[myAdditionalIndentOptions.keySet().size()]); Arrays.sort( fileTypes, new Comparator<FileType>() { @Override public int compare(final FileType o1, final FileType o2) { return o1.getDefaultExtension().compareTo(o2.getDefaultExtension()); } }); for (FileType fileType : fileTypes) { final IndentOptions indentOptions = myAdditionalIndentOptions.get(fileType); Element additionalIndentOptions = new Element(ADDITIONAL_INDENT_OPTIONS); indentOptions.serialize(additionalIndentOptions, getDefaultIndentOptions(fileType)); additionalIndentOptions.setAttribute(FILETYPE, fileType.getDefaultExtension()); if (!additionalIndentOptions.getChildren().isEmpty()) { element.addContent(additionalIndentOptions); } } myCommonSettingsManager.writeExternal(element); }
public static PsiElement[] parsePattern( Project project, String context, String pattern, FileType fileType, Language language, String extension, boolean physical) { int offset = context.indexOf(PATTERN_PLACEHOLDER); final int patternLength = pattern.length(); final String patternInContext = context.replace(PATTERN_PLACEHOLDER, pattern); final String ext = extension != null ? extension : fileType.getDefaultExtension(); final String name = "__dummy." + ext; final PsiFileFactory factory = PsiFileFactory.getInstance(project); final PsiFile file = language == null ? factory.createFileFromText( name, fileType, patternInContext, LocalTimeCounter.currentTime(), physical, true) : factory.createFileFromText(name, language, patternInContext, physical, true); if (file == null) { return PsiElement.EMPTY_ARRAY; } final List<PsiElement> result = new ArrayList<PsiElement>(); PsiElement element = file.findElementAt(offset); if (element == null) { return PsiElement.EMPTY_ARRAY; } PsiElement topElement = element; element = element.getParent(); while (element != null) { if (element.getTextRange().getStartOffset() == offset && element.getTextLength() <= patternLength) { topElement = element; } element = element.getParent(); } if (topElement instanceof PsiFile) { return topElement.getChildren(); } final int endOffset = offset + patternLength; result.add(topElement); topElement = topElement.getNextSibling(); while (topElement != null && topElement.getTextRange().getEndOffset() <= endOffset) { result.add(topElement); topElement = topElement.getNextSibling(); } return result.toArray(new PsiElement[result.size()]); }
public static boolean isEnabled(VirtualFile virtualFile) { boolean enabled = true; if (virtualFile != null) { // FileMode mode = modeFromFile(virtualFile); /*if (mode != null) { enabled = false; } else*/ if (!virtualFile.isDirectory()) { FileType fileType = FileTypeManager.getInstance().getFileTypeByFile(virtualFile); if (StringUtil.isNotEmpty(virtualFile.getExtension()) && fileType.isBinary()) { enabled = false; } } } return enabled; }
private void copyOldIndentOptions(@NonNls final String extension, final IndentOptions options) { final FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(extension); if (fileType != FileTypes.UNKNOWN && fileType != FileTypes.PLAIN_TEXT && !myAdditionalIndentOptions.containsKey(fileType) && !fileType.getDefaultExtension().isEmpty()) { registerAdditionalIndentOptions(fileType, options); // // Upgrade to version 11 // if (fileType instanceof LanguageFileType) { Language lang = ((LanguageFileType) fileType).getLanguage(); CommonCodeStyleSettings langSettings = myCommonSettingsManager.getCommonSettings(lang); if (langSettings != this && langSettings.getIndentOptions() != null) { langSettings.importOldIndentOptions(this); } } } }
@Nullable public static CompletionData getCompletionDataByFileType(FileType fileType) { for (CompletionDataEP ep : Extensions.getExtensions(CompletionDataEP.EP_NAME)) { if (ep.fileType.equals(fileType.getName())) { return ep.getHandler(); } } final NotNullLazyValue<CompletionData> lazyValue = ourCustomCompletionDatas.get(fileType); return lazyValue == null ? null : lazyValue.getValue(); }
@NotNull @Override public CharSequence getContentAsText() { if (myFileType.isBinary()) { throw new IllegalDataException( "Cannot obtain text for binary file type : " + myFileType.getDescription()); } final CharSequence content = getUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY); if (content != null) { return content; } if (myContentAsText == null) { if (myContent != null) { myContentAsText = LoadTextUtil.getTextByBinaryPresentation(myContent, myCharset); myContent = null; // help gc, indices are expected to use bytes or chars but not both } } return myContentAsText; }
@NotNull @Override public CellAppearanceEx forIoFile(@NotNull final File file) { final String absolutePath = file.getAbsolutePath(); if (!file.exists()) { return forInvalidUrl(absolutePath); } if (file.isDirectory()) { return SimpleTextCellAppearance.regular(absolutePath, PlatformIcons.FOLDER_ICON); } final String name = file.getName(); final FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(name); final File parent = file.getParentFile(); final CompositeAppearance appearance = CompositeAppearance.textComment(name, parent.getAbsolutePath()); appearance.setIcon(fileType.getIcon()); return appearance; }
public static boolean canHaveStub(@NotNull VirtualFile file) { final FileType fileType = file.getFileType(); if (fileType instanceof LanguageFileType) { Language l = ((LanguageFileType) fileType).getLanguage(); ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l); if (parserDefinition == null) return false; final IFileElementType elementType = parserDefinition.getFileNodeType(); return elementType instanceof IStubFileElementType && (((IStubFileElementType) elementType).shouldBuildStubFor(file) || IndexingStamp.isFileIndexed( file, INDEX_ID, IndexInfrastructure.getIndexCreationStamp(INDEX_ID))); } if (fileType.isBinary()) { final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); return builder != null && builder.acceptsFile(file); } return false; }
private static int getCumulativeVersion() { int version = VERSION; for (final FileType fileType : FileTypeManager.getInstance().getRegisteredFileTypes()) { if (fileType instanceof LanguageFileType) { Language l = ((LanguageFileType) fileType).getLanguage(); ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l); if (parserDefinition != null) { final IFileElementType type = parserDefinition.getFileNodeType(); if (type instanceof IStubFileElementType) { version += ((IStubFileElementType) type).getStubVersion(); } } } else if (fileType.isBinary()) { final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType); if (builder != null) { version += builder.getStubVersion(); } } } return version; }
private PsiFile createFile() { try { final VirtualFile vFile = getVirtualFile(); if (vFile.isDirectory()) return null; if (isIgnored()) return null; final Project project = myManager.getProject(); if (isPhysical()) { // check directories consistency final VirtualFile parent = vFile.getParent(); if (parent == null) return null; final PsiDirectory psiDir = getManager().findDirectory(parent); if (psiDir == null) return null; } FileType fileType = vFile.getFileType(); PsiFile file = null; if (fileType.isBinary() || vFile.isSpecialFile()) { // TODO check why ClsFileImpl doesn't created automatically with create method file = new ClsFileImpl((PsiManagerImpl) getManager(), this); // file = new PsiBinaryFileImpl((PsiManagerImpl) getManager(), this); } else { if (!isTooLarge(vFile)) { final PsiFile psiFile = createFile(getBaseLanguage()); if (psiFile != null) file = psiFile; } else { file = new PsiPlainTextFileImpl(this); } } return file; } catch (ProcessCanceledException e) { e.printStackTrace(); throw e; } catch (Throwable e) { e.printStackTrace(); LOG.error(e); return null; } }
private static Document createDocument( String value, @Nullable Language language, Project project, @NotNull SimpleDocumentCreator documentCreator) { if (language != null) { final PsiFileFactory factory = PsiFileFactory.getInstance(project); final FileType fileType = language.getAssociatedFileType(); assert fileType != null; final long stamp = LocalTimeCounter.currentTime(); final PsiFile psiFile = factory.createFileFromText( "Dummy." + fileType.getDefaultExtension(), fileType, value, stamp, true, false); documentCreator.customizePsiFile(psiFile); final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile); assert document != null; return document; } else { return EditorFactory.getInstance().createDocument(value); } }
@Nullable protected PsiFile createFile( @NotNull Project project, @NotNull VirtualFile file, @NotNull FileType fileType) { if (fileType.isBinary() || file.isSpecialFile()) { return new PsiBinaryFileImpl((PsiManagerImpl) getManager(), this); } if (!isTooLarge(file)) { final PsiFile psiFile = createFile(getBaseLanguage()); if (psiFile != null) return psiFile; } return new PsiPlainTextFileImpl(this); }
@Override @Nullable public WordsScanner getCacheBuilder(@NotNull FileType fileType) { final WordsScanner scanner = myMap.get(fileType); if (scanner != null) { return scanner; } for (CacheBuilderEP ep : Extensions.getExtensions(CacheBuilderEP.EP_NAME)) { if (ep.getFileType().equals(fileType.getName())) { return ep.getWordsScanner(); } } return null; }
/** {@inheritDoc} */ @Override public int compare(final FileType o1, final FileType o2) { if (o1 == o2) { return 0; } if (o1 == FileTypes.UNKNOWN) { return 1; } if (o2 == FileTypes.UNKNOWN) { return -1; } if (o1.isBinary() && !o2.isBinary()) { return 1; } if (!o1.isBinary() && o2.isBinary()) { return -1; } return o1.getName().compareToIgnoreCase(o2.getName()); }
public void initComponent() { // auto-associate .awl files as xml if not otherwise mapped FileType awlType = FileTypeManager.getInstance().getFileTypeByExtension("awl"); if (awlType.getName().endsWith("UnknownFileType") || awlType.getName().equals("UNKNOWN")) { FileType htmType = FileTypeManager.getInstance().getFileTypeByExtension("xml"); FileTypeManager.getInstance().registerFileType(htmType, new String[] {"awl"}); } // auto-associate .oss files as text if not otherwise mapped FileType ossType = FileTypeManager.getInstance().getFileTypeByExtension("oss"); if (ossType.getName().endsWith("UnknownFileType") || ossType.getName().equals("UNKNOWN")) { FileType txtType = FileTypeManager.getInstance().getFileTypeByExtension("txt"); FileTypeManager.getInstance().registerFileType(txtType, new String[] {"oss"}); } // Load our bundled live templates InputStream is = getClass().getResourceAsStream("/ariba/ideplugin/idea/livetemplates.xml"); if (is != null) { loadTemplates(is, "AribaWeb"); } }
@Override public String getCharset(@NotNull VirtualFile file, byte[] content) { return fileType.getCharset(file, content); }
@Override public Icon getIcon() { return fileType.getIcon(); }
@NotNull @Override public String getDefaultExtension() { return fileType.getDefaultExtension(); }
@NotNull @Override public String getDescription() { return fileType.getDescription(); }
@NotNull @Override public String getName() { return fileType.getName(); }
public static boolean isTextType(FileType fileType) { return fileType != null && !fileType.isBinary(); }
private static boolean isBinaryWithoutDecompiler(VirtualFile file) { final FileType ft = file.getFileType(); return ft.isBinary() && BinaryFileTypeDecompilers.INSTANCE.forFileType(ft) == null; }