Search in sources :

Example 26 with PsiDocumentManager

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

the class ResourceFolderRepositoryTest method testLoadValuesWithBadName.

public void testLoadValuesWithBadName() throws Exception {
    resetScanCounter();
    // If a file had bad value names, test that it can still be parsed.
    VirtualFile file1 = myFixture.copyFileToProject(VALUES_WITH_BAD_NAME, "res/values/values.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    final ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "app*name"));
    //noinspection ConstantConditions
    assertEquals("Animations Demo", resources.getResourceItem(ResourceType.STRING, "app*name").get(0).getResourceValue(false).getValue());
    long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {

        @Override
        public void run() {
            String origString = "<string name=\"app*name\">Animations Demo</string>";
            String newString = "<string name=\"app_name\">Fixed Animations Demo</string>";
            int offset = document.getText().indexOf(origString);
            document.replaceString(offset, offset + origString.length(), newString);
            documentManager.commitDocument(document);
        }
    });
    assertFalse(resources.isScanPending(psiFile1));
    assertTrue(generation < resources.getModificationCount());
    assertFalse(resources.hasResourceItem(ResourceType.STRING, "app*name"));
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
    //noinspection ConstantConditions
    assertEquals("Fixed Animations Demo", resources.getResourceItem(ResourceType.STRING, "app_name").get(0).getResourceValue(false).getValue());
    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 27 with PsiDocumentManager

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

the class ResourceFolderRepositoryTest method testEditValueName.

public void testEditValueName() 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"));
    long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    final int offset = document.getText().indexOf("app_name");
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            document.replaceString(offset, offset + 3, "tap");
            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.STRING, "tap_name"));
    assertFalse(resources.hasResourceItem(ResourceType.STRING, "app_name"));
    resetScanCounter();
    // However, the second edit can then be incremental.
    final long generation2 = resources.getModificationCount();
    final int offset2 = document.getText().indexOf("tap_name");
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            document.replaceString(offset2, offset2 + 3, "rap");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(generation2 < resources.getModificationCount());
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "rap_name"));
    assertFalse(resources.hasResourceItem(ResourceType.STRING, "tap_name"));
    // 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 28 with PsiDocumentManager

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

the class ResourceFolderRepositoryTest method testRemoveValue.

public void testRemoveValue() 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"));
    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 textToRemove = "<string name=\"app_name\">Animations Demo</string>";
            int offset = document.getText().indexOf(textToRemove);
            document.deleteString(offset, offset + textToRemove.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.STRING, "app_name"));
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "title_zoom"));
    resetScanCounter();
    // Now try another edit that is also a delete, where things should be incremental now.
    long generation2 = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            String textToRemove2 = "<string name=\"title_zoom\">Zoom</string>";
            int offset = document.getText().indexOf(textToRemove2);
            document.deleteString(offset, offset + textToRemove2.length());
            documentManager.commitDocument(document);
        }
    });
    assertTrue(generation2 < resources.getModificationCount());
    assertFalse(resources.hasResourceItem(ResourceType.STRING, "app_name"));
    assertFalse(resources.hasResourceItem(ResourceType.STRING, "title_zoom"));
    // 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 29 with PsiDocumentManager

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

the class ResourceFolderRepositoryTest method testAddStringArrayItemElements.

public void testAddStringArrayItemElements() 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);
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    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(resourceValue instanceof ArrayResourceValue);
    ArrayResourceValue arv = (ArrayResourceValue) resourceValue;
    assertEquals(5, arv.getElementCount());
    assertEquals("Question 2", arv.getElement(1));
    assertEquals("Question 3", arv.getElement(2));
    assertEquals("Question 4", arv.getElement(3));
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            final int offset = document.getText().indexOf("<item>Question 3</item>");
            document.insertString(offset, "<item>Question 2.5</item>");
            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("Question 3", resourceValue.getValue());
    assertTrue(resourceValue instanceof ArrayResourceValue);
    arv = (ArrayResourceValue) resourceValue;
    assertEquals(6, arv.getElementCount());
    assertEquals("Question 2", arv.getElement(1));
    assertEquals("Question 2.5", arv.getElement(2));
    assertEquals("Question 3", arv.getElement(3));
    resetScanCounter();
    // However, the second edit can then be incremental.
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            final int offset = document.getText().indexOf("<item>Question 3</item>");
            document.insertString(offset, "<item>Question 2.75</item>");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("Question 2.75", resourceValue.getValue());
    assertTrue(resourceValue instanceof ArrayResourceValue);
    arv = (ArrayResourceValue) resourceValue;
    assertEquals(7, arv.getElementCount());
    assertEquals("Question 2", arv.getElement(1));
    assertEquals("Question 2.5", arv.getElement(2));
    assertEquals("Question 2.75", arv.getElement(3));
    assertEquals("Question 3", arv.getElement(4));
    // 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) ResourceItem(com.android.ide.common.res2.ResourceItem) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 30 with PsiDocumentManager

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

