Skip to content

Commit

Permalink
Updated handling of formatter settings
Browse files Browse the repository at this point in the history
  • Loading branch information
swesteme committed May 11, 2024
1 parent c94fa45 commit 5bbca67
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 41 deletions.
8 changes: 8 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ allprojects {
group 'de.gebit.intellij.autoconfig'

ext {
lombokVersion = '1.18.24'
lombokVersion = '1.18.30'
commonsIoVersion = '2.12.0'
intellijPluginVersion = '1.17.3'
jakartaJwsVersion = '2.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,36 @@

import com.intellij.codeInsight.actions.onSave.FormatOnSaveOptionsBase;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeRegistry;
import com.intellij.openapi.fileTypes.UnknownFileType;
import de.gebit.intellij.autoconfig.state.TransientPluginState;
import de.gebit.intellij.autoconfig.state.TransientPluginStateService;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Function;

/**
* Supplier for default file formats that should be formatted or used for optimization of imports.
*/
public class FormatOnSaveOptionsDefaultsProvider implements FormatOnSaveOptionsBase.DefaultsProvider {

/**
* List of file types used as defaults.
*/
private static Collection<FileType> fileTypes;

@Override
public @NotNull Collection<@NotNull FileType> getFileTypesFormattedOnSaveByDefault() {
return getFileTypes();
return getFileTypes(TransientPluginState::formatFileTypes);
}

@Override
public @NotNull Collection<@NotNull FileType> getFileTypesWithOptimizeImportsOnSaveByDefault() {
return getFileTypes();
return getFileTypes(TransientPluginState::organizeImportFileTypes);
}

@NotNull
private static Collection<FileType> getFileTypes() {
if (fileTypes != null) {
return fileTypes;
private static Collection<FileType> getFileTypes(Function<TransientPluginState, Collection<FileType>> getterMethod) {
TransientPluginState pluginState = TransientPluginStateService.getInstance().getPluginState();
if (pluginState == null) {
return Collections.emptyList();
}

fileTypes = new ArrayList<>();

for (String type : new String[]{"java", "dart", "groovy", "kt"}) {
FileType fileTypeByExtension = FileTypeRegistry.getInstance().getFileTypeByExtension(type);
if (!(fileTypeByExtension instanceof UnknownFileType)) {
fileTypes.add(fileTypeByExtension);
}
}
return fileTypes;
return getterMethod.apply(pluginState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package de.gebit.intellij.autoconfig.handlers;

import com.intellij.codeInsight.actions.onSave.FormatOnSaveOptions;
import com.intellij.codeInsight.actions.onSave.FormatOnSaveOptionsBase;
import com.intellij.codeInsight.actions.onSave.OptimizeImportsOnSaveOptions;
import com.intellij.externalDependencies.ExternalDependenciesManager;
import com.intellij.externalDependencies.impl.ExternalDependenciesManagerImpl;
Expand All @@ -17,12 +18,16 @@
import com.intellij.openapi.vcs.IssueNavigationConfiguration;
import com.intellij.openapi.vcs.IssueNavigationLink;
import de.gebit.intellij.autoconfig.UpdateHandler;
import de.gebit.intellij.autoconfig.model.Formatting;
import de.gebit.intellij.autoconfig.model.GeneralConfiguration;
import de.gebit.intellij.autoconfig.model.IssueNavigation;
import de.gebit.intellij.autoconfig.model.OnSave;
import de.gebit.intellij.autoconfig.state.TransientPluginStateService;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -76,14 +81,28 @@ private void applyIssueNavigationConfiguration(List<IssueNavigation> issueNaviga

private void applyOnSaveOptions(OnSave options, Project project, List<String> updatedConfigs) {
if (options != null) {
FormatOnSaveOptions instance = FormatOnSaveOptions.getInstance(project);
applySetting(options.getFormat(), instance.isRunOnSaveEnabled(), instance::setRunOnSaveEnabled, updatedConfigs, "Code format on save");
OptimizeImportsOnSaveOptions imports = OptimizeImportsOnSaveOptions.getInstance(project);
applySetting(options.getOptimizeImports(), imports.isRunOnSaveEnabled(), imports::setRunOnSaveEnabled, updatedConfigs, "Optimize imports on save");
// first, initialise the application state service used for code formatter options
Formatting optionsFormat = options.getFormat();
Formatting optionsOptimizeImports = options.getOptimizeImports();
TransientPluginStateService.getInstance().initFormatterSettings(getFileTypes(optionsFormat), getFileTypes(optionsOptimizeImports));

setFormattingOptions(updatedConfigs, optionsFormat, "Code format on save", FormatOnSaveOptions.getInstance(project));
setFormattingOptions(updatedConfigs, optionsOptimizeImports, "Optimize imports on save", OptimizeImportsOnSaveOptions.getInstance(project));
}
}

private void setFormattingOptions(List<String> updatedConfigs, Formatting optionsFormat, String description, FormatOnSaveOptionsBase<?> format) {
applySetting(optionsFormat != null, format.isRunOnSaveEnabled(), format::setRunOnSaveEnabled, updatedConfigs, description);
}

private List<String> getFileTypes(@Nullable Formatting formattingOptions) {
if (formattingOptions == null) {
return Collections.emptyList();
}
return formattingOptions.getTypes();
}

void applyPluginHosts(List<String> pluginHosts, Project aProject, List<String> changedConfigs) {
private void applyPluginHosts(List<String> pluginHosts, Project aProject, List<String> changedConfigs) {
if (pluginHosts != null) {
var updateSettings = UpdateSettings.getInstance();
var storedPluginHosts = updateSettings.getStoredPluginHosts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public Class<MavenConfiguration> getConfigurationClass() {
}

@Override
@SuppressWarnings("java:S5738")
public List<String> updateConfiguration(MavenConfiguration maven, Project project) {
List<String> changedConfigs = new ArrayList<>();
var mavenProject = MavenProjectsManager.getInstance(project);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.gebit.intellij.autoconfig.state;

import com.intellij.openapi.fileTypes.FileType;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

/**
* Transient plugin state used for configuration parts that can not be reliably tied to a project.
*
* @param formatFileTypes List of file types that should be formatted.
* @param organizeImportFileTypes List of file types having their imports organised.
*/
public record TransientPluginState(@NotNull Collection<FileType> formatFileTypes, @NotNull Collection<FileType> organizeImportFileTypes) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package de.gebit.intellij.autoconfig.state;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeRegistry;
import com.intellij.openapi.fileTypes.UnknownFileType;
import com.intellij.openapi.project.Project;
import de.gebit.intellij.autoconfig.FormatOnSaveOptionsDefaultsProvider;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
* The application wide settings for the autoconfig plugin. They are necessary because the {@link FormatOnSaveOptionsDefaultsProvider} does not have
* access to the {@link Project} and can therefore not read the configured values.
*/
@Getter
@Service(Service.Level.APP)
public final class TransientPluginStateService {
/**
* Transient state of plugin.
*/
private TransientPluginState pluginState;

public static TransientPluginStateService getInstance() {
return ApplicationManager.getApplication().getService(TransientPluginStateService.class);
}

public void initFormatterSettings(List<String> formatFileTypes, List<String> organizeImportFileTypes) {
pluginState = new TransientPluginState(getFileTypes(formatFileTypes), getFileTypes(organizeImportFileTypes));
}

@NotNull
private static List<FileType> getFileTypes(List<String> formatFileTypes) {
List<FileType> fileTypes = new ArrayList<>();
for (String type : formatFileTypes) {
FileType fileTypeByExtension = FileTypeRegistry.getInstance().getFileTypeByExtension(type);
if (!(fileTypeByExtension instanceof UnknownFileType)) {
fileTypes.add(fileTypeByExtension);
}
}
return fileTypes;
}
}
6 changes: 2 additions & 4 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<idea-plugin url="https://gitlab.local.gebit.de/intellij/trend-tools">
<id>de.gebit.intellij.autoconfig</id>
<name>Configuration updater plugin for IntelliJ</name>
<name>Autoconfig</name>
<vendor email="[email protected]" url="https://www.gebit.de">GEBIT Solutions GmbH</vendor>

<description><![CDATA[<em>Change this by patchPluginXml gradle task</em>]]></description>
<change-notes><![CDATA[<em>Change this by patchPluginXml gradle task</em>]]></change-notes>
<version>0.0.0</version>
<idea-version since-build="231.1"/>
<vendor url="https://github.com/GEBIT/autoconfig-intellij-plugin"
email="info@gebit.de">GEBIT Solutions GmbH</vendor>
email="jetbrains@gebit.de">GEBIT Solutions GmbH</vendor>
<description><![CDATA[
TODO: HTML text describing the autoconfig plugin functionality.
]]>
Expand Down
25 changes: 17 additions & 8 deletions src/main/resources/schema/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@
"$id": "https://www.gebit.de/autoconfig-intellij-plugin/config.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "General configuration",
"definitions": {
"formatting": {
"type": "object",
"description": "Formatting settings",
"properties": {
"types": {
"type": "array",
"description": "File types that should be activated",
"items": {
"type": "string"
}
}
}
}
},
"type": "object",
"properties": {
"onSave": {
"type": "object",
"description": "Actions on save settings.",
"properties": {
"format": {
"type": "boolean",
"description": "Whether source code formatting is enabled."
},
"optimizeImports": {
"type": "boolean",
"description": "Whether import optimization is enabled."
}
"format": { "$ref": "#/definitions/formatting" },
"optimizeImports": { "$ref": "#/definitions/formatting" }
}
},
"issueNavigation": {
Expand Down

0 comments on commit 5bbca67

Please sign in to comment.