Search in sources :

Example 91 with PsiDocumentManager

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

the class ResourceFolderRepositoryTest method testEditDeclareStyleableFlag.

public void testEditDeclareStyleableFlag() throws Exception {
    // Rename, add and remove <flag> and <enum> nodes under a declare styleable and assert
    // that the declare styleable parent is updated
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    final PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    final ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
    // Fetch resource value to ensure it gets replaced after update
    assertTrue(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
    assertFalse(resources.hasResourceItem(ResourceType.ATTR, "ignore_no_format"));
    final ResourceItem style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
    final DeclareStyleableResourceValue srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    assertEquals(5, srv.getAllAttributes().size());
    final AttrResourceValue flagType = findAttr(srv.getAllAttributes(), "flagType");
    assertNotNull(flagType);
    assertEquals(2, flagType.getAttributeValues().size());
    assertEquals(Integer.valueOf(16), flagType.getAttributeValues().get("flag1"));
    assertEquals(Integer.valueOf(32), flagType.getAttributeValues().get("flag2"));
    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() {
            final int offset = document.getText().indexOf("flag1");
            document.insertString(offset + 1, "l");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

        @Override
        public void run() {
            ensureSingleScan();
            assertTrue(generation < resources.getModificationCount());
            assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
            assertTrue(resources.hasResourceItem(ResourceType.ATTR, "flagType"));
            ResourceItem style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
            DeclareStyleableResourceValue srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
            assertNotNull(srv);
            assertEquals(5, srv.getAllAttributes().size());
            AttrResourceValue flagType = findAttr(srv.getAllAttributes(), "flagType");
            assertNotNull(flagType);
            assertEquals(2, flagType.getAttributeValues().size());
            assertNull(flagType.getAttributeValues().get("flag1"));
            assertEquals(Integer.valueOf(16), flagType.getAttributeValues().get("fllag1"));
            // Now insert a new enum and delete one and make sure we're still okay
            resetScanCounter();
            long nextGeneration = resources.getModificationCount();
            WriteCommandAction.runWriteCommandAction(null, new Runnable() {

                @Override
                public void run() {
                    String enumAttr = "<enum name=\"type_stopwatch\" value=\"1\"/>";
                    int offset = document.getText().indexOf(enumAttr);
                    document.deleteString(offset, offset + enumAttr.length());
                    String flagAttr = "<flag name=\"flag2\" value=\"0x20\"/>";
                    offset = document.getText().indexOf(flagAttr);
                    document.insertString(offset, "<flag name=\"flag3\" value=\"0x40\"/>");
                    documentManager.commitDocument(document);
                }
            });
            assertTrue(nextGeneration < resources.getModificationCount());
            assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
            assertTrue(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
            assertTrue(resources.hasResourceItem(ResourceType.ATTR, "flagType"));
            style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
            srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
            assertNotNull(srv);
            assertEquals(5, srv.getAllAttributes().size());
            flagType = findAttr(srv.getAllAttributes(), "flagType");
            assertNotNull(flagType);
            assertEquals(3, flagType.getAttributeValues().size());
            assertEquals(Integer.valueOf(16), flagType.getAttributeValues().get("fllag1"));
            assertEquals(Integer.valueOf(32), flagType.getAttributeValues().get("flag2"));
            assertEquals(Integer.valueOf(64), flagType.getAttributeValues().get("flag3"));
            AttrResourceValue watchType = findAttr(srv.getAllAttributes(), "watchType");
            assertNotNull(watchType);
            assertEquals(1, watchType.getAttributeValues().size());
            assertEquals(Integer.valueOf(0), watchType.getAttributeValues().get("type_countdown"));
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) PsiFile(com.intellij.psi.PsiFile) ResourceItem(com.android.ide.common.res2.ResourceItem) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 92 with PsiDocumentManager

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

the class ResourceFolderRepositoryTest method testEditXmlProcessingInstructionAttrInValues.

public void testEditXmlProcessingInstructionAttrInValues() throws Exception {
    // Test editing an attribute in the XML prologue of a values file.
    VirtualFile file1 = myFixture.copyFileToProject(STRINGS, "res/values/strings.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    final ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
    assertFalse(resources.hasResourceItem(ResourceType.STRING, "app_name2"));
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "hello_world"));
    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 string = "utf-8";
            final int offset = document.getText().indexOf(string);
            assertTrue(offset != -1);
            document.insertString(offset, "t");
            documentManager.commitDocument(document);
        }
    });
    // Edits in XML processing instructions have no effect on the resource repository
    assertEquals(generation, resources.getModificationCount());
    assertFalse(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
}
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 93 with PsiDocumentManager

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

the class ResourceFolderRepositoryTest method testEditColorStateList.

public void testEditColorStateList() throws Exception {
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(COLOR_STATELIST, "res/color/my_state_list.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    final ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.COLOR, "my_state_list"));
    // Edit comment
    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() {
            final int offset = document.getText().indexOf(" -->");
            document.replaceString(offset, offset, "more comment");
            documentManager.commitDocument(document);
        }
    });
    // Shouldn't have caused any change
    assertTrue(generation == resources.getModificationCount());
    ensureIncremental();
    // Edit processing instruction
    generation = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            final int offset = document.getText().indexOf("utf-8");
            document.replaceString(offset, offset + 5, "other encoding");
            documentManager.commitDocument(document);
        }
    });
    // Shouldn't have caused any change
    assertTrue(generation == resources.getModificationCount());
    ensureIncremental();
    // Edit state list
    generation = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            final int offset = document.getText().indexOf("myColor");
            document.replaceString(offset, offset + 7, "myNewColor");
            documentManager.commitDocument(document);
        }
    });
    // Should have caused a modification but not a rescan
    assertTrue(generation < resources.getModificationCount());
    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 94 with PsiDocumentManager

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

