aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-10-04 10:54:40 +0200
committerLaria Carolin Chabowski <laria@laria.me>2020-10-04 11:27:20 +0200
commitd462bbdaa2b54d696d7708e431f4d4039b570d4b (patch)
treefb55f5098522b4263cfc44aa7847c01baaf1c4ff
parent1d07f7ba44163364b22521056e7ff774fc1b1185 (diff)
downloadidea_caseconv-d462bbdaa2b54d696d7708e431f4d4039b570d4b.tar.gz
idea_caseconv-d462bbdaa2b54d696d7708e431f4d4039b570d4b.tar.bz2
idea_caseconv-d462bbdaa2b54d696d7708e431f4d4039b570d4b.zip
Add configuration option for how to treat newlines
-rw-r--r--README.md13
-rw-r--r--src/main/java/me/laria/code/idea_caseconv/SelectionReplacerAction.java37
-rw-r--r--src/main/java/me/laria/code/idea_caseconv/settings/Configurable.java44
-rw-r--r--src/main/java/me/laria/code/idea_caseconv/settings/NewlineMode.java6
-rw-r--r--src/main/java/me/laria/code/idea_caseconv/settings/Settings.java33
-rw-r--r--src/main/java/me/laria/code/idea_caseconv/settings/SettingsComponent.java55
-rw-r--r--src/main/resources/META-INF/plugin.xml17
7 files changed, 202 insertions, 3 deletions
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<Settings> {
+ 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<String> 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 @@
<li>Use the Find Action feature: Bring up the &ldquo;Find Action&rdquo; search dialog (default keyboard shortcut is Ctrl+Shift+A) and type the name of the desired convert action (or type &ldquo;Convert Case&rdquo; to see all actions). Tip: This search uses fuzzy matching so you can just type &ldquo;camcas&rdquo; for &ldquo;Convert Case: camelCase&rdquo;.</li>
<li>Create a keyboard shortcut: Open Settings > Keymap and search for &ldquo;Convert Case&rdquo; and create a keyboard shortcut for the desired action.</li>
</ul>
+ <p>You can configure how newlines should be treated: Go to "Settings" > "Tools" > "Case conversion". The setting "Treat newline as" has these two options:</p>
+ <dl>
+ <dt>White space</dt>
+ <dd>Treat newline like other whitespace (i.e. as a word separator)</dd>
+
+ <dt>Record separator</dt>
+ <dd>Treat newline as a record separator. This converts every line individually</dd>
+ </dl>
</html>]]></description>
<change-notes><![CDATA[<html>
@@ -53,7 +61,13 @@
<depends>com.intellij.modules.lang</depends>
<extensions defaultExtensionNs="com.intellij">
- <!-- Add your extensions here -->
+ <applicationService serviceImplementation="me.laria.code.idea_caseconv.settings.Settings" />
+ <applicationConfigurable
+ parentId="tools"
+ instance="me.laria.code.idea_caseconv.settings.Configurable"
+ id="me.laria.code.idea_caseconv.settings.Configurable"
+ displayName="Case conversion"
+ />
</extensions>
<actions>
@@ -98,5 +112,4 @@
</action>
</group>
</actions>
-
</idea-plugin>