Skip to content

Commit

Permalink
Insert paired (), [] , {}, ""
Browse files Browse the repository at this point in the history
  • Loading branch information
krasa committed Jan 18, 2021
1 parent e5621dd commit 266131a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 23 deletions.
1 change: 1 addition & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,6 @@

<lang.psiStructureViewFactory language="PUML"
implementationClass="org.plantuml.idea.grammar.structure.PumlStructureViewFactory"/>
<typedHandler implementation="org.plantuml.idea.lang.PlantUmlTypedHandlerDelegate"/>
</extensions>
</idea-plugin>
65 changes: 65 additions & 0 deletions src/org/plantuml/idea/lang/PlantUmlTypedHandlerDelegate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.plantuml.idea.lang;

import com.intellij.codeInsight.editorActions.TypedHandlerDelegate;
import com.intellij.codeInsight.editorActions.smartEnter.SmartEnterProcessor;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.plantuml.idea.grammar.psi.PumlTypes;
import org.plantuml.idea.lang.settings.PlantUmlSettings;

import static java.lang.Character.isWhitespace;

public class PlantUmlTypedHandlerDelegate extends TypedHandlerDelegate {

private static PlantUmlSettings settings;

public PlantUmlTypedHandlerDelegate() {
settings = PlantUmlSettings.getInstance();
}

@Override
public @NotNull
Result charTyped(char c, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
if (file instanceof PlantUmlFileImpl) {
processPair(c, editor, file);
}
return Result.CONTINUE;
}

public static void processPair(char c,
@NotNull Editor editor,
@NotNull PsiFile file) {
if (!settings.isInsertPair()) return;
String insert;
if (c == '(') {
insert = ")";
} else if (c == '[') {
insert = "]";
// } else if (c == '<') {
// insert = ">";
} else if (c == '{') {
insert = "}";
} else if (c == '"') {
insert = "\"";
} else {
return;
}

SmartEnterProcessor.commitDocument(editor);
int offset = editor.getCaretModel().getOffset();

CharSequence sequence = editor.getDocument().getCharsSequence();
if (!isWhitespace(sequence.charAt(offset))) return;
if (offset - 2 >= 0 && !isWhitespace(sequence.charAt(offset - 2))) return;

PsiElement element = file.findElementAt(offset - 1);
if (element == null) return;
if (element.getNode().getElementType() == PumlTypes.IDENTIFIER) {
return;
}
editor.getDocument().insertString(offset, insert);
}
}
9 changes: 9 additions & 0 deletions src/org/plantuml/idea/lang/settings/PlantUmlSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class PlantUmlSettings implements PersistentStateComponent<PlantUmlSettin
private boolean usePageTitles = true;
private boolean useGrammar = true;
private boolean keywordHighlighting = true;
private boolean insertPair = true;


