mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-87834 Groovy: 'Split string' intention
This commit is contained in:
+3
@@ -0,0 +1,3 @@
|
||||
def <spot>ArrayList<Integer></spot> getDigits() {
|
||||
return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
def getDigits() {
|
||||
return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
<span style="font-family: verdana,serif; font-size: small;">This intention allows to break line on '\n' and '\n\r' line break symbols.</span>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1077,6 +1077,11 @@
|
||||
<categoryKey>intention.category.groovy/intention.category.conversions</categoryKey>
|
||||
<className>org.jetbrains.plugins.groovy.intentions.conversions.strings.RemoveUnnecessaryEscapeCharactersIntention</className>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<bundleName>org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle</bundleName>
|
||||
<categoryKey>intention.category.groovy/intention.category.conversions</categoryKey>
|
||||
<className>org.jetbrains.plugins.groovy.intentions.conversions.strings.GrBreakStringOnLineBreaksIntention</className>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<bundleName>org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle</bundleName>
|
||||
<categoryKey>intention.category.groovy/intention.category.conversions</categoryKey>
|
||||
|
||||
+2
@@ -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'
|
||||
+138
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+40
@@ -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 'ab<caret>c\\ncde\'''', message, '''\
|
||||
print 'abc\\n' +
|
||||
'cde\'''')
|
||||
}
|
||||
|
||||
void testGString() {
|
||||
doTextTest('''print "a<caret>\\n$x bc\\n"''', message, '''\
|
||||
print "a\\n" +
|
||||
"$x bc\\n"''')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user