-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from luisalves00/main
Format all files before commit
- Loading branch information
Showing
14 changed files
with
169 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/com/github/lipiridi/spotless/applier/trigger/SpotlessCheckinHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.github.lipiridi.spotless.applier.trigger; | ||
|
||
import com.github.lipiridi.spotless.applier.ModuleInfo; | ||
import com.github.lipiridi.spotless.applier.ReformatProcessor; | ||
import com.github.lipiridi.spotless.applier.ui.settings.SpotlessApplierSettingsState; | ||
import com.intellij.openapi.diagnostic.Logger; | ||
import com.intellij.openapi.module.Module; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.project.ProjectUtil; | ||
import com.intellij.openapi.ui.Messages; | ||
import com.intellij.openapi.vcs.changes.CommitExecutor; | ||
import com.intellij.openapi.vcs.changes.ui.BooleanCommitOption; | ||
import com.intellij.openapi.vcs.checkin.CheckinHandler; | ||
import com.intellij.openapi.vcs.ui.RefreshableOnComponent; | ||
import com.intellij.util.PairConsumer; | ||
import java.awt.*; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import javax.swing.*; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class SpotlessCheckinHandler extends CheckinHandler { | ||
private static final Logger LOGGER = Logger.getInstance(SpotlessCheckinHandler.class); | ||
private final SpotlessApplierSettingsState spotlessSettings = SpotlessApplierSettingsState.getInstance(); | ||
private final Project project; | ||
|
||
public SpotlessCheckinHandler(Project project) { | ||
this.project = project; | ||
} | ||
|
||
@Override | ||
@Nullable public RefreshableOnComponent getBeforeCheckinConfigurationPanel() { | ||
return new BooleanCommitOption( | ||
project, | ||
"Reformat code with Spotless", | ||
true, | ||
() -> spotlessSettings.preCommitSpotlessFormating, | ||
(val) -> spotlessSettings.preCommitSpotlessFormating = val) | ||
.withCheckinHandler(this); | ||
} | ||
|
||
@Override | ||
public ReturnResult beforeCheckin( | ||
@Nullable CommitExecutor executor, PairConsumer<Object, Object> additionalDataConsumer) { | ||
if (!spotlessSettings.preCommitSpotlessFormating) { | ||
return ReturnResult.COMMIT; | ||
} | ||
|
||
try { | ||
new ReformatProcessor(project, findRootModule()).run(); | ||
return ReturnResult.COMMIT; | ||
} catch (Exception e) { | ||
handleError(e); | ||
return ReturnResult.CANCEL; | ||
} | ||
} | ||
|
||
private ModuleInfo findRootModule() { | ||
Module[] modules = ProjectUtil.getModules(project); | ||
String projectBasePath = project.getBasePath(); | ||
|
||
return Arrays.stream(modules) | ||
.map(module -> ModuleInfo.create(project, projectBasePath, module)) | ||
.filter(Objects::nonNull) | ||
.filter(ModuleInfo::rootModule) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
private void handleError(Exception e) { | ||
var msg = "Error while reformatting code with Spotless"; | ||
if (e.getMessage() != null) { | ||
msg = msg + ": " + e.getMessage(); | ||
} | ||
LOGGER.info(msg, e); | ||
Messages.showErrorDialog(project, msg, "Error Reformatting Code with Spotless"); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...main/java/com/github/lipiridi/spotless/applier/trigger/SpotlessCheckinHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.github.lipiridi.spotless.applier.trigger; | ||
|
||
import com.intellij.openapi.vcs.CheckinProjectPanel; | ||
import com.intellij.openapi.vcs.changes.CommitContext; | ||
import com.intellij.openapi.vcs.checkin.CheckinHandler; | ||
import com.intellij.openapi.vcs.checkin.CheckinHandlerFactory; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class SpotlessCheckinHandlerFactory extends CheckinHandlerFactory { | ||
|
||
@NotNull @Override | ||
public CheckinHandler createHandler(@NotNull CheckinProjectPanel panel, @NotNull CommitContext commitContext) { | ||
var project = panel.getProject(); | ||
return new SpotlessCheckinHandler(project); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.