com.intellij.AppTopics Java Examples

The following examples show how to use com.intellij.AppTopics. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IntellijLanguageClient.java    From lsp4intellij with Apache License 2.0 6 votes vote down vote up
@Override
public void initComponent() {
    try {
        // Adds project listener.
        ApplicationManager.getApplication().getMessageBus().connect().subscribe(ProjectManager.TOPIC,
                new LSPProjectManagerListener());
        // Adds editor listener.
        EditorFactory.getInstance().addEditorFactoryListener(new LSPEditorListener(), this);
        // Adds VFS listener.
        VirtualFileManager.getInstance().addVirtualFileListener(new VFSListener());
        // Adds document event listener.
        ApplicationManager.getApplication().getMessageBus().connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC,
                new LSPFileDocumentManagerListener());

        // in case if JVM forcefully exit.
        Runtime.getRuntime().addShutdownHook(new Thread(() -> projectToLanguageWrappers.values().stream()
                .flatMap(Collection::stream).filter(RUNNING).forEach(s -> s.stop(true))));

        LOG.info("Intellij Language Client initialized successfully");
    } catch (Exception e) {
        LOG.warn("Fatal error occurred when initializing Intellij language client.", e);
    }
}
 
Example #2
Source File: FlutterSaveActionsManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private FlutterSaveActionsManager(@NotNull Project project) {
  this.myProject = project;

  final MessageBus bus = project.getMessageBus();
  final MessageBusConnection connection = bus.connect();
  connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerListener() {
    @Override
    public void beforeDocumentSaving(@NotNull Document document) {
      // Don't try and format read only docs.
      if (!document.isWritable()) {
        return;
      }

      handleBeforeDocumentSaving(document);
    }
  });
}
 
Example #3
Source File: FlutterSaveActionsManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private FlutterSaveActionsManager(@NotNull Project project) {
  this.myProject = project;

  final MessageBus bus = project.getMessageBus();
  final MessageBusConnection connection = bus.connect();
  connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerListener() {
    @Override
    public void beforeDocumentSaving(@NotNull Document document) {
      // Don't try and format read only docs.
      if (!document.isWritable()) {
        return;
      }

      handleBeforeDocumentSaving(document);
    }
  });
}
 
Example #4
Source File: WakaTime.java    From jetbrains-wakatime with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void setupEventListeners() {
    ApplicationManager.getApplication().invokeLater(new Runnable(){
        public void run() {

            // save file
            MessageBus bus = ApplicationManager.getApplication().getMessageBus();
            connection = bus.connect();
            connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new CustomSaveListener());

            // edit document
            EditorFactory.getInstance().getEventMulticaster().addDocumentListener(new CustomDocumentListener());

            // mouse press
            EditorFactory.getInstance().getEventMulticaster().addEditorMouseListener(new CustomEditorMouseListener());

            // scroll document
            EditorFactory.getInstance().getEventMulticaster().addVisibleAreaListener(new CustomVisibleAreaListener());
        }
    });
}
 
Example #5
Source File: FileDocumentManagerImplTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testSaveDocument_DoNotSaveIfModStampEqualsToFile() throws Exception {
  final VirtualFile file = createFile();
  final DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file);
  assertNotNull(file.toString(), document);
  WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
    @Override
    public void run() {
      document.insertString(0, "zzz");
      document.setModificationStamp(file.getModificationStamp());
    }
  });

  getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
    @Override
    public void beforeDocumentSaving(@Nonnull Document documentToSave) {
      assertNotSame(document, documentToSave);
    }
  });

  myDocumentManager.saveDocument(document);
}
 
Example #6
Source File: ConfigProjectComponent.java    From editorconfig-jetbrains with MIT License 5 votes vote down vote up
public ConfigProjectComponent(Project project) {
    this.project = project;

    // Register project-level config managers
    MessageBus bus = project.getMessageBus();
    codeStyleManager = new CodeStyleManager(project);
    EditorSettingsManager editorSettingsManager = new EditorSettingsManager();
    EncodingManager encodingManager = new EncodingManager(project);
    LineEndingsManager lineEndingsManager = new LineEndingsManager(project);
    bus.connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, codeStyleManager);
    bus.connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC, encodingManager);
    bus.connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC, editorSettingsManager);
    bus.connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC, lineEndingsManager);
}
 
Example #7
Source File: Listener.java    From floobits-intellij with Apache License 2.0 5 votes vote down vote up
public synchronized void start(final EditorEventHandler editorManager) {
    this.editorManager = editorManager;
    connection.subscribe(VirtualFileManager.VFS_CHANGES, this);
    connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, this);
    em.addDocumentListener(this);
    em.addSelectionListener(this);
    em.addCaretListener(this);
    em.addVisibleAreaListener(this);


    virtualFileAdapter = new VirtualFileAdapter() {
        public void beforePropertyChange(@NotNull final VirtualFilePropertyEvent event) {
            if (!event.getPropertyName().equals(VirtualFile.PROP_NAME)) {
                return;
            }
            VirtualFile parent = event.getParent();
            if (parent == null) {
                return;
            }
            String parentPath = parent.getPath();
            // XXX: pretty sure is this wrong.
            String newValue = parentPath + "/" + event.getNewValue().toString();
            String oldValue = parentPath + "/" + event.getOldValue().toString();
            editorManager.rename(oldValue, newValue);
        }
    };
    VirtualFileManager.getInstance().addVirtualFileListener(virtualFileAdapter);
}
 
