Skip to content

Commit

Permalink
Snake case drops numbers #8
Browse files Browse the repository at this point in the history
  • Loading branch information
krasa committed Oct 5, 2015
1 parent 0d5b297 commit 086ff73
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
8 changes: 7 additions & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE idea-plugin PUBLIC "Plugin/DTD" "http://plugins.intellij.net/plugin.dtd">
<idea-plugin url="https://github.com/krasa/StringManipulation">
<name>String Manipulation</name>
<version>4.0.135.445</version>
<version>4.1.135.445</version>
<vendor email="vojta.krasa@gmail.com">original author: Olivier Smedile, current maintainer: Vojtech Krasa</vendor>
<idea-version since-build="135.445"/>

Expand Down Expand Up @@ -311,6 +311,12 @@
<change-notes>
<![CDATA[
<p>
<div>
<h4>Version 4.1</h4>
<ul>
<li>fixed converting with numbers (now: v2Counter3 -> V2_COUNTER3)</li>
</ul>
</div>
<div>
<h4>Version 4.0</h4>
<ul>
Expand Down
35 changes: 20 additions & 15 deletions src/osmedile/intellij/stringmanip/utils/StringUtil.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package osmedile.intellij.stringmanip.utils;

import static java.lang.Character.*;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Character.*;


/**
* @author Olivier Smedile
Expand Down Expand Up @@ -103,32 +103,37 @@ public static String wordsToConstantCase(String s) {
public static String wordsAndHyphenAndCamelToConstantCase(String s) {
StringBuilder buf = new StringBuilder();

char lastChar = 'a';
char[] chars = s.toCharArray();
char previousChar = 'a';
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
boolean isUpperCaseAndPreviousIsUpperCase = isUpperCase(lastChar) && isUpperCase(c);
boolean isUpperCaseAndPreviousIsLowerCase = isLowerCase(lastChar) && isUpperCase(c);
// boolean isLowerCaseLetter = !isWhitespace(c) && '_' != c && !isUpperCase(c);
boolean isUpperCaseAndPreviousIsUpperCase = isUpperCase(previousChar) && isUpperCase(c);
boolean isUpperCaseAndPreviousIsLowerCase = isLowerCase(previousChar) && isUpperCase(c);
// boolean isLowerCaseLetter = !isWhitespace(c) && '_' != c && !isUpperCase(c);
// boolean isLowerCaseAndPreviousIsWhitespace = isWhitespace(lastChar) && isLowerCaseLetter;
boolean previousIsWhitespace = isWhitespace(lastChar);
boolean previousIsWhitespace = isWhitespace(previousChar);
boolean lastOneIsNotUnderscore = buf.length() > 0 && buf.charAt(buf.length() - 1) != '_';
boolean isNotUnderscore = c != '_';
// ORIGINAL if (lastOneIsNotUnderscore && (isUpperCase(c) || isLowerCaseAndPreviousIsWhitespace)) {


//camelCase handling - add extra _
if (lastOneIsNotUnderscore && (isUpperCaseAndPreviousIsLowerCase || previousIsWhitespace || isUpperCaseAndPreviousIsUpperCase)) {
buf.append("_");
} else if (isDigit(previousChar) && isLetter(c)) { //extra _ after number
buf.append('_');
}

if (!isLetter(c) && lastOneIsNotUnderscore && !isNotBorderQuote(c, i, chars)) {
buf.append('_');
} else if (!isWhitespace(c) && (c != '_' || lastOneIsNotUnderscore)) { // uppercase anything, do not add
// whitespace, do not add _ if there
// was previously
if (!isLetterOrDigit(c) && lastOneIsNotUnderscore && !isNotBorderQuote(c, i, chars)) {
buf.append('_'); //replace special chars to _ (not quotes, no double _)
} else if (!isWhitespace(c) && (isNotUnderscore || lastOneIsNotUnderscore)) {
// uppercase anything, do not add whitespace, do not add _ if there was previously
buf.append(Character.toUpperCase(c));
}

lastChar = c;
previousChar = c;
}
if (isWhitespace(lastChar)) {
if (isWhitespace(previousChar)) {
buf.append("_");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public void testTransform() throws Exception {
assertEquals("ANOTHER_T_EST", action.transform("Another t_Est"));
assertEquals("TEST_AGAIN_TEST", action.transform("test again _ _ test"));
assertEquals("TEST_AGAIN_TEST", action.transform("TestAgain_ _ Test"));
assertEquals("V2_COUNTER", action.transform("v2Counter"));
assertEquals("2_V2_COUNTER2", action.transform("2v2Counter2"));
//
//
//
Expand Down
3 changes: 3 additions & 0 deletions test/osmedile/intellij/stringmanip/utils/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public void testToCamelCase() {
}

public void testWordsAndCamelToConstantCase() {
assertEquals("V2_COUNTER", StringUtil.wordsAndHyphenAndCamelToConstantCase("v2Counter"));
assertEquals("V22_COUNTER", StringUtil.wordsAndHyphenAndCamelToConstantCase("v22Counter"));
assertEquals("V22_COUNTER22", StringUtil.wordsAndHyphenAndCamelToConstantCase("v22Counter22"));
assertEquals("THIS_IS_A_TEXT", StringUtil.wordsAndHyphenAndCamelToConstantCase("ThisIsAText"));
assertEquals("WHOAH_A_TEST", StringUtil.wordsAndHyphenAndCamelToConstantCase("WhoahATest"));
assertEquals("WHOAH_A_TEST", StringUtil.wordsAndHyphenAndCamelToConstantCase("Whoah ATest"));
Expand Down

0 comments on commit 086ff73

Please sign in to comment.