Search in sources :

Example 56 with LightVirtualFile

use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.

the class CodeFormatterFacade method wrapLongLinesIfNecessary.

/**
   * Inspects all lines of the given document and wraps all of them that exceed {@link CodeStyleSettings#getRightMargin(Language)}
   * right margin}.
   * <p/>
   * I.e. the algorithm is to do the following for every line:
   * <p/>
   * <pre>
   * <ol>
   *   <li>
   *      Check if the line exceeds {@link CodeStyleSettings#getRightMargin(Language)}  right margin}. Go to the next line in the case of
   *      negative answer;
   *   </li>
   *   <li>Determine line wrap position; </li>
   *   <li>
   *      Perform 'smart wrap', i.e. not only wrap the line but insert additional characters over than line feed if necessary.
   *      For example consider that we wrap a single-line comment - we need to insert comment symbols on a start of the wrapped
   *      part as well. Generally, we get the same behavior as during pressing 'Enter' at wrap position during editing document;
   *   </li>
   * </ol>
   </pre>
   *
   * @param file        file that holds parsed document tree
   * @param document    target document
   * @param startOffset start offset of the first line to check for wrapping (inclusive)
   * @param endOffset   end offset of the first line to check for wrapping (exclusive)
   */
private void wrapLongLinesIfNecessary(@NotNull final PsiFile file, @Nullable final Document document, final int startOffset, final int endOffset) {
    if (!mySettings.getCommonSettings(file.getLanguage()).WRAP_LONG_LINES || PostprocessReformattingAspect.getInstance(file.getProject()).isViewProviderLocked(file.getViewProvider()) || document == null) {
        return;
    }
    FormatterTagHandler formatterTagHandler = new FormatterTagHandler(CodeStyleSettingsManager.getSettings(file.getProject()));
    List<TextRange> enabledRanges = formatterTagHandler.getEnabledRanges(file.getNode(), new TextRange(startOffset, endOffset));
    final VirtualFile vFile = FileDocumentManager.getInstance().getFile(document);
    if ((vFile == null || vFile instanceof LightVirtualFile) && !ApplicationManager.getApplication().isUnitTestMode()) {
        // a formatter affect only PSI and it is out of sync with a document text
        return;
    }
    Editor editor = PsiUtilBase.findEditor(file);
    EditorFactory editorFactory = null;
    if (editor == null) {
        if (!ApplicationManager.getApplication().isDispatchThread()) {
            return;
        }
        editorFactory = EditorFactory.getInstance();
        editor = editorFactory.createEditor(document, file.getProject(), file.getVirtualFile(), false);
    }
    try {
        final Editor editorToUse = editor;
        ApplicationManager.getApplication().runWriteAction(() -> {
            final CaretModel caretModel = editorToUse.getCaretModel();
            final int caretOffset = caretModel.getOffset();
            final RangeMarker caretMarker = editorToUse.getDocument().createRangeMarker(caretOffset, caretOffset);
            doWrapLongLinesIfNecessary(editorToUse, file.getProject(), editorToUse.getDocument(), startOffset, endOffset, enabledRanges);
            if (caretMarker.isValid() && caretModel.getOffset() != caretMarker.getStartOffset()) {
                caretModel.moveToOffset(caretMarker.getStartOffset());
            }
        });
    } finally {
        PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject());
        if (documentManager.isUncommited(document))
            documentManager.commitDocument(document);
        if (editorFactory != null) {
            editorFactory.releaseEditor(editor);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) TextRange(com.intellij.openapi.util.TextRange)

Example 57 with LightVirtualFile

use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.

the class PsiDocumentManagerImplTest method testDocumentFromAlienProjectDoesNotEndUpInMyUncommittedList.

public void testDocumentFromAlienProjectDoesNotEndUpInMyUncommittedList() throws Exception {
    PsiFile file = findFile(createFile());
    final Document document = getDocument(file);
    File temp = createTempDirectory();
    final Project alienProject = createProject(temp + "/alien.ipr", DebugUtil.currentStackTrace());
    boolean succ2 = ProjectManagerEx.getInstanceEx().openProject(alienProject);
    assertTrue(succ2);
    // startup activities
    UIUtil.dispatchAllInvocationEvents();
    try {
        PsiManager alienManager = PsiManager.getInstance(alienProject);
        final String alienText = "alien";
        LightVirtualFile alienVirt = new LightVirtualFile("foo.txt", alienText);
        final PsiFile alienFile = alienManager.findFile(alienVirt);
        final PsiDocumentManagerImpl alienDocManager = (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(alienProject);
        final Document alienDocument = alienDocManager.getDocument(alienFile);
        assertEquals(0, alienDocManager.getUncommittedDocuments().length);
        assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
        WriteCommandAction.runWriteCommandAction(null, () -> {
            changeDocument(alienDocument, getPsiDocumentManager());
            assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
            assertEquals(0, alienDocManager.getUncommittedDocuments().length);
            changeDocument(alienDocument, alienDocManager);
            assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
            assertEquals(1, alienDocManager.getUncommittedDocuments().length);
            changeDocument(document, getPsiDocumentManager());
            assertEquals(1, getPsiDocumentManager().getUncommittedDocuments().length);
            assertEquals(1, alienDocManager.getUncommittedDocuments().length);
            changeDocument(document, alienDocManager);
            assertEquals(1, getPsiDocumentManager().getUncommittedDocuments().length);
            assertEquals(1, alienDocManager.getUncommittedDocuments().length);
        });
    } finally {
        ProjectUtil.closeAndDispose(alienProject);
    }
}
Also used : Project(com.intellij.openapi.project.Project) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockPsiFile(com.intellij.mock.MockPsiFile) Document(com.intellij.openapi.editor.Document) MockDocument(com.intellij.mock.MockDocument) VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockPsiFile(com.intellij.mock.MockPsiFile) File(java.io.File)

Example 58 with LightVirtualFile

use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.

the class PsiDocumentManagerImplTest method testDocumentFromAlienProjectGetsCommittedInBackground.

public void testDocumentFromAlienProjectGetsCommittedInBackground() throws Exception {
    LightVirtualFile virtualFile = createFile();
    PsiFile file = findFile(virtualFile);
    final Document document = getDocument(file);
    File temp = createTempDirectory();
    final Project alienProject = createProject(temp + "/alien.ipr", DebugUtil.currentStackTrace());
    boolean succ2 = ProjectManagerEx.getInstanceEx().openProject(alienProject);
    assertTrue(succ2);
    // startup activities
    UIUtil.dispatchAllInvocationEvents();
    try {
        PsiManager alienManager = PsiManager.getInstance(alienProject);
        final PsiFile alienFile = alienManager.findFile(virtualFile);
        assertNotNull(alienFile);
        final PsiDocumentManagerImpl alienDocManager = (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(alienProject);
        final Document alienDocument = alienDocManager.getDocument(alienFile);
        assertSame(document, alienDocument);
        assertEmpty(alienDocManager.getUncommittedDocuments());
        assertEmpty(getPsiDocumentManager().getUncommittedDocuments());
        WriteCommandAction.runWriteCommandAction(null, () -> {
            document.setText("xxx");
            assertOrderedEquals(getPsiDocumentManager().getUncommittedDocuments(), document);
            assertOrderedEquals(alienDocManager.getUncommittedDocuments(), alienDocument);
        });
        assertEquals("xxx", document.getText());
        assertEquals("xxx", alienDocument.getText());
        waitForCommit(document, TIMEOUT);
        assertTrue("Still not committed: " + document, getPsiDocumentManager().isCommitted(document));
        long t2 = System.currentTimeMillis() + TIMEOUT;
        while (!alienDocManager.isCommitted(alienDocument) && System.currentTimeMillis() < t2) {
            UIUtil.dispatchAllInvocationEvents();
        }
        assertTrue("Still not committed: " + alienDocument, alienDocManager.isCommitted(alienDocument));
    } finally {
        ProjectUtil.closeAndDispose(alienProject);
    }
}
Also used : Project(com.intellij.openapi.project.Project) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockPsiFile(com.intellij.mock.MockPsiFile) Document(com.intellij.openapi.editor.Document) MockDocument(com.intellij.mock.MockDocument) VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockPsiFile(com.intellij.mock.MockPsiFile) File(java.io.File)

Example 59 with LightVirtualFile

use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.

the class GrAnnotationNameValuePairImpl method getDetachedValue.

@Override
@Nullable
public PsiAnnotationMemberValue getDetachedValue() {
    GrNameValuePairStub stub = getStub();
    if (stub != null) {
        String text = stub.getValue();
        PsiAnnotationMemberValue result = SoftReference.dereference(myDetachedValue);
        if (result == null) {
            GrAnnotation annotation = GroovyPsiElementFactory.getInstance(getProject()).createAnnotationFromText("@F(" + text + ")", this);
            ((LightVirtualFile) annotation.getContainingFile().getViewProvider().getVirtualFile()).setWritable(false);
            PsiAnnotationMemberValue value = annotation.findAttributeValue(null);
            myDetachedValue = new SoftReference<>(result = value);
        }
        return result;
    }
    return getValue();
}
Also used : GrNameValuePairStub(org.jetbrains.plugins.groovy.lang.psi.stubs.GrNameValuePairStub) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Nullable(org.jetbrains.annotations.Nullable)

Example 60 with LightVirtualFile

use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.

the class CellOperationTest method testAddCell.

public void testAddCell() throws IOException {
    final String fileName = "testData/emptyFile.ipynb";
    final String fileText = IpnbTestCase.getFileText(fileName);
    final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
    ipnbFile.addCell(IpnbCodeCell.createEmptyCodeCell(), ipnbFile.getCells().size());
    final IpnbCodeCell cell = (IpnbCodeCell) ipnbFile.getCells().get(ipnbFile.getCells().size() - 1);
    assertTrue(cell.getCellOutputs().isEmpty());
    assertNull(cell.getPromptNumber());
    assertTrue(cell.getMetadata().isEmpty());
}
Also used : IpnbCodeCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) IpnbFile(org.jetbrains.plugins.ipnb.format.IpnbFile)

Aggregations

LightVirtualFile (com.intellij.testFramework.LightVirtualFile)69 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 NotNull (org.jetbrains.annotations.NotNull)10 Nullable (org.jetbrains.annotations.Nullable)10 IpnbFile (org.jetbrains.plugins.ipnb.format.IpnbFile)7 Document (com.intellij.openapi.editor.Document)6 FileElement (com.intellij.psi.impl.source.tree.FileElement)6 FileType (com.intellij.openapi.fileTypes.FileType)5 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)5 FileManager (com.intellij.psi.impl.file.impl.FileManager)5 Project (com.intellij.openapi.project.Project)4 SingleRootFileViewProvider (com.intellij.psi.SingleRootFileViewProvider)4 Language (com.intellij.lang.Language)3 FileViewProvider (com.intellij.psi.FileViewProvider)3 PsiFile (com.intellij.psi.PsiFile)3 PsiFileFactoryImpl (com.intellij.psi.impl.PsiFileFactoryImpl)3 File (java.io.File)3 IpnbCell (org.jetbrains.plugins.ipnb.format.cells.IpnbCell)3 VirtualFileWindow (com.intellij.injected.editor.VirtualFileWindow)2 MockDocument (com.intellij.mock.MockDocument)2