Skip to content

Commit

Permalink
Migrated StartupActivities to the ProjectActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
hurricup committed Feb 24, 2024
1 parent a1d7b59 commit a8feed6
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 314 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2023 Alexandr Evstigneev
* Copyright 2015-2024 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,12 +23,9 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.search.GlobalSearchScope;
Expand Down Expand Up @@ -121,7 +118,7 @@ public boolean isMojoAvailable() {
/**
* Queues model update
*/
private void scheduleUpdate() {
void scheduleUpdate() {
LOG.debug("Scheduling update");
myUpdateQueue.queue(Update.create(this, this::updateModel));
}
Expand Down Expand Up @@ -264,21 +261,6 @@ private void doUpdateModel() {
return project.getService(MojoProjectManager.class);
}

public static class Starter implements StartupActivity, DumbAware {
@Override
public void runActivity(@NotNull Project project) {
if (project.isDefault()) {
return;
}
StartupManager.getInstance(project).runAfterOpened(() -> ReadAction.run(() -> {
if (!project.isDisposed()) {
LOG.debug("Project is initialized");
getInstance(project).scheduleUpdate();
}
}));
}
}

private static class Model {
private final @NotNull Map<VirtualFile, MojoProject> myProjectRoots;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2015-2024 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.perl5.lang.mojolicious.model

import com.intellij.openapi.application.readAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity

private val log = Logger.getInstance(MojoProjectManagerStarter::class.java)

class MojoProjectManagerStarter : ProjectActivity {
override suspend fun execute(project: Project) {
if (project.isDefault()) {
return
}
readAction {
if (!project.isDisposed()) {
log.debug("Project is initialized")
MojoProjectManager.getInstance(project).scheduleUpdate()
}
}
}
}
2 changes: 1 addition & 1 deletion mojo/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
implementation="com.perl5.lang.mojolicious.idea.modules.MojoTemplateSourceRootEditHandler"/>

<projectService serviceImplementation="com.perl5.lang.mojolicious.model.MojoProjectManager"/>
<postStartupActivity implementation="com.perl5.lang.mojolicious.model.MojoProjectManager$Starter"/>
<postStartupActivity implementation="com.perl5.lang.mojolicious.model.MojoProjectManagerStarter"/>

<treeStructureProvider implementation="com.perl5.lang.mojolicious.idea.projectView.MojoTreeStructureProvider"/>
</extensions>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2015-2024 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.perl5.lang.perl.fileTypes

import com.intellij.openapi.module.Module
import com.intellij.openapi.project.ModuleListener
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ModuleRootEvent
import com.intellij.openapi.roots.ModuleRootListener
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.util.Function
import com.perl5.lang.perl.util.PerlPluginUtil

class PerlFileTypeServiceWatcher : ProjectActivity, ModuleListener, ModuleRootListener {
override suspend fun execute(project: Project) {
project.messageBus.connect(PerlPluginUtil.getUnloadAwareDisposable(project)).let {
it.subscribe(ModuleRootListener.TOPIC, this);
it.subscribe(ModuleListener.TOPIC, this);
}
reset();
}

override fun modulesAdded(project: Project, modules: List<Module?>) = reset()

override fun modulesRenamed(project: Project, modules: List<Module?>, oldNameProvider: Function<in Module, String?>) = reset()

override fun rootsChanged(event: ModuleRootEvent) = reset()

private fun reset() = PerlFileTypeService.getInstance().reset()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2015-2024 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.perl5.lang.perl.idea.project

import com.intellij.ide.startup.ServiceNotReadyException
import com.intellij.notification.BrowseNotificationAction
import com.intellij.notification.Notification
import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.util.FileContentUtil
import com.perl5.PerlBundle
import com.perl5.lang.perl.idea.configuration.settings.PerlApplicationSettings
import com.perl5.lang.perl.util.PerlPluginUtil

private val log = Logger.getInstance(Perl5ProjectStartupActivity::class.java)

class Perl5ProjectStartupActivity : ProjectActivity {
override suspend fun execute(project: Project) {
if (project.isDefault()) {
return
}
val settings = PerlApplicationSettings.getInstance()
if (settings.shouldShowAnnounce()) {

settings.setAnnounceShown()
val notification = Notification(
"perl5.plugin.update.notification.group",
PerlBundle.message("plugin.update.baloon.title", PerlPluginUtil.getPluginVersion()),
PerlBundle.message("plugin.update.baloon.text"),
NotificationType.INFORMATION
).setImportant(true)
.addAction(
BrowseNotificationAction(PerlBundle.message("plugin.update.baloon.changes"), "https://plugins.jetbrains.com/plugin/7796")
)
.addAction(
BrowseNotificationAction(PerlBundle.message("plugin.update.baloon.tracker"), "https://github.com/hurricup/Perl5-IDEA/issues")
)

Notifications.Bus.notify(notification)
}
if (!ApplicationManager.getApplication().isUnitTestMode()) {
scheduleNamesUpdateWithReparse(project)
}
}

private fun scheduleNamesUpdateWithReparse(project: Project) {
ApplicationManager.getApplication().executeOnPooledThread { initNamesWithReparse(project) }
}


private fun initNamesWithReparse(project: Project) {
if (project.isDisposed()) {
return
}
try {
PerlNamesCache.getInstance(project).forceCacheUpdate()
ApplicationManager.getApplication().invokeLater(FileContentUtil::reparseOpenedFiles)
} catch (e: ServiceNotReadyException) {
log.warn(e)
DumbService.getInstance(project).smartInvokeLater { scheduleNamesUpdateWithReparse(project) }
}
}
}
Loading

0 comments on commit a8feed6

Please sign in to comment.