IDEA-87839 'Split string' intention implemented

This commit is contained in:
Danila Ponomarenko
2012-06-27 13:07:30 +04:00
parent 2a004b3a4e
commit 4123634435
11 changed files with 178 additions and 0 deletions
@@ -0,0 +1,110 @@
/*
* 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 com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
/**
* @author Danila Ponomarenko
*/
public class BreakStringOnLineBreaksIntentionAction extends PsiElementBaseIntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
if (!(element instanceof PsiJavaToken)) {
return false;
}
final PsiJavaToken token = (PsiJavaToken)element;
if (token.getTokenType() != JavaTokenType.STRING_LITERAL) {
return false;
}
final String text = token.getText();
if (text == null) {
return false;
}
final int indexOfSlashN = text.indexOf("\\n");
final int indexOfSlashNSlashR = text.indexOf("\\n\\r");
if (indexOfSlashN == -1 || Comparing.equal(text.substring(indexOfSlashN, text.length()), "\\n\"")){
return false;
}
if (indexOfSlashN != -1 && Comparing.equal(text.substring(indexOfSlashNSlashR, text.length()), "\\n\\r\"")){
return false;
}
return true;
}
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
if (!(element instanceof PsiJavaToken)) {
return;
}
final PsiJavaToken token = (PsiJavaToken)element;
if (token.getTokenType() != JavaTokenType.STRING_LITERAL) {
return;
}
final String text = token.getText();
if (text == null) {
return;
}
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
token.replace(factory.createExpressionFromText(breakOnLineBreaks(text), element));
}
@NotNull
private static String breakOnLineBreaks(@NotNull String string) {
final String result = StringUtil.replace(
string,
new String[]{"\\n\\r", "\\n"},
new String[]{"\\n\\r\" + \n\"", "\\n\" + \n\""}
);
final String redundantSuffix = " + \n\"\"";
return result.endsWith(redundantSuffix) ? result.substring(0, result.length() - redundantSuffix.length()) : result;
}
@NotNull
@Override
public String getText() {
return CodeInsightBundle.message("intention.break.string.on.line.breaks.text");
}
@NotNull
@Override
public String getFamilyName() {
return getText();
}
}
@@ -0,0 +1,7 @@
// "Break string on '\n'" "true"
class A {
String s = "Hello,\n\r" +
"world\n" +
"!\n";
}
@@ -0,0 +1,5 @@
// "Break string on '\n'" "true"
class A {
String s = "Hello,\n\rwor<caret>ld\n!\n";
}
@@ -0,0 +1,5 @@
// "Break string on '\n'" "false"
class A {
String s = "Hello!\n";
}
@@ -0,0 +1,5 @@
// "Break string on '\n'" "false"
class A {
String s = "Hello!\n\r";
}
@@ -0,0 +1,30 @@
/*
* 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 com.intellij.codeInsight.daemon.quickFix;
/**
* @author Danila Ponomarenko
*/
public class BreakStringOnLineBreaksTest extends LightQuickFixTestCase {
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/breakStringOnLineBreaks";
}
}
@@ -209,6 +209,7 @@ intention.replace.cast.with.var.text=Replace ''{0}'' with ''{1}''
intention.replace.cast.with.var.family=Replace cast with variable
intention.convert.color.representation.text=Convert to ''new Color{0}''
intention.convert.color.representation.family=Convert Color representation
intention.break.string.on.line.breaks.text=Break string on '\\n'
intention.create.test=Create Test
@@ -0,0 +1,3 @@
String s = "Hello,\n\r" +
"world\n" +
"!\n";
@@ -0,0 +1 @@
String s = <spot>"Hello,\n\rworld\n!\n"</spot>;
@@ -0,0 +1,5 @@
<html>
<body>
This intention allows to break line on '\n' and '\n\r' line break symbols.
</body>
</html>
+6
View File
@@ -724,6 +724,12 @@
<category>Other</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.BreakStringOnLineBreaksIntentionAction</className>
<category>Other</category>
</intentionAction>
<intentionAction>