use of com.intellij.ide.highlighter.ModuleFileType in project intellij-community by JetBrains.
the class FileTypesTest method testReDetectOnContentsChange.
public void testReDetectOnContentsChange() throws IOException {
final FileTypeRegistry fileTypeManager = FileTypeRegistry.getInstance();
assertTrue(fileTypeManager.getClass().getName(), fileTypeManager instanceof FileTypeManagerImpl);
FileType fileType = fileTypeManager.getFileTypeByFileName("x" + ModuleFileType.DOT_DEFAULT_EXTENSION);
assertTrue(fileType.toString(), fileType instanceof ModuleFileType);
fileType = fileTypeManager.getFileTypeByFileName("x" + ProjectFileType.DOT_DEFAULT_EXTENSION);
assertTrue(fileType.toString(), fileType instanceof ProjectFileType);
FileType module = fileTypeManager.findFileTypeByName("IDEA_MODULE");
assertNotNull(module);
assertFalse(module.equals(PlainTextFileType.INSTANCE));
FileType project = fileTypeManager.findFileTypeByName("IDEA_PROJECT");
assertNotNull(project);
assertFalse(project.equals(PlainTextFileType.INSTANCE));
final Set<VirtualFile> detectorCalled = ContainerUtil.newConcurrentSet();
FileTypeRegistry.FileTypeDetector detector = new FileTypeRegistry.FileTypeDetector() {
@Nullable
@Override
public FileType detect(@NotNull VirtualFile file, @NotNull ByteSequence firstBytes, @Nullable CharSequence firstCharsIfText) {
detectorCalled.add(file);
String text = firstCharsIfText.toString();
FileType result = text.startsWith("TYPE:") ? fileTypeManager.findFileTypeByName(StringUtil.trimStart(text, "TYPE:")) : null;
log("T: my detector run for " + file.getName() + "; result: " + (result == null ? null : result.getName()));
return result;
}
@Override
public int getVersion() {
return 0;
}
};
Extensions.getRootArea().getExtensionPoint(FileTypeRegistry.FileTypeDetector.EP_NAME).registerExtension(detector);
myFileTypeManager.toLog = true;
try {
log("T: ------ akjdhfksdjgf");
File f = createTempFile("xx.asfdasdfas", "akjdhfksdjgf");
VirtualFile vFile = getVirtualFile(f);
ensureRedetected(vFile, detectorCalled);
assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof PlainTextFileType);
log("T: ------ TYPE:IDEA_MODULE");
setFileText(vFile, "TYPE:IDEA_MODULE");
ensureRedetected(vFile, detectorCalled);
assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof ModuleFileType);
log("T: ------ TYPE:IDEA_PROJECT");
setFileText(vFile, "TYPE:IDEA_PROJECT");
ensureRedetected(vFile, detectorCalled);
assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof ProjectFileType);
log("T: ------");
} finally {
Extensions.getRootArea().getExtensionPoint(FileTypeRegistry.FileTypeDetector.EP_NAME).unregisterExtension(detector);
myFileTypeManager.toLog = false;
}
}
Aggregations