diff --git a/META-INF/plugin.xml b/META-INF/plugin.xml index 95e468e0..18d14bfb 100644 --- a/META-INF/plugin.xml +++ b/META-INF/plugin.xml @@ -167,5 +167,6 @@ + diff --git a/src/org/plantuml/idea/lang/PlantUmlTypedHandlerDelegate.java b/src/org/plantuml/idea/lang/PlantUmlTypedHandlerDelegate.java new file mode 100644 index 00000000..c8699df3 --- /dev/null +++ b/src/org/plantuml/idea/lang/PlantUmlTypedHandlerDelegate.java @@ -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); + } +} diff --git a/src/org/plantuml/idea/lang/settings/PlantUmlSettings.java b/src/org/plantuml/idea/lang/settings/PlantUmlSettings.java index a6df5a89..fa4b19e3 100644 --- a/src/org/plantuml/idea/lang/settings/PlantUmlSettings.java +++ b/src/org/plantuml/idea/lang/settings/PlantUmlSettings.java @@ -64,6 +64,7 @@ public class PlantUmlSettings implements PersistentStateComponent - + @@ -366,7 +366,7 @@ - + @@ -412,6 +412,14 @@ + + + + + + + + diff --git a/src/org/plantuml/idea/lang/settings/PlantUmlSettingsPage.java b/src/org/plantuml/idea/lang/settings/PlantUmlSettingsPage.java index fe22ec17..b76bbc72 100644 --- a/src/org/plantuml/idea/lang/settings/PlantUmlSettingsPage.java +++ b/src/org/plantuml/idea/lang/settings/PlantUmlSettingsPage.java @@ -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() { @@ -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; } }