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"));
}
});
}
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();
}
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();
}
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();
}
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();
}
Aggregations