Search in sources :

Example 86 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.

the class ModuleResourceRepositoryTest method testOverlayUpdates2.

public void testOverlayUpdates2() {
    // Like testOverlayUpdates1, but rather than testing changes to layout resources (file-based resource)
    // perform document edits in value-documents
    myFixture.copyFileToProject(LAYOUT, "res/layout/layout1.xml");
    myFixture.copyFileToProject(LAYOUT_OVERLAY, "res2/layout/layout1.xml");
    VirtualFile values1 = myFixture.copyFileToProject(VALUES, "res/values/values.xml");
    VirtualFile values2 = myFixture.copyFileToProject(VALUES_OVERLAY1, "res2/values/values.xml");
    VirtualFile values3 = myFixture.copyFileToProject(VALUES_OVERLAY2, "res3/values/nameDoesNotMatter.xml");
    final VirtualFile values3No = myFixture.copyFileToProject(VALUES_OVERLAY2_NO, "res3/values-no/values.xml");
    VirtualFile res1 = values1.getParent().getParent();
    VirtualFile res2 = values2.getParent().getParent();
    VirtualFile res3 = values3.getParent().getParent();
    ModuleResourceRepository resources = ModuleResourceRepository.createForTest(myFacet, Arrays.asList(res1, res2, res3));
    PsiFile psiValues1 = PsiManager.getInstance(getProject()).findFile(values1);
    assertNotNull(psiValues1);
    PsiFile psiValues2 = PsiManager.getInstance(getProject()).findFile(values2);
    assertNotNull(psiValues2);
    PsiFile psiValues3 = PsiManager.getInstance(getProject()).findFile(values3);
    assertNotNull(psiValues3);
    PsiFile psiValues3No = PsiManager.getInstance(getProject()).findFile(values3No);
    assertNotNull(psiValues3No);
    // Initial state; sanity check from #testOverlays()
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    // Overridden in res2
    assertStringIs(resources, "title_crossfade", "Complex Crossfade");
    // Overridden in res3
    assertStringIs(resources, "title_zoom", "Zoom!");
    // Overridden in res2
    assertStringIs(resources, "unique_string", "Unique");
    // Overridden in res3
    assertStringIs(resources, "another_unique_string", "Another Unique", false);
    // res3 (not unique because we have a values-no item too)
    assertStringIs(resources, "app_name", "Very Different App Name", false);
    // Value resource check:
    // Verify that an edit in a value file, both in a non-overridden and an overridden
    // value, is observed; and that an override in an overridden value is not observed.
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "title_layout_changes"));
    ResourceItem appName = getFirstItem(resources, ResourceType.STRING, "app_name");
    assertItemIsInDir(res3, appName);
    // res3 (not unique because we have a values-no item too)
    assertStringIs(resources, "app_name", "Very Different App Name", false);
    long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiValues3);
    assertNotNull(document);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("Very Different App Name");
            document.insertString(offset, "Not ");
            documentManager.commitDocument(document);
        }
    });
    // The first edit to psiValues3 causes ResourceFolderRepository to transition from non-Psi -> Psi which requires a rescan.
    assertTrue(resources.isScanPending(psiValues3));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(resources.getModificationCount() > generation);
    // Should still be defined in res3 but have new value.
    // The order of items may have swapped if a full rescan is done.
    List<ResourceItem> list = resources.getResourceItem(ResourceType.STRING, "app_name");
    assertNotNull(list);
    assertSize(2, list);
    appName = ContainerUtil.find(list, new Condition<ResourceItem>() {

        @Override
        public boolean value(ResourceItem resourceItem) {
            return resourceItem.getQualifiers().isEmpty();
        }
    });
    assertNotNull(appName);
    assertItemIsInDir(res3, appName);
    ResourceValue appNameResourceValue = appName.getResourceValue(false);
    assertNotNull(appNameResourceValue);
    assertEquals("Not Very Different App Name", appNameResourceValue.getValue());
    // Try renaming the item name.
    generation = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("app_name");
            document.insertString(offset, "r");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.getModificationCount() > generation);
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "rapp_name"));
    appName = getFirstItem(resources, ResourceType.STRING, "app_name");
    // The item is still under res3, but now it's in the Norwegian translation
    assertEquals("no", appName.getSource().getQualifiers());
    assertStringIs(resources, "app_name", "Forskjellig Navn", false);
    // Delete that file:
    generation = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            try {
                values3No.delete(this);
            } catch (IOException e) {
                fail(e.toString());
            }
        }
    });
    assertTrue(resources.getModificationCount() > generation);
    // Now the item is no longer available in res3; should fallback to res 2
    appName = getFirstItem(resources, ResourceType.STRING, "app_name");
    assertItemIsInDir(res2, appName);
    assertStringIs(resources, "app_name", "Different App Name", false);
    // Check that editing an overridden attribute does not count as a change
    final Document document2 = documentManager.getDocument(psiValues1);
    assertNotNull(document2);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document2.getText().indexOf("Animations Demo");
            document2.insertString(offset, "Cool ");
            documentManager.commitDocument(document2);
        }
    });
    // The first edit to psiValues1 causes ResourceFolderRepository to transition from non-Psi -> Psi which requires a rescan.
    assertTrue(resources.isScanPending(psiValues1));
    UIUtil.dispatchAllInvocationEvents();
    // Unaffected by above change
    assertStringIs(resources, "app_name", "Different App Name", false);
    // Finally check that editing an non-overridden attribute also gets picked up as a change
    generation = resources.getModificationCount();
    // Observe after the rescan, so that an edit causes a generation bump.
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document2.getText().indexOf("Layout Changes");
            document2.insertString(offset, "New ");
            documentManager.commitDocument(document2);
        }
    });
    assertTrue(resources.getModificationCount() > generation);
    assertStringIs(resources, "title_layout_changes", "New Layout Changes", false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Condition(com.intellij.openapi.util.Condition) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) PsiFile(com.intellij.psi.PsiFile) ResourceItem(com.android.ide.common.res2.ResourceItem) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 87 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.

