From 0d7fd3f04438ff29ed128ddcf9f153692c4a342a Mon Sep 17 00:00:00 2001 From: Jure Repe Date: Sun, 15 Jan 2023 15:49:09 +0100 Subject: [PATCH 1/2] Add action to reformat all files --- build.gradle.kts | 2 +- gradle.properties | 2 +- .../actions/ReformatCodeProcessor.java | 12 +++++-- ...odeAction.java => ReformatFileAction.java} | 6 ++-- .../actions/ReformatProjectAction.java | 36 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 16 ++++++--- 6 files changed, 62 insertions(+), 12 deletions(-) rename src/main/java/com/github/ragurney/spotless/actions/{ReformatCodeAction.java => ReformatFileAction.java} (84%) create mode 100644 src/main/java/com/github/ragurney/spotless/actions/ReformatProjectAction.java diff --git a/build.gradle.kts b/build.gradle.kts index 85405b2..16b4e6c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { // Kotlin support id("org.jetbrains.kotlin.jvm") version "1.6.0" // Gradle IntelliJ Plugin - id("org.jetbrains.intellij") version "1.10.0" + id("org.jetbrains.intellij") version "1.12.0" // Gradle Changelog Plugin id("org.jetbrains.changelog") version "1.3.1" // Gradle Qodana Plugin diff --git a/gradle.properties b/gradle.properties index 7cc78c1..1d2e083 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ pluginGroup = com.github.ragurney.spotless pluginName = Spotless Gradle # SemVer format -> https://semver.org -pluginVersion = 1.0.10 +pluginVersion = 1.1.0 # See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html # for insight into build numbers and IntelliJ Platform versions. diff --git a/src/main/java/com/github/ragurney/spotless/actions/ReformatCodeProcessor.java b/src/main/java/com/github/ragurney/spotless/actions/ReformatCodeProcessor.java index 765101c..c8be9cd 100644 --- a/src/main/java/com/github/ragurney/spotless/actions/ReformatCodeProcessor.java +++ b/src/main/java/com/github/ragurney/spotless/actions/ReformatCodeProcessor.java @@ -38,11 +38,17 @@ public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor { private static final Logger LOG = Logger.getInstance(ReformatCodeProcessor.class); private static final Version NO_CONFIG_CACHE_MIN_GRADLE_VERSION = new Version(6, 6, 0); + private boolean isReformatAllEnabled = false; public ReformatCodeProcessor(@NotNull PsiFile file) { super(file.getProject(), file, getProgressText(), getCommandName(), true); } + public ReformatCodeProcessor(@NotNull PsiFile file, boolean reformatAll) { + super(file.getProject(), file, getProgressText(), getCommandName(), true); + this.isReformatAllEnabled = reformatAll; + } + @Override protected @NotNull FutureTask prepareTask(@NotNull PsiFile file, boolean processChangedTextOnly) throws IncorrectOperationException { @@ -104,8 +110,10 @@ private ExternalSystemTaskExecutionSettings constructTaskExecutionSettings(PsiFi ExternalSystemTaskExecutionSettings settings = new ExternalSystemTaskExecutionSettings(); settings.setExternalProjectPath(myProject.getBasePath()); settings.setTaskNames(List.of("spotlessApply")); - settings.setScriptParameters( - String.format("-PspotlessIdeHook=\"%s\"%s", fileToProcess.getVirtualFile().getPath(), noConfigCacheOption)); + if (!isReformatAllEnabled) { + settings.setScriptParameters( + String.format("-PspotlessIdeHook=\"%s\"%s", fileToProcess.getVirtualFile().getPath(), noConfigCacheOption)); + } settings.setVmOptions(""); settings.setExternalSystemIdString(GradleConstants.SYSTEM_ID.getId()); return settings; diff --git a/src/main/java/com/github/ragurney/spotless/actions/ReformatCodeAction.java b/src/main/java/com/github/ragurney/spotless/actions/ReformatFileAction.java similarity index 84% rename from src/main/java/com/github/ragurney/spotless/actions/ReformatCodeAction.java rename to src/main/java/com/github/ragurney/spotless/actions/ReformatFileAction.java index fba4d5d..ca546a9 100644 --- a/src/main/java/com/github/ragurney/spotless/actions/ReformatCodeAction.java +++ b/src/main/java/com/github/ragurney/spotless/actions/ReformatFileAction.java @@ -13,10 +13,10 @@ /** - * The action which is called on "Reformat Code with Spotless." + * The action which is called on "Reformat Current File." */ -public class ReformatCodeAction extends AnAction { - private static final Logger LOG = Logger.getInstance(ReformatCodeAction.class); +public class ReformatFileAction extends AnAction { + private static final Logger LOG = Logger.getInstance(ReformatFileAction.class); @Override public void actionPerformed(@NotNull AnActionEvent event) { diff --git a/src/main/java/com/github/ragurney/spotless/actions/ReformatProjectAction.java b/src/main/java/com/github/ragurney/spotless/actions/ReformatProjectAction.java new file mode 100644 index 0000000..bce8adc --- /dev/null +++ b/src/main/java/com/github/ragurney/spotless/actions/ReformatProjectAction.java @@ -0,0 +1,36 @@ +package com.github.ragurney.spotless.actions; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDocumentManager; +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; + + +/** + * The action which is called on "Reformat All Files." + */ +public class ReformatProjectAction extends AnAction { + + @Override + public void actionPerformed(@NotNull AnActionEvent event) { + DataContext dataContext = event.getDataContext(); + event.getData(CommonDataKeys.PSI_FILE); + + final Project project = CommonDataKeys.PROJECT.getData(dataContext); + if (project == null) { + return; + } + + final Editor editor = CommonDataKeys.EDITOR.getData(dataContext); + if (editor != null) { + Optional.ofNullable(PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument())) + .ifPresent(file -> new ReformatCodeProcessor(file, true).run()); + } + } +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index c3554f7..7867e97 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -8,11 +8,17 @@ com.intellij.gradle - + + + - + From 471f5dcfb9823bdba4c03c797ba81619f973f57f Mon Sep 17 00:00:00 2001 From: Jure Repe Date: Mon, 23 Jan 2023 20:43:41 +0100 Subject: [PATCH 2/2] Remove action grouping, lift version --- gradle.properties | 2 +- ...ction.java => ReformatAllFilesAction.java} | 4 ++-- .../spotless/actions/ReformatFileAction.java | 2 +- src/main/resources/META-INF/plugin.xml | 22 +++++++++---------- 4 files changed, 15 insertions(+), 15 deletions(-) rename src/main/java/com/github/ragurney/spotless/actions/{ReformatProjectAction.java => ReformatAllFilesAction.java} (89%) diff --git a/gradle.properties b/gradle.properties index 1d2e083..b35e401 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ pluginGroup = com.github.ragurney.spotless pluginName = Spotless Gradle # SemVer format -> https://semver.org -pluginVersion = 1.1.0 +pluginVersion = 2.0.0 # See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html # for insight into build numbers and IntelliJ Platform versions. diff --git a/src/main/java/com/github/ragurney/spotless/actions/ReformatProjectAction.java b/src/main/java/com/github/ragurney/spotless/actions/ReformatAllFilesAction.java similarity index 89% rename from src/main/java/com/github/ragurney/spotless/actions/ReformatProjectAction.java rename to src/main/java/com/github/ragurney/spotless/actions/ReformatAllFilesAction.java index bce8adc..fd5459a 100644 --- a/src/main/java/com/github/ragurney/spotless/actions/ReformatProjectAction.java +++ b/src/main/java/com/github/ragurney/spotless/actions/ReformatAllFilesAction.java @@ -13,9 +13,9 @@ /** - * The action which is called on "Reformat All Files." + * The action which is called on "Reformat All Files With Spotless". */ -public class ReformatProjectAction extends AnAction { +public class ReformatAllFilesAction extends AnAction { @Override public void actionPerformed(@NotNull AnActionEvent event) { diff --git a/src/main/java/com/github/ragurney/spotless/actions/ReformatFileAction.java b/src/main/java/com/github/ragurney/spotless/actions/ReformatFileAction.java index ca546a9..2b3d85f 100644 --- a/src/main/java/com/github/ragurney/spotless/actions/ReformatFileAction.java +++ b/src/main/java/com/github/ragurney/spotless/actions/ReformatFileAction.java @@ -13,7 +13,7 @@ /** - * The action which is called on "Reformat Current File." + * The action which is called on "Reformat File With Spotless". */ public class ReformatFileAction extends AnAction { private static final Logger LOG = Logger.getInstance(ReformatFileAction.class); diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 7867e97..1c932ab 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -8,17 +8,17 @@ com.intellij.gradle - - - + - + + + +