Search in sources :

Example 21 with Result

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

the class MavenProjectsManagerWatcher method start.

public synchronized void start() {
    final MessageBusConnection myBusConnection = myProject.getMessageBus().connect(myChangedDocumentsQueue);
    myBusConnection.subscribe(VirtualFileManager.VFS_CHANGES, new MyFileChangeListener());
    myBusConnection.subscribe(ProjectTopics.PROJECT_ROOTS, new MyRootChangesListener());
    myChangedDocumentsQueue.makeUserAware(myProject);
    myChangedDocumentsQueue.activate();
    myBusConnection.subscribe(ProjectTopics.MODULES, new ModuleListener() {

        @Override
        public void moduleRemoved(@NotNull Project project, @NotNull Module module) {
            MavenProject mavenProject = myManager.findProject(module);
            if (mavenProject != null && !myManager.isIgnored(mavenProject)) {
                VirtualFile file = mavenProject.getFile();
                if (myManager.isManagedFile(file) && myManager.getModules(mavenProject).isEmpty()) {
                    myManager.removeManagedFiles(Collections.singletonList(file));
                } else {
                    myManager.setIgnoredState(Collections.singletonList(mavenProject), true);
                }
            }
        }

        @Override
        public void moduleAdded(@NotNull final Project project, @NotNull final Module module) {
            // this method is needed to return non-ignored status for modules that were deleted (and thus ignored) and then created again with a different module type
            if (myManager.isMavenizedModule(module)) {
                MavenProject mavenProject = myManager.findProject(module);
                if (mavenProject != null)
                    myManager.setIgnoredState(Collections.singletonList(mavenProject), false);
            }
        }
    });
    DocumentAdapter myDocumentListener = new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent event) {
            Document doc = event.getDocument();
            VirtualFile file = FileDocumentManager.getInstance().getFile(doc);
            if (file == null)
                return;
            String fileName = file.getName();
            boolean isMavenFile = fileName.equals(MavenConstants.POM_XML) || fileName.equals(MavenConstants.PROFILES_XML) || isSettingsFile(file) || fileName.startsWith("pom.");
            if (!isMavenFile)
                return;
            synchronized (myChangedDocuments) {
                myChangedDocuments.add(doc);
            }
            myChangedDocumentsQueue.queue(new Update(MavenProjectsManagerWatcher.this) {

                @Override
                public void run() {
                    final Document[] copy;
                    synchronized (myChangedDocuments) {
                        copy = myChangedDocuments.toArray(new Document[myChangedDocuments.size()]);
                        myChangedDocuments.clear();
                    }
                    MavenUtil.invokeLater(myProject, () -> new WriteAction() {

                        @Override
                        protected void run(@NotNull Result result) throws Throwable {
                            for (Document each : copy) {
                                PsiDocumentManager.getInstance(myProject).commitDocument(each);
                                ((FileDocumentManagerImpl) FileDocumentManager.getInstance()).saveDocument(each, false);
                            }
                        }
                    }.execute());
                }
            });
        }
    };
    EditorFactory.getInstance().getEventMulticaster().addDocumentListener(myDocumentListener, myBusConnection);
    final MavenGeneralSettings.Listener mySettingsPathsChangesListener = new MavenGeneralSettings.Listener() {

        @Override
        public void changed() {
            updateSettingsFilePointers();
            onSettingsChange();
        }
    };
    myGeneralSettings.addListener(mySettingsPathsChangesListener);
    Disposer.register(myChangedDocumentsQueue, new Disposable() {

        @Override
        public void dispose() {
            myGeneralSettings.removeListener(mySettingsPathsChangesListener);
            mySettingsFilesPointers.clear();
        }
    });
    updateSettingsFilePointers();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Disposable(com.intellij.openapi.Disposable) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModuleListener(com.intellij.openapi.project.ModuleListener) ModuleRootListener(com.intellij.openapi.roots.ModuleRootListener) VirtualFilePointerListener(com.intellij.openapi.vfs.pointers.VirtualFilePointerListener) ModuleListener(com.intellij.openapi.project.ModuleListener) WriteAction(com.intellij.openapi.application.WriteAction) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) Document(com.intellij.openapi.editor.Document) Update(com.intellij.util.ui.update.Update) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module)

Example 22 with Result

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

the class MavenOpenOrCreateFilesAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    final Project project = MavenActionUtil.getProject(e.getDataContext());
    if (project == null)
        return;
    final List<File> files = getFiles(e);
    final List<VirtualFile> virtualFiles = collectVirtualFiles(files);
    if (files.size() == 1 && virtualFiles.isEmpty()) {
        new WriteCommandAction(project, e.getPresentation().getText()) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                File file = files.get(0);
                try {
                    final VirtualFile virtualFile = VfsUtil.createDirectoryIfMissing(file.getParent());
                    if (virtualFile != null) {
                        VirtualFile newFile = virtualFile.createChildData(this, file.getName());
                        virtualFiles.add(newFile);
                        MavenUtil.runFileTemplate(project, newFile, getFileTemplate());
                    }
                } catch (IOException ex) {
                    MavenUtil.showError(project, "Cannot create " + file.getName(), ex);
                }
            }
        }.execute();
        return;
    }
    for (VirtualFile each : virtualFiles) {
        new OpenFileDescriptor(project, each).navigate(true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Result(com.intellij.openapi.application.Result)

Example 23 with Result

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

the class GrCreateSubclassAction method createSubclassGroovy.

@Nullable
public static PsiClass createSubclassGroovy(final GrTypeDefinition psiClass, final PsiDirectory targetDirectory, final String className) {
    final Project project = psiClass.getProject();
    final Ref<GrTypeDefinition> targetClass = new Ref<>();
    new WriteCommandAction(project, getTitle(psiClass), getTitle(psiClass)) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
            final GrTypeParameterList oldTypeParameterList = psiClass.getTypeParameterList();
            try {
                targetClass.set(CreateClassActionBase.createClassByType(targetDirectory, className, PsiManager.getInstance(project), psiClass, GroovyTemplates.GROOVY_CLASS, true));
            } catch (final IncorrectOperationException e) {
                ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(project, CodeInsightBundle.message("intention.error.cannot.create.class.message", className) + "\n" + e.getLocalizedMessage(), CodeInsightBundle.message("intention.error.cannot.create.class.title")));
                return;
            }
            startTemplate(oldTypeParameterList, project, psiClass, targetClass.get(), false);
        }
    }.execute();
    if (targetClass.get() == null)
        return null;
    if (!ApplicationManager.getApplication().isUnitTestMode() && !psiClass.hasTypeParameters()) {
        final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.get().getContainingFile(), targetClass.get());
        if (editor == null)
            return targetClass.get();
        chooseAndImplement(psiClass, project, targetClass.get(), editor);
    }
    return targetClass.get();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) GrTypeParameterList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList) Ref(com.intellij.openapi.util.Ref) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) Result(com.intellij.openapi.application.Result) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with Result

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

the class PySmartEnterTest method doTest.

public void doTest() {
    myFixture.configureByFile("codeInsight/smartEnter/" + getTestName(true) + ".py");
    final List<SmartEnterProcessor> processors = getSmartProcessors(PythonLanguage.getInstance());
    new WriteCommandAction(myFixture.getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            final Editor editor = myFixture.getEditor();
            for (SmartEnterProcessor processor : processors) {
                processor.process(myFixture.getProject(), editor, myFixture.getFile());
            }
        }
    }.execute();
    myFixture.checkResultByFile("codeInsight/smartEnter/" + getTestName(true) + "_after.py", true);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Editor(com.intellij.openapi.editor.Editor) SmartEnterProcessor(com.intellij.codeInsight.editorActions.smartEnter.SmartEnterProcessor) Result(com.intellij.openapi.application.Result)

Example 25 with Result

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

the class TreeIncrementalUpdateTest method testInvalidateParent.

public void testInvalidateParent() throws Throwable {
    final MyElement root = getDomManager().createMockElement(MyElement.class, null, true);
    new WriteCommandAction<MyElement>(getProject()) {

        @Override
        protected void run(@NotNull Result<MyElement> result) throws Throwable {
            root.getChild().ensureTagExists();
            root.getChild2().ensureTagExists();
            final MyElement element = root.addChildElement().getChild();
            result.setResult(element);
            element.ensureTagExists().getValue().setText("abc");
            root.addChildElement();
            root.addChildElement();
        }
    }.execute().getResultObject();
    assertTrue(root.isValid());
    final MyElement element = root.getChildElements().get(0).getChild();
    assertTrue(element.isValid());
    final MyElement child = element.getChild();
    final MyElement genericValue = child.getChild();
    new WriteCommandAction(getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            final Document document = getDocument(DomUtil.getFile(element));
            final TextRange range = element.getXmlTag().getTextRange();
            document.replaceString(range.getStartOffset(), range.getEndOffset(), "");
            commitDocument(document);
        }
    }.execute();
    assertFalse(genericValue.isValid());
    assertFalse(child.isValid());
    assertFalse(element.isValid());
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result)

Aggregations

Result (com.intellij.openapi.application.Result)278 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)177 NotNull (org.jetbrains.annotations.NotNull)111 WriteAction (com.intellij.openapi.application.WriteAction)87 VirtualFile (com.intellij.openapi.vfs.VirtualFile)76 Project (com.intellij.openapi.project.Project)55 File (java.io.File)30 Document (com.intellij.openapi.editor.Document)28 Module (com.intellij.openapi.module.Module)28 XmlFile (com.intellij.psi.xml.XmlFile)28 IOException (java.io.IOException)26 PsiFile (com.intellij.psi.PsiFile)25 XmlTag (com.intellij.psi.xml.XmlTag)24 Nullable (org.jetbrains.annotations.Nullable)16 ArrayList (java.util.ArrayList)15 ReadAction (com.intellij.openapi.application.ReadAction)14 Editor (com.intellij.openapi.editor.Editor)12 NlModel (com.android.tools.idea.uibuilder.model.NlModel)11 AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)10 TextRange (com.intellij.openapi.util.TextRange)10