-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support configuring order of source and target in
@Mapping
for Add …
…unmapped property quick fix Fixes #51
- Loading branch information
Showing
10 changed files
with
325 additions
and
6 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
30 changes: 30 additions & 0 deletions
30
src/main/java/org/mapstruct/intellij/settings/ProjectSettings.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,30 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.settings; | ||
|
||
import com.intellij.ide.util.PropertiesComponent; | ||
import com.intellij.openapi.project.Project; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
public interface ProjectSettings { | ||
|
||
String PREFIX = "MapStructPlugin"; | ||
|
||
String PREFER_SOURCE_BEFORE_TARGET_IN_MAPPING = | ||
PREFIX + "PREFER_SOURCE_BEFORE_TARGET_IN_MAPPING"; | ||
|
||
static boolean isPreferSourceBeforeTargetInMapping(@NotNull Project project) { | ||
return PropertiesComponent.getInstance( project ).getBoolean( PREFER_SOURCE_BEFORE_TARGET_IN_MAPPING, false ); | ||
} | ||
|
||
static void setPreferSourceBeforeTargetInMapping(@NotNull Project project, boolean value) { | ||
PropertiesComponent.getInstance( project ) | ||
.setValue( PREFER_SOURCE_BEFORE_TARGET_IN_MAPPING, String.valueOf( value ), "false" ); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/org/mapstruct/intellij/settings/ProjectSettingsComponent.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,55 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.settings; | ||
|
||
import java.awt.BorderLayout; | ||
import javax.swing.JComponent; | ||
import javax.swing.JPanel; | ||
|
||
import com.intellij.ui.IdeBorderFactory; | ||
import com.intellij.ui.components.JBCheckBox; | ||
import com.intellij.util.ui.FormBuilder; | ||
import org.mapstruct.intellij.MapStructBundle; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
public class ProjectSettingsComponent { | ||
|
||
private final JPanel mainPanel; | ||
private final JBCheckBox preferSourceBeforeTargetInMapping; | ||
|
||
public ProjectSettingsComponent() { | ||
this.preferSourceBeforeTargetInMapping = new JBCheckBox( MapStructBundle.message( | ||
"plugin.settings.quickFix.preferSourceBeforeTargetInMapping" ), false ); | ||
JPanel quickFixProperties = new JPanel( new BorderLayout() ); | ||
quickFixProperties.setBorder( IdeBorderFactory.createTitledBorder( MapStructBundle.message( | ||
"plugin.settings.quickFix.title" ), false ) ); | ||
|
||
quickFixProperties.add( this.preferSourceBeforeTargetInMapping, BorderLayout.NORTH ); | ||
this.mainPanel = FormBuilder.createFormBuilder() | ||
.addComponent( quickFixProperties ) | ||
.addComponentFillVertically( new JPanel(), 0 ) | ||
.getPanel(); | ||
} | ||
|
||
public JPanel getPanel() { | ||
return mainPanel; | ||
} | ||
|
||
public JComponent getPreferredFocusedComponent() { | ||
return preferSourceBeforeTargetInMapping; | ||
} | ||
|
||
public boolean getPreferSourceBeforeTargetInMapping() { | ||
return preferSourceBeforeTargetInMapping.isSelected(); | ||
} | ||
|
||
public void setPreferSourceBeforeTargetInMapping(boolean newState) { | ||
preferSourceBeforeTargetInMapping.setSelected( newState ); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/org/mapstruct/intellij/settings/ProjectSettingsPage.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,71 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.settings; | ||
|
||
import javax.swing.JComponent; | ||
|
||
import com.intellij.openapi.options.Configurable; | ||
import com.intellij.openapi.project.Project; | ||
import org.jetbrains.annotations.Nls; | ||
import org.mapstruct.intellij.MapStructBundle; | ||
|
||
import static org.mapstruct.intellij.settings.ProjectSettings.isPreferSourceBeforeTargetInMapping; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
public class ProjectSettingsPage implements Configurable { | ||
|
||
private ProjectSettingsComponent settingsComponent; | ||
|
||
private final Project myProject; | ||
|
||
public ProjectSettingsPage(Project project) { | ||
myProject = project; | ||
} | ||
|
||
@Nls | ||
@Override | ||
public String getDisplayName() { | ||
return MapStructBundle.message( "plugin.settings.title" ); | ||
} | ||
|
||
@Override | ||
public JComponent createComponent() { | ||
settingsComponent = new ProjectSettingsComponent(); | ||
return settingsComponent.getPanel(); | ||
} | ||
|
||
@Override | ||
public JComponent getPreferredFocusedComponent() { | ||
return settingsComponent.getPreferredFocusedComponent(); | ||
} | ||
|
||
@Override | ||
public boolean isModified() { | ||
boolean modified = settingsComponent.getPreferSourceBeforeTargetInMapping() != | ||
isPreferSourceBeforeTargetInMapping( myProject ); | ||
return modified; | ||
} | ||
|
||
@Override | ||
public void apply() { | ||
ProjectSettings.setPreferSourceBeforeTargetInMapping( | ||
myProject, | ||
settingsComponent.getPreferSourceBeforeTargetInMapping() | ||
); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
settingsComponent.setPreferSourceBeforeTargetInMapping( isPreferSourceBeforeTargetInMapping( myProject ) ); | ||
} | ||
|
||
@Override | ||
public void disposeUIResources() { | ||
settingsComponent = null; | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...pstruct/intellij/inspection/UnmappedTargetPropertiesInspectionSourceBeforeTargetTest.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,57 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.inspection; | ||
|
||
import java.util.List; | ||
|
||
import com.intellij.codeInsight.intention.IntentionAction; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.mapstruct.intellij.settings.ProjectSettings; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
public class UnmappedTargetPropertiesInspectionSourceBeforeTargetTest extends BaseInspectionTest { | ||
|
||
@NotNull | ||
@Override | ||
protected Class<UnmappedTargetPropertiesInspection> getInspection() { | ||
return UnmappedTargetPropertiesInspection.class; | ||
} | ||
|
||
@Override | ||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
myFixture.copyFileToProject( | ||
"UnmappedTargetPropertiesData.java", | ||
"org/example/data/UnmappedTargetPropertiesData.java" | ||
); | ||
} | ||
|
||
public void testUnmappedTargetPropertiesSourceBeforeTarget() { | ||
ProjectSettings.setPreferSourceBeforeTargetInMapping( myFixture.getProject(), true ); | ||
doTest(); | ||
String testName = getTestName( false ); | ||
List<IntentionAction> allQuickFixes = myFixture.getAllQuickFixes(); | ||
|
||
assertThat( allQuickFixes ) | ||
.extracting( IntentionAction::getText ) | ||
.as( "Intent Text" ) | ||
.containsExactly( | ||
"Ignore unmapped target property: 'testName'", | ||
"Add unmapped target property: 'testName'", | ||
"Ignore unmapped target property: 'moreTarget'", | ||
"Add unmapped target property: 'moreTarget'", | ||
"Ignore unmapped target property: 'testName'", | ||
"Add unmapped target property: 'testName'" | ||
); | ||
|
||
allQuickFixes.forEach( myFixture::launchAction ); | ||
myFixture.checkResultByFile( testName + "_after.java" ); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
testData/inspection/UnmappedTargetPropertiesSourceBeforeTarget.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,36 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
import org.mapstruct.Context; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.MappingTarget; | ||
import org.mapstruct.Mappings; | ||
import org.example.data.UnmappedTargetPropertiesData.Target; | ||
import org.example.data.UnmappedTargetPropertiesData.Source; | ||
|
||
@Mapper | ||
interface SingleMappingsMapper { | ||
|
||
@Mappings({ | ||
@Mapping(target = "moreTarget", source = "moreSource") | ||
}) | ||
Target <warning descr="Unmapped target property: testName">map</warning>(Source source); | ||
} | ||
|
||
@Mapper | ||
interface SingleMappingMapper { | ||
|
||
@Mapping(target = "testName", source = "name") | ||
Target <warning descr="Unmapped target property: moreTarget">map</warning>(Source source); | ||
} | ||
|
||
@Mapper | ||
interface UpdateMapper { | ||
|
||
@Mapping(target = "moreTarget", source = "moreSource") | ||
void <warning descr="Unmapped target property: testName">update</warning>(@MappingTarget Target target, Source source); | ||
} |
42 changes: 42 additions & 0 deletions
42
testData/inspection/UnmappedTargetPropertiesSourceBeforeTarget_after.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,42 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
import org.mapstruct.Context; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.MappingTarget; | ||
import org.mapstruct.Mappings; | ||
import org.example.data.UnmappedTargetPropertiesData.Target; | ||
import org.example.data.UnmappedTargetPropertiesData.Source; | ||
|
||
@Mapper | ||
interface SingleMappingsMapper { | ||
|
||
@Mappings({ | ||
@Mapping(target = "moreTarget", source = "moreSource"), | ||
@Mapping(target = "testName", ignore = true), | ||
@Mapping(source = "", target = "testName") | ||
}) | ||
Target map(Source source); | ||
} | ||
|
||
@Mapper | ||
interface SingleMappingMapper { | ||
|
||
@Mapping(source = "", target = "moreTarget") | ||
@Mapping(target = "moreTarget", ignore = true) | ||
@Mapping(target = "testName", source = "name") | ||
Target map(Source source); | ||
} | ||
|
||
@Mapper | ||
interface UpdateMapper { | ||
|
||
@Mapping(source = "", target = "testName") | ||
@Mapping(target = "testName", ignore = true) | ||
@Mapping(target = "moreTarget", source = "moreSource") | ||
void update(@MappingTarget Target target, Source source); | ||
} |