the class ResourceFolderRepositoryTest method testRemoveIdValue.

public void testRemoveIdValue() throws Exception {
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    final ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_flip"));
    final long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            String item = "<item type=\"id\" name=\"action_next\" />";
            int offset = document.getText().indexOf(item);
            document.deleteString(offset, offset + item.length());
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(generation < resources.getModificationCount());
    assertFalse(resources.hasResourceItem(ResourceType.ID, "action_next"));
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_flip"));
    resetScanCounter();
    // Try a second edit.
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            String item = "<item type=\"id\" name=\"action_flip\" />";
            int offset = document.getText().indexOf(item);
            document.deleteString(offset, offset + item.length());
            documentManager.commitDocument(document);
        }
    });
    assertTrue(generation < resources.getModificationCount());
    assertFalse(resources.hasResourceItem(ResourceType.ID, "action_next"));
    assertFalse(resources.hasResourceItem(ResourceType.ID, "action_flip"));
    ensureIncremental();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 88 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.

the class ResourceFolderRepositoryTest method testEditValueFileNoOp.

public void testEditValueFileNoOp() throws Exception {
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    Collection<String> strings = resources.getItemsOfType(ResourceType.STRING);
    assertEquals(8, strings.size());
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
    assertTrue(resources.hasResourceItem(ResourceType.INTEGER, "card_flip_time_full"));
    long initial = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    // Edit comment header; should be a no-op
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("Licensed under the");
            document.insertString(offset, "This code is ");
            documentManager.commitDocument(document);
        }
    });
    assertEquals(initial, resources.getModificationCount());
    // Test edit text NOT under an item: no-op
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            // insert BEFORE this
            int offset = document.getText().indexOf(" <item type=\"id\"");
            document.insertString(offset, "Ignored text");
            documentManager.commitDocument(document);
        }
    });
    assertEquals(initial, resources.getModificationCount());
    // Shouldn't have done any full file rescans during the above edits
    ensureIncremental();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 89 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.

the class ResourceFolderRepositoryTest method testAddIdValue.

public void testAddIdValue() throws Exception {
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    final ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
    assertFalse(resources.hasResourceItem(ResourceType.ID, "action_prev"));
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_flip"));
    final long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("<item type=\"id\" name=\"action_next\" />");
            document.insertString(offset, "<item type=\"id\" name=\"action_prev\" />");
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(generation < resources.getModificationCount());
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_prev"));
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_flip"));
    resetScanCounter();
    // Try a second edit.
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("<item type=\"id\" name=\"action_next\" />");
            document.insertString(offset, "<item type=\"id\" name=\"action_mid\" />");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(generation < resources.getModificationCount());
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_prev"));
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_mid"));
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_flip"));
    ensureIncremental();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 90 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.

the class ResourceFolderRepositoryTest method testEditArrayItemText.

public void testEditArrayItemText() throws Exception {
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    // Test that our tools:index and fallback handling for arrays works correctly
    // for getResourceValue()
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    ResourceItem array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
    ResourceValue resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("Question 4", resourceValue.getValue());
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("10", resourceValue.getValue());
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("Question 4");
            document.insertString(offset, "Q");
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("QQuestion 4", resourceValue.getValue());
    resetScanCounter();
    // However, the second edit can then be incremental.
    long generation2 = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("QQuestion 4");
            document.insertString(offset, "Q");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("QQQuestion 4", resourceValue.getValue());
    // Shouldn't have done any full file rescans during the above edits
    ensureIncremental();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) ResourceItem(com.android.ide.common.res2.ResourceItem) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Aggregations

PsiDocumentManager (com.intellij.psi.PsiDocumentManager)140 Document (com.intellij.openapi.editor.Document)111 PsiFile (com.intellij.psi.PsiFile)100 VirtualFile (com.intellij.openapi.vfs.VirtualFile)51 ResourceItem (com.android.ide.common.res2.ResourceItem)26 PsiElement (com.intellij.psi.PsiElement)24 Project (com.intellij.openapi.project.Project)22 TextRange (com.intellij.openapi.util.TextRange)13 NotNull (org.jetbrains.annotations.NotNull)12 Nullable (org.jetbrains.annotations.Nullable)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 Editor (com.intellij.openapi.editor.Editor)7 XmlFile (com.intellij.psi.xml.XmlFile)6 FileType (com.intellij.openapi.fileTypes.FileType)5 XmlTag (com.intellij.psi.xml.XmlTag)5 ASTNode (com.intellij.lang.ASTNode)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2