From 904b2cbff1b49ba39f89be4e42a26d1f4b26c66a Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Tue, 25 Jun 2019 23:05:59 +0200 Subject: Transform to gradle project --- .gitignore | 3 + Case Conversion.iml | 13 -- build.gradle | 23 +++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 56177 bytes gradle/wrapper/gradle-wrapper.properties | 5 + gradlew | 172 +++++++++++++++++++++ gradlew.bat | 84 ++++++++++ resources/META-INF/plugin.xml | 81 ---------- settings.gradle | 2 + .../laria/code/idea_caseconv/CamelCaseAction.java | 18 +++ .../laria/code/idea_caseconv/DashCaseAction.java | 10 ++ .../laria/code/idea_caseconv/FlipCaseAction.java | 12 ++ .../laria/code/idea_caseconv/LowerCaseAction.java | 8 + .../laria/code/idea_caseconv/PascalCaseAction.java | 10 ++ .../me/laria/code/idea_caseconv/Replacement.java | 25 +++ .../idea_caseconv/ScreamingSnakeCaseAction.java | 10 ++ .../idea_caseconv/SelectionReplacerAction.java | 59 +++++++ .../code/idea_caseconv/SeparateWordsAction.java | 10 ++ .../laria/code/idea_caseconv/SnakeCaseAction.java | 10 ++ .../laria/code/idea_caseconv/TitleCaseAction.java | 10 ++ .../laria/code/idea_caseconv/UpperCaseAction.java | 8 + .../code/idea_caseconv/WordSplitConverter.java | 36 +++++ .../laria/code/idea_caseconv/WordTransformer.java | 7 + src/main/resources/META-INF/plugin.xml | 81 ++++++++++ .../laria/code/idea_caseconv/CamelCaseAction.java | 18 --- .../laria/code/idea_caseconv/DashCaseAction.java | 10 -- .../laria/code/idea_caseconv/FlipCaseAction.java | 12 -- .../laria/code/idea_caseconv/LowerCaseAction.java | 8 - .../laria/code/idea_caseconv/PascalCaseAction.java | 10 -- src/me/laria/code/idea_caseconv/Replacement.java | 25 --- .../idea_caseconv/ScreamingSnakeCaseAction.java | 10 -- .../idea_caseconv/SelectionReplacerAction.java | 59 ------- .../code/idea_caseconv/SeparateWordsAction.java | 10 -- .../laria/code/idea_caseconv/SnakeCaseAction.java | 10 -- .../laria/code/idea_caseconv/TitleCaseAction.java | 10 -- .../laria/code/idea_caseconv/UpperCaseAction.java | 8 - .../code/idea_caseconv/WordSplitConverter.java | 36 ----- .../laria/code/idea_caseconv/WordTransformer.java | 7 - 38 files changed, 603 insertions(+), 327 deletions(-) delete mode 100644 Case Conversion.iml create mode 100644 build.gradle create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat delete mode 100644 resources/META-INF/plugin.xml create mode 100644 settings.gradle create mode 100644 src/main/java/me/laria/code/idea_caseconv/CamelCaseAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/DashCaseAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/FlipCaseAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/LowerCaseAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/PascalCaseAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/Replacement.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/ScreamingSnakeCaseAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/SelectionReplacerAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/SeparateWordsAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/SnakeCaseAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/TitleCaseAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/UpperCaseAction.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/WordSplitConverter.java create mode 100644 src/main/java/me/laria/code/idea_caseconv/WordTransformer.java create mode 100644 src/main/resources/META-INF/plugin.xml delete mode 100644 src/me/laria/code/idea_caseconv/CamelCaseAction.java delete mode 100644 src/me/laria/code/idea_caseconv/DashCaseAction.java delete mode 100644 src/me/laria/code/idea_caseconv/FlipCaseAction.java delete mode 100644 src/me/laria/code/idea_caseconv/LowerCaseAction.java delete mode 100644 src/me/laria/code/idea_caseconv/PascalCaseAction.java delete mode 100644 src/me/laria/code/idea_caseconv/Replacement.java delete mode 100644 src/me/laria/code/idea_caseconv/ScreamingSnakeCaseAction.java delete mode 100644 src/me/laria/code/idea_caseconv/SelectionReplacerAction.java delete mode 100644 src/me/laria/code/idea_caseconv/SeparateWordsAction.java delete mode 100644 src/me/laria/code/idea_caseconv/SnakeCaseAction.java delete mode 100644 src/me/laria/code/idea_caseconv/TitleCaseAction.java delete mode 100644 src/me/laria/code/idea_caseconv/UpperCaseAction.java delete mode 100644 src/me/laria/code/idea_caseconv/WordSplitConverter.java delete mode 100644 src/me/laria/code/idea_caseconv/WordTransformer.java diff --git a/.gitignore b/.gitignore index ba1f399..b413141 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ Case Conversion.jar out/ +.gradle/ +.idea/ +build/ diff --git a/Case Conversion.iml b/Case Conversion.iml deleted file mode 100644 index e025b20..0000000 --- a/Case Conversion.iml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..f2cfa25 --- /dev/null +++ b/build.gradle @@ -0,0 +1,23 @@ +plugins { + id 'java' + id 'org.jetbrains.intellij' version '0.4.9' +} + +group 'me.laria.code' +version '0.5.1' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' +} + +// See https://github.com/JetBrains/gradle-intellij-plugin/ +intellij { + version '2018.1.1' + updateSinceUntilBuild false +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..94336fc Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..290541c --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..cccdd3d --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..e95643d --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml deleted file mode 100644 index 9ab02a7..0000000 --- a/resources/META-INF/plugin.xml +++ /dev/null @@ -1,81 +0,0 @@ - - me.laria.code.idea_caseconv - Case conversion - 0.5.1 - Laria Chabowski - - -

