mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
add live template macros: dashesToCamelCase (IDEA-135255) and lowercaseAndDash (IDEA-147176)
This commit is contained in:
@@ -788,7 +788,7 @@ class Foo {
|
||||
final Template template = manager.createTemplate("result", "user", '$A$ $B$ c');
|
||||
template.addVariable('A', new EmptyNode(), true)
|
||||
|
||||
def macroCallNode = new MacroCallNode(new SnakeCaseMacro())
|
||||
def macroCallNode = new MacroCallNode(new SplitWordsMacro.SnakeCaseMacro())
|
||||
macroCallNode.addParameter(new VariableNode('A', null))
|
||||
template.addVariable('B', macroCallNode, false)
|
||||
|
||||
|
||||
+14
-5
@@ -15,14 +15,14 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.template
|
||||
|
||||
import com.intellij.codeInsight.template.macro.CapitalizeAndUnderscoreMacro
|
||||
import com.intellij.codeInsight.template.macro.SnakeCaseMacro
|
||||
import com.intellij.codeInsight.template.macro.ConvertToCamelCaseMacro
|
||||
import com.intellij.codeInsight.template.macro.SplitWordsMacro
|
||||
import junit.framework.TestCase
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
class CapitalizeAndUnderscoreTest extends TestCase {
|
||||
class NamingConventionMacrosTest extends TestCase {
|
||||
|
||||
public void "test capitalize and underscore"() {
|
||||
assert "FOO_BAR" == cau("fooBar")
|
||||
@@ -40,12 +40,21 @@ class CapitalizeAndUnderscoreTest extends TestCase {
|
||||
assert "a_b_c_d_e_f_g" == snakeCase("a-b.c/d|e*f+g")
|
||||
assert "a_b" == snakeCase("a--b")
|
||||
}
|
||||
|
||||
public void "test lowercase and dash"() {
|
||||
assert "foo-bar" == new SplitWordsMacro.LowercaseAndDash().convertString("FOO_BAR")
|
||||
}
|
||||
|
||||
public void "test dashes to camel case"() {
|
||||
assert "fooBar" == new ConvertToCamelCaseMacro.ReplaceDashesToCamelCaseMacro().convertString("foo-bar")?.toString()
|
||||
assert "fooBar" == new ConvertToCamelCaseMacro.ReplaceDashesToCamelCaseMacro().convertString("FOO-BAR")?.toString()
|
||||
}
|
||||
|
||||
private static def snakeCase(String s) {
|
||||
return new SnakeCaseMacro().convertString(s)
|
||||
return new SplitWordsMacro.SnakeCaseMacro().convertString(s)
|
||||
}
|
||||
private static def cau(String s) {
|
||||
return new CapitalizeAndUnderscoreMacro().convertString(s)
|
||||
return new SplitWordsMacro.CapitalizeAndUnderscoreMacro().convertString(s)
|
||||
}
|
||||
|
||||
}
|
||||
+36
-13
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.template.macro;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.intellij.codeInsight.CodeInsightBundle;
|
||||
import com.intellij.codeInsight.template.Expression;
|
||||
import com.intellij.codeInsight.template.ExpressionContext;
|
||||
@@ -29,9 +30,13 @@ import java.util.List;
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class ReplaceUnderscoresToCamelCaseMacro extends MacroBase {
|
||||
public ReplaceUnderscoresToCamelCaseMacro() {
|
||||
super("underscoresToCamelCase", CodeInsightBundle.message("macro.undescoresToCamelCase.string"));
|
||||
public abstract class ConvertToCamelCaseMacro extends MacroBase {
|
||||
|
||||
private final String mySeparator;
|
||||
|
||||
private ConvertToCamelCaseMacro(String name, String description, String separator) {
|
||||
super(name, description);
|
||||
mySeparator = separator;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -39,17 +44,35 @@ public class ReplaceUnderscoresToCamelCaseMacro extends MacroBase {
|
||||
protected Result calculateResult(@NotNull Expression[] params, ExpressionContext context, boolean quick) {
|
||||
final String text = getTextResult(params, context, true);
|
||||
if (text != null) {
|
||||
|
||||
final List<String> strings = StringUtil.split(text, "_");
|
||||
if (strings.size() > 0) {
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
buf.append(strings.get(0).toLowerCase());
|
||||
for (int i = 1; i < strings.size(); i++) {
|
||||
buf.append(StringUtil.capitalize(strings.get(i).toLowerCase()));
|
||||
}
|
||||
return new TextResult(buf.toString());
|
||||
}
|
||||
return convertString(text);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("StringToUpperCaseOrToLowerCaseWithoutLocale")
|
||||
@Nullable
|
||||
@VisibleForTesting
|
||||
public Result convertString(String text) {
|
||||
final List<String> strings = StringUtil.split(text, mySeparator);
|
||||
if (strings.size() > 0) {
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
buf.append(strings.get(0).toLowerCase());
|
||||
for (int i = 1; i < strings.size(); i++) {
|
||||
buf.append(StringUtil.capitalize(strings.get(i).toLowerCase()));
|
||||
}
|
||||
return new TextResult(buf.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class ReplaceUnderscoresToCamelCaseMacro extends ConvertToCamelCaseMacro {
|
||||
public ReplaceUnderscoresToCamelCaseMacro() {
|
||||
super("underscoresToCamelCase", CodeInsightBundle.message("macro.undescoresToCamelCase.string"), "_");
|
||||
}
|
||||
}
|
||||
public static class ReplaceDashesToCamelCaseMacro extends ConvertToCamelCaseMacro {
|
||||
public ReplaceDashesToCamelCaseMacro() {
|
||||
super("dashesToCamelCase", "dashesToCamelCase(String)", "-");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.template.macro;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class SnakeCaseMacro extends CapitalizeAndUnderscoreMacro {
|
||||
public SnakeCaseMacro() {
|
||||
super("snakeCase", "snakeCase(String)");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String convertCase(String word) {
|
||||
return word.toLowerCase();
|
||||
}
|
||||
}
|
||||
+48
-13
@@ -28,13 +28,12 @@ import org.jetbrains.annotations.NotNull;
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class CapitalizeAndUnderscoreMacro extends MacroBase {
|
||||
public CapitalizeAndUnderscoreMacro() {
|
||||
super("capitalizeAndUnderscore", CodeInsightBundle.message("macro.capitalizeAndUnderscore.string"));
|
||||
}
|
||||
public abstract class SplitWordsMacro extends MacroBase {
|
||||
private final char mySeparator;
|
||||
|
||||
protected CapitalizeAndUnderscoreMacro(String name, String description) {
|
||||
private SplitWordsMacro(String name, String description, char separator) {
|
||||
super(name, description);
|
||||
mySeparator = separator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,25 +48,61 @@ public class CapitalizeAndUnderscoreMacro extends MacroBase {
|
||||
@VisibleForTesting
|
||||
public String convertString(String text) {
|
||||
final String[] words = NameUtil.nameToWords(text);
|
||||
boolean insertUnderscore = false;
|
||||
boolean insertSeparator = false;
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
for (String word : words) {
|
||||
if (!Character.isLetterOrDigit(word.charAt(0))) {
|
||||
buf.append("_");
|
||||
insertUnderscore = false;
|
||||
buf.append(mySeparator);
|
||||
insertSeparator = false;
|
||||
continue;
|
||||
}
|
||||
if (insertUnderscore) {
|
||||
buf.append("_");
|
||||
if (insertSeparator) {
|
||||
buf.append(mySeparator);
|
||||
} else {
|
||||
insertUnderscore = true;
|
||||
insertSeparator = true;
|
||||
}
|
||||
buf.append(convertCase(word));
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
protected String convertCase(String word) {
|
||||
return StringUtil.toUpperCase(word);
|
||||
@NotNull protected abstract String convertCase(@NotNull String word);
|
||||
|
||||
public static class CapitalizeAndUnderscoreMacro extends SplitWordsMacro {
|
||||
|
||||
public CapitalizeAndUnderscoreMacro() {
|
||||
super("capitalizeAndUnderscore", CodeInsightBundle.message("macro.capitalizeAndUnderscore.string"), '_');
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String convertCase(@NotNull String word) {
|
||||
return StringUtil.toUpperCase(word);
|
||||
}
|
||||
}
|
||||
|
||||
public static class SnakeCaseMacro extends SplitWordsMacro {
|
||||
public SnakeCaseMacro() {
|
||||
super("snakeCase", "snakeCase(String)", '_');
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String convertCase(@NotNull String word) {
|
||||
//noinspection StringToUpperCaseOrToLowerCaseWithoutLocale
|
||||
return word.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
public static class LowercaseAndDash extends SplitWordsMacro {
|
||||
public LowercaseAndDash() {
|
||||
super("lowercaseAndDash", "lowercaseAndDash(String)", '-');
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String convertCase(@NotNull String word) {
|
||||
//noinspection StringToUpperCaseOrToLowerCaseWithoutLocale
|
||||
return word.toLowerCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -470,9 +470,11 @@
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.LineNumberMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.FileNameMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.FileNameWithoutExtensionMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.ReplaceUnderscoresToCamelCaseMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.CapitalizeAndUnderscoreMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.SnakeCaseMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.ConvertToCamelCaseMacro$ReplaceUnderscoresToCamelCaseMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.ConvertToCamelCaseMacro$ReplaceDashesToCamelCaseMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.SplitWordsMacro$CapitalizeAndUnderscoreMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.SplitWordsMacro$SnakeCaseMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.SplitWordsMacro$LowercaseAndDash"/>
|
||||
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.CompleteMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.CompleteSmartMacro"/>
|
||||
|
||||
Reference in New Issue
Block a user