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 --- README.md | 13 +++++ .../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 ++++++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 17 ++++++- 7 files changed, 202 insertions(+), 3 deletions(-) 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 diff --git a/README.md b/README.md index 7b8c18a..1357c9b 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,19 @@ There are multiple ways to do this: - Create a keyboard shortcut: Open Settings > Keymap and search for "Convert Case" and create a keyboard shortcut for the desired action. +Configuration +------------- + +The plugin has a configuration page in the "Settings" window of the IDE. +You can find it under Tools > Case conversion. + +Currently there is only one configuration option: + +- **Treat newline as**: Can be either "White space" in which case selected + line breaks will be treated like every other white space (i.e. as a word + separator), or it can be "Record separator" in which case each line will + be converted individually. The default is "White space". + Contributing ------------ 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; + } +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 84a690e..9517e40 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -27,6 +27,14 @@
  • Use the Find Action feature: Bring up the “Find Action” search dialog (default keyboard shortcut is Ctrl+Shift+A) and type the name of the desired convert action (or type “Convert Case” to see all actions). Tip: This search uses fuzzy matching so you can just type “camcas” for “Convert Case: camelCase”.
  • Create a keyboard shortcut: Open Settings > Keymap and search for “Convert Case” and create a keyboard shortcut for the desired action.
  • +

    You can configure how newlines should be treated: Go to "Settings" > "Tools" > "Case conversion". The setting "Treat newline as" has these two options:

    +
    +
    White space
    +
    Treat newline like other whitespace (i.e. as a word separator)
    + +
    Record separator
    +
    Treat newline as a record separator. This converts every line individually
    +
    ]]> @@ -53,7 +61,13 @@ com.intellij.modules.lang - + + @@ -98,5 +112,4 @@ - -- cgit v1.2.3-54-g00ecf