the class ResourceFolderRepositoryTest method testAddPluralItems.

public void testAddPluralItems() 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);
    assertTrue(resources.hasResourceItem(ResourceType.PLURALS, "my_plural"));
    ResourceItem plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
    ResourceValue resourceValue = plural.getResourceValue(false);
    assertNotNull(resourceValue);
    assertInstanceOf(resourceValue, PluralsResourceValue.class);
    PluralsResourceValue prv = (PluralsResourceValue) resourceValue;
    assertEquals(3, prv.getPluralsCount());
    assertEquals("@string/hello_two", resourceValue.getValue());
    assertEquals("two", prv.getQuantity(1));
    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 quantity=\"two\">@string/hello_two</item>");
            document.insertString(offset, "<item quantity=\"one_and_half\">@string/hello_one_and_half</item>");
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
    resourceValue = plural.getResourceValue(false);
    assertNotNull(resourceValue);
    assertInstanceOf(resourceValue, PluralsResourceValue.class);
    prv = (PluralsResourceValue) resourceValue;
    assertEquals(4, prv.getPluralsCount());
    assertEquals("@string/hello_two", resourceValue.getValue());
    assertEquals("one_and_half", prv.getQuantity(1));
    assertTrue(resources.getModificationCount() > generation);
    resetScanCounter();
    // Now try a second edit.
    long generation2 = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("<item quantity=\"one_and_half\">@string/hello_one_and_half</item>");
            document.insertString(offset, "<item quantity=\"one_and_a_quarter\">@string/hello_one_and_a_quarter</item>");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.getModificationCount() > generation2);
    plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
    resourceValue = plural.getResourceValue(false);
    assertNotNull(resourceValue);
    assertInstanceOf(resourceValue, PluralsResourceValue.class);
    prv = (PluralsResourceValue) resourceValue;
    assertEquals(5, prv.getPluralsCount());
    assertEquals("@string/hello_two", resourceValue.getValue());
    assertEquals("one_and_a_quarter", prv.getQuantity(1));
    // 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)

Example 95 with PsiDocumentManager

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

the class ResourceFolderRepositoryTest method testAddStyleItem.

public void testAddStyleItem() 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);
    assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkActionBar"));
    ResourceItem style = getOnlyItem(resources, ResourceType.STYLE, "DarkActionBar");
    StyleResourceValue srv = (StyleResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    assertSameElements(srv.getNames(), "android:background", "android:textColor");
    ResourceValue background = srv.getItem("background", true);
    assertNotNull(background);
    assertEquals("@android:color/transparent", background.getValue());
    ResourceValue textColor = srv.getItem("textColor", true);
    assertNotNull(textColor);
    assertEquals("#008", textColor.getValue());
    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 name=\"android:background\"");
            assertTrue(offset > 0);
            document.insertString(offset, "<item name=\"android:textSize\">20sp</item>");
            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.STYLE, "DarkActionBar"));
    style = getOnlyItem(resources, ResourceType.STYLE, "DarkActionBar");
    srv = (StyleResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    assertSameElements(srv.getNames(), "android:background", "android:textSize", "android:textColor");
    ResourceValue textSize = srv.getItem("textSize", true);
    assertNotNull(textSize);
    assertEquals("20sp", textSize.getValue());
    resetScanCounter();
    // Now try a second edit.
    long generation2 = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("<item name=\"android:background\"");
            assertTrue(offset > 0);
            document.insertString(offset, "<item name=\"android:typeface\">monospace</item>");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(generation2 < resources.getModificationCount());
    assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkActionBar"));
    style = getOnlyItem(resources, ResourceType.STYLE, "DarkActionBar");
    srv = (StyleResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    assertSameElements(srv.getNames(), "android:background", "android:typeface", "android:textSize", "android:textColor");
    ResourceValue typeface = srv.getItem("typeface", true);
    assertNotNull(typeface);
    assertEquals("monospace", typeface.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