From 01b37e3e42c950dcc23143ee4accf6dd5a550dfd Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 28 May 2012 18:16:15 +0400 Subject: [PATCH] 'default' completion in extension methods --- .../completion/Java18CompletionData.java | 46 +++++++++++++++++++ .../completion/JavaCompletionContributor.java | 23 ++++++++-- .../completion/JavaCompletionData.java | 6 +-- .../keywords/defaultInExtMethod.java | 18 ++++++++ .../keywords/defaultInExtMethod_after.java | 18 ++++++++ .../completion/KeywordCompletionTest.java | 1 + 6 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 java/java-impl/src/com/intellij/codeInsight/completion/Java18CompletionData.java create mode 100644 java/java-tests/testData/codeInsight/completion/keywords/defaultInExtMethod.java create mode 100644 java/java-tests/testData/codeInsight/completion/keywords/defaultInExtMethod_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/Java18CompletionData.java b/java/java-impl/src/com/intellij/codeInsight/completion/Java18CompletionData.java new file mode 100644 index 000000000000..d627fc535b0b --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/completion/Java18CompletionData.java @@ -0,0 +1,46 @@ +/* + * 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.completion; + +import com.intellij.psi.PsiKeyword; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiParameterList; +import com.intellij.psi.filters.AndFilter; +import com.intellij.psi.filters.ClassFilter; +import com.intellij.psi.filters.ElementFilter; +import com.intellij.psi.filters.TextFilter; +import com.intellij.psi.filters.classes.InterfaceFilter; +import com.intellij.psi.filters.position.LeftNeighbour; +import com.intellij.psi.filters.position.ParentElementFilter; + +public class Java18CompletionData extends Java15CompletionData { + @Override + protected void initVariantsInMethodScope() { + super.initVariantsInMethodScope(); + + { + // in extension method + ElementFilter position = new AndFilter( + new LeftNeighbour(new AndFilter( + new TextFilter(")"), + new ParentElementFilter(new ClassFilter(PsiParameterList.class)))), + new ParentElementFilter(new InterfaceFilter(), 3)); + CompletionVariant variant = new CompletionVariant(PsiMethod.class, position); + variant.addCompletion(PsiKeyword.DEFAULT); + registerVariant(variant); + } + } +} diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java index 2e787a533784..a1e9fb3ce9f6 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -36,6 +36,7 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.patterns.ElementPattern; import com.intellij.patterns.PatternCondition; import com.intellij.patterns.PsiNameValuePairPattern; +import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; import com.intellij.psi.filters.*; import com.intellij.psi.filters.classes.AssignableFromContextFilter; @@ -66,8 +67,22 @@ import static com.intellij.patterns.PsiJavaPatterns.*; public class JavaCompletionContributor extends CompletionContributor { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.completion.JavaCompletionContributor"); - private static final Java15CompletionData ourJava15CompletionData = new Java15CompletionData(); - private static final JavaCompletionData ourJavaCompletionData = new JavaCompletionData(); + private static final Map ourCompletionData; + + static { + ourCompletionData = new LinkedHashMap(); + ourCompletionData.put(LanguageLevel.JDK_1_8, new Java18CompletionData()); + ourCompletionData.put(LanguageLevel.JDK_1_3, new Java15CompletionData()); + ourCompletionData.put(LanguageLevel.JDK_1_3, new JavaCompletionData()); + } + + private static JavaCompletionData getCompletionData(LanguageLevel level) { + final Set> entries = ourCompletionData.entrySet(); + for (Map.Entry entry : entries) { + if (entry.getKey().isAtLeast(level)) return entry.getValue(); + } + return ourCompletionData.get(LanguageLevel.JDK_1_3); + } private static final PsiNameValuePairPattern NAME_VALUE_PAIR = psiNameValuePair().withSuperParent(2, psiElement(PsiAnnotation.class)); @@ -338,7 +353,7 @@ public class JavaCompletionContributor extends CompletionContributor { PsiElement position = parameters.getPosition(); final Set lookupSet = new LinkedHashSet(); final Set keywordVariants = new HashSet(); - final JavaCompletionData completionData = PsiUtil.isLanguageLevel5OrHigher(position) ? ourJava15CompletionData : ourJavaCompletionData; + final JavaCompletionData completionData = getCompletionData(PsiUtil.getLanguageLevel(position)); completionData.addKeywordVariants(keywordVariants, position, parameters.getOriginalFile()); completionData.completeKeywordsBySet(lookupSet, keywordVariants, position, result.getPrefixMatcher(), parameters.getOriginalFile()); completionData.fillCompletions(parameters, result); diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java index 618d7196e467..bb12266e5564 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -311,7 +311,7 @@ public class JavaCompletionData extends JavaAwareCompletionData{ } } - private void initVariantsInMethodScope() { + protected void initVariantsInMethodScope() { // Completion for classes in method throws section // position { @@ -363,7 +363,7 @@ public class JavaCompletionData extends JavaAwareCompletionData{ registerVariant(variant); } -// Catch/Finnaly completion +// Catch/Finally completion { final ElementFilter position = new LeftNeighbour(new AndFilter( new TextFilter("}"), diff --git a/java/java-tests/testData/codeInsight/completion/keywords/defaultInExtMethod.java b/java/java-tests/testData/codeInsight/completion/keywords/defaultInExtMethod.java new file mode 100644 index 000000000000..04b2a8a7035f --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/keywords/defaultInExtMethod.java @@ -0,0 +1,18 @@ +/* + * 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. + */ +interface Foo { + String foo() def +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/keywords/defaultInExtMethod_after.java b/java/java-tests/testData/codeInsight/completion/keywords/defaultInExtMethod_after.java new file mode 100644 index 000000000000..22031018f7bc --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/keywords/defaultInExtMethod_after.java @@ -0,0 +1,18 @@ +/* + * 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. + */ +interface Foo { + String foo() default +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/KeywordCompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/KeywordCompletionTest.java index 26aca320322a..a8d77fc098e2 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/KeywordCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/KeywordCompletionTest.java @@ -86,6 +86,7 @@ public class KeywordCompletionTest extends LightCompletionTestCase { public void testContinue() throws Exception { doTest(false); } public void testThrowsOnSeparateLine() throws Exception { doTest(false); } public void testDefaultInAnno() throws Exception { doTest(false); } + public void testDefaultInExtMethod() throws Exception { doTest(false); } public void testNullInMethodCall() throws Exception { doTest(false); } public void testNullInMethodCall2() throws Exception { doTest(false); }