use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testEditXmlProcessingInstructionAttrInLayout.
public void testEditXmlProcessingInstructionAttrInLayout() throws Exception {
// Test editing an attribute in the XML prologue of a layout file with IDs.
VirtualFile file1 = myFixture.copyFileToProject(LAYOUT1, "res/layout/layout.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
final ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
assertTrue(resources.hasResourceItem(ResourceType.LAYOUT, "layout"));
assertTrue(resources.hasResourceItem(ResourceType.ID, "noteArea"));
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();
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testRemoveStyleItem.
public void testRemoveStyleItem() 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() {
String item = ("<item name=\"android:textColor\">#008</item>");
int offset = document.getText().indexOf(item);
assertTrue(offset > 0);
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());
assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkActionBar"));
style = getOnlyItem(resources, ResourceType.STYLE, "DarkActionBar");
srv = (StyleResourceValue) style.getResourceValue(false);
assertNotNull(srv);
assertSameElements(srv.getNames(), "android:background");
background = srv.getItem("background", true);
assertNotNull(background);
assertEquals("@android:color/transparent", background.getValue());
resetScanCounter();
// Try second edit, which is incremental.
long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
String item = ("<item name=\"android:background\">@android:color/transparent</item>");
int offset = document.getText().indexOf(item);
assertTrue(offset > 0);
document.deleteString(offset, offset + item.length());
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);
assertEmpty(srv.getNames());
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testEditStyleItemName.
public void testEditStyleItemName() 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, "DarkTheme"));
ResourceItem style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
StyleResourceValue srv = (StyleResourceValue) style.getResourceValue(false);
assertNotNull(srv);
ResourceValue actionBarStyle = srv.getItem("actionBarStyle", true);
assertNotNull(actionBarStyle);
assertEquals("@style/DarkActionBar", actionBarStyle.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("android:actionBarStyle");
document.insertString(offset + 8, "n");
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, "DarkTheme"));
style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
srv = (StyleResourceValue) style.getResourceValue(false);
assertNotNull(srv);
actionBarStyle = srv.getItem("nactionBarStyle", true);
assertNotNull(actionBarStyle);
assertEquals("@style/DarkActionBar", actionBarStyle.getValue());
resetScanCounter();
// Now try another edit, where things should be incremental now.
long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("android:nactionBarStyle");
document.insertString(offset + 8, "i");
documentManager.commitDocument(document);
}
});
assertTrue(generation2 < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkTheme"));
style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
srv = (StyleResourceValue) style.getResourceValue(false);
assertNotNull(srv);
actionBarStyle = srv.getItem("inactionBarStyle", true);
assertNotNull(actionBarStyle);
assertEquals("@style/DarkActionBar", actionBarStyle.getValue());
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testInsertNewElementWithId.
public void testInsertNewElementWithId() throws Exception {
resetScanCounter();
// Make some miscellaneous edits in the file that have no bearing on the
// project resources and therefore end up doing no work
VirtualFile file1 = myFixture.copyFileToProject(LAYOUT1, "res/layout/layout1.xml");
final PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
assert (psiFile1 instanceof XmlFile);
final XmlFile xmlFile = (XmlFile) psiFile1;
final ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
Collection<String> layouts = resources.getItemsOfType(ResourceType.LAYOUT);
assertEquals(1, layouts.size());
assertNotNull(resources.getResourceItem(ResourceType.LAYOUT, "layout1"));
final long initial = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
// Insert tag (with an id) in layout file: should incrementally update set of ids
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
final XmlTag header = findTagById(xmlFile, "text2");
assertNotNull(header);
int indentAreaBeforeTag = header.getTextOffset() - 1;
document.insertString(indentAreaBeforeTag, "<LinearLayout android:id=\"@+id/newid1\"><Child android:id=\"@+id/newid2\"/></LinearLayout>");
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
assertTrue(initial < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.ID, "newid1"));
assertTrue(resources.hasResourceItem(ResourceType.ID, "newid2"));
resetScanCounter();
// A second update should be incremental.
long generation = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
final XmlTag header = findTagById(xmlFile, "text2");
assertNotNull(header);
int indentAreaBeforeTag = header.getTextOffset() - 1;
document.insertString(indentAreaBeforeTag, "<LinearLayout android:id=\"@+id/newid3\"><Child android:id=\"@+id/newid4\"/></LinearLayout>");
documentManager.commitDocument(document);
}
});
assertFalse(resources.isScanPending(psiFile1));
assertTrue(generation < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.ID, "newid1"));
assertTrue(resources.hasResourceItem(ResourceType.ID, "newid2"));
assertTrue(resources.hasResourceItem(ResourceType.ID, "newid3"));
assertTrue(resources.hasResourceItem(ResourceType.ID, "newid4"));
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testIssue64239.
public void testIssue64239() throws Exception {
// If you duplicate a string, then change its contents (which still duplicated),
// and then finally rename the string, then the value of the second clone will
// continue to be referred from the first string:
// <string name="foo">value 1</foo>
// <string name="foo">value 2</foo>
// then change the second string name to
// <string name="foo2">value 2</foo>
// If you now evaluate the value of foo, you get "value 1". Basically while the
// two strings are (illegally) aliasing, the value of the first string is replaced.
// TODO: Test both *duplicating* a node, as well as manually typing in a brand
// new one with the same result
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"));
//noinspection ConstantConditions
assertEquals("My Application 574", resources.getResourceItem(ResourceType.STRING, "app_name").get(0).getResourceValue(false).getValue());
final long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
final int offset = document.getText().indexOf("</resources>");
assertTrue(offset != -1);
final String string = "<string name=\"app_name\">New Value</string>";
// First duplicate the line:
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
document.insertString(offset, string);
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, "app_name"));
resetScanCounter();
// Second edit (duplicate again)
long generation2 = resources.getModificationCount();
final int offset2 = document.getText().indexOf("</resources>");
final String string2 = "<string name=\"app_name\">Another Value</string>";
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
document.insertString(offset2, string2);
documentManager.commitDocument(document);
}
});
assertTrue(generation2 < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
// Then replace the name of the duplicated string
long generation3 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
int startOffset = offset + "<string name=\"".length();
document.replaceString(startOffset, startOffset + "app_name".length(), "new_name");
documentManager.commitDocument(document);
}
});
assertFalse(resources.isScanPending(psiFile1));
assertTrue(generation3 < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
assertTrue(resources.hasResourceItem(ResourceType.STRING, "new_name"));
//noinspection ConstantConditions
assertEquals("New Value", resources.getResourceItem(ResourceType.STRING, "new_name").get(0).getResourceValue(false).getValue());
//noinspection ConstantConditions
assertEquals("My Application 574", resources.getResourceItem(ResourceType.STRING, "app_name").get(0).getResourceValue(false).getValue());
// Replace the second duplicate.
long generation4 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
int startOffset = offset2 + "<string name=\"".length();
document.replaceString(startOffset, startOffset + "app_name".length(), "new_name2");
documentManager.commitDocument(document);
}
});
assertFalse(resources.isScanPending(psiFile1));
assertTrue(generation4 < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
assertTrue(resources.hasResourceItem(ResourceType.STRING, "new_name"));
assertTrue(resources.hasResourceItem(ResourceType.STRING, "new_name2"));
//noinspection ConstantConditions
assertEquals("New Value", resources.getResourceItem(ResourceType.STRING, "new_name").get(0).getResourceValue(false).getValue());
//noinspection ConstantConditions
assertEquals("Another Value", resources.getResourceItem(ResourceType.STRING, "new_name2").get(0).getResourceValue(false).getValue());
//noinspection ConstantConditions
assertEquals("My Application 574", resources.getResourceItem(ResourceType.STRING, "app_name").get(0).getResourceValue(false).getValue());
ensureIncremental();
}
Aggregations