Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActionOnSave #33

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pluginUntilBuild = 221.*

# IntelliJ Platform Properties -> https://github.com/JetBrains/gradle-intellij-plugin#intellij-platform-properties
platformType = IC
platformVersion = 2021.1.2
platformVersion = 2021.2.1

# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
platformPlugins = com.intellij.gradle:211.7442.57
platformPlugins = com.intellij.gradle

# Java language level used to compile sources and to generate the files for - Java 11 is required since 2020.3
javaVersion = 11
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.ragurney.spotless.actions;

import com.github.ragurney.spotless.config.SpotlessConfiguration;
import com.intellij.ide.actionsOnSave.impl.ActionsOnSaveFileDocumentManagerListener;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;

/** Action to be called whenever a document is saved */
public class SpotlessActionOnSave extends ActionsOnSaveFileDocumentManagerListener.ActionOnSave {
@Override
public boolean isEnabledForProject(@NotNull Project project) {
return SpotlessConfiguration.getInstance(project).isRunOnSaveEnabled();
}

@Override
public void processDocuments(@NotNull Project project, @NotNull Document @NotNull [] documents) {
SpotlessConfiguration spotlessConfiguration = SpotlessConfiguration.getInstance(project);
if (!spotlessConfiguration.isRunOnSaveEnabled()) return;

for (Document document : documents) {
Optional.ofNullable(PsiDocumentManager.getInstance(project).getPsiFile(document))
.ifPresent(file -> new ReformatCodeProcessor(file).run());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.ragurney.spotless.config;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

/** Configuration to store whether spotless should be executed on save */
@State(name = "SpotlessConfiguration", storages = @Storage("spotless.xml"))
public class SpotlessConfiguration
implements PersistentStateComponent<SpotlessConfiguration.State> {

private static final boolean SPOTLESS_ON_SAVE_DEFAULT = false;
private @NotNull State state = new State();

@NotNull
public static SpotlessConfiguration getInstance(@NotNull Project project) {
return project.getService(SpotlessConfiguration.class);
}

@Override
public @NotNull State getState() {
return state;
}

@Override
public void loadState(@NotNull State state) {
this.state = state;
}

public boolean isRunOnSaveEnabled() {
return state.runOnSave;
}

public void setRunOnSave(boolean runOnSave) {
state.runOnSave = runOnSave;
}

static class State {
public boolean runOnSave = SPOTLESS_ON_SAVE_DEFAULT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.ragurney.spotless.config;

import com.intellij.ide.actionsOnSave.ActionOnSaveComment;
import com.intellij.ide.actionsOnSave.ActionOnSaveContext;
import com.intellij.ide.actionsOnSave.ActionOnSaveInfo;
import com.intellij.ide.actionsOnSave.ActionOnSaveInfoProvider;
import com.intellij.openapi.util.NlsContexts;
import java.util.Collection;
import java.util.Collections;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@SuppressWarnings("UnstableApiUsage")
public class SpotlessOnSaveInfoProvider extends ActionOnSaveInfoProvider {
@Override
protected @NotNull Collection<? extends ActionOnSaveInfo> getActionOnSaveInfos(
@NotNull ActionOnSaveContext context) {
return Collections.singleton(new SpotlessActionOnSaveInfo(context));
}

static class SpotlessActionOnSaveInfo extends ActionOnSaveInfo {

private boolean isActionOnSaveEnabled;

protected SpotlessActionOnSaveInfo(@NotNull ActionOnSaveContext context) {
super(context);
isActionOnSaveEnabled = SpotlessConfiguration.getInstance(getProject()).isRunOnSaveEnabled();
}

@Override
protected void apply() {
SpotlessConfiguration.getInstance(getProject()).setRunOnSave(isActionOnSaveEnabled);
}

@Override
protected boolean isModified() {
return isActionOnSaveEnabled
!= SpotlessConfiguration.getInstance(getProject()).isRunOnSaveEnabled();
}

@Override
public @NotNull @NlsContexts.Checkbox String getActionOnSaveName() {
return "Run spotless";
}

@Override
public boolean isActionOnSaveEnabled() {
return isActionOnSaveEnabled;
}

@Override
public void setActionOnSaveEnabled(boolean enabled) {
isActionOnSaveEnabled = enabled;
}

@Override
public @Nullable ActionOnSaveComment getComment() {
return ActionOnSaveComment.info("Runs spotless apply on changed files");
}
}
}
10 changes: 10 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@
<add-to-group group-id="CodeFormatGroup" relative-to-action="ReformatCode" anchor="before"/>
</action>
</actions>

<extensions defaultExtensionNs="com.intellij">
<projectService serviceImplementation="com.github.ragurney.spotless.config.SpotlessConfiguration"/>
<actionOnSaveInfoProvider id="SpotlessOnSaveInfoProvider"
implementation="com.github.ragurney.spotless.config.SpotlessOnSaveInfoProvider"
order="after FormatOnSaveInfoProvider, before BuildOnSaveInfoProvider, before FileWatcherOnSaveInfoProvider, before UploadOnSaveInfoProvider"/>
<actionOnSave id="SpotlessActionOnSave"
implementation="com.github.ragurney.spotless.actions.SpotlessActionOnSave"
order="after FormatOnSaveAction"/>
</extensions>
</idea-plugin>