Search in sources :

Example 1 with RunResult

use of com.intellij.openapi.application.RunResult in project android by JetBrains.

the class ModuleModelDataService method importData.

private void importData(@NotNull Collection<DataNode<T>> toImport, @NotNull Project project, @NotNull IdeModifiableModelsProvider modelsProvider) throws Throwable {
    RunResult result = new WriteCommandAction.Simple(project) {

        @Override
        protected void run() throws Throwable {
            if (project.isDisposed()) {
                return;
            }
            Map<String, T> modelsByName = indexByModuleName(toImport);
            importData(toImport, project, modelsProvider, modelsByName);
        }
    }.execute();
    Throwable error = result.getThrowable();
    if (error != null) {
        throw error;
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RunResult(com.intellij.openapi.application.RunResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with RunResult

use of com.intellij.openapi.application.RunResult in project idea-php-typo3-plugin by cedricziel.

the class GenerateActionControllerAction method write.

@Override
protected void write(@NotNull Project project, @NotNull PsiDirectory extensionRootDirectory, @NotNull String className) {
    if (!className.endsWith("Controller")) {
        className += "Controller";
    }
    final String finalClassName = className;
    RunResult<PsiElement> elementRunResult = new WriteCommandAction<PsiElement>(project) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            PsiElement extensionFile;
            Map<String, String> context = new HashMap<>();
            String calculatedNamespace = ExtensionUtility.findDefaultNamespace(extensionRootDirectory);
            if (calculatedNamespace == null) {
                result.setResult(null);
                return;
            }
            calculatedNamespace += "Controller";
            context.put("namespace", calculatedNamespace);
            context.put("className", finalClassName);
            try {
                extensionFile = ExtensionFileGenerationUtil.fromTemplate("extension_file/ExtbaseActionController.php", "Classes/Controller", finalClassName + ".php", extensionRootDirectory, context, project);
                if (extensionFile != null) {
                    result.setResult(extensionFile);
                }
            } catch (IncorrectOperationException e) {
            // file already exists
            }
        }
    }.execute();
    if (elementRunResult.getResultObject() != null) {
        new OpenFileDescriptor(project, elementRunResult.getResultObject().getContainingFile().getVirtualFile(), 0).navigate(true);
    } else {
        Messages.showErrorDialog("Cannot create extension file", "Error");
    }
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) HashMap(java.util.HashMap) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult)

Example 3 with RunResult

use of com.intellij.openapi.application.RunResult in project idea-php-typo3-plugin by cedricziel.

the class GenerateExtbaseEntityAction method write.

@Override
protected void write(@NotNull Project project, @NotNull PsiDirectory extensionRootDirectory, @NotNull String className) {
    final String finalClassName = className;
    RunResult<PsiElement> elementRunResult = new WriteCommandAction<PsiElement>(project) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            PsiElement extensionFile;
            Map<String, String> context = new HashMap<>();
            String calculatedNamespace = ExtensionUtility.findDefaultNamespace(extensionRootDirectory);
            if (calculatedNamespace == null) {
                result.setResult(null);
                return;
            }
            calculatedNamespace += "Domain\\Model";
            context.put("namespace", calculatedNamespace);
            context.put("className", finalClassName);
            try {
                extensionFile = ExtensionFileGenerationUtil.fromTemplate("extension_file/ExtbaseEntity.php", "Classes/Domain/Model", finalClassName + ".php", extensionRootDirectory, context, project);
                if (extensionFile != null) {
                    result.setResult(extensionFile);
                }
            } catch (IncorrectOperationException e) {
            // file already exists
            }
        }
    }.execute();
    if (elementRunResult.getResultObject() != null) {
        new OpenFileDescriptor(project, elementRunResult.getResultObject().getContainingFile().getVirtualFile(), 0).navigate(true);
    } else {
        Messages.showErrorDialog("Cannot create extension file", "Error");
    }
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) HashMap(java.util.HashMap) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult)

Example 4 with RunResult

use of com.intellij.openapi.application.RunResult in project intellij-community by JetBrains.

the class GenericCompilerRunner method processTarget.