Convert between cases.

-

The Plugin provides actions to convert to the following cases:

-
    -
  • snake_case
  • -
  • dash-case
  • -
  • camelCase
  • -
  • PascalCase
  • -
  • SCREAMING_SNAKE_CASE
  • -
  • UPPER CASE
  • -
  • lower case
  • -
  • fLIP cASE
  • -
  • Title Case
  • -
- ]]>
- - - - - - - - - com.intellij.modules.lang - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..be486f2 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'idea_caseconv' + diff --git a/src/main/java/me/laria/code/idea_caseconv/CamelCaseAction.java b/src/main/java/me/laria/code/idea_caseconv/CamelCaseAction.java new file mode 100644 index 0000000..93ca85b --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/CamelCaseAction.java @@ -0,0 +1,18 @@ +package me.laria.code.idea_caseconv; + +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; + +public class CamelCaseAction extends SelectionReplacerAction { + @NotNull + public static String ucfirst(String s) { + s = s.toLowerCase(); + return s.substring(0, 1).toUpperCase().concat(s.substring(1)); + } + + @Override + protected String replace(String s) { + return WordSplitConverter.convert(s, "", (i, part) -> Optional.of(i == 0 ? part.toLowerCase() : ucfirst(part))); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/DashCaseAction.java b/src/main/java/me/laria/code/idea_caseconv/DashCaseAction.java new file mode 100644 index 0000000..5e030ab --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/DashCaseAction.java @@ -0,0 +1,10 @@ +package me.laria.code.idea_caseconv; + +import java.util.Optional; + +public class DashCaseAction extends SelectionReplacerAction { + @Override + protected String replace(String s) { + return WordSplitConverter.convert(s, "-", (i, s1) -> Optional.of(s1.toLowerCase())); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/FlipCaseAction.java b/src/main/java/me/laria/code/idea_caseconv/FlipCaseAction.java new file mode 100644 index 0000000..22df45a --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/FlipCaseAction.java @@ -0,0 +1,12 @@ +package me.laria.code.idea_caseconv; + +public class FlipCaseAction extends SelectionReplacerAction { + @Override + protected String replace(String s) { + StringBuilder builder = new StringBuilder(); + + s.codePoints().forEach(c -> builder.appendCodePoint(Character.isLowerCase(c) ? Character.toUpperCase(c) : Character.toLowerCase(c))); + + return builder.toString(); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/LowerCaseAction.java b/src/main/java/me/laria/code/idea_caseconv/LowerCaseAction.java new file mode 100644 index 0000000..d32507e --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/LowerCaseAction.java @@ -0,0 +1,8 @@ +package me.laria.code.idea_caseconv; + +public class LowerCaseAction extends SelectionReplacerAction { + @Override + protected String replace(String s) { + return s.toLowerCase(); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/PascalCaseAction.java b/src/main/java/me/laria/code/idea_caseconv/PascalCaseAction.java new file mode 100644 index 0000000..fb13265 --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/PascalCaseAction.java @@ -0,0 +1,10 @@ +package me.laria.code.idea_caseconv; + +import java.util.Optional; + +public class PascalCaseAction extends SelectionReplacerAction { + @Override + protected String replace(String s) { + return WordSplitConverter.convert(s, "", (i, part) -> Optional.of(CamelCaseAction.ucfirst(part))); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/Replacement.java b/src/main/java/me/laria/code/idea_caseconv/Replacement.java new file mode 100644 index 0000000..804812b --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/Replacement.java @@ -0,0 +1,25 @@ +package me.laria.code.idea_caseconv; + +import com.intellij.openapi.editor.Document; +import org.jetbrains.annotations.NotNull; + +class Replacement implements Comparable { + private final int a; + private final int b; + private final String s; + + public Replacement(int a, int b, String s) { + this.a = Integer.min(a, b); + this.b = Integer.max(a, b); + this.s = s; + } + + public void doReplace(Document doc) { + doc.replaceString(this.a, this.b, this.s); + } + + @Override + public int compareTo(@NotNull Replacement o) { + return Integer.compare(this.a, o.a); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/ScreamingSnakeCaseAction.java b/src/main/java/me/laria/code/idea_caseconv/ScreamingSnakeCaseAction.java new file mode 100644 index 0000000..c77bbfe --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/ScreamingSnakeCaseAction.java @@ -0,0 +1,10 @@ +package me.laria.code.idea_caseconv; + +import java.util.Optional; + +public class ScreamingSnakeCaseAction extends SelectionReplacerAction { + @Override + protected String replace(String s) { + return WordSplitConverter.convert(s, "_", (i, part) -> Optional.of(part.toUpperCase())); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/SelectionReplacerAction.java b/src/main/java/me/laria/code/idea_caseconv/SelectionReplacerAction.java new file mode 100644 index 0000000..692fd6c --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/SelectionReplacerAction.java @@ -0,0 +1,59 @@ +package me.laria.code.idea_caseconv; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.editor.Caret; +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 java.util.ArrayList; + +abstract class SelectionReplacerAction extends AnAction { + @Override + public void update(final AnActionEvent e) { + //Get required data keys + final Project project = e.getData(CommonDataKeys.PROJECT); + final Editor editor = e.getData(CommonDataKeys.EDITOR); + //Set visibility only in case of existing project and editor and if some text in the editor is selected + e.getPresentation().setVisible( + project != null + && editor != null + && editor.getSelectionModel().hasSelection() + ); + } + + @Override + public void actionPerformed(AnActionEvent anActionEvent) { + Editor editor = anActionEvent.getData(CommonDataKeys.EDITOR); + Project project = anActionEvent.getData(CommonDataKeys.PROJECT); + assert editor != null; + assert project != null; + + CaretModel caretModel = editor.getCaretModel(); + Document document = editor.getDocument(); + + ArrayList replacements = new ArrayList<>(); + for (Caret caret : caretModel.getAllCarets()) { + if (!caret.hasSelection()) { + continue; + } + + replacements.add(new Replacement( + caret.getSelectionStart(), + caret.getSelectionEnd(), + this.replace(caret.getSelectedText()) + )); + } + + // Sort in reverse order so a replacement won't mess up the indices of the other replacements + replacements.sort((o1, o2) -> -o1.compareTo(o2)); + + WriteCommandAction.runWriteCommandAction(project, () -> replacements.forEach(r -> r.doReplace(document))); + } + + abstract protected String replace(String s); +} diff --git a/src/main/java/me/laria/code/idea_caseconv/SeparateWordsAction.java b/src/main/java/me/laria/code/idea_caseconv/SeparateWordsAction.java new file mode 100644 index 0000000..6f3f4fe --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/SeparateWordsAction.java @@ -0,0 +1,10 @@ +package me.laria.code.idea_caseconv; + +import java.util.Optional; + +public class SeparateWordsAction extends SelectionReplacerAction { + @Override + protected String replace(String s) { + return WordSplitConverter.convert(s, " ", (i, part) -> Optional.of(part.toLowerCase())); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/SnakeCaseAction.java b/src/main/java/me/laria/code/idea_caseconv/SnakeCaseAction.java new file mode 100644 index 0000000..9853676 --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/SnakeCaseAction.java @@ -0,0 +1,10 @@ +package me.laria.code.idea_caseconv; + +import java.util.Optional; + +public class SnakeCaseAction extends SelectionReplacerAction { + @Override + protected String replace(String s) { + return WordSplitConverter.convert(s, "_", (i, part) -> Optional.of(part.toLowerCase())); + } +} \ No newline at end of file diff --git a/src/main/java/me/laria/code/idea_caseconv/TitleCaseAction.java b/src/main/java/me/laria/code/idea_caseconv/TitleCaseAction.java new file mode 100644 index 0000000..70024c3 --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/TitleCaseAction.java @@ -0,0 +1,10 @@ +package me.laria.code.idea_caseconv; + +import java.util.Optional; + +public class TitleCaseAction extends SelectionReplacerAction { + @Override + protected String replace(String s) { + return WordSplitConverter.convert(s, " ", (i, part) -> Optional.of(CamelCaseAction.ucfirst(part))); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/UpperCaseAction.java b/src/main/java/me/laria/code/idea_caseconv/UpperCaseAction.java new file mode 100644 index 0000000..8478f4f --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/UpperCaseAction.java @@ -0,0 +1,8 @@ +package me.laria.code.idea_caseconv; + +public class UpperCaseAction extends SelectionReplacerAction { + @Override + protected String replace(String s) { + return s.toUpperCase(); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/WordSplitConverter.java b/src/main/java/me/laria/code/idea_caseconv/WordSplitConverter.java new file mode 100644 index 0000000..9b87a25 --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/WordSplitConverter.java @@ -0,0 +1,36 @@ +package me.laria.code.idea_caseconv; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +class WordSplitConverter { + private static List split(String s) { + Pattern decamelize = Pattern.compile("(? parts = new ArrayList<>(); + for (String p : all_parts) { + p = p.trim(); + if (!p.equals("")) { + parts.add(p.toLowerCase()); + } + } + + return parts; + } + + public static String convert(String in, String glue, WordTransformer transform) { + List partsIn = split(in); + ArrayList partsOut = new ArrayList<>(); + + int i = 0; + for (String part : partsIn) { + transform.transform(i, part).ifPresent(partsOut::add); + i++; + } + + return String.join(glue, partsOut); + } +} diff --git a/src/main/java/me/laria/code/idea_caseconv/WordTransformer.java b/src/main/java/me/laria/code/idea_caseconv/WordTransformer.java new file mode 100644 index 0000000..ad0aeab --- /dev/null +++ b/src/main/java/me/laria/code/idea_caseconv/WordTransformer.java @@ -0,0 +1,7 @@ +package me.laria.code.idea_caseconv; + +import java.util.Optional; + +interface WordTransformer { + Optional transform(int i, String s); +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml new file mode 100644 index 0000000..9ab02a7 --- /dev/null +++ b/src/main/resources/META-INF/plugin.xml @@ -0,0 +1,81 @@ + + me.laria.code.idea_caseconv + Case conversion + 0.5.1 + Laria Chabowski + + +

Convert between cases.

+

The Plugin provides actions to convert to the following cases:

+
    +
  • snake_case
  • +
  • dash-case
  • +
  • camelCase
  • +
  • PascalCase
  • +
  • SCREAMING_SNAKE_CASE
  • +
  • UPPER CASE
  • +
  • lower case
  • +
  • fLIP cASE
  • +
  • Title Case
  • +
+ ]]>
+ + + + + + + + + com.intellij.modules.lang + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/src/me/laria/code/idea_caseconv/CamelCaseAction.java b/src/me/laria/code/idea_caseconv/CamelCaseAction.java deleted file mode 100644 index 93ca85b..0000000 --- a/src/me/laria/code/idea_caseconv/CamelCaseAction.java +++ /dev/null @@ -1,18 +0,0 @@ -package me.laria.code.idea_caseconv; - -import org.jetbrains.annotations.NotNull; - -import java.util.Optional; - -public class CamelCaseAction extends SelectionReplacerAction { - @NotNull - public static String ucfirst(String s) { - s = s.toLowerCase(); - return s.substring(0, 1).toUpperCase().concat(s.substring(1)); - } - - @Override - protected String replace(String s) { - return WordSplitConverter.convert(s, "", (i, part) -> Optional.of(i == 0 ? part.toLowerCase() : ucfirst(part))); - } -} diff --git a/src/me/laria/code/idea_caseconv/DashCaseAction.java b/src/me/laria/code/idea_caseconv/DashCaseAction.java deleted file mode 100644 index 5e030ab..0000000 --- a/src/me/laria/code/idea_caseconv/DashCaseAction.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.laria.code.idea_caseconv; - -import java.util.Optional; - -public class DashCaseAction extends SelectionReplacerAction { - @Override - protected String replace(String s) { - return WordSplitConverter.convert(s, "-", (i, s1) -> Optional.of(s1.toLowerCase())); - } -} diff --git a/src/me/laria/code/idea_caseconv/FlipCaseAction.java b/src/me/laria/code/idea_caseconv/FlipCaseAction.java deleted file mode 100644 index 22df45a..0000000 --- a/src/me/laria/code/idea_caseconv/FlipCaseAction.java +++ /dev/null @@ -1,12 +0,0 @@ -package me.laria.code.idea_caseconv; - -public class FlipCaseAction extends SelectionReplacerAction { - @Override - protected String replace(String s) { - StringBuilder builder = new StringBuilder(); - - s.codePoints().forEach(c -> builder.appendCodePoint(Character.isLowerCase(c) ? Character.toUpperCase(c) : Character.toLowerCase(c))); - - return builder.toString(); - } -} diff --git a/src/me/laria/code/idea_caseconv/LowerCaseAction.java b/src/me/laria/code/idea_caseconv/LowerCaseAction.java deleted file mode 100644 index d32507e..0000000 --- a/src/me/laria/code/idea_caseconv/LowerCaseAction.java +++ /dev/null @@ -1,8 +0,0 @@ -package me.laria.code.idea_caseconv; - -public class LowerCaseAction extends SelectionReplacerAction { - @Override - protected String replace(String s) { - return s.toLowerCase(); - } -} diff --git a/src/me/laria/code/idea_caseconv/PascalCaseAction.java b/src/me/laria/code/idea_caseconv/PascalCaseAction.java deleted file mode 100644 index fb13265..0000000 --- a/src/me/laria/code/idea_caseconv/PascalCaseAction.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.laria.code.idea_caseconv; - -import java.util.Optional; - -public class PascalCaseAction extends SelectionReplacerAction { - @Override - protected String replace(String s) { - return WordSplitConverter.convert(s, "", (i, part) -> Optional.of(CamelCaseAction.ucfirst(part))); - } -} diff --git a/src/me/laria/code/idea_caseconv/Replacement.java b/src/me/laria/code/idea_caseconv/Replacement.java deleted file mode 100644 index 804812b..0000000 --- a/src/me/laria/code/idea_caseconv/Replacement.java +++ /dev/null @@ -1,25 +0,0 @@ -package me.laria.code.idea_caseconv; - -import com.intellij.openapi.editor.Document; -import org.jetbrains.annotations.NotNull; - -class Replacement implements Comparable { - private final int a; - private final int b; - private final String s; - - public Replacement(int a, int b, String s) { - this.a = Integer.min(a, b); - this.b = Integer.max(a, b); - this.s = s; - } - - public void doReplace(Document doc) { - doc.replaceString(this.a, this.b, this.s); - } - - @Override - public int compareTo(@NotNull Replacement o) { - return Integer.compare(this.a, o.a); - } -} diff --git a/src/me/laria/code/idea_caseconv/ScreamingSnakeCaseAction.java b/src/me/laria/code/idea_caseconv/ScreamingSnakeCaseAction.java deleted file mode 100644 index c77bbfe..0000000 --- a/src/me/laria/code/idea_caseconv/ScreamingSnakeCaseAction.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.laria.code.idea_caseconv; - -import java.util.Optional; - -public class ScreamingSnakeCaseAction extends SelectionReplacerAction { - @Override - protected String replace(String s) { - return WordSplitConverter.convert(s, "_", (i, part) -> Optional.of(part.toUpperCase())); - } -} diff --git a/src/me/laria/code/idea_caseconv/SelectionReplacerAction.java b/src/me/laria/code/idea_caseconv/SelectionReplacerAction.java deleted file mode 100644 index 692fd6c..0000000 --- a/src/me/laria/code/idea_caseconv/SelectionReplacerAction.java +++ /dev/null @@ -1,59 +0,0 @@ -package me.laria.code.idea_caseconv; - -import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.actionSystem.CommonDataKeys; -import com.intellij.openapi.command.WriteCommandAction; -import com.intellij.openapi.editor.Caret; -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 java.util.ArrayList; - -abstract class SelectionReplacerAction extends AnAction { - @Override - public void update(final AnActionEvent e) { - //Get required data keys - final Project project = e.getData(CommonDataKeys.PROJECT); - final Editor editor = e.getData(CommonDataKeys.EDITOR); - //Set visibility only in case of existing project and editor and if some text in the editor is selected - e.getPresentation().setVisible( - project != null - && editor != null - && editor.getSelectionModel().hasSelection() - ); - } - - @Override - public void actionPerformed(AnActionEvent anActionEvent) { - Editor editor = anActionEvent.getData(CommonDataKeys.EDITOR); - Project project = anActionEvent.getData(CommonDataKeys.PROJECT); - assert editor != null; - assert project != null; - - CaretModel caretModel = editor.getCaretModel(); - Document document = editor.getDocument(); - - ArrayList replacements = new ArrayList<>(); - for (Caret caret : caretModel.getAllCarets()) { - if (!caret.hasSelection()) { - continue; - } - - replacements.add(new Replacement( - caret.getSelectionStart(), - caret.getSelectionEnd(), - this.replace(caret.getSelectedText()) - )); - } - - // Sort in reverse order so a replacement won't mess up the indices of the other replacements - replacements.sort((o1, o2) -> -o1.compareTo(o2)); - - WriteCommandAction.runWriteCommandAction(project, () -> replacements.forEach(r -> r.doReplace(document))); - } - - abstract protected String replace(String s); -} diff --git a/src/me/laria/code/idea_caseconv/SeparateWordsAction.java b/src/me/laria/code/idea_caseconv/SeparateWordsAction.java deleted file mode 100644 index 6f3f4fe..0000000 --- a/src/me/laria/code/idea_caseconv/SeparateWordsAction.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.laria.code.idea_caseconv; - -import java.util.Optional; - -public class SeparateWordsAction extends SelectionReplacerAction { - @Override - protected String replace(String s) { - return WordSplitConverter.convert(s, " ", (i, part) -> Optional.of(part.toLowerCase())); - } -} diff --git a/src/me/laria/code/idea_caseconv/SnakeCaseAction.java b/src/me/laria/code/idea_caseconv/SnakeCaseAction.java deleted file mode 100644 index 9853676..0000000 --- a/src/me/laria/code/idea_caseconv/SnakeCaseAction.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.laria.code.idea_caseconv; - -import java.util.Optional; - -public class SnakeCaseAction extends SelectionReplacerAction { - @Override - protected String replace(String s) { - return WordSplitConverter.convert(s, "_", (i, part) -> Optional.of(part.toLowerCase())); - } -} \ No newline at end of file diff --git a/src/me/laria/code/idea_caseconv/TitleCaseAction.java b/src/me/laria/code/idea_caseconv/TitleCaseAction.java deleted file mode 100644 index 70024c3..0000000 --- a/src/me/laria/code/idea_caseconv/TitleCaseAction.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.laria.code.idea_caseconv; - -import java.util.Optional; - -public class TitleCaseAction extends SelectionReplacerAction { - @Override - protected String replace(String s) { - return WordSplitConverter.convert(s, " ", (i, part) -> Optional.of(CamelCaseAction.ucfirst(part))); - } -} diff --git a/src/me/laria/code/idea_caseconv/UpperCaseAction.java b/src/me/laria/code/idea_caseconv/UpperCaseAction.java deleted file mode 100644 index 8478f4f..0000000 --- a/src/me/laria/code/idea_caseconv/UpperCaseAction.java +++ /dev/null @@ -1,8 +0,0 @@ -package me.laria.code.idea_caseconv; - -public class UpperCaseAction extends SelectionReplacerAction { - @Override - protected String replace(String s) { - return s.toUpperCase(); - } -} diff --git a/src/me/laria/code/idea_caseconv/WordSplitConverter.java b/src/me/laria/code/idea_caseconv/WordSplitConverter.java deleted file mode 100644 index 9b87a25..0000000 --- a/src/me/laria/code/idea_caseconv/WordSplitConverter.java +++ /dev/null @@ -1,36 +0,0 @@ -package me.laria.code.idea_caseconv; - -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; - -class WordSplitConverter { - private static List split(String s) { - Pattern decamelize = Pattern.compile("(? parts = new ArrayList<>(); - for (String p : all_parts) { - p = p.trim(); - if (!p.equals("")) { - parts.add(p.toLowerCase()); - } - } - - return parts; - } - - public static String convert(String in, String glue, WordTransformer transform) { - List partsIn = split(in); - ArrayList partsOut = new ArrayList<>(); - - int i = 0; - for (String part : partsIn) { - transform.transform(i, part).ifPresent(partsOut::add); - i++; - } - - return String.join(glue, partsOut); - } -} diff --git a/src/me/laria/code/idea_caseconv/WordTransformer.java b/src/me/laria/code/idea_caseconv/WordTransformer.java deleted file mode 100644 index ad0aeab..0000000 --- a/src/me/laria/code/idea_caseconv/WordTransformer.java +++ /dev/null @@ -1,7 +0,0 @@ -package me.laria.code.idea_caseconv; - -import java.util.Optional; - -interface WordTransformer { - Optional transform(int i, String s); -} -- cgit v1.2.3-70-g09d2