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();
}
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);
}
}
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();
}
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);
}
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());
}
Aggregations