use of com.intellij.openapi.application.impl.ApplicationImpl in project intellij-community by JetBrains.
the class JoinLinesHandler method doExecute.
@Override
public void doExecute(@NotNull final Editor editor, @Nullable Caret caret, final DataContext dataContext) {
assert caret != null;
if (!(editor.getDocument() instanceof DocumentEx)) {
myOriginalHandler.execute(editor, caret, dataContext);
return;
}
final DocumentEx doc = (DocumentEx) editor.getDocument();
final Project project = CommonDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(editor.getContentComponent()));
if (project == null)
return;
final PsiDocumentManager docManager = PsiDocumentManager.getInstance(project);
PsiFile psiFile = docManager.getPsiFile(doc);
if (psiFile == null) {
myOriginalHandler.execute(editor, caret, dataContext);
return;
}
LogicalPosition caretPosition = caret.getLogicalPosition();
int startLine = caretPosition.line;
int endLine = startLine + 1;
if (caret.hasSelection()) {
startLine = doc.getLineNumber(caret.getSelectionStart());
endLine = doc.getLineNumber(caret.getSelectionEnd());
if (doc.getLineStartOffset(endLine) == caret.getSelectionEnd())
endLine--;
}
final int startReformatOffset = CharArrayUtil.shiftBackward(doc.getCharsSequence(), doc.getLineEndOffset(startLine), " \t");
// joining lines, several times if selection is multiline
int lineCount = endLine - startLine;
int line = startLine;
((ApplicationImpl) ApplicationManager.getApplication()).runWriteActionWithProgressInDispatchThread("Join Lines", project, null, IdeBundle.message("action.stop"), indicator -> {
Ref<Integer> caretRestoreOffset = new Ref<>(-1);
CodeEditUtil.setNodeReformatStrategy(node -> node.getTextRange().getStartOffset() >= startReformatOffset);
try {
for (int count = 0; count < lineCount; count++) {
indicator.checkCanceled();
indicator.setFraction(((double) count) / lineCount);
ProgressManager.getInstance().executeNonCancelableSection(() -> doJoinTwoLines(doc, project, docManager, psiFile, line, caretRestoreOffset));
}
} finally {
CodeEditUtil.setNodeReformatStrategy(null);
}
positionCaret(editor, caret, caretRestoreOffset.get());
});
}
use of com.intellij.openapi.application.impl.ApplicationImpl in project intellij-community by JetBrains.
the class BaseRefactoringProcessor method doRefactoring.
private void doRefactoring(@NotNull final Collection<UsageInfo> usageInfoSet) {
for (Iterator<UsageInfo> iterator = usageInfoSet.iterator(); iterator.hasNext(); ) {
UsageInfo usageInfo = iterator.next();
final PsiElement element = usageInfo.getElement();
if (element == null || !isToBeChanged(usageInfo)) {
iterator.remove();
}
}
LocalHistoryAction action = LocalHistory.getInstance().startAction(getCommandName());
final UsageInfo[] writableUsageInfos = usageInfoSet.toArray(new UsageInfo[usageInfoSet.size()]);
try {
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
RefactoringListenerManagerImpl listenerManager = (RefactoringListenerManagerImpl) RefactoringListenerManager.getInstance(myProject);
myTransaction = listenerManager.startTransaction();
final Map<RefactoringHelper, Object> preparedData = new LinkedHashMap<>();
final Runnable prepareHelpersRunnable = new Runnable() {
@Override
public void run() {
for (final RefactoringHelper helper : Extensions.getExtensions(RefactoringHelper.EP_NAME)) {
Object operation = ApplicationManager.getApplication().runReadAction(new Computable<Object>() {
@Override
public Object compute() {
return helper.prepareOperation(writableUsageInfos);
}
});
preparedData.put(helper, operation);
}
}
};
ProgressManager.getInstance().runProcessWithProgressSynchronously(prepareHelpersRunnable, "Prepare ...", false, myProject);
Runnable performRefactoringRunnable = () -> {
final String refactoringId = getRefactoringId();
if (refactoringId != null) {
RefactoringEventData data = getBeforeData();
if (data != null) {
data.addUsages(usageInfoSet);
}
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, data);
}
try {
if (refactoringId != null) {
UndoableAction action1 = new UndoRefactoringAction(myProject, refactoringId);
UndoManager.getInstance(myProject).undoableActionPerformed(action1);
}
performRefactoring(writableUsageInfos);
} finally {
if (refactoringId != null) {
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, getAfterData(writableUsageInfos));
}
}
};
ApplicationImpl app = (ApplicationImpl) ApplicationManagerEx.getApplicationEx();
if (Registry.is("run.refactorings.under.progress")) {
app.runWriteActionWithProgressInDispatchThread(getCommandName(), myProject, null, null, indicator -> performRefactoringRunnable.run());
} else {
app.runWriteAction(performRefactoringRunnable);
}
DumbService.getInstance(myProject).completeJustSubmittedTasks();
for (Map.Entry<RefactoringHelper, Object> e : preparedData.entrySet()) {
//noinspection unchecked
e.getKey().performOperation(myProject, e.getValue());
}
myTransaction.commit();
app.runWriteAction(() -> performPsiSpoilingRefactoring());
} finally {
action.finish();
}
int count = writableUsageInfos.length;
if (count > 0) {
StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.refactoring.result", count));
} else {
if (!isPreviewUsages(writableUsageInfos)) {
StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.noUsages"));
}
}
}
use of com.intellij.openapi.application.impl.ApplicationImpl in project intellij-community by JetBrains.
the class VfsRootAccess method assertAccessInTests.
@TestOnly
static void assertAccessInTests(@NotNull VirtualFileSystemEntry child, @NotNull NewVirtualFileSystem delegate) {
final Application application = ApplicationManager.getApplication();
if (SHOULD_PERFORM_ACCESS_CHECK && application.isUnitTestMode() && application instanceof ApplicationImpl && ((ApplicationImpl) application).isComponentsCreated() && !ApplicationInfoImpl.isInStressTest()) {
if (delegate != LocalFileSystem.getInstance() && delegate != JarFileSystem.getInstance()) {
return;
}
// root' children are loaded always
if (child.getParent() == null || child.getParent().getParent() == null) {
return;
}
Set<String> allowed = ApplicationManager.getApplication().runReadAction(new Computable<Set<String>>() {
@Override
public Set<String> compute() {
return allowedRoots();
}
});
boolean isUnder = allowed == null || allowed.isEmpty();
if (!isUnder) {
String childPath = child.getPath();
if (delegate == JarFileSystem.getInstance()) {
VirtualFile local = JarFileSystem.getInstance().getVirtualFileForJar(child);
assert local != null : child;
childPath = local.getPath();
}
for (String root : allowed) {
if (FileUtil.startsWith(childPath, root)) {
isUnder = true;
break;
}
if (root.startsWith(JarFileSystem.PROTOCOL_PREFIX)) {
String rootLocalPath = FileUtil.toSystemIndependentName(PathUtil.toPresentableUrl(root));
isUnder = FileUtil.startsWith(childPath, rootLocalPath);
if (isUnder)
break;
}
}
}
assert isUnder : "File accessed outside allowed roots: " + child + ";\nAllowed roots: " + new ArrayList<>(allowed);
}
}
use of com.intellij.openapi.application.impl.ApplicationImpl in project intellij-community by JetBrains.
the class TestWriteActionUnderProgress method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
ApplicationImpl app = (ApplicationImpl) ApplicationManager.getApplication();
boolean success = app.runWriteActionWithProgressInDispatchThread("Progress", null, null, null, TestWriteActionUnderProgress::runIndeterminateProgress);
assert success;
app.runWriteActionWithProgressInBackgroundThread("Cancellable Progress", null, null, "Stop", TestWriteActionUnderProgress::runDeterminateProgress);
}
Aggregations