use of com.intellij.openapi.project.DumbService in project intellij-community by JetBrains.
the class TemplateState method reformat.
private void reformat() {
final PsiFile file = getPsiFile();
if (file != null) {
CodeStyleManager style = CodeStyleManager.getInstance(myProject);
DumbService dumbService = DumbService.getInstance(myProject);
for (TemplateOptionalProcessor processor : dumbService.filterByDumbAwareness(Extensions.getExtensions(TemplateOptionalProcessor.EP_NAME))) {
try {
processor.processText(myProject, myTemplate, myDocument, myTemplateRange, myEditor);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myDocument);
// and reformat wouldn't be able to fix them
if (myTemplate.isToIndent()) {
if (!myTemplateIndented) {
LOG.assertTrue(myTemplateRange.isValid(), presentTemplate(myTemplate));
smartIndent(myTemplateRange.getStartOffset(), myTemplateRange.getEndOffset());
myTemplateIndented = true;
}
}
if (myTemplate.isToReformat()) {
try {
int endSegmentNumber = myTemplate.getEndSegmentNumber();
PsiDocumentManager.getInstance(myProject).commitDocument(myDocument);
RangeMarker dummyAdjustLineMarkerRange = null;
int endVarOffset = -1;
if (endSegmentNumber >= 0) {
endVarOffset = mySegments.getSegmentStart(endSegmentNumber);
TextRange range = CodeStyleManagerImpl.insertNewLineIndentMarker(file, myDocument, endVarOffset);
if (range != null)
dummyAdjustLineMarkerRange = myDocument.createRangeMarker(range);
}
int reformatStartOffset = myTemplateRange.getStartOffset();
int reformatEndOffset = myTemplateRange.getEndOffset();
if (dummyAdjustLineMarkerRange == null && endVarOffset >= 0) {
// There is a possible case that indent marker element was not inserted (e.g. because there is no blank line
// at the target offset). However, we want to reformat white space adjacent to the current template (if any).
PsiElement whiteSpaceElement = CodeStyleManagerImpl.findWhiteSpaceNode(file, endVarOffset);
if (whiteSpaceElement != null) {
TextRange whiteSpaceRange = whiteSpaceElement.getTextRange();
if (whiteSpaceElement.getContainingFile() != null) {
// Support injected white space nodes.
whiteSpaceRange = InjectedLanguageManager.getInstance(file.getProject()).injectedToHost(whiteSpaceElement, whiteSpaceRange);
}
reformatStartOffset = Math.min(reformatStartOffset, whiteSpaceRange.getStartOffset());
reformatEndOffset = Math.max(reformatEndOffset, whiteSpaceRange.getEndOffset());
}
}
style.reformatText(file, reformatStartOffset, reformatEndOffset);
unblockDocument();
if (dummyAdjustLineMarkerRange != null && dummyAdjustLineMarkerRange.isValid()) {
//[ven] TODO: [max] correct javadoc reformatting to eliminate isValid() check!!!
mySegments.replaceSegmentAt(endSegmentNumber, dummyAdjustLineMarkerRange.getStartOffset(), dummyAdjustLineMarkerRange.getEndOffset());
myDocument.deleteString(dummyAdjustLineMarkerRange.getStartOffset(), dummyAdjustLineMarkerRange.getEndOffset());
PsiDocumentManager.getInstance(myProject).commitDocument(myDocument);
}
if (endSegmentNumber >= 0) {
final int offset = mySegments.getSegmentStart(endSegmentNumber);
final int lineStart = myDocument.getLineStartOffset(myDocument.getLineNumber(offset));
// if $END$ is at line start, put it at correct indentation
if (myDocument.getCharsSequence().subSequence(lineStart, offset).toString().trim().isEmpty()) {
final int adjustedOffset = style.adjustLineIndent(file, offset);
mySegments.replaceSegmentAt(endSegmentNumber, adjustedOffset, adjustedOffset);
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
}
use of com.intellij.openapi.project.DumbService in project intellij-community by JetBrains.
the class JavaDirectInheritorsSearcher method calculateDirectSubClasses.
@NotNull
private static PsiClass[] calculateDirectSubClasses(@NotNull Project project, @NotNull PsiClass baseClass, @NotNull String baseClassName, @NotNull SearchScope useScope) {
DumbService dumbService = DumbService.getInstance(project);
GlobalSearchScope globalUseScope = dumbService.runReadActionInSmartMode(() -> StubHierarchyInheritorSearcher.restrictScope(GlobalSearchScopeUtil.toGlobalSearchScope(useScope, project)));
Collection<PsiReferenceList> candidates = dumbService.runReadActionInSmartMode(() -> JavaSuperClassNameOccurenceIndex.getInstance().get(baseClassName, project, globalUseScope));
// memory/speed optimisation: it really is a map(string -> PsiClass or List<PsiClass>)
final Map<String, Object> classesWithFqn = new HashMap<>();
processConcurrentlyIfTooMany(candidates, referenceList -> {
ProgressManager.checkCanceled();
ApplicationManager.getApplication().runReadAction(() -> {
final PsiClass candidate = (PsiClass) referenceList.getParent();
boolean isInheritor = candidate.isInheritor(baseClass, false);
if (isInheritor) {
String fqn = candidate.getQualifiedName();
synchronized (classesWithFqn) {
Object value = classesWithFqn.get(fqn);
if (value == null) {
classesWithFqn.put(fqn, candidate);
} else if (value instanceof PsiClass) {
List<PsiClass> list = new ArrayList<>();
list.add((PsiClass) value);
list.add(candidate);
classesWithFqn.put(fqn, list);
} else {
@SuppressWarnings("unchecked") List<PsiClass> list = (List<PsiClass>) value;
list.add(candidate);
}
}
}
});
return true;
});
final List<PsiClass> result = new ArrayList<>();
for (Object value : classesWithFqn.values()) {
if (value instanceof PsiClass) {
result.add((PsiClass) value);
} else {
@SuppressWarnings("unchecked") List<PsiClass> list = (List<PsiClass>) value;
result.addAll(list);
}
}
Collection<PsiAnonymousClass> anonymousCandidates = dumbService.runReadActionInSmartMode(() -> JavaAnonymousClassBaseRefOccurenceIndex.getInstance().get(baseClassName, project, globalUseScope));
processConcurrentlyIfTooMany(anonymousCandidates, candidate -> {
boolean isInheritor = dumbService.runReadActionInSmartMode(() -> candidate.isInheritor(baseClass, false));
if (isInheritor) {
synchronized (result) {
result.add(candidate);
}
}
return true;
});
boolean isEnum = ReadAction.compute(baseClass::isEnum);
if (isEnum) {
// abstract enum can be subclassed in the body
PsiField[] fields = ReadAction.compute(baseClass::getFields);
for (final PsiField field : fields) {
ProgressManager.checkCanceled();
if (field instanceof PsiEnumConstant) {
PsiEnumConstantInitializer initializingClass = ReadAction.compute(((PsiEnumConstant) field)::getInitializingClass);
if (initializingClass != null) {
// it surely is an inheritor
result.add(initializingClass);
}
}
}
}
return result.isEmpty() ? PsiClass.EMPTY_ARRAY : result.toArray(new PsiClass[result.size()]);
}
use of com.intellij.openapi.project.DumbService in project intellij-community by JetBrains.
the class AnalysisScope method commitAndRunInSmartMode.
private static void commitAndRunInSmartMode(final Runnable runnable, final Project project) {
while (true) {
final DumbService dumbService = DumbService.getInstance(project);
dumbService.waitForSmartMode();
boolean passed = PsiDocumentManager.getInstance(project).commitAndRunReadAction(() -> {
if (dumbService.isDumb())
return false;
runnable.run();
return true;
});
if (passed) {
break;
}
}
}
use of com.intellij.openapi.project.DumbService in project android by JetBrains.
the class ChooseClassDialog method openDialog.
/**
* Open a dialog if indices are available, otherwise show an error message.
*
* @return class name if user has selected one, null otherwise
*/
@Nullable
public static String openDialog(Module module, String title, boolean includeAll, @Nullable Condition<PsiClass> filter, @NotNull String... classes) {
final Project project = module.getProject();
final DumbService dumbService = DumbService.getInstance(project);
if (dumbService.isDumb()) {
// Variable "title" contains a string like "Classes", "Activities", "Fragments".
dumbService.showDumbModeNotification(String.format("%1$s are not available while indices are updating.", title));
return null;
}
final ChooseClassDialog dialog = new ChooseClassDialog(module, title, includeAll, filter, classes);
return dialog.showAndGet() ? dialog.getClassName() : null;
}
use of com.intellij.openapi.project.DumbService in project intellij-community by JetBrains.
the class DefaultMessageHandler method isDumbMode.
private boolean isDumbMode() {
final DumbService dumbService = DumbService.getInstance(myProject);
boolean isDumb = dumbService.isDumb();
if (isDumb) {
// wait some time
for (int idx = 0; idx < 5; idx++) {
TimeoutUtil.sleep(10L);
isDumb = dumbService.isDumb();
if (!isDumb) {
break;
}
}
}
return isDumb;
}
Aggregations