From 85a30f0ff5a243a1bc27ef2aa7a0a7448ea6de87 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Wed, 17 Jan 2018 22:00:21 +0300 Subject: [PATCH] Add substringBefore macro (WEB-30691) --- .../template/LiveTemplateTest.groovy | 31 ++++++++------- .../template/macro/SubstringBeforeMacro.java | 38 +++++++++++++++++++ .../src/META-INF/LangExtensions.xml | 1 + 3 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/template/macro/SubstringBeforeMacro.java diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/LiveTemplateTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/LiveTemplateTest.groovy index 97441346e3e0..1f2654960bb3 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/LiveTemplateTest.groovy +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/LiveTemplateTest.groovy @@ -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", "" + 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()) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/macro/SubstringBeforeMacro.java b/platform/lang-impl/src/com/intellij/codeInsight/template/macro/SubstringBeforeMacro.java new file mode 100644 index 000000000000..6db8edd9ffa8 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/macro/SubstringBeforeMacro.java @@ -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; + } +} diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index cefd60f88b1c..d90030e76d7f 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -511,6 +511,7 @@ +