From d462bbdaa2b54d696d7708e431f4d4039b570d4b Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Sun, 4 Oct 2020 10:54:40 +0200 Subject: Add configuration option for how to treat newlines --- .../idea_caseconv/SelectionReplacerAction.java | 37 ++++++++++++++- .../code/idea_caseconv/settings/Configurable.java | 44 +++++++++++++++++ .../code/idea_caseconv/settings/NewlineMode.java | 6 +++ .../code/idea_caseconv/settings/Settings.java | 33 +++++++++++++ .../idea_caseconv/settings/SettingsComponent.java | 55 ++++++++++++++++++++++ 5 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/laria/code/idea_caseconv/settings/Configurable.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/settings/NewlineMode.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/settings/Settings.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/settings/SettingsComponent.java (limited to 'src/main/java/me/laria') diff --git a/src/main/java/me/laria/code/idea_caseconv/SelectionReplacerAction.java b/src/main/java/me/laria/code/idea_caseconv/SelectionReplacerAction.java index 692fd6c..088a1e2 100644 --- a/src/main/java/me/laria/code/idea_caseconv/SelectionReplacerAction.java +++ b/src/main/java/me/laria/code/idea_caseconv/SelectionReplacerAction.java @@ -9,8 +9,11 @@ import com.intellij.openapi.editor.CaretModel; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; +import me.laria.code.idea_caseconv.settings.Settings; import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; abstract class SelectionReplacerAction extends AnAction { @Override @@ -42,10 +45,42 @@ abstract class SelectionReplacerAction extends AnAction { continue; } + String selectedText = caret.getSelectedText(); + assert selectedText != null; // because we checked .hasSelection() above, selectedText will not be null. + + StringBuilder replacement = new StringBuilder(); + switch (Settings.getInstance().newlineMode) { + case RECORD_SEPARATOR: + Pattern p = Pattern.compile("\r?\n"); + Matcher m = p.matcher(selectedText); + + String record; + int off = 0; + while (m.find()) { + record = selectedText.substring(off, m.start()); + if (!record.isEmpty()) { + replacement.append(replace(record)); + } + + replacement.append(m.group(0)); // append the line separator + + off = m.end(); + } + + record = selectedText.substring(off); + if (!record.isEmpty()) { + replacement.append(replace(record)); + } + break; + case WHITESPACE: + replacement.append(replace(selectedText)); + break; + } + replacements.add(new Replacement( caret.getSelectionStart(), caret.getSelectionEnd(), - this.replace(caret.getSelectedText()) + replacement.toString() )); } diff --git a/src/main/java/me/laria/code/idea_caseconv/settings/Configurable.java b/src/main/java/me/laria/code/idea_caseconv/settings/Configurable.java new file mode 100644 index 0000000..056afd7 --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/settings/Configurable.java @@ -0,0 +1,44 @@ +package me.laria.code.idea_caseconv.settings; + +import com.intellij.openapi.options.ConfigurationException; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +public class Configurable implements com.intellij.openapi.options.Configurable { + private SettingsComponent settingsComponent = null; + + @Nls(capitalization = Nls.Capitalization.Sentence) + @Override + public String getDisplayName() { + return "Case conversion"; + } + + @Nullable + @Override + public JComponent createComponent() { + settingsComponent = new SettingsComponent(); + return settingsComponent.getPanel(); + } + + @Override + public void disposeUIResources() { + settingsComponent = null; + } + + @Override + public boolean isModified() { + return Settings.getInstance().newlineMode != settingsComponent.getNewlineMode(); + } + + @Override + public void apply() throws ConfigurationException { + Settings.getInstance().newlineMode = settingsComponent.getNewlineMode(); + } + + @Override + public void reset() { + settingsComponent.setNewlineMode(Settings.getInstance().newlineMode); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/settings/NewlineMode.java b/src/main/java/me/laria/code/idea_caseconv/settings/NewlineMode.java new file mode 100644 index 0000000..2f669bf --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/settings/NewlineMode.java @@ -0,0 +1,6 @@ +package me.laria.code.idea_caseconv.settings; + +public enum NewlineMode { + WHITESPACE, + RECORD_SEPARATOR, +} diff --git a/src/main/java/me/laria/code/idea_caseconv/settings/Settings.java b/src/main/java/me/laria/code/idea_caseconv/settings/Settings.java new file mode 100644 index 0000000..28f15b1 --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/settings/Settings.java @@ -0,0 +1,33 @@ +package me.laria.code.idea_caseconv.settings; + +import com.intellij.openapi.components.PersistentStateComponent; +import com.intellij.openapi.components.ServiceManager; +import com.intellij.openapi.components.State; +import com.intellij.openapi.components.Storage; +import com.intellij.util.xmlb.XmlSerializerUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@State( + name = "me.laria.code.idea_caseconv.SettingsState", + storages = {@Storage("me_laria_code_idea_caseconv_plugin.xml")} +) +public class Settings implements PersistentStateComponent { + public NewlineMode newlineMode = NewlineMode.WHITESPACE; + + + public static Settings getInstance() { + return ServiceManager.getService(Settings.class); + } + + @Nullable + @Override + public Settings getState() { + return this; + } + + @Override + public void loadState(@NotNull Settings state) { + XmlSerializerUtil.copyBean(state, this); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/settings/SettingsComponent.java b/src/main/java/me/laria/code/idea_caseconv/settings/SettingsComponent.java new file mode 100644 index 0000000..8de59eb --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/settings/SettingsComponent.java @@ -0,0 +1,55 @@ +package me.laria.code.idea_caseconv.settings; + +import com.intellij.openapi.ui.ComboBox; +import com.intellij.util.ui.FormBuilder; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; + +public class SettingsComponent { + final private JPanel mainPanel; + final private ComboBox newlineModeSelect; + + public SettingsComponent() { + newlineModeSelect = new ComboBox<>(new String[]{ + "White space", + "Record separator", + }); + + mainPanel = FormBuilder.createFormBuilder() + .addLabeledComponent( + "Treat newline as", + newlineModeSelect, + 1, + false + ) + .addComponentFillVertically(new JPanel(), 0) + .getPanel(); + } + + public NewlineMode getNewlineMode() { + switch (newlineModeSelect.getSelectedIndex()) { + case 0: + return NewlineMode.WHITESPACE; + case 1: + return NewlineMode.RECORD_SEPARATOR; + default: + throw new IndexOutOfBoundsException("newlineModeSelect returned invalid index"); + } + } + + public void setNewlineMode(@NotNull NewlineMode newlineMode) { + switch (newlineMode) { + case WHITESPACE: + newlineModeSelect.setSelectedIndex(0); + break; + case RECORD_SEPARATOR: + newlineModeSelect.setSelectedIndex(1); + break; + } + } + + public JComponent getPanel() { + return mainPanel; + } +} -- cgit v1.2.3-70-g09d2