public static PlantUmlSettings getInstance() {
Expand Down Expand Up @@ -318,4 +319,12 @@ public boolean isKeywordHighlighting() {
public void setKeywordHighlighting(final boolean keywordHighlighting) {
this.keywordHighlighting = keywordHighlighting;
}

public boolean isInsertPair() {
return insertPair;
}

public void setInsertPair(final boolean insertPair) {
this.insertPair = insertPair;
}
}
12 changes: 10 additions & 2 deletions src/org/plantuml/idea/lang/settings/PlantUmlSettingsPage.form
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
</component>
</children>
</grid>
<grid id="7a535" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="7a535" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="7" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand All @@ -366,7 +366,7 @@
<children>
<component id="db7be" class="javax.swing.JCheckBox" binding="showUrlLinksBorder">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="2" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Show URL links border"/>
Expand Down Expand Up @@ -412,6 +412,14 @@
<text value="Render URL links"/>
</properties>
</component>
<component id="57c8f" class="javax.swing.JCheckBox" binding="insertPair">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Insert paired (), [] , {}, &quot;&quot;"/>
</properties>
</component>
</children>
</grid>
<component id="3f0ea" class="javax.swing.JLabel">
Expand Down
46 changes: 25 additions & 21 deletions src/org/plantuml/idea/lang/settings/PlantUmlSettingsPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class PlantUmlSettingsPage implements Configurable {
private JCheckBox usePageTitles;
private JCheckBox grammarSupport;
private JCheckBox keywordHighlighting;
private JCheckBox insertPair;

public PlantUmlSettingsPage() {
browse.addActionListener(new ActionListener() {
Expand Down Expand Up @@ -165,68 +166,71 @@ private void createUIComponents() {
}

public void setData(PlantUmlSettings data) {
renderDelay.setText(data.getRenderDelay());
cacheSize.setText(data.getCacheSize());
encoding.setText(data.getEncoding());
renderUrlLinks.setSelected(data.isRenderUrlLinks());
plantUMLErrorAnnotationExperimentalCheckBox.setSelected(data.isErrorAnnotationEnabled());
textFieldDotExecutable.setText(data.getDotExecutable());
usePreferentiallyGRAPHIZ_DOT.setSelected(data.isUsePreferentiallyGRAPHIZ_DOT());
config.setText(data.getConfig());
showUrlLinksBorder.setSelected(data.isShowUrlLinksBorder());
PLANTUML_LIMIT_SIZE.setText(data.getPLANTUML_LIMIT_SIZE());
includePaths.setText(data.getIncludedPaths());
switchToBundledAfterUpdate.setSelected(data.isSwitchToBundledAfterUpdate());
customPlantumlJar.setText(data.getCustomPlantumlJarPath());
renderDelay.setText(data.getRenderDelay());
cacheSize.setText(data.getCacheSize());
PLANTUML_LIMIT_SIZE.setText(data.getPLANTUML_LIMIT_SIZE());
showUrlLinksBorder.setSelected(data.isShowUrlLinksBorder());
usePageTitles.setSelected(data.isUsePageTitles());
grammarSupport.setSelected(data.isUseGrammar());
plantUMLErrorAnnotationExperimentalCheckBox.setSelected(data.isErrorAnnotationEnabled());
keywordHighlighting.setSelected(data.isKeywordHighlighting());
renderUrlLinks.setSelected(data.isRenderUrlLinks());
insertPair.setSelected(data.isInsertPair());
}

public void getData(PlantUmlSettings data) {
data.setRenderDelay(renderDelay.getText());
data.setCacheSize(cacheSize.getText());
data.setEncoding(encoding.getText());
data.setRenderUrlLinks(renderUrlLinks.isSelected());
data.setErrorAnnotationEnabled(plantUMLErrorAnnotationExperimentalCheckBox.isSelected());
data.setDotExecutable(textFieldDotExecutable.getText());
data.setUsePreferentiallyGRAPHIZ_DOT(usePreferentiallyGRAPHIZ_DOT.isSelected());
data.setConfig(config.getText());
data.setShowUrlLinksBorder(showUrlLinksBorder.isSelected());
data.setPLANTUML_LIMIT_SIZE(PLANTUML_LIMIT_SIZE.getText());
data.setIncludedPaths(includePaths.getText());
data.setSwitchToBundledAfterUpdate(switchToBundledAfterUpdate.isSelected());
data.setCustomPlantumlJarPath(customPlantumlJar.getText());
data.setRenderDelay(renderDelay.getText());
data.setCacheSize(cacheSize.getText());
data.setPLANTUML_LIMIT_SIZE(PLANTUML_LIMIT_SIZE.getText());
data.setShowUrlLinksBorder(showUrlLinksBorder.isSelected());
data.setUsePageTitles(usePageTitles.isSelected());
data.setUseGrammar(grammarSupport.isSelected());
data.setErrorAnnotationEnabled(plantUMLErrorAnnotationExperimentalCheckBox.isSelected());
data.setKeywordHighlighting(keywordHighlighting.isSelected());
data.setRenderUrlLinks(renderUrlLinks.isSelected());
data.setInsertPair(insertPair.isSelected());
}

public boolean isModified(PlantUmlSettings data) {
if (renderDelay.getText() != null ? !renderDelay.getText().equals(data.getRenderDelay()) : data.getRenderDelay() != null)
return true;
if (cacheSize.getText() != null ? !cacheSize.getText().equals(data.getCacheSize()) : data.getCacheSize() != null)
return true;
if (encoding.getText() != null ? !encoding.getText().equals(data.getEncoding()) : data.getEncoding() != null)
return true;
if (renderUrlLinks.isSelected() != data.isRenderUrlLinks()) return true;
if (plantUMLErrorAnnotationExperimentalCheckBox.isSelected() != data.isErrorAnnotationEnabled()) return true;
if (textFieldDotExecutable.getText() != null ? !textFieldDotExecutable.getText().equals(data.getDotExecutable()) : data.getDotExecutable() != null)
return true;
if (usePreferentiallyGRAPHIZ_DOT.isSelected() != data.isUsePreferentiallyGRAPHIZ_DOT()) return true;
if (config.getText() != null ? !config.getText().equals(data.getConfig()) : data.getConfig() != null)
return true;
if (showUrlLinksBorder.isSelected() != data.isShowUrlLinksBorder()) return true;
if (PLANTUML_LIMIT_SIZE.getText() != null ? !PLANTUML_LIMIT_SIZE.getText().equals(data.getPLANTUML_LIMIT_SIZE()) : data.getPLANTUML_LIMIT_SIZE() != null)
return true;
if (includePaths.getText() != null ? !includePaths.getText().equals(data.getIncludedPaths()) : data.getIncludedPaths() != null)
return true;
if (switchToBundledAfterUpdate.isSelected() != data.isSwitchToBundledAfterUpdate()) return true;
if (customPlantumlJar.getText() != null ? !customPlantumlJar.getText().equals(data.getCustomPlantumlJarPath()) : data.getCustomPlantumlJarPath() != null)
return true;
if (renderDelay.getText() != null ? !renderDelay.getText().equals(data.getRenderDelay()) : data.getRenderDelay() != null)
return true;
if (cacheSize.getText() != null ? !cacheSize.getText().equals(data.getCacheSize()) : data.getCacheSize() != null)
return true;
if (PLANTUML_LIMIT_SIZE.getText() != null ? !PLANTUML_LIMIT_SIZE.getText().equals(data.getPLANTUML_LIMIT_SIZE()) : data.getPLANTUML_LIMIT_SIZE() != null)
return true;
if (showUrlLinksBorder.isSelected() != data.isShowUrlLinksBorder()) return true;
if (usePageTitles.isSelected() != data.isUsePageTitles()) return true;
if (grammarSupport.isSelected() != data.isUseGrammar()) return true;
if (plantUMLErrorAnnotationExperimentalCheckBox.isSelected() != data.isErrorAnnotationEnabled()) return true;
if (keywordHighlighting.isSelected() != data.isKeywordHighlighting()) return true;
if (renderUrlLinks.isSelected() != data.isRenderUrlLinks()) return true;
if (insertPair.isSelected() != data.isInsertPair()) return true;
return false;
}
}

0 comments on commit 266131a

Please sign in to comment.