raw string literals: string assignability (IDEA-189075)

This commit is contained in:
Anna.Kozlova
2018-04-11 19:09:17 +02:00
parent 1c30af9eee
commit 6b42992013
3 changed files with 63 additions and 3 deletions

View File

@@ -65,7 +65,7 @@ public class PsiLiteralExpressionImpl
if (type == JavaTokenType.CHARACTER_LITERAL) {
return PsiType.CHAR;
}
if (type == JavaTokenType.STRING_LITERAL) {
if (type == JavaTokenType.STRING_LITERAL || type == JavaTokenType.RAW_STRING_LITERAL) {
PsiManagerEx manager = getManager();
GlobalSearchScope resolveScope = ResolveScopeManager.getElementResolveScope(this);
return PsiType.getJavaLangString(manager, resolveScope);
@@ -113,6 +113,11 @@ public class PsiLiteralExpressionImpl
return innerText == null ? null : internedParseStringCharacters(innerText);
}
if (type == JavaTokenType.RAW_STRING_LITERAL) {
String rawString = getRawString();
return rawString == null ? null : internedParseStringCharacters(rawString);
}
String text = NUMERIC_LITERALS.contains(type) ? getCanonicalText().toLowerCase(Locale.ENGLISH) : getCanonicalText();
final int textLength = text.length();
@@ -164,6 +169,18 @@ public class PsiLiteralExpressionImpl
}
return text;
}
private String getRawString() {
String text = getCanonicalText();
int pos = 0;
int length = text.length();
while (pos < length && text.charAt(pos) == '`') pos++;
if (length - pos <= pos) return null;
return text.substring(pos, length - pos);
}
@Nullable
private static String internedParseStringCharacters(final String chars) {
@@ -193,14 +210,15 @@ public class PsiLiteralExpressionImpl
@Override
public boolean isValidHost() {
return getLiteralElementType() == JavaTokenType.STRING_LITERAL;
IElementType elementType = getLiteralElementType();
return elementType == JavaTokenType.STRING_LITERAL || elementType == JavaTokenType.RAW_STRING_LITERAL;
}
@Override
@NotNull
public PsiReference[] getReferences() {
IElementType type = getLiteralElementType();
if (type != JavaTokenType.STRING_LITERAL && type != JavaTokenType.INTEGER_LITERAL) {
if (type != JavaTokenType.STRING_LITERAL && type != JavaTokenType.RAW_STRING_LITERAL && type != JavaTokenType.INTEGER_LITERAL) {
return PsiReference.EMPTY_ARRAY; // there are references in int literals in SQL API parameters
}
return PsiReferenceService.getService().getContributedReferences(this);

View File

@@ -0,0 +1,10 @@
class Test {
void m(String s) {}
<T> void n(T s) {}
{
String s = ``abc``;
m(`abc`);
n(`abc`);
String[] array = {``abc``};
}
}

View File

@@ -0,0 +1,32 @@
// 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.daemon;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.IdeaTestUtil;
public class LightAdvRawStringLiteralsHighlightingTest extends LightDaemonAnalyzerTestCase {
private static final String BASE_PATH = "/codeInsight/daemonCodeAnalyzer/advRawStringLiteral";
@Override
protected void setUp() throws Exception {
super.setUp();
setLanguageLevel(LanguageLevel.JDK_11_PREVIEW);
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_11, getModule(), getTestRootDisposable());
}
private void doTest() {
doTest(BASE_PATH + "/" + getTestName(false) + ".java", false, false);
}
public void testStringAssignability() {
doTest();
}
@Override
protected Sdk getProjectJDK() {
return IdeaTestUtil.getMockJdk9();
}
}