use of com.intellij.psi.PsiPlainTextFile in project intellij-community by JetBrains.
the class FileTypesTest method testAutoDetected.
public void testAutoDetected() throws IOException {
File dir = createTempDirectory();
File file = FileUtil.createTempFile(dir, "x", "xxx_xx_xx", true);
FileUtil.writeToFile(file, "xxx xxx xxx xxx");
VirtualFile virtualFile = getVirtualFile(file);
assertNotNull(virtualFile);
PsiFile psi = getPsiManager().findFile(virtualFile);
assertTrue(psi instanceof PsiPlainTextFile);
assertEquals(FileTypes.PLAIN_TEXT, virtualFile.getFileType());
}
use of com.intellij.psi.PsiPlainTextFile in project intellij-community by JetBrains.
the class FileTypesTest method testAutoDetectEmptyFile.
public void testAutoDetectEmptyFile() throws IOException {
File dir = createTempDirectory();
File file = FileUtil.createTempFile(dir, "x", "xxx_xx_xx", true);
VirtualFile virtualFile = getVirtualFile(file);
assertNotNull(virtualFile);
assertEquals(FileTypes.UNKNOWN, virtualFile.getFileType());
PsiFile psi = getPsiManager().findFile(virtualFile);
assertTrue(psi instanceof PsiBinaryFile);
assertEquals(FileTypes.UNKNOWN, virtualFile.getFileType());
setBinaryContent(virtualFile, "xxxxxxx".getBytes(CharsetToolkit.UTF8_CHARSET));
assertEquals(FileTypes.PLAIN_TEXT, virtualFile.getFileType());
PsiFile after = getPsiManager().findFile(virtualFile);
assertNotSame(psi, after);
assertFalse(psi.isValid());
assertTrue(after.isValid());
assertTrue(after instanceof PsiPlainTextFile);
}
use of com.intellij.psi.PsiPlainTextFile in project intellij-community by JetBrains.
the class FileTypesTest method _testStressPlainTextFileWithEverIncreasingLength2.
public void _testStressPlainTextFileWithEverIncreasingLength2() throws IOException, InterruptedException {
FrequentEventDetector.disableUntil(getTestRootDisposable());
File f = createTempFile("xx.asdkjfhlkasjdhf", StringUtil.repeatSymbol(' ', (int) PersistentFSConstants.FILE_LENGTH_TO_CACHE_THRESHOLD - 100));
VirtualFile virtualFile = getVirtualFile(f);
assertEquals(PlainTextFileType.INSTANCE, virtualFile.getFileType());
PsiFile psiFile = getPsiManager().findFile(virtualFile);
assertTrue(psiFile instanceof PsiPlainTextFile);
int NThreads = 1;
int N = 10;
List<Thread> threads = ContainerUtil.map(new Object[NThreads], o -> new Thread(() -> {
for (int i = 0; i < N; i++) {
ApplicationManager.getApplication().runReadAction(() -> {
String text = psiFile.getText();
System.out.println("text = " + text.length());
});
if (i % 1 == 0) {
try {
FileUtil.appendToFile(f, StringUtil.repeatSymbol(' ', 50));
LocalFileSystem.getInstance().refreshFiles(Collections.singletonList(virtualFile));
System.out.println("f = " + f.length() + "; virtualFile=" + virtualFile.getLength() + "; psiFile=" + psiFile.isValid() + "; type=" + virtualFile.getFileType());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}, "reader"));
threads.forEach(Thread::start);
for (Thread thread : threads) {
while (thread.isAlive()) {
//refresh
UIUtil.dispatchAllInvocationEvents();
}
}
}
use of com.intellij.psi.PsiPlainTextFile in project intellij-community by JetBrains.
the class FileTypesTest method _testStressPlainTextFileWithEverIncreasingLength.
public void _testStressPlainTextFileWithEverIncreasingLength() throws IOException, InterruptedException {
FrequentEventDetector.disableUntil(getTestRootDisposable());
File f = createTempFile("xx.lkjlkjlkjlj", "a");
VirtualFile virtualFile = getVirtualFile(f);
assertEquals(PlainTextFileType.INSTANCE, virtualFile.getFileType());
//PsiFile psiFile = getPsiManager().findFile(virtualFile);
//assertTrue(psiFile instanceof PsiPlainTextFile);
int NThreads = 8;
int N = 1000;
Random random = new Random();
AtomicReference<Exception> exception = new AtomicReference<>();
List<Thread> threads = ContainerUtil.map(new Object[NThreads], o -> new Thread(() -> {
try {
for (int i = 0; i < N; i++) {
boolean isText = ReadAction.compute(() -> {
if (virtualFile.getFileType().isBinary()) {
return false;
} else {
LoadTextUtil.loadText(virtualFile);
return true;
}
});
if (random.nextInt(3) == 0) {
new WriteCommandAction.Simple(getProject()) {
@Override
protected void run() throws Throwable {
byte[] bytes = new byte[(int) PersistentFSConstants.FILE_LENGTH_TO_CACHE_THRESHOLD + (isText ? 1 : 0)];
Arrays.fill(bytes, (byte) ' ');
virtualFile.setBinaryContent(bytes);
}
}.execute().throwException();
System.out.println(i + "; f = " + f.length() + "; virtualFile=" + virtualFile.getLength() + "; type=" + virtualFile.getFileType());
}
}
} catch (Exception e) {
exception.set(e);
e.printStackTrace();
throw new RuntimeException(e);
}
}, "reader"));
threads.forEach(Thread::start);
for (Thread thread : threads) {
while (thread.isAlive()) {
if (exception.get() != null)
throw new RuntimeException(exception.get());
//refresh
UIUtil.dispatchAllInvocationEvents();
}
}
if (exception.get() != null)
throw new RuntimeException(exception.get());
}
Aggregations