use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testRemoveIntegerArrayItemElements.
public void testRemoveIntegerArrayItemElements() 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, "integers"));
ResourceItem array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
ResourceValue resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("10", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
ArrayResourceValue arv = (ArrayResourceValue) resourceValue;
assertEquals(2, arv.getElementCount());
assertEquals("10", arv.getElement(0));
assertEquals("20", arv.getElement(1));
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
String item = "<item>10</item>";
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(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertTrue(resourceValue instanceof ArrayResourceValue);
arv = (ArrayResourceValue) resourceValue;
assertEquals(1, arv.getElementCount());
assertEquals("20", resourceValue.getValue());
resetScanCounter();
// Try a second edit.
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
String item = "<item>20</item>";
int offset = document.getText().indexOf(item);
document.deleteString(offset, offset + item.length());
documentManager.commitDocument(document);
}
});
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertTrue(resourceValue instanceof ArrayResourceValue);
arv = (ArrayResourceValue) resourceValue;
assertEquals(0, arv.getElementCount());
// 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 ResourceNotificationManagerTest method editText.
private void editText(@NotNull PsiFile file, final String location, final int length, @Nullable final String text) {
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(file);
assertNotNull(document);
// Insert a comment at the beginning
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
final String documentText = document.getText();
int delta = location.indexOf('^');
assertTrue("Missing ^ describing caret offset in text window " + location, delta != -1);
String target = location.substring(0, delta) + location.substring(delta + 1);
int offset = documentText.indexOf(target);
assertTrue("Could not find " + target + " in " + documentText, offset != -1);
if (text != null) {
if (length == 0) {
document.insertString(offset + delta, text);
} else {
document.replaceString(offset + delta, offset + delta + length, text);
}
} else {
document.deleteString(offset + delta, offset + delta + length);
}
documentManager.commitDocument(document);
}
});
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testEditIdFromDrawable.
public void testEditIdFromDrawable() throws Exception {
resetScanCounter();
// Mix PNGs and XML in the same directory.
myFixture.copyFileToProject(DRAWABLE, "res/drawable-v21/logo.png");
VirtualFile file1 = myFixture.copyFileToProject(DRAWABLE_ID_SCAN, "res/drawable-v21/drawable_with_ids.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
Collection<String> ids = resources.getItemsOfType(ResourceType.ID);
assertNotNull(ids);
assertContainsElements(ids, "focused_state", "default_state", "pressed_state");
Collection<String> drawables = resources.getItemsOfType(ResourceType.DRAWABLE);
assertContainsElements(drawables, "logo", "drawable_with_ids");
// Now test an ID edit, to make sure that gets picked up too incrementally, just like layouts.
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
// Edit value should cause update
long generation = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
final int offset = document.getText().indexOf("focused_state");
document.replaceString(offset, offset + 1, "l");
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
assertTrue(generation < resources.getModificationCount());
resetScanCounter();
assertTrue(resources.hasResourceItem(ResourceType.ID, "locused_state"));
assertFalse(resources.hasResourceItem(ResourceType.ID, "focused_state"));
drawables = resources.getItemsOfType(ResourceType.DRAWABLE);
assertContainsElements(drawables, "logo", "drawable_with_ids");
// Now try another edit, where things should be incremental now.
long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
final int offset = document.getText().indexOf("default_state");
document.insertString(offset, "dd");
documentManager.commitDocument(document);
}
});
assertFalse(resources.isScanPending(psiFile1));
assertTrue(resources.hasResourceItem(ResourceType.ID, "dddefault_state"));
assertFalse(resources.hasResourceItem(ResourceType.ID, "default_state"));
drawables = resources.getItemsOfType(ResourceType.DRAWABLE);
assertContainsElements(drawables, "logo", "drawable_with_ids");
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
use of com.intellij.psi.PsiDocumentManager in project intellij-plugins by JetBrains.
the class CfmlTypedHandler method insertCloseTagIfNeeded.
public static boolean insertCloseTagIfNeeded(Editor editor, PsiFile file, Project project) {
final Document document = editor.getDocument();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
int offset = editor.getCaretModel().getOffset();
documentManager.commitDocument(document);
char charAtOffset = DocumentUtils.getCharAt(document, offset);
if (charAtOffset != '>') {
EditorModificationUtil.insertStringAtCaret(editor, ">", true, 0);
}
EditorModificationUtil.moveCaretRelatively(editor, 1);
++offset;
if (DocumentUtils.getCharAt(document, offset - 2) == '/') {
return false;
}
HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset - 2);
while (!iterator.atEnd() && !iterator.getTokenType().equals(CfmlTokenTypes.CF_TAG_NAME)) {
if (CfmlUtil.isControlToken(iterator.getTokenType())) {
return false;
}
iterator.retreat();
}
if (!iterator.atEnd()) {
iterator.retreat();
if (!iterator.atEnd() && iterator.getTokenType().equals(CfmlTokenTypes.LSLASH_ANGLEBRACKET)) {
return false;
}
iterator.advance();
}
if (iterator.atEnd()) {
return false;
}
String tagName = document.getCharsSequence().subSequence(iterator.getStart(), iterator.getEnd()).toString();
if (CfmlUtil.isSingleCfmlTag(tagName, project) || CfmlUtil.isUserDefined(tagName)) {
return false;
}
PsiElement tagElement = file.findElementAt(iterator.getStart());
while (tagElement != null && !(tagElement instanceof CfmlTag)) {
tagElement = tagElement.getParent();
}
if (tagElement == null) {
return false;
}
boolean doInsertion = false;
if (tagElement.getLastChild() instanceof PsiErrorElement) {
doInsertion = true;
} else {
iterator = ((EditorEx) editor).getHighlighter().createIterator(0);
while (!iterator.atEnd() && iterator.getStart() < offset) {
if (iterator.getTokenType() == CfmlTokenTypes.CF_TAG_NAME) {
String currentTagName = document.getCharsSequence().subSequence(iterator.getStart(), iterator.getEnd()).toString();
if (tagName.equals(currentTagName)) {
PsiElement currentTagElement = file.findElementAt(iterator.getStart());
currentTagElement = PsiTreeUtil.getParentOfType(currentTagElement, CfmlTag.class);
if (currentTagElement.getLastChild() instanceof PsiErrorElement) {
doInsertion = true;
break;
}
}
}
iterator.advance();
}
}
// tag name in lowercase
String tagNameFromPsi = ((CfmlTag) tagElement).getTagName();
if (doInsertion && CfmlUtil.isEndTagRequired(tagNameFromPsi, project)) {
if (!Comparing.equal(tagNameFromPsi, tagName, false)) {
// use tagName because it has proper case
tagName = tagNameFromPsi;
}
EditorModificationUtil.insertStringAtCaret(editor, "</" + tagName + ">", true, 0);
return true;
}
return false;
}
use of com.intellij.psi.PsiDocumentManager in project intellij-plugins by JetBrains.
the class CreateMxmlFileIntentionBase method invoke.
public void invoke(@NotNull final Project project, @Nullable final Editor editor, final PsiFile file) throws IncorrectOperationException {
final Module module = ModuleUtil.findModuleForPsiElement(file);
if (module == null) {
return;
}
final Pair<String, PsiDirectory> fileTextAndDir = getFileTextAndDir(module);
if (fileTextAndDir.first == null || fileTextAndDir.second == null) {
return;
}
ApplicationManager.getApplication().runWriteAction(() -> {
try {
final String fileName = myClassName + JavaScriptSupportLoader.MXML_FILE_EXTENSION_DOT;
final PsiFile newFile = fileTextAndDir.second.createFile(fileName);
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
final Document document = psiDocumentManager.getDocument(newFile);
document.setText(fileTextAndDir.first);
psiDocumentManager.commitDocument(document);
CodeStyleManager.getInstance(project).reformat(newFile);
FileEditorManager.getInstance(project).openFile(newFile.getVirtualFile(), true);
if (myCreatedClassFqnConsumer != null) {
final String packageName = ProjectRootManager.getInstance(project).getFileIndex().getPackageNameByDirectory(fileTextAndDir.second.getVirtualFile());
myCreatedClassFqnConsumer.consume(packageName + (packageName.isEmpty() ? "" : ".") + myClassName);
}
} catch (IncorrectOperationException e) {
Messages.showErrorDialog(project, e.getMessage(), getText());
}
});
}
Aggregations