[java] disable char -> string conversion on erroneous code

compilation fix is provided, let's skip second one

GitOrigin-RevId: 0717eac74cca13e80e5c2fd8313df75c11fab576
This commit is contained in:
Anna Kozlova
2021-08-09 07:56:29 +00:00
committed by intellij-monorepo-bot
parent b8fe362d14
commit a1d710d5ee
5 changed files with 46 additions and 33 deletions
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2011 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-2021 JetBrains s.r.o. and contributors. 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.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
@@ -21,6 +7,7 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
@@ -41,7 +28,14 @@ public class ConvertToStringLiteralAction implements IntentionActionWithFixAllOp
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
final PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
return PsiUtil.isJavaToken(element, JavaTokenType.CHARACTER_LITERAL);
if (PsiUtil.isJavaToken(element, JavaTokenType.CHARACTER_LITERAL)) {
StringBuilder chars = new StringBuilder();
String text = element.getText();
return text.length() > 1 &&
PsiLiteralExpressionImpl.parseStringCharacters(text.substring(1, text.length() - 1), chars, null) &&
chars.length() == 1;
}
return false;
}
@Override
@@ -0,0 +1,20 @@
/*
* Copyright 2000-2011 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.
*/
class C {
{
System.out.println('<caret>ab');
}
}
@@ -15,6 +15,6 @@
*/
class C {
{
System.out.println('<caret>aaa');
System.out.println('<caret>a');
}
}
@@ -15,6 +15,6 @@
*/
class C {
{
System.out.println("aaa");
System.out.println("a");
}
}
@@ -1,25 +1,15 @@
/*
* 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-2021 JetBrains s.r.o. and contributors. 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.intention;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.testFramework.fixtures.CodeInsightTestUtil;
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
import org.junit.Assert;
import java.util.List;
public class ConvertToStringLiteralTest extends JavaCodeInsightFixtureTestCase {
@@ -35,6 +25,15 @@ public class ConvertToStringLiteralTest extends JavaCodeInsightFixtureTestCase {
CodeInsightTestUtil.doIntentionTest(myFixture, myIntention, "Simple.java", "Simple_after.java");
}
public void testNotAvailable() {
myFixture.configureByFile(getTestName(false) + ".java");
List<IntentionAction> availableIntentions = myFixture.getAvailableIntentions();
final IntentionAction intentionAction = CodeInsightTestUtil.findIntentionByText(availableIntentions, myIntention);
if (intentionAction != null) {
Assert.fail("Action found: " + myIntention);
}
}
@Override
protected String getTestDataPath() {
return JavaTestUtil.getJavaTestDataPath() + "/codeInsight/convertToStringLiteral/";