private <T extends BuildTarget, Item extends CompileItem<Key, SourceState, OutputState>, Key, SourceState, OutputState> boolean processTarget(T target, final int targetId, final GenericCompiler<Key, SourceState, OutputState> compiler, final GenericCompilerInstance<T, Item, Key, SourceState, OutputState> instance, final GenericCompilerCache<Key, SourceState, OutputState> cache) throws IOException, ExitException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Processing target '" + target + "' (id=" + targetId + ") by " + compiler);
    }
    final List<Item> items = instance.getItems(target);
    checkForErrorsOrCanceled();
    final List<GenericCompilerProcessingItem<Item, SourceState, OutputState>> toProcess = new ArrayList<>();
    final THashSet<Key> keySet = new THashSet<>(new SourceItemHashingStrategy<>(compiler));
    final Ref<IOException> exception = Ref.create(null);
    final Map<Item, SourceState> sourceStates = new HashMap<>();
    DumbService.getInstance(myProject).runReadActionInSmartMode(() -> {
        try {
            for (Item item : items) {
                final Key key = item.getKey();
                keySet.add(key);
                if (item.isExcluded())
                    continue;
                final GenericCompilerCache.PersistentStateData<SourceState, OutputState> data = cache.getState(targetId, key);
                SourceState sourceState = data != null ? data.mySourceState : null;
                final OutputState outputState = data != null ? data.myOutputState : null;
                if (myForceCompile || sourceState == null || !item.isSourceUpToDate(sourceState) || outputState == null || !item.isOutputUpToDate(outputState)) {
                    sourceStates.put(item, item.computeSourceState());
                    toProcess.add(new GenericCompilerProcessingItem<>(item, sourceState, outputState));
                }
            }
        } catch (IOException e) {
            exception.set(e);
        }
    });
    if (!exception.isNull()) {
        throw exception.get();
    }
    final List<Key> toRemove = new ArrayList<>();
    cache.processSources(targetId, key -> {
        if (!keySet.contains(key)) {
            toRemove.add(key);
        }
        return true;
    });
    if (LOG.isDebugEnabled()) {
        LOG.debug(toProcess.size() + " items will be processed, " + toRemove.size() + " items will be removed");
        for (int i = 0; i < getItemsCountToShowInLog(toProcess.size()); i++) {
            LOG.debug("to process:" + toProcess.get(i).getItem().getKey());
        }
        for (int i = 0; i < getItemsCountToShowInLog(toRemove.size()); i++) {
            LOG.debug("to delete:" + toRemove.get(i));
        }
    }
    if (toProcess.isEmpty() && toRemove.isEmpty()) {
        return false;
    }
    if (myOnlyCheckStatus) {
        throw new ExitException(ExitStatus.CANCELLED);
    }
    List<GenericCompilerCacheState<Key, SourceState, OutputState>> obsoleteItems = new ArrayList<>();
    for (Key key : toRemove) {
        final GenericCompilerCache.PersistentStateData<SourceState, OutputState> data = cache.getState(targetId, key);
        obsoleteItems.add(new GenericCompilerCacheState<>(key, data.mySourceState, data.myOutputState));
    }
    final List<Item> processedItems = new ArrayList<>();
    final List<File> filesToRefresh = new ArrayList<>();
    final List<File> dirsToRefresh = new ArrayList<>();
    instance.processItems(target, toProcess, obsoleteItems, new GenericCompilerInstance.OutputConsumer<Item>() {

        @Override
        public void addFileToRefresh(@NotNull File file) {
            filesToRefresh.add(file);
        }

        @Override
        public void addDirectoryToRefresh(@NotNull File dir) {
            dirsToRefresh.add(dir);
        }

        @Override
        public void addProcessedItem(@NotNull Item sourceItem) {
            processedItems.add(sourceItem);
        }
    });
    checkForErrorsOrCanceled();
    CompilerUtil.runInContext(myContext, CompilerBundle.message("progress.updating.caches"), () -> {
        for (Key key : toRemove) {
            cache.remove(targetId, key);
        }
        CompilerUtil.refreshIOFiles(filesToRefresh);
        CompilerUtil.refreshIODirectories(dirsToRefresh);
        if (LOG.isDebugEnabled()) {
            LOG.debug("refreshed " + filesToRefresh.size() + " files and " + dirsToRefresh.size() + " dirs");
            for (int i = 0; i < getItemsCountToShowInLog(filesToRefresh.size()); i++) {
                LOG.debug("file: " + filesToRefresh.get(i));
            }
            for (int i = 0; i < getItemsCountToShowInLog(dirsToRefresh.size()); i++) {
                LOG.debug("dir: " + dirsToRefresh.get(i));
            }
        }
        final RunResult runResult = new ReadAction() {

            protected void run(@NotNull final Result result) throws Throwable {
                for (Item item : processedItems) {
                    SourceState sourceState = sourceStates.get(item);
                    if (sourceState == null) {
                        sourceState = item.computeSourceState();
                    }
                    cache.putState(targetId, item.getKey(), sourceState, item.computeOutputState());
                }
            }
        }.executeSilently();
        Throwable throwable = runResult.getThrowable();
        if (throwable instanceof IOException)
            throw (IOException) throwable;
        else if (throwable != null)
            throw new RuntimeException(throwable);
    });
    return true;
}
Also used : GenericCompilerCache(com.intellij.compiler.impl.generic.GenericCompilerCache) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult) ReadAction(com.intellij.openapi.application.ReadAction) RunResult(com.intellij.openapi.application.RunResult) IOException(java.io.IOException) THashSet(gnu.trove.THashSet) File(java.io.File)

