From be40b01d0e94e471b326cb6f4b840f7dae607cf8 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 29 Dec 2011 15:08:42 +0400 Subject: [PATCH] Disable keyword completion in comments, strings and chars --- .../JetKeywordCompletionContributor.java | 61 +++++++++++++++---- .../completion/keywords/InBlockComment.kt | 12 ++++ idea/testData/completion/keywords/InChar.kt | 11 ++++ .../completion/keywords/InClassBeforeFun.kt | 59 ++++++++++++++++++ idea/testData/completion/keywords/InString.kt | 11 ++++ .../completion/keywords/LineComment.kt | 9 +++ .../keywords/PropertySetterGetter.kt | 7 +++ 7 files changed, 158 insertions(+), 12 deletions(-) create mode 100644 idea/testData/completion/keywords/InBlockComment.kt create mode 100644 idea/testData/completion/keywords/InChar.kt create mode 100644 idea/testData/completion/keywords/InClassBeforeFun.kt create mode 100644 idea/testData/completion/keywords/InString.kt create mode 100644 idea/testData/completion/keywords/LineComment.kt create mode 100644 idea/testData/completion/keywords/PropertySetterGetter.kt diff --git a/idea/src/org/jetbrains/jet/plugin/completion/JetKeywordCompletionContributor.java b/idea/src/org/jetbrains/jet/plugin/completion/JetKeywordCompletionContributor.java index 7ab756f2607..cbb33d12787 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/JetKeywordCompletionContributor.java +++ b/idea/src/org/jetbrains/jet/plugin/completion/JetKeywordCompletionContributor.java @@ -3,18 +3,17 @@ package org.jetbrains.jet.plugin.completion; import com.google.common.base.Function; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; +import com.intellij.codeInsight.CommentUtil; import com.intellij.codeInsight.completion.*; import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.patterns.ElementPattern; import com.intellij.patterns.PlatformPatterns; import com.intellij.psi.PsiElement; -import com.intellij.psi.filters.AndFilter; -import com.intellij.psi.filters.ElementFilter; -import com.intellij.psi.filters.NotFilter; -import com.intellij.psi.filters.TextFilter; +import com.intellij.psi.filters.*; import com.intellij.psi.filters.position.FilterPattern; import com.intellij.psi.filters.position.LeftNeighbour; +import com.intellij.psi.filters.position.PositionElementFilter; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.ProcessingContext; import org.jetbrains.annotations.NotNull; @@ -35,12 +34,51 @@ public class JetKeywordCompletionContributor extends CompletionContributor { private final static InsertHandler KEYWORDS_INSERT_HANDLER = new JetKeywordInsertHandler(); private final static InsertHandler FUNCTION_INSERT_HANDLER = new JetFunctionInsertHandler( JetFunctionInsertHandler.CaretPosition.AFTER_BRACKETS); - + + private final static ElementFilter GENERAL_FILTER = new NotFilter(new OrFilter( + new CommentFilter(), // or + new ParentFilter(new ClassFilter(JetLiteralStringTemplateEntry.class)), // or + new ParentFilter(new ClassFilter(JetConstantExpression.class)), // or + new LeftNeighbour(new TextFilter(".")) + )); + private final static List FUNCTION_KEYWORDS = Lists.newArrayList("get", "set"); + private static class CommentFilter implements ElementFilter { + @Override + public boolean isAcceptable(Object element, PsiElement context) { + if (!(element instanceof PsiElement)) { + return false; + } + + return CommentUtil.isComment((PsiElement) element); + } + + @Override + public boolean isClassAcceptable(Class hintClass) { + return true; + } + } + + private static class ParentFilter extends PositionElementFilter { + public ParentFilter(ElementFilter filter) { + setFilter(filter); + } + + @Override + public boolean isAcceptable(Object element, PsiElement context) { + if (!(element instanceof PsiElement)) { + return false; + } + PsiElement parent = ((PsiElement) element).getParent(); + return parent != null && getFilter().isAcceptable(parent, context); + } + } + private static class InTopFilter implements ElementFilter { @Override public boolean isAcceptable(Object element, PsiElement context) { + //noinspection unchecked return PsiTreeUtil.getParentOfType(context, JetFile.class, false, JetClass.class, JetFunction.class) != null; } @@ -53,8 +91,8 @@ public class JetKeywordCompletionContributor extends CompletionContributor { private static class InNonClassBlockFilter implements ElementFilter { @Override public boolean isAcceptable(Object element, PsiElement context) { - return PsiTreeUtil.getParentOfType(context, JetFunction.class, true, JetClass.class) != null && - PsiTreeUtil.getParentOfType(context, JetBlockExpression.class, true, JetFunction.class) != null; + //noinspection unchecked + return PsiTreeUtil.getParentOfType(context, JetBlockExpression.class, true, JetClassBody.class) != null; } @Override @@ -78,7 +116,8 @@ public class JetKeywordCompletionContributor extends CompletionContributor { private static class InClassBodyFilter implements ElementFilter { @Override public boolean isAcceptable(Object element, PsiElement context) { - return PsiTreeUtil.getParentOfType(context, JetClassBody.class, true, JetFunction.class) != null; + //noinspection unchecked + return PsiTreeUtil.getParentOfType(context, JetClassBody.class, true, JetBlockExpression.class) != null; } @Override @@ -126,8 +165,6 @@ public class JetKeywordCompletionContributor extends CompletionContributor { } } - - public JetKeywordCompletionContributor() { registerScopeKeywordsCompletion(new InTopFilter(), "abstract", "class", "enum", "final", "fun", "get", "import", "inline", @@ -135,7 +172,7 @@ public class JetKeywordCompletionContributor extends CompletionContributor { "trait", "type", "val", "var"); registerScopeKeywordsCompletion(new InClassBodyFilter(), - "abstract", "class", "enum", "final", "fun", "inline", "internal", "get", + "abstract", "class", "enum", "final", "fun", "get", "inline", "internal", "open", "override", "private", "protected", "public", "set", "trait", "type", "val", "var"); @@ -157,6 +194,6 @@ public class JetKeywordCompletionContributor extends CompletionContributor { private static ElementPattern getPlacePattern(final ElementFilter placeFilter) { return PlatformPatterns.psiElement().and( - new FilterPattern(new AndFilter(new NotFilter(new LeftNeighbour(new TextFilter("."))), placeFilter))); + new FilterPattern(new AndFilter(GENERAL_FILTER, placeFilter))); } } diff --git a/idea/testData/completion/keywords/InBlockComment.kt b/idea/testData/completion/keywords/InBlockComment.kt new file mode 100644 index 00000000000..d2d5aac91d9 --- /dev/null +++ b/idea/testData/completion/keywords/InBlockComment.kt @@ -0,0 +1,12 @@ +class TestClass { + /* */ + fun test() { + '' + } +} + +// ABSENT: abstract, annotation, as, break, by, catch, class, continue, default, do +// ABSENT: else, enum, false, final, finally, for, fun, get, if, import, in, inline +// ABSENT: internal, is, null, object, open, out, override, package, private, protected, public +// ABSENT: ref, return, set, super, This, this, throw, trait, true, try, type, val, var +// ABSENT: vararg, when, where, while \ No newline at end of file diff --git a/idea/testData/completion/keywords/InChar.kt b/idea/testData/completion/keywords/InChar.kt new file mode 100644 index 00000000000..8277e8f3333 --- /dev/null +++ b/idea/testData/completion/keywords/InChar.kt @@ -0,0 +1,11 @@ +class TestClass { + fun test() { + '' + } +} + +// ABSENT: abstract, annotation, as, break, by, catch, class, continue, default, do +// ABSENT: else, enum, false, final, finally, for, fun, get, if, import, in, inline +// ABSENT: internal, is, null, object, open, out, override, package, private, protected, public +// ABSENT: ref, return, set, super, This, this, throw, trait, true, try, type, val, var +// ABSENT: vararg, when, where, while \ No newline at end of file diff --git a/idea/testData/completion/keywords/InClassBeforeFun.kt b/idea/testData/completion/keywords/InClassBeforeFun.kt new file mode 100644 index 00000000000..1250e6486a0 --- /dev/null +++ b/idea/testData/completion/keywords/InClassBeforeFun.kt @@ -0,0 +1,59 @@ +public class Test { + + + + fun test() { + + } +} + +// EXIST: abstract +// ?ABSENT: annotation +// ABSENT: as +// ABSENT: break +// ABSENT: by +// ABSENT: catch +// EXIST: class +// ABSENT: continue +// ABSENT: default +// ABSENT: do +// ABSENT: else +// EXIST: enum +// ABSENT: false +// EXIST: final +// ABSENT: finally +// ABSENT: for +// EXIST: fun +// EXIST: get +// ABSENT: if +// ABSENT: import +// ABSENT: in +// EXIST: inline +// EXIST: internal +// ABSENT: is +// ABSENT: null +// ABSENT: object +// EXIST: open +// ABSENT: out +// EXIST: override +// ABSENT: package +// EXIST: private +// EXIST: protected +// EXIST: public +// ABSENT: ref +// ABSENT: return +// EXIST: set +// ABSENT: super +// ABSENT: This +// ABSENT: this +// ABSENT: throw +// EXIST: trait +// ABSENT: true +// ABSENT: try +// EXIST: type +// EXIST: val +// EXIST: var +// ABSENT: vararg +// ABSENT: when +// ABSENT: where +// ABSENT: while \ No newline at end of file diff --git a/idea/testData/completion/keywords/InString.kt b/idea/testData/completion/keywords/InString.kt new file mode 100644 index 00000000000..3dacac7d2f3 --- /dev/null +++ b/idea/testData/completion/keywords/InString.kt @@ -0,0 +1,11 @@ +class TestClass { + fun test() { + "" + } +} + +// ABSENT: abstract, annotation, as, break, by, catch, class, continue, default, do +// ABSENT: else, enum, false, final, finally, for, fun, get, if, import, in, inline +// ABSENT: internal, is, null, object, open, out, override, package, private, protected, public +// ABSENT: ref, return, set, super, This, this, throw, trait, true, try, type, val, var +// ABSENT: vararg, when, where, while \ No newline at end of file diff --git a/idea/testData/completion/keywords/LineComment.kt b/idea/testData/completion/keywords/LineComment.kt new file mode 100644 index 00000000000..270fd6fb843 --- /dev/null +++ b/idea/testData/completion/keywords/LineComment.kt @@ -0,0 +1,9 @@ +class TestClass { + // +} + +// ABSENT: abstract, annotation, as, break, by, catch, class, continue, default, do +// ABSENT: else, enum, false, final, finally, for, fun, get, if, import, in, inline +// ABSENT: internal, is, null, object, open, out, override, package, private, protected, public +// ABSENT: ref, return, set, super, This, this, throw, trait, true, try, type, val, var +// ABSENT: vararg, when, where, while \ No newline at end of file diff --git a/idea/testData/completion/keywords/PropertySetterGetter.kt b/idea/testData/completion/keywords/PropertySetterGetter.kt new file mode 100644 index 00000000000..16a0ed279d6 --- /dev/null +++ b/idea/testData/completion/keywords/PropertySetterGetter.kt @@ -0,0 +1,7 @@ +class Some { + val a : Int + +} + +// EXIST: get +// EXIST: set \ No newline at end of file