Search in sources :

Example 1 with PsiPlainTextFile

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());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiPlainTextFile(com.intellij.psi.PsiPlainTextFile) PsiFile(com.intellij.psi.PsiFile) PsiBinaryFile(com.intellij.psi.PsiBinaryFile) File(java.io.File) PsiPlainTextFile(com.intellij.psi.PsiPlainTextFile)

Example 2 with PsiPlainTextFile

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiPlainTextFile(com.intellij.psi.PsiPlainTextFile) PsiFile(com.intellij.psi.PsiFile) PsiBinaryFile(com.intellij.psi.PsiBinaryFile) File(java.io.File) PsiBinaryFile(com.intellij.psi.PsiBinaryFile) PsiPlainTextFile(com.intellij.psi.PsiPlainTextFile)

Example 3 with 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();
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiPlainTextFile(com.intellij.psi.PsiPlainTextFile) PsiFile(com.intellij.psi.PsiFile) PsiBinaryFile(com.intellij.psi.PsiBinaryFile) File(java.io.File) PsiPlainTextFile(com.intellij.psi.PsiPlainTextFile)

Example 4 with PsiPlainTextFile

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());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvalidDataException(com.intellij.openapi.util.InvalidDataException) IOException(java.io.IOException) WriteExternalException(com.intellij.openapi.util.WriteExternalException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiPlainTextFile(com.intellij.psi.PsiPlainTextFile) PsiFile(com.intellij.psi.PsiFile) PsiBinaryFile(com.intellij.psi.PsiBinaryFile) File(java.io.File)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 PsiBinaryFile (com.intellij.psi.PsiBinaryFile)4 PsiFile (com.intellij.psi.PsiFile)4 PsiPlainTextFile (com.intellij.psi.PsiPlainTextFile)4 File (java.io.File)4 IOException (java.io.IOException)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 InvalidDataException (com.intellij.openapi.util.InvalidDataException)1 WriteExternalException (com.intellij.openapi.util.WriteExternalException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1