Search in sources :

Example 76 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 77 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 78 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 79 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 80 with LightVirtualFile

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

the class PythonRunConfigurationProducer method isConfigurationFromContext.

@Override
public boolean isConfigurationFromContext(PythonRunConfiguration configuration, ConfigurationContext context) {
    final Location location = context.getLocation();
    if (location == null)
        return false;
    final PsiFile script = location.getPsiElement().getContainingFile();
    if (!isAvailable(location, script))
        return false;
    final VirtualFile virtualFile = script.getVirtualFile();
    if (virtualFile == null)
        return false;
    if (virtualFile instanceof LightVirtualFile)
        return false;
    final String workingDirectory = configuration.getWorkingDirectory();
    final String scriptName = configuration.getScriptName();
    final String path = virtualFile.getPath();
    return scriptName.equals(path) || path.equals(new File(workingDirectory, scriptName).getAbsolutePath());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) PsiFile(com.intellij.psi.PsiFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Location(com.intellij.execution.Location)

Aggregations

LightVirtualFile (com.intellij.testFramework.LightVirtualFile)102 VirtualFile (com.intellij.openapi.vfs.VirtualFile)26 NotNull (org.jetbrains.annotations.NotNull)14 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)13 Nullable (org.jetbrains.annotations.Nullable)13 PsiFile (com.intellij.psi.PsiFile)12 Project (com.intellij.openapi.project.Project)11 Document (com.intellij.openapi.editor.Document)9 FileEditor (com.intellij.openapi.fileEditor.FileEditor)8 FileType (com.intellij.openapi.fileTypes.FileType)8 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)7 FileManager (com.intellij.psi.impl.file.impl.FileManager)7 IpnbFile (org.jetbrains.plugins.ipnb.format.IpnbFile)7 BlobExplorerFileEditor (com.microsoft.intellij.helpers.storage.BlobExplorerFileEditor)6 QueueFileEditor (com.microsoft.intellij.helpers.storage.QueueFileEditor)6 TableFileEditor (com.microsoft.intellij.helpers.storage.TableFileEditor)6 Language (com.intellij.lang.Language)5 PsiFileFactoryImpl (com.intellij.psi.impl.PsiFileFactoryImpl)5 FileElement (com.intellij.psi.impl.source.tree.FileElement)5 FileViewProvider (com.intellij.psi.FileViewProvider)4