Skip to content

Commit

Permalink
修复注释问题
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhihang committed Jun 4, 2021
1 parent a55e523 commit fc2b11d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 18 deletions.
21 changes: 18 additions & 3 deletions src/main/java/com/liuzhihang/toolkit/ui/ParamPreview.form
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.liuzhihang.toolkit.ui.ParamPreviewForm">
<grid id="27dc6" binding="rootPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="rootPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="600" height="400"/>
Expand All @@ -13,20 +13,35 @@
<children>
<grid id="d57ea" binding="tailToolbarPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<scrollpane id="2148d" binding="paramScrollPane">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</scrollpane>
<grid id="771ec" binding="headToolbarPanel" layout-manager="BorderLayout" hgap="3" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="d8cd4" class="javax.swing.JLabel" binding="fileReference">
<constraints border-constraint="West"/>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>
70 changes: 66 additions & 4 deletions src/main/java/com/liuzhihang/toolkit/ui/ParamPreviewForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* @author liuzhihang
Expand All @@ -41,6 +42,8 @@ public class ParamPreviewForm {
@NonNls
public static final String TOOLKIT_PREVIEW_POPUP = "com.intellij.toolkit.preview.popup";

private final AtomicBoolean myIsPinned = new AtomicBoolean(false);

private final Project project;
private final PsiFile psiFile;
private final PsiClass psiClass;
Expand All @@ -50,6 +53,8 @@ public class ParamPreviewForm {

private JPanel rootPanel;
private JPanel tailToolbarPanel;
private JPanel headToolbarPanel;
private JLabel fileReference;
private JScrollPane paramScrollPane;


Expand All @@ -69,6 +74,8 @@ public ParamPreviewForm(@NotNull Project project, @NotNull PsiFile psiFile,
this.psiElementFactory = JavaPsiFacade.getElementFactory(project);

initUI();
initTitle();
initHeadToolbar();
addMouseListeners();
initParamData();
initTailRightToolbar();
Expand All @@ -81,11 +88,65 @@ private void initUI() {
tailToolbarPanel.setBorder(JBUI.Borders.empty());
}

private void initTitle() {

if (psiClass != null) {
fileReference.setText(psiClass.getQualifiedName());
} else if (psiFile != null) {
fileReference.setText(psiFile.getName());
} else {
fileReference.setText("Toolkit");
}
}

private void initHeadToolbar() {
DefaultActionGroup group = new DefaultActionGroup();

group.add(new AnAction("Setting", "Toolkit settings", AllIcons.General.GearPlain) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
myIsPinned.set(true);
SettingsForm.getInstance(project).popup();
}
});

group.addSeparator();

group.add(new ToggleAction("Pin", "Pin window", AllIcons.General.Pin_tab) {

@Override
public boolean isDumbAware() {
return true;
}

@Override
public boolean isSelected(@NotNull AnActionEvent e) {
return myIsPinned.get();
}

@Override
public void setSelected(@NotNull AnActionEvent e, boolean state) {
myIsPinned.set(state);
}
});

ActionToolbarImpl toolbar = (ActionToolbarImpl) ActionManager.getInstance()
.createActionToolbar("ToolkitHeadToolbar", group, true);
toolbar.setTargetComponent(headToolbarPanel);

toolbar.setForceMinimumSize(true);
toolbar.setLayoutPolicy(ActionToolbar.NOWRAP_LAYOUT_POLICY);
Utils.setSmallerFontForChildren(toolbar);

headToolbarPanel.add(toolbar.getComponent(), BorderLayout.EAST);
}

private void addMouseListeners() {
WindowMoveListener windowMoveListener = new WindowMoveListener(rootPanel);
rootPanel.addMouseListener(windowMoveListener);
rootPanel.addMouseMotionListener(windowMoveListener);

headToolbarPanel.addMouseListener(windowMoveListener);
headToolbarPanel.addMouseMotionListener(windowMoveListener);

}

Expand Down Expand Up @@ -143,9 +204,10 @@ public void actionPerformed(@NotNull AnActionEvent e) {
javaCodeStyleManager.optimizeImports(psiClass.getContainingFile());
javaCodeStyleManager.shortenClassReferences(psiClass);

CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformat(psiClass);
codeStyleManager.reformat(psiClass, false);
// 格式化注释
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(psiClass.getProject());

codeStyleManager.reformatText(psiClass.getContainingFile(), 0, psiClass.getTextLength() + 1);
});

popup.cancel();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/liuzhihang/toolkit/ui/ToolkitForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void addMouseListeners() {
private void initHeadToolbar() {
DefaultActionGroup group = new DefaultActionGroup();

group.add(new AnAction("Setting", "Doc view settings", AllIcons.General.GearPlain) {
group.add(new AnAction("Setting", "Toolkit settings", AllIcons.General.GearPlain) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
// ShowSettingsUtil.getInstance().showSettingsDialog(e.getProject(), SettingsConfigurable.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,14 @@ public static void commentHandler(@NotNull Settings settings, @NotNull PsiElemen
String comment
= "/* \n"
+ "Create from Json string by IDEA plugin —— Toolkit\n\n"
+ GsonFormatUtil.gsonFormat(new GsonBuilder().setPrettyPrinting().create(), jsonObject)
+ " */";
+ GsonFormatUtil.gsonFormat(new GsonBuilder().serializeNulls().setPrettyPrinting().create(), jsonObject)
+ "\n*/";

PsiField psiField = allFields[0];

PsiComment commentFromText = psiElementFactory.createCommentFromText(comment, null);
PsiComment commentFromText = psiElementFactory.createCommentFromText(comment, psiField);

psiField.addBefore(commentFromText, psiField.getFirstChild());

// // 格式化注释
// CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(psiClass.getProject());
// int startOffset = commentFromText.getTextOffset();
// int endOffset = commentFromText.getTextOffset() + commentFromText.getText().length();
// codeStyleManager.reformatText(psiClass.getContainingFile(), startOffset, endOffset + 1);
psiField.addBefore(commentFromText, psiField);

} catch (Exception ignored) {
}
Expand Down

0 comments on commit fc2b11d

Please sign in to comment.