diff --git a/platform/platform-impl/src/com/intellij/openapi/fileTypes/impl/FileTypeDetectionService.java b/platform/platform-impl/src/com/intellij/openapi/fileTypes/impl/FileTypeDetectionService.java index f756f6747e49..241f45e9449a 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileTypes/impl/FileTypeDetectionService.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileTypes/impl/FileTypeDetectionService.java @@ -18,7 +18,6 @@ import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Pair; -import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.io.ByteArraySequence; import com.intellij.openapi.util.io.ByteSequence; import com.intellij.openapi.util.io.FileUtil; @@ -107,39 +106,31 @@ final class FileTypeDetectionService implements Disposable { public @Nullable ChangeApplier prepareChange(@NotNull List events) { Collection files = ContainerUtil.map2Set(events, event -> { ProgressManager.checkCanceled(); - VirtualFile file = - event instanceof VFileCreateEvent /* avoid expensive find child here */ || isReparseEvent(event) ? null : event.getFile(); + VirtualFile file = event instanceof VFileCreateEvent /* avoid expensive find child here */ || isReparseEvent(event) ? null : event.getFile(); VirtualFile filtered = file != null && wasAutoDetectedBefore(file) && isDetectable(file) ? file : null; - myFileTypeManager.log(() -> "after() VFS event " + - event + - "; filtered file: " + - filtered + - " (file: " + - file + - "; wasAutoDetectedBefore(file): " + - (file == null ? null : wasAutoDetectedBefore(file)) + - "; isDetectable(file): " + - (file == null ? null : isDetectable(file)) + - "; file.getLength(): " + - (file == null ? null : file.getLength()) + - "; file.isValid(): " + - (file == null ? null : file.isValid()) + - "; file.is(VFileProperty.SPECIAL): " + - (file == null ? null : file.is(VFileProperty.SPECIAL)) + - "; packedFlags.get(id): " + - (file instanceof VirtualFileWithId - ? readableFlags(packedFlags.get(((VirtualFileWithId)file).getId())) - : null) + - "; file.getFileSystem():" + - (file == null ? null : file.getFileSystem()) + - ")"); + if (toLog()) { + log("F: after() VFS event " + event + + "; filtered file: " + filtered + + " (file: " + file + + "; wasAutoDetectedBefore(file): " + (file == null ? null : wasAutoDetectedBefore(file)) + + "; isDetectable(file): " + (file == null ? null : isDetectable(file)) + + "; file.getLength(): " + (file == null ? null : file.getLength()) + + "; file.isValid(): " + (file == null ? null : file.isValid()) + + "; file.is(VFileProperty.SPECIAL): " + (file == null ? null : file.is(VFileProperty.SPECIAL)) + + "; packedFlags.get(id): " + (file instanceof VirtualFileWithId ? readableFlags(packedFlags.get(((VirtualFileWithId)file).getId())) : null) + + "; file.getFileSystem():" + (file == null ? null : file.getFileSystem()) + ")"); + } return filtered; }); files.remove(null); - myFileTypeManager.log("after() VFS events: " + events + "; files: " + files); + if (toLog()) { + log("F: after() VFS events: " + events+"; files: "+files); + } ProgressManager.checkCanceled(); if (!files.isEmpty() && RE_DETECT_ASYNC) { - myFileTypeManager.log("after() queued to redetect: " + files); + if (toLog()) { + log("F: after() queued to redetect: " + files); + } for (VirtualFile file : files) { finishRedetectionIfEnqueued(file); @@ -191,6 +182,14 @@ final class FileTypeDetectionService implements Disposable { Disposer.register(app, this); } + private boolean toLog() { + return myFileTypeManager.toLog(); + } + + private void log(String s) { + myFileTypeManager.log(s); + } + @NotNull FileType getOrDetectFromContent(@NotNull VirtualFile file, byte @Nullable [] content) { if (!isDetectable(file)) { @@ -225,27 +224,29 @@ final class FileTypeDetectionService implements Disposable { flags = BitUtil.set(flags, ATTRIBUTES_WERE_LOADED_MASK, true); packedFlags.set(id, flags); - myFileTypeManager.log("getOrDetectFromContent(" + file.getName() + "): readFlagsFromCache() = " + readableFlags(flags)); + if (toLog()) { + log("F: getOrDetectFromContent(" + file.getName() + "): readFlagsFromCache() = " + readableFlags(flags)); + } } boolean autoDetectWasRun = BitUtil.isSet(flags, AUTO_DETECT_WAS_RUN_MASK); if (autoDetectWasRun) { FileType type = textOrBinaryFromCachedFlags(flags); - myFileTypeManager.log("getOrDetectFromContent(" + - file.getName() + - "): cached type = " + - (type == null ? null : type.getName()) + - "; packedFlags.get(id):" + - readableFlags(flags) + - "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): " + - file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY)); + if (toLog()) { + log("F: getOrDetectFromContent("+file.getName()+"):" + + " cached type = "+(type==null?null:type.getName())+ + "; packedFlags.get(id):"+ readableFlags(flags)+ + "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): "+file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY)); + } if (type != null) { return type; } } } FileType fileType = getFileTypeDetectedFromContent(file); - myFileTypeManager.log("getOrDetectFromContent(" + file.getName() + "): " + - "getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY) = " + (fileType == null ? null : fileType.getName())); + if (toLog()) { + log("F: getOrDetectFromContent("+file.getName()+"): " + + "getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY) = "+(fileType == null ? null : fileType.getName())); + } if (fileType == null) { // run autodetection try { @@ -256,7 +257,10 @@ final class FileTypeDetectionService implements Disposable { } } - myFileTypeManager.log("getOrDetectFromContent(" + file.getName() + "): getFileType after detect run = " + fileType.getName()); + if (toLog()) { + log("F: getOrDetectFromContent("+file.getName()+"): getFileType after detect run = "+fileType.getName()); + } + return fileType; } @@ -277,7 +281,9 @@ final class FileTypeDetectionService implements Disposable { void clearCaches() { packedFlags.clear(); clearPersistentAttributes(); - myFileTypeManager.log("clearCaches()"); + if (toLog()) { + log("F: clearCaches()"); + } } private void onDetectorsChange() { @@ -338,7 +344,9 @@ final class FileTypeDetectionService implements Disposable { int count = fileTypeChangedCount.incrementAndGet(); autoDetectedAttribute = autoDetectedAttribute.newVersion(count); PropertiesComponent.getInstance().setValue(FILE_TYPE_CHANGED_COUNTER_PROPERTY, Integer.toString(count)); - myFileTypeManager.log("clearPersistentAttributes()"); + if (toLog()) { + log("F: clearPersistentAttributes()"); + } } private void cacheAutoDetectedFileType(@NotNull VirtualFile file, @NotNull FileType fileType) { @@ -356,22 +364,22 @@ final class FileTypeDetectionService implements Disposable { if (wasAutodetectedAsText || wasAutodetectedAsBinary) { file.putUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY, null); - myFileTypeManager.log("cacheAutoDetectedFileType(" + - file.getName() + - ") cached to " + - fileType.getName() + - " flags = " + - readableFlags(flags) + - "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): " + - file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY)); + if (toLog()) { + log("F: cacheAutoDetectedFileType("+file.getName()+") " + + "cached to " + fileType.getName() + + " flags = "+ readableFlags(flags)+ + "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): "+file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY)); + } return; } } file.putUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY, fileType.getName()); - myFileTypeManager.log("cacheAutoDetectedFileType(" + file.getName() + ") " + - "cached to " + fileType.getName() + - " flags = " + readableFlags(flags) + - "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): " + file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY)); + if (toLog()) { + log("F: cacheAutoDetectedFileType("+file.getName()+") " + + "cached to " + fileType.getName() + + " flags = "+ readableFlags(flags)+ + "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): "+file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY)); + } } private void awakeReDetectExecutor() { @@ -421,7 +429,9 @@ final class FileTypeDetectionService implements Disposable { List changed = new ArrayList<>(); List crashed = new ArrayList<>(); for (VirtualFile file : files) { - myFileTypeManager.log("reDetect(" + file.getName() + ") " + file.getName()); + if (toLog()) { + log("F: reDetect(" + file.getName() + ") " + file.getName()); + } int id = ((VirtualFileWithId)file).getId(); long flags = packedFlags.get(id); @@ -429,10 +439,11 @@ final class FileTypeDetectionService implements Disposable { ObjectUtils.notNull(getFileTypeDetectedFromContent(file), PlainTextFileType.INSTANCE)); FileType after = myFileTypeManager.getByFile(file); - myFileTypeManager.log("reDetect(" + file.getName() + - ") prepare to redetect. flags: " + readableFlags(flags) + - "; beforeType: " + before.getName() + - "; afterByFileType: " + (after == null ? null : after.getName())); + + if (toLog()) { + log("F: reDetect(" + file.getName() + ") prepare to redetect. flags: " + readableFlags(flags) + + "; beforeType: " + before.getName() + "; afterByFileType: " + (after == null ? null : after.getName())); + } if (after == null || FileTypeManagerImpl.mightBeReplacedByDetectedFileType(after)) { try { @@ -440,16 +451,11 @@ final class FileTypeDetectionService implements Disposable { } catch (IOException e) { crashed.add(file); - myFileTypeManager.log("reDetect(" + - file.getName() + - ") before: " + - before.getName() + - "; after: crashed with " + - e.getMessage() + - "; now getFileType()=" + - file.getFileType().getName() + - "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): " + - file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY)); + if (toLog()) { + log("F: reDetect(" + file.getName() + ") " + "before: " + before.getName() + "; after: crashed with " + e.getMessage() + + "; now getFileType()=" + file.getFileType().getName() + + "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): " + file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY)); + } continue; } } @@ -460,16 +466,11 @@ final class FileTypeDetectionService implements Disposable { flags = 0; packedFlags.set(id, flags); } - myFileTypeManager.log("reDetect(" + - file.getName() + - ") before: " + - before.getName() + - "; after: " + - after.getName() + - "; now getFileType()=" + - file.getFileType().getName() + - "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): " + - file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY)); + if (toLog()) { + log("F: reDetect(" + file.getName() + ") before: " + before.getName() + "; after: " + after.getName() + + "; now getFileType()=" + file.getFileType().getName() + "; getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY): " + + file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY)); + } if (before != after) { changed.add(file); @@ -515,44 +516,19 @@ final class FileTypeDetectionService implements Disposable { } private int readSafely(@NotNull InputStream stream, byte @NotNull [] buffer, int length) throws IOException { - int read1 = stream.read(buffer, 0, length); - if (read1 > 0) { - return read1; + int n = stream.read(buffer, 0, length); + if (n <= 0) { + // maybe locked because someone else is writing to it + // repeat inside read action to guarantee all writes are finished + if (toLog()) { + log("F: processFirstBytes(): inputStream.read() returned "+n+"; retrying with read action. stream="+ streamInfo(stream)); + } + n = stream.read(buffer, 0, length); + if (toLog()) { + log("F: processFirstBytes(): under read action inputStream.read() returned "+n+"; stream="+ streamInfo(stream)); + } } - - Ref ioExceptionRef = Ref.create(); - // maybe locked because someone else is writing to it - // repeat inside read action to guarantee all writes are finished - myFileTypeManager.log(() -> { - try { - return "processFirstBytes(): inputStream.read() returned " + read1 + - "; retrying with read action. stream=" + streamInfo(stream); - } - catch (IOException e) { - ioExceptionRef.set(e); - return null; - } - }); - if (!ioExceptionRef.isNull()) { - throw ioExceptionRef.get(); - } - - int read2 = stream.read(buffer, 0, length); - myFileTypeManager.log(() -> { - try { - return "processFirstBytes(): under read action inputStream.read() returned " + read2 + - "; stream=" + streamInfo(stream); - } - catch (IOException e) { - ioExceptionRef.set(e); - return null; - } - }); - if (!ioExceptionRef.isNull()) { - throw ioExceptionRef.get(); - } - - return read2; + return n; } @NotNull @@ -560,24 +536,15 @@ final class FileTypeDetectionService implements Disposable { List detectors = FileTypeRegistry.FileTypeDetector.EP_NAME.getExtensionList(); FileType fileType = detect(file, bytes, detectors); - Ref ioExceptionRef = Ref.create(); - myFileTypeManager.log(() -> { + if (toLog()) { try (InputStream newStream = ((FileSystemInterface)file.getFileSystem()).getInputStream(file)) { - byte[] buffer = new byte[50]; - int read = newStream.read(buffer, 0, buffer.length); - return "detectFromContentAndCache(" + file.getName() + - "): result: " + fileType.getName() + - "; newStream: " + streamInfo(newStream) + - "; read: " + read + - "; buffer: " + Arrays.toString(buffer); + byte[] buffer2 = new byte[50]; + int n2 = newStream.read(buffer2, 0, buffer2.length); + log("F: detectFromContentAndCache(" + file.getName() + "): result: " + fileType.getName() + + "; newStream: " + streamInfo(newStream) + + "; read: " + n2 + + "; buffer: " + Arrays.toString(buffer2)); } - catch (IOException e) { - ioExceptionRef.set(e); - return null; - } - }); - if (!ioExceptionRef.isNull()) { - throw ioExceptionRef.get(); } if (LOG.isDebugEnabled()) { @@ -600,19 +567,9 @@ final class FileTypeDetectionService implements Disposable { @NotNull private ByteArraySequence readFirstBytesFromFile(VirtualFile file, Integer bufferLength) throws IOException { try (InputStream inputStream = ((FileSystemInterface)file.getFileSystem()).getInputStream(file)) { - Ref ioExceptionRef = Ref.create(); - myFileTypeManager.log(() -> { - try { - return "detectFromContentAndCache(" + file.getName() + "): inputStream=" + streamInfo(inputStream); - } - catch (IOException e) { - return null; - } - }); - if (!ioExceptionRef.isNull()) { - throw ioExceptionRef.get(); + if (toLog()) { + log("F: detectFromContentAndCache(" + file.getName() + "):" + " inputStream=" + streamInfo(inputStream)); } - int fileLength = (int)Math.min(file.getLength(), Integer.MAX_VALUE); byte[] content = new byte[Math.min(fileLength, bufferLength)]; int n = readSafely(inputStream, content, content.length); @@ -650,11 +607,11 @@ final class FileTypeDetectionService implements Disposable { return LoadTextUtil.processTextFromBinaryPresentationOrNull(firstBytes, file, true, true, PlainTextFileType.INSTANCE, (@Nullable CharSequence text) -> { - myFileTypeManager.log("detectFromContentAndCache.processFirstBytes(" + file.getName() + - "): bytes length=" + firstBytes.length() + - "; isText=" + (text != null) + - "; text='" + (text == null ? null : StringUtil.first(text, 100, true)) + - "', detectors=" + detectors); + if (toLog()) { + log("F: detectFromContentAndCache.processFirstBytes(" + file.getName() + "): bytes length=" + firstBytes.length() + + "; isText=" + (text != null) + "; text='" + (text == null ? null : StringUtil.first(text, 100, true)) + "'" + + ", detectors=" + detectors); + } FileType detected = null; for (FileTypeRegistry.FileTypeDetector detector : detectors) { try { @@ -667,9 +624,9 @@ final class FileTypeDetectionService implements Disposable { LOG.error("Detector " + detector + " (" + detector.getClass() + ") exception occurred:", e); } if (detected != null) { - myFileTypeManager.log("detectFromContentAndCache.processFirstBytes(" + file.getName() + - "): detector " + detector + - " type as " + detected.getName()); + if (toLog()) { + log("F: detectFromContentAndCache.processFirstBytes(" + file.getName() + "): detector " + detector + " type as " + detected.getName()); + } break; } } @@ -680,8 +637,10 @@ final class FileTypeDetectionService implements Disposable { } if (detected == null) { detected = text == null ? UnknownFileType.INSTANCE : PlainTextFileType.INSTANCE; - myFileTypeManager.log("detectFromContentAndCache.processFirstBytes(" + file.getName() + "): " + - "no detector was able to detect. assigned " + detected.getName()); + if (toLog()) { + log("F: detectFromContentAndCache.processFirstBytes(" + file.getName() + "): " + + "no detector was able to detect. assigned " + detected.getName()); + } } return detected; }); diff --git a/platform/platform-impl/src/com/intellij/openapi/fileTypes/impl/FileTypeManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/fileTypes/impl/FileTypeManagerImpl.java index ec04424436db..61f1f2db2851 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileTypes/impl/FileTypeManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileTypes/impl/FileTypeManagerImpl.java @@ -55,7 +55,6 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.function.Function; -import java.util.function.Supplier; @State(name = "FileTypeManager", storages = @Storage("filetypes.xml"), additionalExportDirectory = FileTypeManagerImpl.FILE_SPEC) public class FileTypeManagerImpl extends FileTypeManagerEx implements PersistentStateComponent { @@ -529,36 +528,13 @@ public class FileTypeManagerImpl extends FileTypeManagerEx implements Persistent } } - private boolean isLoggingEnabled; - - void log(@NonNls String message) { - if (isLoggingEnabled) { - logDetailed(message); - } - } - - void log(@NotNull Supplier<@Nullable String> lazyMessage) { - if (isLoggingEnabled) { - String message = lazyMessage.get(); - if (message != null) { - logDetailed(message); - } - } - } - - private static void logDetailed(@NonNls String message) { - LOG.debug("F:" + message + " - " + Thread.currentThread()); - } - @TestOnly - void runAndLog(@NotNull ThrowableRunnable runnable) throws T { - isLoggingEnabled = true; - try { - runnable.run(); - } - finally { - isLoggingEnabled = false; - } + boolean toLog; + boolean toLog() { + return toLog; + } + void log(@NonNls String message) { + LOG.debug(message + " - " + Thread.currentThread()); } @TestOnly @@ -645,7 +621,9 @@ public class FileTypeManagerImpl extends FileTypeManagerEx implements Persistent FileType fileType = file.isDirectory() ? null : file.getFileType(); Pair old = FILE_TYPE_FIXED_TEMPORARILY.get(); FILE_TYPE_FIXED_TEMPORARILY.set(new Pair<>(file, fileType)); - log("freezeFileTypeTemporarilyIn(" + file.getName() + ") to " + fileType); + if (toLog()) { + log("F: freezeFileTypeTemporarilyIn(" + file.getName() + ") to " + fileType +" in "+Thread.currentThread()); + } try { runnable.run(); } @@ -656,7 +634,9 @@ public class FileTypeManagerImpl extends FileTypeManagerEx implements Persistent else { FILE_TYPE_FIXED_TEMPORARILY.set(old); } - log("unfreezeFileType(" + file.getName() + ")"); + if (toLog()) { + log("F: unfreezeFileType(" + file.getName() + ") in "+Thread.currentThread()); + } } } @@ -702,7 +682,9 @@ public class FileTypeManagerImpl extends FileTypeManagerEx implements Persistent Pair fixedType = FILE_TYPE_FIXED_TEMPORARILY.get(); if (fixedType != null && fixedType.getFirst().equals(file)) { FileType fileType = fixedType.getSecond(); - log("getByFile(" + file.getName() + ") was frozen to " + fileType.getName()); + if (toLog()) { + log("F: getByFile(" + file.getName() + ") was frozen to " + fileType.getName()+" in "+Thread.currentThread()); + } return fileType; } @@ -715,7 +697,9 @@ public class FileTypeManagerImpl extends FileTypeManagerEx implements Persistent for (FileTypeIdentifiableByVirtualFile specialType : mySpecialFileTypes) { if (specialType.isMyFileType(file)) { - log("getByFile(" + file.getName() + "): Special file type: " + specialType.getName()); + if (toLog()) { + log("getByFile(" + file.getName() + "): Special file type: " + specialType.getName()); + } boolean willBeRedetectedAnyway = mightBeReplacedByDetectedFileType(specialType) && FileTypeDetectionService.isDetectable(file); if (willBeRedetectedAnyway) { LOG.error("File type '"+specialType +"' is inconsistent. " + @@ -730,7 +714,9 @@ public class FileTypeManagerImpl extends FileTypeManagerEx implements Persistent if (fileType == UnknownFileType.INSTANCE || fileType == DetectedByContentFileType.INSTANCE) { fileType = null; } - log("getByFile(" + file.getName() + ") By name file type: " + (fileType == null ? null : fileType.getName())); + if (toLog()) { + log("F: getByFile(" + file.getName() + ") By name file type: "+(fileType == null ? null : fileType.getName())); + } return fileType; } diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/fileTypes/impl/FileTypesTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/fileTypes/impl/FileTypesTest.java index da02e3fdbaf6..0e5e35f0d07d 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/fileTypes/impl/FileTypesTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/fileTypes/impl/FileTypesTest.java @@ -324,9 +324,7 @@ public class FileTypesTest extends HeavyPlatformTestCase { FileType result = text != null && text.startsWith("TYPE:") ? myFileTypeManager.findFileTypeByName(StringUtil.trimStart(text, "TYPE:")) : null; - LOG.debug("T: my detector run for " + file.getName() + - "; result: " + (result == null ? null : result.getName()) + - " (text=" + text + ")"); + log("T: my detector run for "+file.getName()+"; result: "+(result == null ? null : result.getName())+" (text="+text+")"); return result; } @@ -336,45 +334,56 @@ public class FileTypesTest extends HeavyPlatformTestCase { } }; runWithDetector(detector, () -> { - LOG.debug("T: ------ akjdhfksdjgf"); + 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.debug("T: ------ TYPE:IDEA_MODULE"); - setFileText(vFile, "TYPE:IDEA_MODULE"); + log("T: ------ TYPE:IDEA_MODULE"); + setFileText(vFile, "TYPE:IDEA_MODULE"); ensureRedetected(vFile, detectorCalled); assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof ModuleFileType); - LOG.debug("T: ------ TYPE:IDEA_PROJECT"); + log("T: ------ TYPE:IDEA_PROJECT"); setFileText(vFile, "TYPE:IDEA_PROJECT"); ensureRedetected(vFile, detectorCalled); assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof ProjectFileType); - LOG.debug("T: ------"); + log("T: ------"); }); } - private void runWithDetector(@NotNull FileTypeRegistry.FileTypeDetector detector, - @NotNull ThrowableRunnable runnable) throws T { + private void runWithDetector(@NotNull FileTypeRegistry.@NotNull FileTypeDetector detector, @NotNull ThrowableRunnable runnable) throws T { FileTypeRegistry.FileTypeDetector.EP_NAME.getPoint().registerExtension(detector, getTestRootDisposable()); - myFileTypeManager.runAndLog(runnable); + FileTypeManagerImpl fileTypeManager = (FileTypeManagerImpl)FileTypeManager.getInstance(); + fileTypeManager.toLog = true; + try { + runnable.run(); + } + finally { + fileTypeManager.toLog = false; + } + } + + private static void log(String message) { + LOG.debug(message); + //System.out.println(message); } private void ensureRedetected(VirtualFile vFile, Set detectorCalled) { PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); - LOG.debug("T: ensureRedetected: commit. re-detect queue: " + myFileTypeManager.dumpReDetectQueue()); + log("T: ensureRedetected: commit. re-detect queue: "+myFileTypeManager.dumpReDetectQueue()); UIUtil.dispatchAllInvocationEvents(); - LOG.debug("T: ensureRedetected: dispatch. re-detect queue: " + myFileTypeManager.dumpReDetectQueue()); + log("T: ensureRedetected: dispatch. re-detect queue: "+ myFileTypeManager.dumpReDetectQueue()); myFileTypeManager.drainReDetectQueue(); - LOG.debug("T: ensureRedetected: drain. re-detect queue: " + myFileTypeManager.dumpReDetectQueue()); + log("T: ensureRedetected: drain. re-detect queue: "+myFileTypeManager.dumpReDetectQueue()); UIUtil.dispatchAllInvocationEvents(); - LOG.debug("T: ensureRedetected: dispatch. re-detect queue: " + myFileTypeManager.dumpReDetectQueue()); + log("T: ensureRedetected: dispatch. re-detect queue: "+myFileTypeManager.dumpReDetectQueue()); FileType type = vFile.getFileType(); - LOG.debug("T: ensureRedetected: getFileType (" + type.getName() + ") re-detect queue: " + myFileTypeManager.dumpReDetectQueue()); + log("T: ensureRedetected: getFileType ("+type.getName()+") re-detect queue: "+myFileTypeManager.dumpReDetectQueue()); assertTrue(detectorCalled.contains(vFile)); detectorCalled.clear(); - LOG.debug("T: ensureRedetected: clear"); + log("T: ensureRedetected: clear"); } public void testReassignTextFileType() { @@ -692,8 +701,7 @@ public class FileTypesTest extends HeavyPlatformTestCase { public FileType detect(@NotNull VirtualFile file, @NotNull ByteSequence firstBytes, @Nullable CharSequence firstCharsIfText) { detectorCalled.add(file); FileType result = FileUtil.isHashBangLine(firstCharsIfText, "stuff") ? stuffType : null; - LOG.debug("T: my detector for file " + file.getName() + - " run. result=" + (result == null ? null : result.getName())); + log("T: my detector for file "+file.getName()+" run. result="+(result == null ? null : result.getName())); return result; } @@ -703,24 +711,24 @@ public class FileTypesTest extends HeavyPlatformTestCase { } }; runWithDetector(detector, () -> { - LOG.debug("T: ------ akjdhfksdjgf"); + log("T: ------ akjdhfksdjgf"); File f = createTempFile("xx.asfdasdfas", "akjdhfksdjgf"); VirtualFile file = getVirtualFile(f); ensureRedetected(file, detectorCalled); assertTrue(file.getFileType().toString(), file.getFileType() instanceof PlainTextFileType); - LOG.debug("T: ------ my"); - setFileText(file, "#!stuff\nxx"); + log("T: ------ my"); + setFileText(file, "#!stuff\nxx"); ensureRedetected(file, detectorCalled); assertEquals(stuffType, file.getFileType()); - LOG.debug("T: ------ reload"); + log("T: ------ reload"); myFileTypeManager.drainReDetectQueue(); getPsiManager().dropPsiCaches(); ensureRedetected(file, detectorCalled); assertSame(file.getFileType().toString(), file.getFileType(), stuffType); - LOG.debug("T: ------"); + log("T: ------"); }); }