per-position language level checking when suggesting completion keywords (EA-29624)

This commit is contained in:
peter
2011-11-10 20:12:05 +01:00
parent 37e9d89ca0
commit 675559f5cc
3 changed files with 12 additions and 1 deletions
@@ -48,7 +48,7 @@ public class BasicExpressionCompletionContributor {
public static LookupElement createKeywordLookupItem(final PsiElement element, final String s) {
try {
final PsiKeyword keyword = JavaPsiFacade.getInstance(element.getProject()).getElementFactory().createKeyword(s);
final PsiKeyword keyword = JavaPsiFacade.getInstance(element.getProject()).getElementFactory().createKeyword(s, element);
return new KeywordLookupItem(keyword, element).setAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE);
}
catch (IncorrectOperationException e) {
@@ -333,6 +333,7 @@ public interface PsiElementFactory extends PsiJavaParserFacade, JVMElementFactor
* @throws IncorrectOperationException if <code>text</code> is not a valid Java keyword.
*/
@NotNull PsiKeyword createKeyword(@NotNull @NonNls String keyword) throws IncorrectOperationException;
@NotNull PsiKeyword createKeyword(@NotNull @NonNls String keyword, PsiElement context) throws IncorrectOperationException;
/**
* Creates an import statement for importing the specified class.
@@ -18,6 +18,7 @@ package com.intellij.psi.impl;
import com.intellij.lang.*;
import com.intellij.lang.java.parser.JavaParserUtil;
import com.intellij.lang.java.parser.StatementParser;
import com.intellij.lexer.JavaLexer;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
@@ -503,6 +504,15 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps
return new LightKeyword(myManager, text);
}
@NotNull
@Override
public PsiKeyword createKeyword(@NotNull @NonNls String keyword, PsiElement context) throws IncorrectOperationException {
if (!JavaLexer.isKeyword(keyword, PsiUtil.getLanguageLevel(context))) {
throw new IncorrectOperationException("\"" + keyword + "\" is not a keyword.");
}
return new LightKeyword(myManager, keyword);
}
@NotNull
@Override
public PsiImportStatement createImportStatement(@NotNull final PsiClass aClass) throws IncorrectOperationException {