Skip to content

Commit

Permalink
JSON Action 拆分
Browse files Browse the repository at this point in the history
  • Loading branch information
YangLang116 committed Nov 25, 2024
1 parent 421e152 commit d5f7ef0
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 51 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ gradleVersion=17
pluginSinceBuild=223
platformType=IC
platformVersion=2024.2.4
asPath=/Applications/Android Studio.app/Contents
#asPath=F:\\as
#asPath=/Applications/Android Studio.app/Contents
asPath=F:\\as
platformBundledPlugins=com.intellij.java,org.intellij.groovy,org.jetbrains.plugins.yaml
platformPlugins=org.jetbrains.android:242.23726.103,Dart:242.22855.32,io.flutter:82.1.3
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.project.Project;
import com.xtu.plugin.flutter.base.utils.PluginUtils;
import icons.PluginIcons;
import org.jetbrains.annotations.NotNull;

public class IFlutterActionGroup extends DefaultActionGroup {

public IFlutterActionGroup() {
//noinspection DialogTitleCapitalization
super("iFlutter", "Flutter Development Kit", PluginIcons.LOGO);
}

@Override
@NotNull
public ActionUpdateThread getActionUpdateThread() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

public class DartGenerateJSONMethodHandler extends BaseDartGenerateHandler {

private final boolean hasFromJson;
private final boolean hasToJson;
private final boolean createFromJson;
private final boolean createToJson;

public DartGenerateJSONMethodHandler(boolean hasFromJson, boolean hasToJson) {
this.hasFromJson = hasFromJson;
this.hasToJson = hasToJson;
public DartGenerateJSONMethodHandler(boolean createFromJson, boolean createToJson) {
this.createFromJson = createFromJson;
this.createToJson = createToJson;
}

@NotNull
Expand All @@ -32,7 +32,9 @@ protected String getTitle() {
protected BaseCreateMethodsFix<DartComponent> createFix(@NotNull DartClass dartClass) {
Project project = dartClass.getProject();
ProjectStorageEntity storageEntity = ProjectStorageService.getStorage(project);
return new DartGenerateJsonMethodCodeFix(dartClass, storageEntity.supportNullSafety, this.hasFromJson, hasToJson);
return new DartGenerateJsonMethodCodeFix(dartClass,
storageEntity.supportNullSafety,
this.createFromJson, createToJson);
}

protected void collectCandidates(@NotNull DartClass dartClass, @NotNull List<DartComponent> candidates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@

public class DartGenerateJsonMethodCodeFix extends BaseCreateMethodsFix<DartComponent> {

private final boolean hasFromJson;
private final boolean hasToJson;
private final boolean createFromJson;
private final boolean createToJson;
private final boolean supportNullSafety;

public DartGenerateJsonMethodCodeFix(@NotNull DartClass dartClass,
boolean supportNullSafety,
boolean hasFromJson, boolean hasToJson) {
boolean createFromJson,
boolean createToJson) {
super(dartClass);
this.supportNullSafety = supportNullSafety;
this.hasFromJson = hasFromJson;
this.hasToJson = hasToJson;
this.createFromJson = createFromJson;
this.createToJson = createToJson;
}

@NotNull
Expand All @@ -55,8 +56,8 @@ protected Template buildTemplate(@NotNull Project project,
List<GenJSONMethodFieldDescriptor> dartFieldList = parseFieldList(elementsToProcess);
Template template = templateManager.createTemplate(this.getClass().getName(), "Dart");
template.setToReformat(true);
if (!hasFromJson) addFromJSONSegment(project, template, dartFieldList);
if (!hasToJson) addToJSONSegment(template, dartFieldList);
if (createFromJson) addFromJSONSegment(project, template, dartFieldList);
if (createToJson) addToJSONSegment(template, dartFieldList);
return template;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.xtu.plugin.flutter.action.generate.json.action;

import com.jetbrains.lang.dart.psi.DartClass;
import org.jetbrains.annotations.NotNull;

public class DartGenerateFromJSONMethodAction extends DartGenerateJSONMethodAction {

@Override
protected boolean needGenToJson(@NotNull DartClass dartClass) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
package com.xtu.plugin.flutter.action.generate.json;
package com.xtu.plugin.flutter.action.generate.json.action;

import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.lang.dart.ide.generation.BaseDartGenerateAction;
import com.jetbrains.lang.dart.ide.generation.BaseDartGenerateHandler;
import com.jetbrains.lang.dart.psi.DartClass;
import com.jetbrains.lang.dart.psi.DartFactoryConstructorDeclaration;
import com.jetbrains.lang.dart.util.DartResolveUtil;
import com.xtu.plugin.flutter.action.generate.json.DartGenerateJSONMethodHandler;
import com.xtu.plugin.flutter.base.utils.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class DartGenerateJSONMethodAction extends BaseDartGenerateAction {

private boolean hasFromJson;
private boolean hasToJson;
private boolean createFromJson;
private boolean createToJson;

protected boolean doEnable(@Nullable DartClass dartClass) {
if (dartClass == null) return false;
return !doesClassContainFromJsonAndToJson(dartClass);
this.createFromJson = needGenFromJson(dartClass);
this.createToJson = needGenToJson(dartClass);
return this.createFromJson || this.createToJson;
}

public boolean doesClassContainFromJsonAndToJson(@NotNull DartClass dartClass) {
boolean hasFromJson = false;
DartFactoryConstructorDeclaration[] constructorDeclarationList = PsiTreeUtil.getChildrenOfType(
DartResolveUtil.getBody(dartClass),
DartFactoryConstructorDeclaration.class);
protected boolean needGenFromJson(@NotNull DartClass dartClass) {
DartFactoryConstructorDeclaration[] constructorDeclarationList = PsiTreeUtil.getChildrenOfType(DartResolveUtil.getBody(dartClass), DartFactoryConstructorDeclaration.class);
if (constructorDeclarationList != null) {
for (DartFactoryConstructorDeclaration constructor : constructorDeclarationList) {
if (StringUtils.equals(constructor.getName(), "fromJson")) {
hasFromJson = true;
break;
return false;
}
}
}
boolean hasToJson = doesClassContainMethod(dartClass, "toJson");
this.hasFromJson = hasFromJson;
this.hasToJson = hasToJson;
return hasFromJson && hasToJson;
return true;
}

protected boolean needGenToJson(@NotNull DartClass dartClass) {
return !doesClassContainMethod(dartClass, "toJson");
}

@Override
protected @NotNull BaseDartGenerateHandler getGenerateHandler() {
return new DartGenerateJSONMethodHandler(hasFromJson, hasToJson);
return new DartGenerateJSONMethodHandler(createFromJson, createToJson);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.xtu.plugin.flutter.action.generate.json.action;

import com.jetbrains.lang.dart.psi.DartClass;
import org.jetbrains.annotations.NotNull;

public class DartGenerateToJSONMethodAction extends DartGenerateJSONMethodAction {

@Override
protected boolean needGenFromJson(@NotNull DartClass dartClass) {
return false;
}
}
47 changes: 28 additions & 19 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@
<projectViewNodeDecorator implementation="com.xtu.plugin.flutter.decorator.AssetNodeDecorator"/>
</extensions>
<actions>
<group id="com.xtu.plugin.flutter.group"
icon="PluginIcons.LOGO"
text="iFlutter"
description="Flutter Development Kit"
class="com.xtu.plugin.flutter.action.IFlutterActionGroup"
popup="true">
<group popup="true"
id="com.xtu.plugin.flutter.group.comm"
class="com.xtu.plugin.flutter.action.IFlutterActionGroup">
<add-to-group group-id="ProjectViewPopupMenu" anchor="after"
relative-to-action="ProjectViewPopupMenuRefactoringGroup"/>
<action id="com.xtu.plugin.flutter.GenerateResAction"
Expand Down Expand Up @@ -106,19 +103,31 @@
description="Open iFlutter usage documentation"
class="com.xtu.plugin.flutter.action.usage.UsageAction"/>
</group>
<action id="com.xtu.plugin.flutter.DartGenerateFromJsonAndToJsonAction"
icon="PluginIcons.CODE"
text="FromJson and ToJson"
description="Generate fromJson | toJson [iFlutter]"
class="com.xtu.plugin.flutter.action.generate.json.DartGenerateJSONMethodAction">
<group popup="false"
id="com.xtu.plugin.flutter.group.code"
class="com.xtu.plugin.flutter.action.IFlutterActionGroup">
<add-to-group anchor="after" relative-to-action="Generate.EqualsAndHashcode.Dart" group-id="GenerateGroup"/>
</action>
<action id="com.xtu.plugin.flutter.DartGenerateConstructorAction"
icon="PluginIcons.CODE"
text="Constructor for Json"
description="Generate a constructor for fromJson | toJson [iFlutter]"
class="com.xtu.plugin.flutter.action.generate.constructor.DartGenerateConstructorAction">
<add-to-group anchor="after" relative-to-action="Generate.EqualsAndHashcode.Dart" group-id="GenerateGroup"/>
</action>
<separator/>
<action id="com.xtu.plugin.flutter.constructor"
icon="PluginIcons.CODE"
text="Constructor for Json"
description="Generate a constructor for fromJson | toJson [iFlutter]"
class="com.xtu.plugin.flutter.action.generate.constructor.DartGenerateConstructorAction"/>
<action id="com.xtu.plugin.flutter.from.json"
icon="PluginIcons.CODE"
text="FromJson"
description="Generate fromJson [iFlutter]"
class="com.xtu.plugin.flutter.action.generate.json.action.DartGenerateFromJSONMethodAction"/>
<action id="com.xtu.plugin.flutter.to.json"
icon="PluginIcons.CODE"
text="ToJson"
description="toJson [iFlutter]"
class="com.xtu.plugin.flutter.action.generate.json.action.DartGenerateToJSONMethodAction"/>
<action id="com.xtu.plugin.flutter.json"
icon="PluginIcons.CODE"
text="FromJson and ToJson"
description="Generate fromJson | toJson [iFlutter]"
class="com.xtu.plugin.flutter.action.generate.json.action.DartGenerateJSONMethodAction"/>
</group>
</actions>
</idea-plugin>
4 changes: 3 additions & 1 deletion src/main/resources/META-INF/yaml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<annotator language="yaml" implementationClass="com.xtu.plugin.flutter.annotator.PackageUpdateAnnotator"/>
</extensions>
<actions>
<group>
<group popup="false"
id="com.xtu.plugin.flutter.group.yaml"
class="com.xtu.plugin.flutter.action.IFlutterActionGroup">
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
<action id="com.xtu.plugin.flutter.ConvertDependencyToLocalAction"
icon="PluginIcons.FETCH"
Expand Down

0 comments on commit d5f7ef0

Please sign in to comment.