use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class AppResourceRepositoryTest method testMerging.
public void testMerging() {
// Like testOverlayUpdates1, but rather than testing changes to layout resources (file-based resource)
// perform document edits in value-documents
VirtualFile layoutFile = myFixture.copyFileToProject(LAYOUT, "res/layout/layout1.xml");
VirtualFile res1 = myFixture.copyFileToProject(VALUES, "res/values/values.xml").getParent().getParent();
VirtualFile res2 = myFixture.copyFileToProject(VALUES_OVERLAY1, "res2/values/values.xml").getParent().getParent();
VirtualFile res3 = myFixture.copyFileToProject(VALUES_OVERLAY2, "res3/values/nameDoesNotMatter.xml").getParent().getParent();
myFixture.copyFileToProject(VALUES_OVERLAY2_NO, "res3/values-no/values.xml");
assertNotSame(res1, res2);
assertNotSame(res1, res3);
assertNotSame(res2, res3);
// res3 is not used as an overlay here; instead we use it to simulate an AAR library below
final ModuleResourceRepository moduleRepository = ModuleResourceRepository.createForTest(myFacet, Arrays.asList(res1, res2));
final ProjectResourceRepository projectResources = ProjectResourceRepository.createForTest(myFacet, Collections.<LocalResourceRepository>singletonList(moduleRepository));
final AppResourceRepository appResources = AppResourceRepository.createForTest(myFacet, Collections.<LocalResourceRepository>singletonList(projectResources), Collections.<FileResourceRepository>emptyList());
assertTrue(appResources.hasResourceItem(ResourceType.STRING, "title_card_flip"));
assertFalse(appResources.hasResourceItem(ResourceType.STRING, "non_existent_title_card_flip"));
assertTrue(projectResources.hasResourceItem(ResourceType.STRING, "title_card_flip"));
assertFalse(projectResources.hasResourceItem(ResourceType.STRING, "non_existent_title_card_flip"));
assertTrue(moduleRepository.hasResourceItem(ResourceType.STRING, "title_card_flip"));
assertFalse(moduleRepository.hasResourceItem(ResourceType.STRING, "non_existent_title_card_flip"));
FileResourceRepository aar1 = FileResourceRepository.get(VfsUtilCore.virtualToIoFile(res3), null);
appResources.updateRoots(Arrays.asList(projectResources, aar1), Collections.singletonList(aar1));
assertTrue(appResources.hasResourceItem(ResourceType.STRING, "another_unique_string"));
assertTrue(aar1.hasResourceItem(ResourceType.STRING, "another_unique_string"));
assertFalse(projectResources.hasResourceItem(ResourceType.STRING, "another_unique_string"));
assertFalse(moduleRepository.hasResourceItem(ResourceType.STRING, "another_unique_string"));
assertTrue(appResources.hasResourceItem(ResourceType.STRING, "title_card_flip"));
assertFalse(appResources.hasResourceItem(ResourceType.STRING, "non_existent_title_card_flip"));
// Update module resource repository and assert that changes make it all the way up
PsiFile layoutPsiFile = PsiManager.getInstance(getProject()).findFile(layoutFile);
assertNotNull(layoutPsiFile);
assertTrue(moduleRepository.hasResourceItem(ResourceType.ID, "btn_title_refresh"));
final ResourceItem item = ModuleResourceRepositoryTest.getFirstItem(moduleRepository, ResourceType.ID, "btn_title_refresh");
final long generation = moduleRepository.getModificationCount();
final long projectGeneration = projectResources.getModificationCount();
final long appGeneration = appResources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(layoutPsiFile);
assertNotNull(document);
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
String string = "<ImageView style=\"@style/TitleBarSeparator\" />";
int offset = document.getText().indexOf(string);
document.deleteString(offset, offset + string.length());
documentManager.commitDocument(document);
}
});
assertTrue(moduleRepository.isScanPending(layoutPsiFile));
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
assertTrue(generation < moduleRepository.getModificationCount());
assertTrue(projectGeneration < projectResources.getModificationCount());
assertTrue(appGeneration < appResources.getModificationCount());
// Should still be defined:
assertTrue(moduleRepository.hasResourceItem(ResourceType.ID, "btn_title_refresh"));
assertTrue(appResources.hasResourceItem(ResourceType.ID, "btn_title_refresh"));
assertTrue(projectResources.hasResourceItem(ResourceType.ID, "btn_title_refresh"));
ResourceItem newItem = ModuleResourceRepositoryTest.getFirstItem(appResources, ResourceType.ID, "btn_title_refresh");
assertNotNull(newItem.getSource());
// However, should be a different item
assertNotSame(item, newItem);
}
});
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class GradleEditorModelUtil method removeEntity.
/**
* Removes given entity from the underlying source file if possible.
* <p/>
* <b>Note:</b> this method tried to preserve code style after removing the entity.
*
* @param entity an entity to remove
* @return <code>null</code> as an indication that given entity was successfully removed; an error message otherwise
*/
@Nullable
public static String removeEntity(@NotNull GradleEditorEntity entity, boolean commit) {
GradleEditorSourceBinding location = entity.getEntityLocation();
RangeMarker marker = location.getRangeMarker();
if (!marker.isValid()) {
return "source mapping is outdated for entity " + entity;
}
Document document = FileDocumentManager.getInstance().getDocument(location.getFile());
if (document == null) {
return "can't find a document for file " + location.getFile();
}
int startLine = document.getLineNumber(marker.getStartOffset());
int endLine = document.getLineNumber(marker.getEndOffset());
CharSequence text = document.getCharsSequence();
String ws = " \t";
int start = CharArrayUtil.shiftBackward(text, document.getLineStartOffset(startLine), marker.getStartOffset() - 1, ws);
int end = CharArrayUtil.shiftForward(text, marker.getEndOffset(), document.getLineEndOffset(endLine), ws);
if (start == document.getLineStartOffset(startLine) && startLine > 0) {
// Remove line feed at the end of the previous line.
start--;
} else if (end == document.getLineEndOffset(endLine) && endLine < document.getLineCount() - 1) {
//Remove trailing line feed.
end++;
}
document.deleteString(start, end);
if (commit) {
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(location.getProject());
psiDocumentManager.commitDocument(document);
}
return null;
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class GradleFileModel method applyChanges.
public void applyChanges() {
myGradleDslFile.applyChanges();
// Check for any postponed psi operations and complete them to unblock the underlying document for further modifications.
GroovyPsiElement psiElement = myGradleDslFile.getPsiElement();
assert psiElement instanceof PsiFile;
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(getProject());
Document document = psiDocumentManager.getDocument((PsiFile) psiElement);
if (document == null) {
return;
}
if (psiDocumentManager.isDocumentBlockedByPsi(document)) {
psiDocumentManager.doPostponedOperationsAndUnblockDocument(document);
}
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class TemplateUtils method reformatAndRearrange.
/**
* Reformats and rearranges the file (entirely or part of it)
*
* Note: reformatting the PSI file requires that this be wrapped in a write command.
*
* @param project The project which contains the given element
* @param virtualFile Virtual file to be reformatted and rearranged, if null, the entire file will be considered
* @param psiElement The element in the file to be reformatted and rearranged
* @param keepDocumentLocked True if the document will still be modified in the same write action
*/
private static void reformatAndRearrange(@NotNull Project project, @NotNull VirtualFile virtualFile, @Nullable PsiElement psiElement, boolean keepDocumentLocked) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
if (document == null) {
// The file could be a binary file with no editing support...
return;
}
psiDocumentManager.commitDocument(document);
PsiFile psiFile = psiDocumentManager.getPsiFile(document);
if (psiFile != null) {
TextRange textRange = psiElement == null ? psiFile.getTextRange() : psiElement.getTextRange();
CodeStyleManager.getInstance(project).reformatRange(psiFile, textRange.getStartOffset(), textRange.getEndOffset());
// The textRange of psiElement in the file can change after reformatting
textRange = psiElement == null ? psiFile.getTextRange() : psiElement.getTextRange();
psiDocumentManager.doPostponedOperationsAndUnblockDocument(document);
ServiceManager.getService(project, ArrangementEngine.class).arrange(psiFile, Collections.singleton(textRange));
if (keepDocumentLocked) {
psiDocumentManager.commitDocument(document);
}
}
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class LayoutUsageData method replaceByTagContent.
private static void replaceByTagContent(Project project, XmlTag tagToReplace, XmlTag tagToInline) throws AndroidRefactoringErrorException {
final ASTNode node = tagToInline.getNode();
if (node == null) {
throw new AndroidRefactoringErrorException();
}
final ASTNode startTagEnd = XmlChildRole.START_TAG_END_FINDER.findChild(node);
final ASTNode closingTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(node);
if (startTagEnd == null || closingTagStart == null) {
throw new AndroidRefactoringErrorException();
}
final int contentStart = startTagEnd.getTextRange().getEndOffset();
final int contentEnd = closingTagStart.getTextRange().getStartOffset();
if (contentStart < 0 || contentEnd < 0 || contentStart >= contentEnd) {
throw new AndroidRefactoringErrorException();
}
final PsiFile file = tagToInline.getContainingFile();
if (file == null) {
throw new AndroidRefactoringErrorException();
}
final String textToInline = file.getText().substring(contentStart, contentEnd).trim();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document document = documentManager.getDocument(tagToReplace.getContainingFile());
if (document == null) {
throw new AndroidRefactoringErrorException();
}
final TextRange range = tagToReplace.getTextRange();
document.replaceString(range.getStartOffset(), range.getEndOffset(), textToInline);
documentManager.commitDocument(document);
}
Aggregations