Add substringBefore macro (WEB-30691)

This commit is contained in:
Alexander Zolotov
2018-01-17 22:13:50 +03:00
parent 12341747ff
commit 85a30f0ff5
3 changed files with 54 additions and 16 deletions
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 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.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.template
import com.intellij.JavaTestUtil
@@ -525,7 +511,7 @@ class Outer {
}
void "_testIterForceBraces"() {
def settings = CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE);
def settings = CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE)
settings.IF_BRACE_FORCE = CommonCodeStyleSettings.FORCE_BRACES_ALWAYS
try {
@@ -1019,6 +1005,19 @@ class Foo {
assert !state
myFixture.checkResult('tableName tableNameID c')
}
void "test substringBefore macro"() {
final TemplateManager manager = TemplateManager.getInstance(getProject())
final Template template = manager.createTemplate("result", "user", '$A$ $B$ $C$')
template.addVariable('A', 'substringBefore("hello.world", ".")', '"empty"', false)
template.addVariable('B', 'substringBefore("hello world", ".")', '"empty"', false)
template.addVariable('C', 'substringBefore("hello world")', '"empty"', false)
myFixture.configureByText "a.txt", "<caret>"
startTemplate(template)
state.nextTab()
assert !state
myFixture.checkResult('hello empty empty')
}
void "test snakeCase should convert hyphens to underscores"() {
final TemplateManager manager = TemplateManager.getInstance(getProject())
@@ -0,0 +1,38 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.template.macro;
import com.intellij.codeInsight.template.Expression;
import com.intellij.codeInsight.template.ExpressionContext;
import com.intellij.codeInsight.template.Result;
import com.intellij.codeInsight.template.TextResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class SubstringBeforeMacro extends MacroBase {
public SubstringBeforeMacro() {
super("substringBefore", "snakeCase(String, Delimiter)");
}
@Override
protected Result calculateResult(@NotNull Expression[] params, ExpressionContext context, boolean quick) {
if (params.length == 2) {
String string = getTextResult(params[0], context);
if (string == null) {
return null;
}
String delimiter = getTextResult(params[1], context);
if (delimiter == null) {
return null;
}
int indexOf = string.indexOf(delimiter);
return new TextResult(indexOf > 0 ? string.substring(0, indexOf) : "");
}
return null;
}
@Nullable
private static String getTextResult(@NotNull Expression param, ExpressionContext context) {
Result result = param.calculateResult(context);
return result != null ? result.toString() : null;
}
}
@@ -511,6 +511,7 @@
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.CompleteMacro"/>
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.CompleteSmartMacro"/>
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.ClassNameCompleteMacro"/>
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.SubstringBeforeMacro"/>
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.CommentMacro$LineCommentStart"/>
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.CommentMacro$BlockCommentStart"/>