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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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,34 @@
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 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).isRunOnSave();
}

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

for (Document document : documents) {
var file = PsiDocumentManager.getInstance(project).getPsiFile(document);
ragurney marked this conversation as resolved.
Show resolved Hide resolved

if (file == null) {
ragurney marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

new ReformatCodeProcessor(file).run();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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> {

ragurney marked this conversation as resolved.
Show resolved Hide resolved

private static final boolean SPOTLESS_ON_SAVE_DEFAULT = false;
private @NotNull State myState = new State();
ragurney marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

public boolean isRunOnSave() {
ragurney marked this conversation as resolved.
Show resolved Hide resolved
return myState.myRunOnSave;
}

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

static class State {
public boolean myRunOnSave = SPOTLESS_ON_SAVE_DEFAULT;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;

@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()).isRunOnSave();
}

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

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

@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>