diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/JavaSoftKeywordHighlighting.kt b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/JavaSoftKeywordHighlighting.kt index f6185a665ba5..2118bf920b16 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/JavaSoftKeywordHighlighting.kt +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/JavaSoftKeywordHighlighting.kt @@ -62,7 +62,7 @@ private class JavaSoftKeywordHighlightingVisitor(private val results: MutableLis JavaRecursiveElementVisitor() { override fun visitKeyword(keyword: PsiKeyword) { - if (JavaLexer.isSoftKeyword(keyword.text, level)) { + if (JavaLexer.isSoftKeyword(keyword.node.chars, level)) { val info = HighlightInfo.newHighlightInfo(JavaHighlightInfoTypes.JAVA_KEYWORD).range(keyword).create() if (info != null) { results += info diff --git a/java/java-psi-impl/src/com/intellij/lang/java/lexer/JavaLexer.java b/java/java-psi-impl/src/com/intellij/lang/java/lexer/JavaLexer.java index 38a90e030d03..ff11af0624c8 100644 --- a/java/java-psi-impl/src/com/intellij/lang/java/lexer/JavaLexer.java +++ b/java/java-psi-impl/src/com/intellij/lang/java/lexer/JavaLexer.java @@ -23,6 +23,7 @@ import com.intellij.psi.impl.source.tree.JavaDocElementType; import com.intellij.psi.tree.IElementType; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.text.CharArrayUtil; +import com.intellij.util.text.CharSequenceHashingStrategy; import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -37,7 +38,8 @@ public class JavaLexer extends LexerBase { RETURN, SHORT, STATIC, STRICTFP, SUPER, SWITCH, SYNCHRONIZED, THIS, THROW, THROWS, TRANSIENT, TRY, VOID, VOLATILE, WHILE, TRUE, FALSE, NULL); - private static final Set JAVA9_KEYWORDS = ContainerUtil.newTroveSet( + private static final Set JAVA9_KEYWORDS = ContainerUtil.newTroveSet( + CharSequenceHashingStrategy.CASE_SENSITIVE, MODULE, REQUIRES, EXPORTS, USES, PROVIDES, TO, WITH); public static boolean isKeyword(String id, @NotNull LanguageLevel level) { @@ -46,8 +48,8 @@ public class JavaLexer extends LexerBase { level.isAtLeast(LanguageLevel.JDK_1_5) && ENUM.equals(id); } - public static boolean isSoftKeyword(String id, @NotNull LanguageLevel level) { - return level.isAtLeast(LanguageLevel.JDK_1_9) && JAVA9_KEYWORDS.contains(id); + public static boolean isSoftKeyword(CharSequence id, @NotNull LanguageLevel level) { + return id != null && level.isAtLeast(LanguageLevel.JDK_1_9) && JAVA9_KEYWORDS.contains(id); } private final _JavaLexer myFlexLexer; diff --git a/java/java-tests/testSrc/com/intellij/lang/java/lexer/JavaKeywordsTest.kt b/java/java-tests/testSrc/com/intellij/lang/java/lexer/JavaKeywordsTest.kt new file mode 100644 index 000000000000..3b41656a7187 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/lang/java/lexer/JavaKeywordsTest.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2016 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.lang.java.lexer + +import com.intellij.pom.java.LanguageLevel +import com.intellij.util.text.ByteArrayCharSequence +import com.intellij.util.text.CharArrayCharSequence +import org.junit.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class JavaKeywordsTest { + @Test fun hardAndSoft() { + assertTrue(JavaLexer.isKeyword("char", LanguageLevel.JDK_1_9)) + assertFalse(JavaLexer.isSoftKeyword("char", LanguageLevel.JDK_1_9)) + assertFalse(JavaLexer.isKeyword("module", LanguageLevel.JDK_1_9)) + assertTrue(JavaLexer.isSoftKeyword("module", LanguageLevel.JDK_1_9)) + } + + @Test fun sequences() { + assertTrue(JavaLexer.isSoftKeyword(ByteArrayCharSequence.convertToBytesIfAsciiString("module"), LanguageLevel.JDK_1_9)) + assertTrue(JavaLexer.isSoftKeyword(CharArrayCharSequence("[module]".toCharArray(), 1, 7), LanguageLevel.JDK_1_9)) + } + + @Test fun nullTolerance() { + assertFalse(JavaLexer.isKeyword(null, LanguageLevel.JDK_1_3)) + assertFalse(JavaLexer.isSoftKeyword(null, LanguageLevel.JDK_1_9)) + } +} \ No newline at end of file