Skip to content

Commit

Permalink
Merge pull request #47 from jrepe/add_reformat_all_files
Browse files Browse the repository at this point in the history
Add action to reformat all files
  • Loading branch information
ragurney authored Jan 23, 2023
2 parents 1d5f44a + 471f5dc commit 3faf41c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pluginGroup = com.github.ragurney.spotless
pluginName = Spotless Gradle
# SemVer format -> https://semver.org
pluginVersion = 1.0.10
pluginVersion = 2.0.0

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
Original file line number Diff line number Diff line change
@@ -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 With Spotless".
*/
public class ReformatAllFilesAction 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> prepareTask(@NotNull PsiFile file, boolean processChangedTextOnly)
throws IncorrectOperationException {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@


/**
* The action which is called on "Reformat Code with Spotless."
* The action which is called on "Reformat File With Spotless".
*/
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) {
Expand Down
12 changes: 9 additions & 3 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
<depends>com.intellij.gradle</depends>

<actions>
<action id="com.github.ragurney.spotless.actions.ReformatCodeAction"
class="com.github.ragurney.spotless.actions.ReformatCodeAction" text="Reformat Code with Spotless"
<action id="com.github.ragurney.spotless.actions.ReformatFileAction"
class="com.github.ragurney.spotless.actions.ReformatFileAction" text="Reformat File With Spotless"
icon="SpotlessIcons.SPOTLESS_DEFAULT_ICON"
description="Reformats code with Spotless">
description="Reformats current file with Spotless">
<add-to-group group-id="CodeFormatGroup" relative-to-action="ReformatCode" anchor="before"/>
</action>
<action id="com.github.ragurney.spotless.actions.ReformatAllFilesAction"
class="com.github.ragurney.spotless.actions.ReformatAllFilesAction" text="Reformat All Files With Spotless"
icon="SpotlessIcons.SPOTLESS_DEFAULT_ICON"
description="Reformats all files with Spotless">
<add-to-group group-id="CodeFormatGroup" relative-to-action="ReformatCode" anchor="before" />
</action>
</actions>
</idea-plugin>

0 comments on commit 3faf41c

Please sign in to comment.