diff --git a/plugins/groovy/resources/intentionDescriptions/GrBreakStringOnLineBreaksIntention/after.groovy.template b/plugins/groovy/resources/intentionDescriptions/GrBreakStringOnLineBreaksIntention/after.groovy.template new file mode 100644 index 000000000000..a30765f6a90a --- /dev/null +++ b/plugins/groovy/resources/intentionDescriptions/GrBreakStringOnLineBreaksIntention/after.groovy.template @@ -0,0 +1,3 @@ +def ArrayList getDigits() { + return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +} \ No newline at end of file diff --git a/plugins/groovy/resources/intentionDescriptions/GrBreakStringOnLineBreaksIntention/before.groovy.template b/plugins/groovy/resources/intentionDescriptions/GrBreakStringOnLineBreaksIntention/before.groovy.template new file mode 100644 index 000000000000..a4d2f148943b --- /dev/null +++ b/plugins/groovy/resources/intentionDescriptions/GrBreakStringOnLineBreaksIntention/before.groovy.template @@ -0,0 +1,3 @@ +def getDigits() { + return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +} \ No newline at end of file diff --git a/plugins/groovy/resources/intentionDescriptions/GrBreakStringOnLineBreaksIntention/description.html b/plugins/groovy/resources/intentionDescriptions/GrBreakStringOnLineBreaksIntention/description.html new file mode 100644 index 000000000000..1b84c119c902 --- /dev/null +++ b/plugins/groovy/resources/intentionDescriptions/GrBreakStringOnLineBreaksIntention/description.html @@ -0,0 +1,5 @@ + + +This intention allows to break line on '\n' and '\n\r' line break symbols. + + \ No newline at end of file diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index fcc2e9173aea..9a575e9cc5a9 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -1077,6 +1077,11 @@ intention.category.groovy/intention.category.conversions org.jetbrains.plugins.groovy.intentions.conversions.strings.RemoveUnnecessaryEscapeCharactersIntention + + org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle + intention.category.groovy/intention.category.conversions + org.jetbrains.plugins.groovy.intentions.conversions.strings.GrBreakStringOnLineBreaksIntention + org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle intention.category.groovy/intention.category.conversions diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties index 1dd19b4c38a1..d6e5edadc570 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties @@ -167,3 +167,5 @@ create.field.for.parameter.0 = Create Field for Parameter {0} create.field.for.parameter=Create Field for Parameter remove.unnecessary.escape.characters.intention.name=Remove unnecessary escape characters remove.unnecessary.escape.characters.intention.family.name=Remove unnecessary escape characters +gr.break.string.on.line.breaks.intention.name=Break string on '\\n' +gr.break.string.on.line.breaks.intention.family.name=Break string on '\\n' \ No newline at end of file diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/conversions/strings/GrBreakStringOnLineBreaksIntention.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/conversions/strings/GrBreakStringOnLineBreaksIntention.java new file mode 100644 index 000000000000..cc8228cd5798 --- /dev/null +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/conversions/strings/GrBreakStringOnLineBreaksIntention.java @@ -0,0 +1,138 @@ +/* + * Copyright 2000-2012 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 org.jetbrains.plugins.groovy.intentions.conversions.strings; + +import com.intellij.lang.ASTNode; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.intellij.psi.tree.IElementType; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.intentions.base.Intention; +import org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate; +import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString; + +import static org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes.*; +import static org.jetbrains.plugins.groovy.lang.psi.util.GrStringUtil.getStartQuote; +import static org.jetbrains.plugins.groovy.lang.psi.util.GrStringUtil.removeQuotes; + +/** + * @author Max Medvedev + */ +public class GrBreakStringOnLineBreaksIntention extends Intention { + @Override + protected void processIntention(@NotNull PsiElement element, Project project, Editor editor) throws IncorrectOperationException { + final String text = invokeImpl(element); + final GrExpression newExpr = GroovyPsiElementFactory.getInstance(project).createExpressionFromText(text); + ((GrExpression)element).replaceWithExpression(newExpr, true); + } + + @NotNull + @Override + protected PsiElementPredicate getElementPredicate() { + return new PsiElementPredicate() { + @Override + public boolean satisfiedBy(PsiElement element) { + return element instanceof GrLiteral && !element.getText().equals(invokeImpl(element)); + } + }; + } + + private static String invokeImpl(PsiElement element) { + final String text = element.getText(); + final String quote = getStartQuote(text); + + if (!("'".equals(quote) || "\"".equals(quote))) return text; + if (!text.contains("\\n")) return text; + + String value = removeQuotes(text); + + StringBuilder buffer = new StringBuilder(); + if (element instanceof GrString) { + processGString(element, quote, value, buffer); + } + else { + processSimpleString(quote, value, buffer); + } + + final String result = buffer.toString(); + if (result.endsWith("+\n\"\"")) return result.substring(0, result.length() - 4); + + return result; + } + + private static void processGString(PsiElement element, String quote, String value, StringBuilder buffer) { + final ASTNode node = element.getNode(); + + for (ASTNode child = node.getFirstChildNode(); child != null; child = child.getTreeNext()) { + final IElementType type = child.getElementType(); + if (type == mGSTRING_BEGIN || type == mGSTRING_END) continue; + if (type == GSTRING_INJECTION) { + buffer.append(child.getText()); + } + else { + value = child.getText(); + int prev = 0; + if (!isInjection(child.getTreePrev())) { + buffer.append(quote); + } + for (int pos = value.indexOf("\\n"); pos >= 0; pos = value.indexOf("\\n", prev)) { + int end = checkForR(value, pos); + buffer.append(value.substring(prev, end)); + prev = end; + buffer.append(quote); + buffer.append("+\n"); + buffer.append(quote); + } + buffer.append(value.substring(prev, value.length())); + if (!isInjection(child.getTreeNext())) { + buffer.append(quote); + } + } + } + } + + private static boolean isInjection(ASTNode next) { + return next != null && next.getElementType() == GSTRING_INJECTION; + } + + private static void processSimpleString(String quote, String value, StringBuilder buffer) { + int prev = 0; + for (int pos = value.indexOf("\\n"); pos >= 0; pos = value.indexOf("\\n", prev)) { + buffer.append(quote); + int end = checkForR(value, pos); + buffer.append(value.substring(prev, end)); + prev = end; + buffer.append(quote); + buffer.append("+\n"); + } + buffer.append(quote); + buffer.append(value.substring(prev, value.length())); + buffer.append(quote); + } + + private static int checkForR(String value, int pos) { + pos += 2; + if (value.length() > pos + 2 && "\r".equals(value.substring(pos, pos + 2))) return pos + 2; + return pos; + } + + +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/GrBreakStringOnLineBreaksTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/GrBreakStringOnLineBreaksTest.groovy new file mode 100644 index 000000000000..8f4c5afd3604 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/GrBreakStringOnLineBreaksTest.groovy @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2012 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 org.jetbrains.plugins.groovy.intentions; + + +import org.jetbrains.plugins.groovy.util.TestUtils + +/** + * @author Max Medvedev + */ +public class GrBreakStringOnLineBreaksTest extends GrIntentionTestCase { + private static final String message = GroovyIntentionsBundle.message('gr.break.string.on.line.breaks.intention.name') + + final String basePath = TestUtils.testDataPath + "intentions/breakStringOnLineBreaks/" + + void testSimple() { + doTextTest('''print 'abc\\ncde\'''', message, '''\ +print 'abc\\n' + + 'cde\'''') + } + + void testGString() { + doTextTest('''print "a\\n$x bc\\n"''', message, '''\ +print "a\\n" + + "$x bc\\n"''') + } +}