Skip to content

Commit

Permalink
Show notification in case no config changes were made by plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
swesteme authored and nailujx86 committed Jun 21, 2024
1 parent 81806e1 commit ac445df
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package de.gebit.plugins.autoconfig.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import de.gebit.plugins.autoconfig.AutoconfigStartup;
import de.gebit.plugins.autoconfig.ConfigurationLoaderService;
import de.gebit.plugins.autoconfig.util.Notifications;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;

/**
* Action to apply the projects Autoconfig-Configurations manually
* Action to apply the projects Autoconfig-Configurations manually.
*/
public class RunAutoconfigAction extends DumbAwareAction {
@Override
Expand All @@ -23,6 +30,31 @@ public void actionPerformed(@NotNull AnActionEvent e) {
configurationLoaderService.resetConfigurationCache();
}

new AutoconfigStartup().runAutoconfig(project);
// save currently open and unsaved config files
saveUnsavedAutoconfigs();

// perform changes
List<String> changedSettings = new AutoconfigStartup().runAutoconfig(project);

// notify user if no changes were necessary
if (changedSettings == null || changedSettings.isEmpty()) {
Notifications.showInfo("No settings have been changed.", project);
}
}

private void saveUnsavedAutoconfigs() {
FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
Arrays.stream(fileDocumentManager.getUnsavedDocuments())
.filter(d -> isAutoconfigFile(fileDocumentManager.getFile(d)))
.forEach(fileDocumentManager::saveDocument);
}

private boolean isAutoconfigFile(@Nullable VirtualFile document) {
if (document == null) {
return false;
}
String[] components = document.getUrl().split("/");
String lastComponent = components[components.length - 1];
return lastComponent.startsWith("autoconfig") && lastComponent.endsWith(".yaml");
}
}

0 comments on commit ac445df

Please sign in to comment.