use of com.intellij.psi.PsiDocumentManager in project intellij-plugins by JetBrains.
the class JSShortenFQNamesProcessor method processText.
public void processText(final Project project, final Template template, final Document document, final RangeMarker templateRange, final Editor editor) {
if (!template.isToShortenLongNames())
return;
try {
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
psiDocumentManager.commitDocument(document);
final PsiFile hostFile = PsiUtilBase.getPsiFileInEditor(editor, project);
final PsiFile file = (hostFile != null && JavaScriptSupportLoader.isFlexMxmFile(hostFile)) ? InjectedLanguageUtil.findInjectedPsiNoCommit(hostFile, templateRange.getStartOffset()) : hostFile;
if (file instanceof JSFile && file.getLanguage().isKindOf(JavaScriptSupportLoader.ECMA_SCRIPT_L4)) {
final ActionScriptUnusedImportsHelper.Results unusedImportsResults = ActionScriptUnusedImportsHelper.getUnusedImports(file);
for (final JSReferenceExpression reference : unusedImportsResults.fqnsToReplaceWithShortName) {
final TextRange range = InjectedLanguageManager.getInstance(project).injectedToHost(file, reference.getTextRange());
if (TextRange.create(templateRange).contains(range)) {
final String shortName = StringUtil.getShortName(reference.getReferencedName());
final String resolved = JSImportHandlingUtil.resolveTypeName(shortName, reference);
// insert import statement if needed
if (shortName.equals(resolved)) {
final FormatFixer fixer = ImportUtils.insertImportStatements(reference, Collections.singletonList(reference.getText()));
if (fixer != null) {
fixer.fixFormat();
}
}
// shorten FQN
reference.replace(JSChangeUtil.createExpressionFromText(project, shortName).getPsi());
}
}
psiDocumentManager.doPostponedOperationsAndUnblockDocument(document);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testEditIdAttributeValue2.
public void testEditIdAttributeValue2() throws Exception {
// Edit the id attribute value: rather than by making a full value replacement,
// perform a tiny edit on the character content; this takes a different code
// path in the incremental updater
resetScanCounter();
// Edit the id attribute value of a layout item to change the set of available ids
VirtualFile file1 = myFixture.copyFileToProject(LAYOUT1, "res/layout/layout1.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
Collection<String> layouts = resources.getItemsOfType(ResourceType.LAYOUT);
assertEquals(1, layouts.size());
assertNotNull(resources.getResourceItem(ResourceType.LAYOUT, "layout1"));
assertTrue(resources.hasResourceItem(ResourceType.ID, "noteArea"));
assertFalse(resources.hasResourceItem(ResourceType.ID, "note2Area"));
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("noteArea");
document.insertString(offset + 4, "2");
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.ID, "note2Area"));
assertFalse(resources.hasResourceItem(ResourceType.ID, "noteArea"));
resetScanCounter();
// A second update should be incremental.
long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
final int offset = document.getText().indexOf("note2Area");
document.insertString(offset + 5, "3");
documentManager.commitDocument(document);
}
});
assertTrue(resources.hasResourceItem(ResourceType.ID, "note23Area"));
assertFalse(resources.hasResourceItem(ResourceType.ID, "note2Area"));
assertTrue(resources.getModificationCount() > generation2);
// Also check that for IDs the ResourceValue is nothing of consequence.
ResourceItem idItem = getOnlyItem(resources, ResourceType.ID, "note23Area");
ResourceValue idValue = idItem.getResourceValue(false);
assertNotNull(idValue);
assertEquals("", idValue.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 testEditValueText.
public void testEditValueText() 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"));
assertTrue(resources.hasResourceItem(ResourceType.INTEGER, "card_flip_time_full"));
long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
// Edit value should cause update
final int screenSlideOffset = document.getText().indexOf("Screen Slide");
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
document.replaceString(screenSlideOffset + 3, screenSlideOffset + 3, "e");
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
assertTrue(generation < resources.getModificationCount());
resetScanCounter();
// Now try another edit, where things should be incremental now.
final int screeenSlideOffset = document.getText().indexOf("Screeen Slide");
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
document.replaceString(screeenSlideOffset + 3, screeenSlideOffset + 3, "e");
documentManager.commitDocument(document);
}
});
long generation2 = resources.getModificationCount();
// NO revision bump yet, because the resource value hasn't been observed!
assertEquals(generation2, resources.getModificationCount());
// Now observe it, do another edit, and see what happens
List<ResourceItem> labelList = resources.getResourceItem(ResourceType.STRING, "title_screen_slide");
assertNotNull(labelList);
assertEquals(1, labelList.size());
ResourceItem slideLabel = labelList.get(0);
ResourceValue resourceValue = slideLabel.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("Screeeen Slide", resourceValue.getValue());
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
document.deleteString(screenSlideOffset + 3, screenSlideOffset + 7);
documentManager.commitDocument(document);
}
});
assertTrue(generation2 < resources.getModificationCount());
resourceValue = slideLabel.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("Scrn Slide", resourceValue.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 testChangeType.
public void testChangeType() throws Exception {
resetScanCounter();
VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
final ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
assertFalse(resources.hasResourceItem(ResourceType.DIMEN, "action_next"));
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("\"id\" name=\"action_next\" />") + 1;
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
document.replaceString(offset, offset + 2, "dimen");
documentManager.commitDocument(document);
}
});
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
ensureSingleScan();
assertTrue(generation < resources.getModificationCount());
assertFalse(resources.hasResourceItem(ResourceType.ID, "action_next"));
assertTrue(resources.hasResourceItem(ResourceType.DIMEN, "action_next"));
}
});
}
use of com.intellij.psi.PsiDocumentManager in project android by JetBrains.
the class ResourceFolderRepositoryTest method testEditStyleName.
public void testEditStyleName() throws Exception {
resetScanCounter();
VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
final ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkTheme"));
// Change style 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() {
int offset = document.getText().indexOf("DarkTheme");
document.replaceString(offset, offset + 4, "Grey");
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.STYLE, "DarkTheme"));
assertTrue(resources.hasResourceItem(ResourceType.STYLE, "GreyTheme"));
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("GreyTheme");
document.replaceString(offset, offset + 4, "Light");
documentManager.commitDocument(document);
}
});
assertTrue(generation2 < resources.getModificationCount());
assertFalse(resources.hasResourceItem(ResourceType.STYLE, "DarkTheme"));
assertTrue(resources.hasResourceItem(ResourceType.STYLE, "LightTheme"));
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
Aggregations