the class NlModelTest method testMoveInHierarchyWithWrongXmlTags.

public void testMoveInHierarchyWithWrongXmlTags() throws Exception {
    ModelBuilder modelBuilder = model("linear.xml", component(LINEAR_LAYOUT).withBounds(0, 0, 1000, 1000).matchParentWidth().matchParentHeight().withAttribute(ANDROID_URI, ATTR_ORIENTATION, VALUE_VERTICAL).children(component(FRAME_LAYOUT).withBounds(100, 100, 100, 100).width("100dp").height("100dp").children(component(BUTTON).withBounds(100, 100, 100, 100).width("100dp").height("100dp"))));
    NlModel model = modelBuilder.build();
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:1000x1000, instance=0}\n" + "    NlComponent{tag=<FrameLayout>, bounds=[100,100:100x100, instance=1}\n" + "        NlComponent{tag=<Button>, bounds=[100,100:100x100, instance=2}", myTreeDumper.toTree(model.getComponents()));
    XmlTag originalRoot = model.getFile().getRootTag();
    assertThat(originalRoot).isNotNull();
    XmlTag originalFrameLayout = originalRoot.getSubTags()[0];
    final Project project = model.getProject();
    WriteCommandAction.runWriteCommandAction(project, () -> {
        PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
        Document document = manager.getDocument(model.getFile());
        assertThat(document).isNotNull();
        document.setText("<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + "    android:layout_width=\"match_parent\"\n" + "    android:layout_height=\"match_parent\"\n" + "    android:orientation=\"vertical\">\n" + "    <Button\n" + "        android:layout_width=\"100dp\"\n" + "        android:layout_height=\"100dp\"\n" + "        android:text=\"Button\" />\n" + "    <FrameLayout\n" + "        android:layout_width=\"100dp\"\n" + "        android:layout_height=\"100dp\">\n" + "\n" + "    </FrameLayout>\n" + "\n" + "</LinearLayout>");
        manager.commitAllDocuments();
    });
    // Manually construct the view hierarchy
    // Assert that component identity is preserved
    List<ViewInfo> views = Lists.newArrayList();
    XmlTag newRoot = model.getFile().getRootTag();
    assertThat(newRoot).isNotNull();
    XmlTag[] newRootSubTags = newRoot.getSubTags();
    XmlTag newButton = newRootSubTags[0];
    assertThat(originalRoot).isSameAs(newRoot);
    assertThat(originalFrameLayout).isSameAs(newButton);
    TagSnapshot snapshot = TagSnapshot.createTagSnapshot(newRoot);
    ViewInfo viewInfo = new ViewInfo("android.widget.LinearLayout", snapshot, 0, 0, 500, 500);
    views.add(viewInfo);
    ViewInfo buttonInfo = new ViewInfo("android.widget.Button", snapshot.children.get(0), 0, 0, 500, 500);
    buttonInfo.setChildren(Collections.emptyList());
    ViewInfo frameViewInfo = new ViewInfo("android.widget.TextView", snapshot.children.get(1), 0, 0, 300, 300);
    frameViewInfo.setChildren(Collections.emptyList());
    viewInfo.setChildren(Arrays.asList(buttonInfo, frameViewInfo));
    model.updateHierarchy(newRoot, views);
    assertEquals("NlComponent{tag=<LinearLayout>, bounds=[0,0:500x500, instance=3}\n" + // since before the reparse instance=1 was associated with a <FrameLayout> !
    "    NlComponent{tag=<Button>, bounds=[0,0:500x500, instance=4}\n" + "    NlComponent{tag=<FrameLayout>, bounds=[0,0:300x300, instance=5}", myTreeDumper.toTree(model.getComponents()));
}
Also used : ModelBuilder(com.android.tools.idea.uibuilder.fixtures.ModelBuilder) Project(com.intellij.openapi.project.Project) TagSnapshot(com.android.tools.idea.rendering.TagSnapshot) Document(com.intellij.openapi.editor.Document) XmlTag(com.intellij.psi.xml.XmlTag) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) ViewInfo(com.android.ide.common.rendering.api.ViewInfo)

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