Example 5 with RunResult

use of com.intellij.openapi.application.RunResult in project intellij-community by JetBrains.

the class VirtualFileDeleteProvider method deleteElement.

public void deleteElement(@NotNull DataContext dataContext) {
    final VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
    if (files == null || files.length == 0)
        return;
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    String message = createConfirmationMessage(files);
    int returnValue = Messages.showOkCancelDialog(message, UIBundle.message("delete.dialog.title"), ApplicationBundle.message("button.delete"), CommonBundle.getCancelButtonText(), Messages.getQuestionIcon());
    if (returnValue != Messages.OK)
        return;
    Arrays.sort(files, FileComparator.getInstance());
    List<String> problems = ContainerUtil.newLinkedList();
    CommandProcessor.getInstance().executeCommand(project, () -> {
        new Task.Modal(project, "Deleting Files...", true) {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                indicator.setIndeterminate(false);
                int i = 0;
                for (VirtualFile file : files) {
                    indicator.checkCanceled();
                    indicator.setText2(file.getPresentableUrl());
                    indicator.setFraction((double) i / files.length);
                    i++;
                    RunResult result = new WriteAction() {

                        @Override
                        protected void run(@NotNull Result result) throws Throwable {
                            file.delete(this);
                        }
                    }.executeSilently();
                    if (result.hasException()) {
                        LOG.info("Error when deleting " + file, result.getThrowable());
                        problems.add(file.getName());
                    }
                }
            }

            @Override
            public void onSuccess() {
                reportProblems();
            }

            @Override
            public void onCancel() {
                reportProblems();
            }

            private void reportProblems() {
                if (!problems.isEmpty()) {
                    reportDeletionProblem(problems);
                }
            }
        }.queue();
    }, "Deleting files", null);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) WriteAction(com.intellij.openapi.application.WriteAction) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult) Project(com.intellij.openapi.project.Project) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) RunResult(com.intellij.openapi.application.RunResult)

Aggregations

RunResult (com.intellij.openapi.application.RunResult)9 Result (com.intellij.openapi.application.Result)8 WriteAction (com.intellij.openapi.application.WriteAction)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)3 PsiElement (com.intellij.psi.PsiElement)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 IOException (java.io.IOException)2 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)1 CompleteMacro (com.intellij.codeInsight.template.macro.CompleteMacro)1 GenericCompilerCache (com.intellij.compiler.impl.generic.GenericCompilerCache)1 ReadAction (com.intellij.openapi.application.ReadAction)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1