Example #8
Source File: DelayedDocumentWatcher.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void activate() {
  if (myConnection == null) {
    myListenerDisposable = Disposable.newDisposable();
    Disposer.register(myProject, myListenerDisposable);
    EditorFactory.getInstance().getEventMulticaster().addDocumentListener(myListener, myListenerDisposable);
    myConnection = ApplicationManager.getApplication().getMessageBus().connect(myProject);
    myConnection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
      @Override
      public void beforeAllDocumentsSaving() {
        myDocumentSavingInProgress = true;
        ApplicationManager.getApplication().invokeLater(() -> myDocumentSavingInProgress = false, ModalityState.any());
      }
    });
  }
}
 
Example #9
Source File: FileDocumentManagerImplTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testReplaceDocumentTextWithTheSameText() throws Exception {
  final VirtualFile file = createFile();
  final DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file);

  final String newText = "test text";
  assertNotNull(file.toString(), document);
  WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
    @Override
    public void run() {
      document.replaceString(0, document.getTextLength(), newText);
      assertTrue(myDocumentManager.isDocumentUnsaved(document));
      myDocumentManager.saveDocument(document);

      getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
        @Override
        public void beforeDocumentSaving(@Nonnull Document documentToSave) {
          assertNotSame(document, documentToSave);
        }
      });

      final long modificationStamp = document.getModificationStamp();

      document.replaceString(0, document.getTextLength(), newText);
      if (myDocumentManager.isDocumentUnsaved(document)) {
        assertTrue(document.getModificationStamp() > modificationStamp);
      }
      else {
        assertEquals(modificationStamp, document.getModificationStamp());
      }
    }
  });
}
 
Example #10
Source File: VetoSavingCommittingDocumentsAdapter.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Inject
public VetoSavingCommittingDocumentsAdapter(final FileDocumentManager fileDocumentManager) {
  myFileDocumentManager = fileDocumentManager;

  ApplicationManager.getApplication().getMessageBus().connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
    @Override
    public void beforeAllDocumentsSaving() {
      Map<Document, Project> documentsToWarn = getDocumentsBeingCommitted();
      if (!documentsToWarn.isEmpty()) {
        boolean allowSave = showAllowSaveDialog(documentsToWarn);
        updateSaveability(documentsToWarn, allowSave);
      }
    }
  });
}
 
Example #11
Source File: ConsoleHistoryController.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void install() {
  class Listener extends FileDocumentManagerAdapter implements ProjectEx.ProjectSaved {
    @Override
    public void beforeDocumentSaving(@Nonnull Document document) {
      if (document == myConsole.getEditorDocument()) {
        saveHistory();
      }
    }

    @Override
    public void saved(@Nonnull Project project) {
      saveHistory();
    }
  }
  Listener listener = new Listener();
  ApplicationManager.getApplication().getMessageBus().connect(myConsole).subscribe(ProjectEx.ProjectSaved.TOPIC, listener);
  myConsole.getProject().getMessageBus().connect(myConsole).subscribe(AppTopics.FILE_DOCUMENT_SYNC, listener);

  myConsole.getVirtualFile().putUserData(CONTROLLER_KEY, this);
  Disposer.register(myConsole, new Disposable() {
    @Override
    public void dispose() {
      myConsole.getVirtualFile().putUserData(CONTROLLER_KEY, null);
      saveHistory();
    }
  });
  if (myHelper.getModel().getHistorySize() == 0) {
    loadHistory(myHelper.getId());
  }
  configureActions();
  myLastSaveStamp = getCurrentTimeStamp();
}
 
Example #12
Source File: PsiVFSListener.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Inject
public PsiVFSListener(@Nonnull Project project, Provider<ProjectFileIndex> fileIndex) {
  installGlobalListener();

  myProject = project;
  myFileTypeManager = FileTypeManager.getInstance();
  myFileIndex = fileIndex;
  myManager = (PsiManagerImpl)PsiManager.getInstance(project);
  myFileManager = (FileManagerImpl)myManager.getFileManager();

  if (project.isDefault()) {
    return;
  }

  // events must handled only after pre-startup (https://upsource.jetbrains.com/intellij/review/IDEA-CR-47395)
  StartupManager.getInstance(project).registerPreStartupActivity(() -> {
    MessageBusConnection connection = project.getMessageBus().connect();
    connection.subscribe(ProjectTopics.PROJECT_ROOTS, new MyModuleRootListener());
    connection.subscribe(FileTypeManager.TOPIC, new FileTypeListener() {
      @Override
      public void fileTypesChanged(@Nonnull FileTypeEvent e) {
        myFileManager.processFileTypesChanged(e.getRemovedFileType() != null);
      }
    });
    connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new MyFileDocumentManagerAdapter());
  });
}
 
Example #13
Source File: PsiDocumentManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public PsiDocumentManagerImpl(@Nonnull Project project, @Nonnull DocumentCommitProcessor documentCommitProcessor) {
  super(project, documentCommitProcessor);

  EditorFactory.getInstance().getEventMulticaster().addDocumentListener(this, this);
  ((EditorEventMulticasterImpl)EditorFactory.getInstance().getEventMulticaster()).addPrioritizedDocumentListener(new PriorityEventCollector(), this);
  MessageBusConnection connection = project.getMessageBus().connect(this);
  connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerListener() {
    @Override
    public void fileContentLoaded(@Nonnull final VirtualFile virtualFile, @Nonnull Document document) {
      PsiFile psiFile = ReadAction.compute(() -> myProject.isDisposed() || !virtualFile.isValid() ? null : getCachedPsiFile(virtualFile));
      fireDocumentCreated(document, psiFile);
    }
  });
  Disposer.register(this, () -> ((DocumentCommitThread)myDocumentCommitProcessor).cancelTasksOnProjectDispose(project));
}