snakeCase() macro for live templates (PY-9989)

This commit is contained in:
Dmitry Jemerov
2013-10-31 21:00:17 +01:00
parent 4216441340
commit 9c4cb80d86
3 changed files with 41 additions and 2 deletions
@@ -32,23 +32,31 @@ public class CapitalizeAndUnderscoreMacro extends MacroBase {
super("capitalizeAndUnderscore", CodeInsightBundle.message("macro.capitalizeAndUnderscore.string"));
}
protected CapitalizeAndUnderscoreMacro(String name, String description) {
super(name, description);
}
@Override
protected Result calculateResult(@NotNull Expression[] params, ExpressionContext context, boolean quick) {
String text = getTextResult(params, context, true);
if (text != null && text.length() > 0) {
final String[] words = NameUtil.nameToWords(text);
boolean insertUnderscore = false;
final StringBuffer buf = new StringBuffer();
final StringBuilder buf = new StringBuilder();
for (String word : words) {
if (insertUnderscore) {
buf.append("_");
} else {
insertUnderscore = true;
}
buf.append(StringUtil.toUpperCase(word));
buf.append(convertCase(word));
}
return new TextResult(buf.toString());
}
return null;
}
protected String convertCase(String word) {
return StringUtil.toUpperCase(word);
}
}
@@ -0,0 +1,30 @@
/*
* 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();
}
}
@@ -432,6 +432,7 @@
<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.CompleteMacro"/>
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.CompleteSmartMacro"/>